KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > modules > ParameterLoader


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.modules;
18
19 // jetspeed stuff
20
import org.apache.jetspeed.modules.parameters.ParameterPresentationStyle;
21 import org.apache.jetspeed.modules.parameters.ParameterPresentationStyleFactory;
22 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
23 import org.apache.jetspeed.services.logging.JetspeedLogger;
24 import org.apache.jetspeed.services.resources.JetspeedResources;
25
26 // Java Core Classes
27
import java.util.Vector JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 // Turbine Utility Classes
33
import org.apache.turbine.modules.GenericLoader;
34 import org.apache.turbine.services.TurbineServices;
35 import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService;
36 import org.apache.turbine.util.RunData;
37
38 /**
39  * The purpose of this class is to allow one to load and execute
40  * Parameter modules.
41  *
42  * @author <a HREF="mailto:mark_orciuch@ngsltd.com">Mark Orciuch</a>
43  * @version $Id: ParameterLoader.java,v 1.5 2004/02/23 03:01:32 jford Exp $
44  */

45 public class ParameterLoader extends GenericLoader
46 {
47     /**
48      * The single instance of this class.
49      */

50     private static ParameterLoader instance = new ParameterLoader(JetspeedResources.getInt("parameter.cache.size", 50));
51
52     /**
53      * Static initialization of the logger for this class
54      */

55     private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(ParameterLoader.class.getName());
56     
57     /**
58      * These ctor's are private to force clients to use getInstance()
59      * to access this class.
60      */

61     private ParameterLoader() {
62
63         super();
64     }
65
66     /**
67      * These ctor's are private to force clients to use getInstance()
68      * to access this class.
69      *
70      * @param i
71      */

72     private ParameterLoader(int i) {
73         super(i);
74     }
75
76     /**
77      * Adds an instance of an object into the hashtable.
78      *
79      * @param name Name of object.
80      * @param param
81      */

82     private void addInstance(String JavaDoc name, ParameterPresentationStyle param) {
83
84         if (cache()) {
85             this.put(name, (ParameterPresentationStyle)param );
86         }
87     }
88
89     /**
90      * Attempts to load and render a parameter using custom style. For example, one might define
91      * a custom parameter presentation style TextArea which displays current value of the parameter
92      * using HTML text area presentation. Assuming that TextArea is rendered using two optional
93      * parameters: rows and cols, the map passed to this method could contain the following values:
94      * <li>symbols.style.rows = 5
95      * <li>symbols.style.cols = 80
96      * and the call might look like this:
97      *<p>
98      * String symbols = eval(data, "TextArea", "symbols", "MSFT,SUNW,EMC,ORCL", parms);
99      *
100      * @param data Turbine information.
101      * @param provider Custom parameter class name (without the package)
102      * @param name Name for rendered HTML tag
103      * @param value Current value
104      * @param parms Optional rendition parameters
105      * @return
106      * @exception Exception a generic exception.
107      */

108     public String JavaDoc eval(RunData data, String JavaDoc provider, String JavaDoc name, String JavaDoc value, Map JavaDoc parms) throws Exception JavaDoc {
109
110         // Execute parameter
111
ParameterPresentationStyle prm = getInstance(provider);
112
113         // Filter out style params
114
Map JavaDoc styleparms = extractStyleParameters(parms, name);
115         prm.setParms(styleparms);
116
117         return prm.getContent(data, name, value, styleparms);
118
119     }
120
121     /**
122      * This method is not used.
123      *
124      * @param data Turbine information.
125      * @param name Name of object that will execute the screen.
126      * @exception Exception a generic exception.
127      */

128     public void exec(RunData data, String JavaDoc name) throws Exception JavaDoc {
129
130         //this.eval(data, name);
131
}
132
133     /**
134      * Pulls out an instance of the object by name. Name is just the
135      * single name of the object.
136      *
137      * @param provider Name of object instance.
138      * @return A Screen with the specified name, or null.
139      * @exception Exception a generic exception.
140      */

141     public ParameterPresentationStyle getInstance(String JavaDoc provider) throws Exception JavaDoc {
142
143         ParameterPresentationStyle prm = null;
144
145         // Check if the parameter is already in the cache
146
if (cache() && this.containsKey(provider)) {
147
148             prm = (ParameterPresentationStyle) this.get(provider);
149             if ( logger.isDebugEnabled() ) {
150                 logger.debug("ParameterLoader: Serving parameter: " + provider + ", prm=" + prm + " from cache");
151             }
152
153         } else {
154
155             // We get the broker service
156
AssemblerBrokerService ab =
157                 (AssemblerBrokerService)TurbineServices.getInstance()
158                 .getService (AssemblerBrokerService.SERVICE_NAME);
159
160             try {
161                 // Attempt to load the presentation style
162
prm = (ParameterPresentationStyle)ab.getAssembler("parameter", provider);
163                 if (prm == null) {
164                     if ( logger.isDebugEnabled() ) {
165                         logger.debug("ParameterLoader: Registering the factory");
166                     }
167                     ab.registerFactory("parameter", new ParameterPresentationStyleFactory());
168                     prm = (ParameterPresentationStyle)ab.getAssembler("parameter", provider);
169                 }
170                 if ( logger.isDebugEnabled() ) {
171                     logger.debug("ParameterLoader: Loaded parameter: "+provider+", prm="+prm);
172                 }
173             } catch (ClassCastException JavaDoc cce) {
174                 prm = null;
175                 logger.error( "Error loading presentation style", cce );
176             }
177
178             if (prm == null) {
179                 // If we did not find a screen we should try and give
180
// the user a reason for that...
181
// FIX ME: The AssemblerFactories should each add it's own string here...
182
Vector JavaDoc packages = JetspeedResources.getVector("module.packages");
183
184                 throw new ClassNotFoundException JavaDoc( "\n\n\tRequested Parameter not found: " +
185                                                   provider + "\n" +
186                                                   "\tTurbine looked in the following modules.packages path: \n\t" +
187                                                   packages.toString() + "\n");
188             } else if(cache()) {
189
190                 addInstance(provider, prm);
191             }
192
193         }
194
195         return prm;
196     }
197
198     /**
199      * The method through which this class is accessed.
200      *
201      * @return The single instance of this class.
202      */

203     public static ParameterLoader getInstance() {
204
205         return instance;
206     }
207
208     /**
209      * Extracts any parameters to parameter style.
210      *
211      * @param parms portlet parameters
212      * @param parm parameter name
213      * @return hashtable of optional parameters for the style
214      */

215     public static Map JavaDoc extractStyleParameters(Map JavaDoc parms, String JavaDoc parmName) {
216
217         Hashtable JavaDoc result = new Hashtable JavaDoc();
218
219         if (parms != null) {
220             String JavaDoc key = parmName.concat(".style.");
221             Iterator JavaDoc it = parms.keySet().iterator();
222             while (it.hasNext()) {
223                 String JavaDoc parmkey = (String JavaDoc)it.next();
224                 if (parmkey.startsWith(key)) {
225                     try {
226                         String JavaDoc stylekey = parmkey.substring(parmkey.lastIndexOf(".")+1);
227                         if ( logger.isDebugEnabled() )
228                         {
229                             logger.debug("ParameterLoader: parm name [" + parmName + "] - storing option [" + stylekey +
230                                       "] with value [" + parms.get(parmkey) + "]");
231                         }
232                         result.put(stylekey, parms.get(parmkey));
233                     } catch (Exception JavaDoc e) {
234                         logger.error("Error extracting params", e);
235                     }
236                 }
237             }
238         }
239
240         return result;
241     }
242
243 }
244
Popular Tags