View Javadoc

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 at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * 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 and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.util;
18  
19  import org.apache.turbine.util.RunData;
20  import org.apache.jetspeed.portal.portlets.VelocityPortlet;
21  import org.apache.jetspeed.services.Registry;
22  import org.apache.jetspeed.om.registry.Parameter;
23  import org.apache.jetspeed.om.registry.PortletEntry;
24  import org.apache.jetspeed.portal.Portlet;
25  import org.apache.jetspeed.portal.PortletConfig;
26  
27  /***
28   * Defines standard utility functions for config parameters
29   *
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   */
35  public class PortletConfigState
36  {
37  
38      /***
39      Returns the parameter for this name from the xreg
40      */
41      public static String getConfigParameter(Portlet portlet,
42                                              String attrName,
43                                              String attrDefaultValue)
44      {
45          PortletConfig pc = portlet.getPortletConfig();
46          return pc.getInitParameter(attrName, attrDefaultValue);
47      }
48  
49      /***
50      Returns the parameter for this name from the psml
51      */
52      public static String getInstanceParameter(Portlet portlet,
53                                                RunData rundata,
54                                                String attrName)
55      {
56          return portlet.getAttribute( attrName, null, rundata);
57      }
58  
59      /***
60       * Sets the parameter in the psml
61       */
62      public static void setInstanceParameter(Portlet portlet,
63                                       RunData rundata,
64                                       String attrName,
65                                       String attrValue)
66      {
67          portlet.setAttribute(attrName, attrValue, rundata);
68      }
69  
70      public static void clearInstanceParameter(Portlet portlet,
71                                         RunData rundata,
72                                         String attrName)
73      {
74          if( portlet.getAttribute(attrName, null, rundata) != null )
75              portlet.setAttribute(attrName, null, rundata);
76      }
77  
78      /*
79       * Gets the parameter using the fallback routine - first checks PSML,
80       * in case it doesn't find it then it looks up the registry
81       */
82      public static String getParameter(Portlet portlet,
83                                 RunData rundata,
84                                 String attrName,
85                                 String attrDefValue)
86      {
87          String str = getInstanceParameter( portlet, rundata, attrName);
88          if (str == null)
89          {
90              str = getConfigParameter(portlet, attrName, attrDefValue);
91          }
92          return str;
93      }
94  
95      /***
96      Returns the parameter for this name from the xreg
97      */
98      public static String getConfigParameter(VelocityPortlet portlet,
99                                              String attrName,
100                                             String attrDefaultValue)
101     {
102         return getConfigParameter((Portlet) portlet, attrName, attrDefaultValue);
103     }
104 
105     /***
106     Returns the parameter for this name from the psml
107     */
108     public static String getInstanceParameter(VelocityPortlet portlet,
109                                               RunData rundata,
110                                               String attrName)
111     {
112         return getInstanceParameter((Portlet) portlet, rundata, attrName);
113     }
114 
115     /***
116      * Sets the parameter in the psml
117      */
118     public static void setInstanceParameter(VelocityPortlet portlet,
119                                      RunData rundata,
120                                      String attrName,
121                                      String attrValue)
122     {
123         setInstanceParameter((Portlet) portlet, rundata, attrName, attrValue);
124     }
125 
126     public static void clearInstanceParameter(VelocityPortlet portlet,
127                                        RunData rundata,
128                                        String attrName)
129     {
130         clearInstanceParameter((Portlet) portlet,rundata,attrName);
131     }
132 
133     /*
134      * Gets the parameter using the fallback routine - first checks PSML,
135      * in case it doesn't find it then it looks up the registry
136      */
137     public static String getParameter(VelocityPortlet portlet,
138                                RunData rundata,
139                                String attrName,
140                                String attrDefValue)
141     {
142         return getParameter((Portlet) portlet, rundata, attrName, attrDefValue);
143     }
144     
145     /***
146      * Sets the registry (.xreg) value of this portlet.  Use this method because
147      * PortletConfig.setInitParameter() is all but useless in this case.  The portlet
148      * config availble in the Portlet is never saved back to the registry.
149      */
150     public static void setPortletConfigParameter(Portlet portlet, String name, String value)
151     {
152         PortletEntry pEntry = (PortletEntry) Registry.getEntry(Registry.PORTLET, portlet.getName());
153 
154         if (pEntry != null)
155         {
156             Parameter param = pEntry.getParameter(name);
157             portlet.getPortletConfig().setInitParameter(name, value);
158             if (param != null)
159             {
160                 param.setValue(value);
161             }
162             else
163             {
164                 pEntry.addParameter(name, value);
165             }
166         }
167     }
168 }