1 16 package org.apache.cocoon.components; 17 18 import org.apache.avalon.excalibur.component.ExcaliburComponentSelector; 19 import org.apache.avalon.excalibur.component.RoleManager; 20 import org.apache.avalon.framework.component.Component; 21 import org.apache.avalon.framework.component.ComponentException; 22 import org.apache.avalon.framework.configuration.Configuration; 23 import org.apache.avalon.framework.configuration.ConfigurationException; 24 import org.apache.avalon.framework.configuration.DefaultConfiguration; 25 26 33 public class ExtendedComponentSelector extends ExcaliburComponentSelector 34 implements ParentAware { 35 36 37 protected RoleManager roles; 38 39 40 protected ExtendedComponentSelector parentSelector; 41 42 43 protected ComponentLocator parentLocator; 44 45 46 protected ClassLoader classLoader; 47 48 49 protected String roleName; 50 51 52 protected String defaultHint; 53 54 55 private String location; 56 57 58 59 public ExtendedComponentSelector() { 60 this.classLoader = Thread.currentThread().getContextClassLoader(); 61 } 62 63 64 public ExtendedComponentSelector(ClassLoader loader) { 65 super(loader); 66 67 if (loader == null) { 68 this.classLoader = Thread.currentThread().getContextClassLoader(); 69 } else { 70 this.classLoader = loader; 71 } 72 } 73 74 84 protected String getComponentInstanceName() { 85 return null; 86 } 87 88 94 protected String getClassAttributeName() { 95 return "class"; 96 } 97 98 106 protected String getDefaultHintAttributeName() { 107 return "default"; 108 } 109 110 113 public void setRoleManager(RoleManager roles) { 114 super.setRoleManager(roles); 115 this.roles = roles; 116 } 117 118 126 134 135 141 protected String getRoleName(Configuration config) { 142 String roleName = config.getAttribute("role", null); 144 if (roleName == null && this.roles != null) { 145 roleName = this.roles.getRoleForName(config.getName()); 146 } 147 148 return roleName; 149 } 150 151 169 public void configure(Configuration config) throws ConfigurationException { 170 171 this.location = config.getLocation(); 173 174 this.roleName = getRoleName(config); 175 176 DefaultConfiguration temp = new DefaultConfiguration(config.getName(), this.location); 180 if (config.getAttribute("role", null) != null) { 181 temp.setAttribute("role", this.roleName); 182 } 183 super.configure(temp); 184 185 this.defaultHint = config.getAttribute(this.getDefaultHintAttributeName(), null); 187 188 String compInstanceName = getComponentInstanceName(); 190 191 Configuration[] instances = config.getChildren(); 192 193 for (int i = 0; i < instances.length; i++) { 194 Configuration instance = instances[i]; 195 196 Object hint = instance.getAttribute("name").trim(); 197 198 String classAttr = instance.getAttribute(getClassAttributeName(), null); 199 String className; 200 201 if (compInstanceName == null) { 202 if (classAttr == null) { 204 className = this.roles.getDefaultClassNameForHint(roleName, instance.getName()); 205 } else { 206 className = classAttr.trim(); 207 } 208 209 } else { 210 if (compInstanceName.equals(instance.getName())) { 212 className = (classAttr == null) ? null : classAttr.trim(); 213 } else { 214 className = this.roles.getDefaultClassNameForHint(roleName, instance.getName()); 215 } 216 } 217 218 if (className == null) { 219 String message = "Unable to determine class name for component named '" + hint + 220 "' at " + instance.getLocation(); 221 222 getLogger().error(message); 223 throw new ConfigurationException(message); 224 } 225 226 try { 227 Class clazz = this.classLoader.loadClass(className); 228 addComponent(hint, clazz, instance); 229 230 } catch (Exception e) { 231 String message = "Could not load class " + className + " for component named '" + 232 hint + "' at " + instance.getLocation(); 233 234 getLogger().error(message, e); 235 throw new ConfigurationException(message, e); 236 } 237 } 238 } 239 240 243 public String getDefaultHint() { 244 if (this.defaultHint == null && this.parentSelector != null) { 246 return this.parentSelector.getDefaultHint(); 247 } 248 249 return this.defaultHint; 250 } 251 252 255 public Component select(Object hint) throws ComponentException { 256 if (hint == null) { 257 hint = this.defaultHint; 258 } 259 260 if (parentSelector == null) { 261 return super.select(hint); 263 } 264 265 try { 266 final Component component = super.select(hint); 268 return component; 269 270 } catch (ComponentException original) { 271 try { 272 final Component component = this.parentSelector.select(hint); 274 return component; 275 276 } catch (ComponentException nested) { 277 279 if (nested.getCause() != null) { 280 throw nested; 282 } 283 284 throw original; 286 } 287 } 288 } 289 290 293 public void release(Component component) { 294 if (this.parentSelector != null && this.parentSelector.canRelease(component)) { 296 this.parentSelector.release(component); 298 } else { 299 super.release(component); 301 } 302 } 303 304 307 public boolean hasComponent(Object hint) { 308 boolean exists = super.hasComponent(hint); 309 if (!exists && this.parentSelector != null) { 310 exists = this.parentSelector.hasComponent(hint); 311 } 312 return exists; 313 } 314 315 322 protected boolean hasDeclaredComponent(Object hint) { 323 return super.hasComponent(hint); 324 } 325 326 329 public void setParentLocator(ComponentLocator locator) 330 throws ComponentException { 331 if (this.parentSelector != null) { 332 throw new ComponentException(null, "Parent selector is already set"); 333 } 334 this.parentLocator = locator; 335 this.parentSelector = (ExtendedComponentSelector) locator.lookup(); 336 } 337 338 341 public void dispose() { 342 super.dispose(); 343 if (this.parentLocator != null) { 344 this.parentLocator.release(this.parentSelector); 345 this.parentLocator = null; 346 this.parentSelector = null; 347 } 348 } 349 350 353 protected boolean canRelease(Component component) { 354 if (this.parentSelector != null && this.parentSelector.canRelease(component)) { 355 return true; 356 } 357 358 return super.canRelease(component); 359 } 360 } 361 | Popular Tags |