1 16 17 package org.apache.jetspeed.modules; 18 19 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 import java.util.Vector ; 28 import java.util.Map ; 29 import java.util.Hashtable ; 30 import java.util.Iterator ; 31 32 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 45 public class ParameterLoader extends GenericLoader 46 { 47 50 private static ParameterLoader instance = new ParameterLoader(JetspeedResources.getInt("parameter.cache.size", 50)); 51 52 55 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(ParameterLoader.class.getName()); 56 57 61 private ParameterLoader() { 62 63 super(); 64 } 65 66 72 private ParameterLoader(int i) { 73 super(i); 74 } 75 76 82 private void addInstance(String name, ParameterPresentationStyle param) { 83 84 if (cache()) { 85 this.put(name, (ParameterPresentationStyle)param ); 86 } 87 } 88 89 108 public String eval(RunData data, String provider, String name, String value, Map parms) throws Exception { 109 110 ParameterPresentationStyle prm = getInstance(provider); 112 113 Map styleparms = extractStyleParameters(parms, name); 115 prm.setParms(styleparms); 116 117 return prm.getContent(data, name, value, styleparms); 118 119 } 120 121 128 public void exec(RunData data, String name) throws Exception { 129 130 } 132 133 141 public ParameterPresentationStyle getInstance(String provider) throws Exception { 142 143 ParameterPresentationStyle prm = null; 144 145 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 AssemblerBrokerService ab = 157 (AssemblerBrokerService)TurbineServices.getInstance() 158 .getService (AssemblerBrokerService.SERVICE_NAME); 159 160 try { 161 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 cce) { 174 prm = null; 175 logger.error( "Error loading presentation style", cce ); 176 } 177 178 if (prm == null) { 179 Vector packages = JetspeedResources.getVector("module.packages"); 183 184 throw new ClassNotFoundException ( "\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 203 public static ParameterLoader getInstance() { 204 205 return instance; 206 } 207 208 215 public static Map extractStyleParameters(Map parms, String parmName) { 216 217 Hashtable result = new Hashtable (); 218 219 if (parms != null) { 220 String key = parmName.concat(".style."); 221 Iterator it = parms.keySet().iterator(); 222 while (it.hasNext()) { 223 String parmkey = (String )it.next(); 224 if (parmkey.startsWith(key)) { 225 try { 226 String 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 e) { 234 logger.error("Error extracting params", e); 235 } 236 } 237 } 238 } 239 240 return result; 241 } 242 243 } 244 | Popular Tags |