1 16 package org.apache.cocoon.components.modules.input; 17 18 import org.apache.avalon.framework.CascadingRuntimeException; 19 import org.apache.avalon.framework.component.ComponentManager; 20 import org.apache.avalon.framework.component.ComponentSelector; 21 import org.apache.avalon.framework.configuration.Configuration; 22 import org.apache.avalon.framework.service.ServiceManager; 23 import org.apache.avalon.framework.service.ServiceSelector; 24 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 29 33 public class InputModuleHelper { 34 35 37 protected final static String INPUT_MODULE_SELECTOR = InputModule.ROLE+"Selector"; 38 39 40 private final static int OP_GET = 0; 41 private final static int OP_VALUES = 1; 42 private final static int OP_NAMES = 2; 43 44 45 private Map inputModules; 46 private ComponentManager componentManager; 47 private ComponentSelector componentInputSelector; 48 private ServiceManager serviceManager; 49 private ServiceSelector serviceInputSelector; 50 51 54 private InputModule getInputModule(String name) 55 throws CascadingRuntimeException { 56 if ( this.inputModules == null ) { 57 throw new RuntimeException ("ModuleHelper is not setup correctly."); 58 } 59 InputModule module = (InputModule) this.inputModules.get(name); 60 if ( module == null ) { 61 try { 62 if ( this.componentManager != null ) { 63 if (this.componentInputSelector.hasComponent(name)) { 64 module = (InputModule) this.componentInputSelector.select(name); 65 } 66 } else { 67 if (this.serviceInputSelector.isSelectable(name)) { 68 module = (InputModule) this.serviceInputSelector.select(name); 69 } 70 } 71 } catch (Exception e) { 72 throw new CascadingRuntimeException("Unable to lookup input module " + name, e); 73 } 74 if ( module == null ) { 75 throw new RuntimeException ("No such InputModule: "+name); 76 } 77 this.inputModules.put(name, module); 78 } 79 return module; 80 } 81 82 101 private Object get(int op, String name, String attr, Map objectModel, Configuration conf) throws CascadingRuntimeException { 102 103 Object value = null; 104 final InputModule input = this.getInputModule(name); 105 106 try { 107 108 switch (op) { 109 case OP_GET: 110 value = input.getAttribute(attr, conf, objectModel); 111 break; 112 case OP_VALUES: 113 value = input.getAttributeValues(attr, conf, objectModel); 114 break; 115 case OP_NAMES: 116 value = input.getAttributeNames(conf, objectModel); 117 break; 118 } 119 120 } catch (Exception e) { 121 throw new CascadingRuntimeException("Error accessing attribute '"+attr+"' from input module '"+name+"'. "+e.getMessage(), e); 122 } 123 124 return value; 125 } 126 127 private Object get(int op, String name, String attr, Map objectModel) throws RuntimeException { 128 return get(op, name, attr, objectModel, null); 129 } 130 131 132 133 141 public void setup(ComponentManager manager) throws RuntimeException { 142 143 this.inputModules = new HashMap (); 144 this.componentManager = manager; 145 try { 146 this.componentInputSelector = (ComponentSelector) this.componentManager.lookup(INPUT_MODULE_SELECTOR); 147 } catch (Exception e) { 148 throw new CascadingRuntimeException("Could not obtain selector for InputModule.",e); 149 } 150 } 151 152 159 public void setup(ServiceManager manager) throws RuntimeException { 160 161 this.inputModules = new HashMap (); 162 this.serviceManager = manager; 163 try { 164 this.serviceInputSelector = (ServiceSelector) this.serviceManager.lookup(INPUT_MODULE_SELECTOR); 165 } catch (Exception e) { 166 throw new CascadingRuntimeException("Could not obtain selector for InputModule.",e); 167 } 168 } 169 170 171 183 public Object getAttribute(Map objectModel, Configuration conf, String module, String name, Object deflt) throws RuntimeException { 184 185 Object result = this.get(OP_GET, module, name, objectModel, conf); 186 if (result == null) result = deflt; 187 return result; 188 } 189 190 195 public Object getAttribute(Map objectModel, String module, String name, Object deflt) throws RuntimeException { 196 return getAttribute(objectModel, null, module, name, deflt); 197 } 198 199 200 212 public Object [] getAttributeValues(Map objectModel, Configuration conf, String module, String name, Object [] deflt) throws RuntimeException { 213 214 Object [] result = (Object []) this.get(OP_VALUES, module, name, objectModel, conf); 215 if (result == null) result = deflt; 216 return result; 217 } 218 219 224 public Object [] getAttributeValues(Map objectModel, String module, String name, Object [] deflt) throws RuntimeException { 225 return getAttributeValues(objectModel, null, module, name, deflt); 226 } 227 228 229 238 public Iterator getAttributeNames(Map objectModel, Configuration conf, String module) throws RuntimeException { 239 240 return (Iterator ) this.get(OP_NAMES, module, null, objectModel); 241 } 242 243 247 public Iterator getAttributeNames(Map objectModel, String module) throws RuntimeException { 248 return getAttributeNames(objectModel, (Configuration)null, module); 249 } 250 251 252 253 258 public void releaseAll() throws RuntimeException { 259 260 if ( this.inputModules != null ) { 261 if ( this.componentManager != null ) { 263 try { 264 Iterator iter = this.inputModules.keySet().iterator(); 265 while (iter.hasNext()) { 266 this.componentInputSelector.release((InputModule) this.inputModules.get(iter.next())); 267 } 268 this.inputModules = null; 269 this.componentManager.release(this.componentInputSelector); 270 this.componentManager = null; 271 this.inputModules = null; 272 } catch (Exception e) { 273 throw new CascadingRuntimeException("Could not release InputModules.",e); 274 } 275 276 } 277 if ( this.serviceManager != null ) { 278 try { 279 Iterator iter = this.inputModules.keySet().iterator(); 280 while (iter.hasNext()) { 281 this.serviceInputSelector.release(this.inputModules.get(iter.next())); 282 } 283 this.inputModules = null; 284 this.serviceManager.release(this.serviceInputSelector); 285 this.serviceManager = null; 286 this.inputModules = null; 287 } catch (Exception e) { 288 throw new CascadingRuntimeException("Could not release InputModules.",e); 289 } 290 291 } 292 } 293 } 294 295 } 296 | Popular Tags |