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.modules;
1819// jetspeed stuff20import org.apache.jetspeed.modules.parameters.ParameterPresentationStyle;
21import org.apache.jetspeed.modules.parameters.ParameterPresentationStyleFactory;
22import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
23import org.apache.jetspeed.services.logging.JetspeedLogger;
24import org.apache.jetspeed.services.resources.JetspeedResources;
2526// Java Core Classes27import java.util.Vector;
28import java.util.Map;
29import java.util.Hashtable;
30import java.util.Iterator;
3132// Turbine Utility Classes33import org.apache.turbine.modules.GenericLoader;
34import org.apache.turbine.services.TurbineServices;
35import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService;
36import org.apache.turbine.util.RunData;
3738/***39 * The purpose of this class is to allow one to load and execute40 * 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 */45publicclassParameterLoaderextends GenericLoader
46 {
47/***48 * The single instance of this class.49 */50privatestaticParameterLoader instance = newParameterLoader(JetspeedResources.getInt("parameter.cache.size", 50));
5152/***53 * Static initialization of the logger for this class54 */55privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(ParameterLoader.class.getName());
5657/***58 * These ctor's are private to force clients to use getInstance()59 * to access this class.60 */61privateParameterLoader() {
6263super();
64 }
6566/***67 * These ctor's are private to force clients to use getInstance()68 * to access this class.69 * 70 * @param i71 */72privateParameterLoader(int i) {
73super(i);
74 }
7576/***77 * Adds an instance of an object into the hashtable.78 * 79 * @param name Name of object.80 * @param param81 */82privatevoid addInstance(String name, ParameterPresentationStyle param) {
8384if (cache()) {
85this.put(name, (ParameterPresentationStyle)param );
86 }
87 }
8889/***90 * Attempts to load and render a parameter using custom style. For example, one might define91 * a custom parameter presentation style TextArea which displays current value of the parameter92 * using HTML text area presentation. Assuming that TextArea is rendered using two optional93 * parameters: rows and cols, the map passed to this method could contain the following values:94 * <li>symbols.style.rows = 595 * <li>symbols.style.cols = 8096 * 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 tag103 * @param value Current value104 * @param parms Optional rendition parameters105 * @return 106 * @exception Exception a generic exception.107 */108public String eval(RunData data, String provider, String name, String value, Map parms) throws Exception {
109110// Execute parameter111ParameterPresentationStyle prm = getInstance(provider);
112113// Filter out style params114 Map styleparms = extractStyleParameters(parms, name);
115 prm.setParms(styleparms);
116117return prm.getContent(data, name, value, styleparms);
118119 }
120121/***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 */128publicvoid exec(RunData data, String name) throws Exception {
129130//this.eval(data, name);131 }
132133/***134 * Pulls out an instance of the object by name. Name is just the135 * 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 */141publicParameterPresentationStyle getInstance(String provider) throws Exception {
142143ParameterPresentationStyle prm = null;
144145// Check if the parameter is already in the cache146if (cache() && this.containsKey(provider)) {
147148 prm = (ParameterPresentationStyle) this.get(provider);
149if ( logger.isDebugEnabled() ) {
150 logger.debug("ParameterLoader: Serving parameter: " + provider + ", prm=" + prm + " from cache");
151 }
152153 } else {
154155// We get the broker service156 AssemblerBrokerService ab =
157 (AssemblerBrokerService)TurbineServices.getInstance()
158 .getService (AssemblerBrokerService.SERVICE_NAME);
159160try {
161// Attempt to load the presentation style162 prm = (ParameterPresentationStyle)ab.getAssembler("parameter", provider);
163if (prm == null) {
164if ( logger.isDebugEnabled() ) {
165 logger.debug("ParameterLoader: Registering the factory");
166 }
167 ab.registerFactory("parameter", newParameterPresentationStyleFactory());
168 prm = (ParameterPresentationStyle)ab.getAssembler("parameter", provider);
169 }
170if ( logger.isDebugEnabled() ) {
171 logger.debug("ParameterLoader: Loaded parameter: "+provider+", prm="+prm);
172 }
173 } catch (ClassCastException cce) {
174 prm = null;
175 logger.error( "Error loading presentation style", cce );
176 }
177178if (prm == null) {
179// If we did not find a screen we should try and give180// the user a reason for that...181// FIX ME: The AssemblerFactories should each add it's own string here...182Vector packages = JetspeedResources.getVector("module.packages");
183184thrownew ClassNotFoundException( "\n\n\tRequested Parameter not found: " +
185 provider + "\n" +
186"\tTurbine looked in the following modules.packages path: \n\t" +
187packages.toString() +"\n");
188 } elseif(cache()) {
189190 addInstance(provider, prm);
191 }
192193 }
194195return prm;
196 }
197198/***199 * The method through which this class is accessed.200 * 201 * @return The single instance of this class.202 */203publicstaticParameterLoader getInstance() {
204205return instance;
206 }
207208/***209 * Extracts any parameters to parameter style.210 * 211 * @param parms portlet parameters212 * @param parm parameter name213 * @return hashtable of optional parameters for the style214 */215publicstatic Map extractStyleParameters(Map parms, String parmName) {
216217 Hashtable result = new Hashtable();
218219if (parms != null) {
220 String key = parmName.concat(".style.");
221 Iterator it = parms.keySet().iterator();
222while (it.hasNext()) {
223 String parmkey = (String)it.next();
224if (parmkey.startsWith(key)) {
225try {
226 String stylekey = parmkey.substring(parmkey.lastIndexOf(".")+1);
227if ( 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 e) {
234 logger.error("Error extracting params", e);
235 }
236 }
237 }
238 }
239240return result;
241 }
242243 }