1/*2 * Copyright 2000-2001,2004 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.util;
1819import org.apache.turbine.util.RunData;
20import org.apache.jetspeed.portal.portlets.VelocityPortlet;
21import org.apache.jetspeed.services.Registry;
22import org.apache.jetspeed.om.registry.Parameter;
23import org.apache.jetspeed.om.registry.PortletEntry;
24import org.apache.jetspeed.portal.Portlet;
25import org.apache.jetspeed.portal.PortletConfig;
2627/***28 * Defines standard utility functions for config parameters29 *30 * @author <a href="mailto:david@apache.org">David Sean Taylor</a>31 * @author <a href="mailto:morciuch@apache.org">Mark Orciuch</a> 32 * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>33 * @version $Id: PortletConfigState.java,v 1.4 2004/02/23 03:23:42 jford Exp $34 */35publicclassPortletConfigState36 {
3738/***39 Returns the parameter for this name from the xreg40 */41publicstatic String getConfigParameter(Portlet portlet,
42 String attrName,
43 String attrDefaultValue)
44 {
45PortletConfig pc = portlet.getPortletConfig();
46return pc.getInitParameter(attrName, attrDefaultValue);
47 }
4849/***50 Returns the parameter for this name from the psml51 */52publicstatic String getInstanceParameter(Portlet portlet,
53 RunData rundata,
54 String attrName)
55 {
56return portlet.getAttribute( attrName, null, rundata);
57 }
5859/***60 * Sets the parameter in the psml61 */62publicstaticvoid setInstanceParameter(Portlet portlet,
63 RunData rundata,
64 String attrName,
65 String attrValue)
66 {
67 portlet.setAttribute(attrName, attrValue, rundata);
68 }
6970publicstaticvoid clearInstanceParameter(Portlet portlet,
71 RunData rundata,
72 String attrName)
73 {
74if( portlet.getAttribute(attrName, null, rundata) != null )
75 portlet.setAttribute(attrName, null, rundata);
76 }
7778/*79 * Gets the parameter using the fallback routine - first checks PSML,80 * in case it doesn't find it then it looks up the registry81 */82publicstatic String getParameter(Portlet portlet,
83 RunData rundata,
84 String attrName,
85 String attrDefValue)
86 {
87 String str = getInstanceParameter( portlet, rundata, attrName);
88if (str == null)
89 {
90 str = getConfigParameter(portlet, attrName, attrDefValue);
91 }
92return str;
93 }
9495/***96 Returns the parameter for this name from the xreg97 */98publicstatic String getConfigParameter(VelocityPortlet portlet,
99 String attrName,
100 String attrDefaultValue)
101 {
102return getConfigParameter((Portlet) portlet, attrName, attrDefaultValue);
103 }
104105/***106 Returns the parameter for this name from the psml107 */108publicstatic String getInstanceParameter(VelocityPortlet portlet,
109 RunData rundata,
110 String attrName)
111 {
112return getInstanceParameter((Portlet) portlet, rundata, attrName);
113 }
114115/***116 * Sets the parameter in the psml117 */118publicstaticvoid setInstanceParameter(VelocityPortlet portlet,
119 RunData rundata,
120 String attrName,
121 String attrValue)
122 {
123 setInstanceParameter((Portlet) portlet, rundata, attrName, attrValue);
124 }
125126publicstaticvoid clearInstanceParameter(VelocityPortlet portlet,
127 RunData rundata,
128 String attrName)
129 {
130 clearInstanceParameter((Portlet) portlet,rundata,attrName);
131 }
132133/*134 * Gets the parameter using the fallback routine - first checks PSML,135 * in case it doesn't find it then it looks up the registry136 */137publicstatic String getParameter(VelocityPortlet portlet,
138 RunData rundata,
139 String attrName,
140 String attrDefValue)
141 {
142return getParameter((Portlet) portlet, rundata, attrName, attrDefValue);
143 }
144145/***146 * Sets the registry (.xreg) value of this portlet. Use this method because147 * PortletConfig.setInitParameter() is all but useless in this case. The portlet148 * config availble in the Portlet is never saved back to the registry.149 */150publicstaticvoid setPortletConfigParameter(Portlet portlet, String name, String value)
151 {
152PortletEntry pEntry = (PortletEntry) Registry.getEntry(Registry.PORTLET, portlet.getName());
153154if (pEntry != null)
155 {
156Parameter param = pEntry.getParameter(name);
157 portlet.getPortletConfig().setInitParameter(name, value);
158if (param != null)
159 {
160 param.setValue(value);
161 }
162else163 {
164 pEntry.addParameter(name, value);
165 }
166 }
167 }
168 }