1 16 17 package org.apache.cocoon.components.modules.input; 18 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 import org.apache.avalon.framework.activity.Disposable; 23 import org.apache.avalon.framework.component.ComponentException; 24 import org.apache.avalon.framework.component.ComponentManager; 25 import org.apache.avalon.framework.component.ComponentSelector; 26 import org.apache.avalon.framework.component.Composable; 27 import org.apache.avalon.framework.configuration.Configuration; 28 import org.apache.avalon.framework.configuration.ConfigurationException; 29 import org.apache.avalon.framework.thread.ThreadSafe; 30 31 41 public abstract class AbstractMetaModule extends AbstractInputModule 42 implements Composable, Disposable { 43 44 45 protected ComponentManager manager; 46 47 48 protected ComponentSelector inputSelector = null; 49 50 51 protected InputModule input = null; 52 53 54 protected String defaultInput = "request-param"; 56 57 protected Configuration inputConf = null; 60 61 protected boolean initialized = false; 62 63 64 65 protected final static String INPUT_MODULE_SELECTOR = InputModule.ROLE+"Selector"; 66 67 68 private final static int OP_GET = 0; 69 private final static int OP_VALUES = 1; 70 private final static int OP_NAMES = 2; 71 private final static String [] OPNAME = new String [] {"GET_VALUE", "GET_VALUES", "GET_NAMES"}; 72 73 74 78 public void compose(ComponentManager manager) throws ComponentException { 79 80 this.manager=manager; 81 } 82 83 84 96 public synchronized void lazy_initialize() { 97 98 try { 99 if (!this.initialized) { 101 this.inputSelector=(ComponentSelector) this.manager.lookup(INPUT_MODULE_SELECTOR); 102 if (this.inputSelector != null && this.inputSelector instanceof ThreadSafe) { 103 104 if (this.defaultInput != null) { 105 this.input = obtainModule(this.defaultInput); 106 } 107 108 } else if (!(this.inputSelector instanceof ThreadSafe) ) { 109 this.manager.release(this.inputSelector); 110 this.inputSelector = null; 111 } 112 113 this.initialized = true; 114 } 115 } catch (Exception e) { 116 if (getLogger().isWarnEnabled()) 117 getLogger().error("A problem occurred setting up input modules :'" + e.getMessage(), e); 118 } 119 } 120 121 122 126 public void dispose() { 127 128 if (this.inputSelector != null) { 129 if (this.input != null) 130 this.inputSelector.release(this.input); 131 this.manager.release(this.inputSelector); 132 } 133 } 134 135 136 139 protected InputModule obtainModule(String type) { 140 ComponentSelector inputSelector = this.inputSelector; 141 InputModule module = null; 142 try { 143 if (inputSelector == null) { 144 inputSelector=(ComponentSelector) this.manager.lookup(INPUT_MODULE_SELECTOR); 145 } 146 if (inputSelector.hasComponent(type)) { 147 if (type != null && inputSelector.hasComponent(type)) { 148 module = (InputModule) inputSelector.select(type); 149 } 150 if (!(module instanceof ThreadSafe) ) { 151 inputSelector.release(module); 152 module = null; 153 } 154 } 155 if (type != null && module == null) 156 if (getLogger().isWarnEnabled()) 157 getLogger().warn("A problem occurred setting up '" + type 158 + "': Selector is not null, Component is " 159 + (inputSelector.hasComponent(type)? "known" : "unknown")); 160 161 } catch (ComponentException ce) { 162 if (getLogger().isWarnEnabled()) 163 getLogger().warn("Could not obtain selector for InputModules: "+ce.getMessage()); 164 } finally { 165 if (this.inputSelector == null) 166 this.manager.release(inputSelector); 167 } 169 170 return module; 171 } 172 173 174 177 protected void releaseModule(InputModule module) { 178 ComponentSelector inputSelector = this.inputSelector; 179 if (module != null) { 180 try { 181 if (inputSelector == null) 183 inputSelector=(ComponentSelector) this.manager.lookup(INPUT_MODULE_SELECTOR); 184 185 inputSelector.release(module); 186 module = null; 187 188 } catch (ComponentException ce) { 189 if (getLogger().isWarnEnabled()) 190 getLogger().warn("Could not obtain selector for InputModules: "+ce.getMessage()); 191 } finally { 192 if (this.inputSelector == null) 193 this.manager.release(inputSelector); 194 } 195 } 196 } 197 198 203 protected Iterator getNames(Map objectModel, 204 InputModule staticMod, String staticModName, Configuration staticModConf) 205 throws ConfigurationException { 206 207 return (Iterator ) this.get(OP_NAMES, null, objectModel, staticMod, staticModName, staticModConf, null, null, null); 208 } 209 210 216 protected Iterator getNames(Map objectModel, 217 InputModule staticMod, String staticModName, Configuration staticModConf, 218 InputModule dynamicMod, String dynamicModName, Configuration dynamicModConf) 219 throws ConfigurationException { 220 221 return (Iterator ) this.get(OP_NAMES, null, objectModel, staticMod, staticModName, staticModConf, dynamicMod, dynamicModName, dynamicModConf); 222 } 223 224 protected Object getValue(String attr, Map objectModel, ModuleHolder holder) throws ConfigurationException{ 225 return this.getValue(attr, objectModel, holder.input, holder.name, holder.config); 226 } 227 228 protected Object getValue(String attr, Map objectModel, ModuleHolder staticHolder, ModuleHolder dynamicHolder) throws ConfigurationException{ 229 return this.getValue(attr, objectModel, staticHolder.input, staticHolder.name, dynamicHolder.config); 230 } 231 232 protected Object [] getValues(String attr, Map objectModel, ModuleHolder holder) throws ConfigurationException{ 233 return this.getValues(attr, objectModel, holder.input, holder.name, holder.config); 234 } 235 236 protected Object [] getValues(String attr, Map objectModel, ModuleHolder staticHolder, ModuleHolder dynamicHolder) throws ConfigurationException{ 237 return this.getValues(attr, objectModel, staticHolder.input, staticHolder.name, dynamicHolder.config); 238 } 239 240 245 protected Object getValue(String attr, Map objectModel, 246 InputModule staticMod, String staticModName, Configuration staticModConf) 247 throws ConfigurationException { 248 249 return this.get(OP_GET, attr, objectModel, staticMod, staticModName, staticModConf, null, null, null); 250 } 251 252 253 259 protected Object getValue(String attr, Map objectModel, 260 InputModule staticMod, String staticModName, Configuration staticModConf, 261 InputModule dynamicMod, String dynamicModName, Configuration dynamicModConf) 262 throws ConfigurationException { 263 264 return this.get(OP_GET, attr, objectModel, staticMod, staticModName, staticModConf, dynamicMod, dynamicModName, dynamicModConf); 265 } 266 267 272 protected Object [] getValues(String attr, Map objectModel, 273 InputModule staticMod, String staticModName, Configuration staticModConf) 274 throws ConfigurationException { 275 276 return (Object []) this.get(OP_VALUES, attr, objectModel, staticMod, staticModName, staticModConf, null, null, null); 277 } 278 279 285 protected Object [] getValues(String attr, Map objectModel, 286 InputModule staticMod, String staticModName, Configuration staticModConf, 287 InputModule dynamicMod, String dynamicModName, Configuration dynamicModConf) 288 throws ConfigurationException { 289 290 return (Object []) this.get(OP_VALUES, attr, objectModel, staticMod, staticModName, staticModConf, dynamicMod, dynamicModName, dynamicModConf); 291 } 292 293 294 303 private Object get(int op, String attr, Map objectModel, 304 InputModule staticMod, String staticModName, Configuration staticModConf, 305 InputModule dynamicMod, String dynamicModName, Configuration dynamicModConf) 306 throws ConfigurationException { 307 308 ComponentSelector cs = this.inputSelector; 309 Object value = null; 310 String name = null; 311 InputModule input = null; 312 Configuration conf = null; 313 boolean release = false; 314 315 try { 316 if (cs == null) { 317 try { 318 cs = (ComponentSelector) this.manager.lookup(INPUT_MODULE_SELECTOR); 319 } catch (ComponentException e) { 320 throw new ConfigurationException("Could not find MetaModule's module selector", e); 321 } 322 } 323 324 boolean useDynamic; 325 if (dynamicMod == null && dynamicModName == null) { 326 useDynamic = false; 327 input = staticMod; 328 name = staticModName; 329 conf = staticModConf; 330 } else { 331 useDynamic = true; 332 input = dynamicMod; 333 name = dynamicModName; 334 conf = dynamicModConf; 335 } 336 337 if (getLogger().isDebugEnabled()) { 338 getLogger().debug("MetaModule performing op "+OPNAME[op]+" on " + 339 (useDynamic?"dynamically":"statically") + " " + 340 (input==null?"created":"assigned") + 341 " module '"+name+"', using config "+dynamicModConf); 342 } 343 344 if (input == null) { 345 if (cs.hasComponent(name)) { 346 release = true; 347 try { 348 input = (InputModule) cs.select(name); 349 } catch (ComponentException e) { 350 throw new ConfigurationException( 351 "MetaModule unable to create "+ 352 (useDynamic ? "dynamically" : "statically")+ 353 " specified internal module '"+name+"'", e); 354 } 355 } else { 356 throw new ConfigurationException("MetaModule: No such InputModule: "+name); 357 } 358 } 359 360 switch (op) { 361 case OP_GET: 362 value = input.getAttribute(attr, conf, objectModel); 363 break; 364 case OP_VALUES: 365 value = input.getAttributeValues(attr, conf, objectModel); 366 break; 367 case OP_NAMES: 368 value = input.getAttributeNames(conf, objectModel); 369 break; 370 } 371 372 if (getLogger().isDebugEnabled()) 373 getLogger().debug("using "+name+" as "+input+" for "+op+" ("+attr+") and "+conf+" gives "+value); 374 375 } finally { 376 if (release) 377 cs.release(input); 378 379 if (this.inputSelector == null) 380 this.manager.release(cs); 381 } 382 383 return value; 384 } 385 386 } 387 | Popular Tags |