1 4 package org.oddjob.arooa.reflect; 5 6 import org.apache.commons.beanutils.BeanUtilsBean; 7 import org.apache.commons.beanutils.PropertyUtils; 8 import org.oddjob.arooa.ArooaException; 9 import org.oddjob.arooa.ArooaContext; 10 import org.oddjob.arooa.ArooaConstants; 11 import org.oddjob.arooa.Location; 12 import org.oddjob.arooa.registry.ComponentRegistry; 13 import org.oddjob.arooa.registry.Path; 14 import org.xml.sax.Locator ; 15 16 19 public class DefaultRegistryLookup implements RegistryLookup { 20 21 private final BeanUtilsBeanHelper bubh; 22 private final ComponentRegistry registry; 23 private boolean strictSubstituation; 24 25 public DefaultRegistryLookup(String elementTag, ArooaContext context) { 26 if (context == null) { 27 throw new NullPointerException ("Context can't be null!"); 28 } 29 registry = (ComponentRegistry) context.get(ArooaConstants.COMPONENT_REGISTRY); 30 this.strictSubstituation = (ArooaConstants.SUBPOLICY_STRICT.equals( 31 context.get(ArooaConstants.SUBSTITUTION_POLICY))); 32 BeanUtilsBean bub = (BeanUtilsBean) context.get(ArooaConstants.BEAN_UTILS_BEAN); 33 if (bub == null) { 34 this.bubh = new BeanUtilsBeanHelper(BeanUtilsBean.getInstance()); 35 } else { 36 Locator locator = context.getLocator(); 37 Location location; 38 if (locator != null) { 39 location = new Location(locator.getSystemId(), locator 40 .getLineNumber(), locator.getColumnNumber()); 41 } else { 42 location = null; 43 } 44 this.bubh = new BeanUtilsBeanHelper(bub, elementTag, location); 45 } 46 47 } 48 49 public DefaultRegistryLookup(ComponentRegistry registry) { 50 this(registry, 51 BeanUtilsBean.getInstance()); 52 } 53 54 public DefaultRegistryLookup(ComponentRegistry registry, BeanUtilsBean bub) { 55 this.registry = registry; 56 this.bubh = new BeanUtilsBeanHelper(bub); 57 } 58 59 60 74 public void setProperty(String property, Object value) { 75 int delimIndex = property.indexOf(PropertyUtils.NESTED_DELIM); 76 if ( delimIndex < 0) { 77 throw new ArooaException("Can't specify an id as a property to set."); 78 } 79 String compName = property.substring(0, delimIndex); 80 Object component = registry.objectForPath(new Path(compName)); 81 String propertyName = property.substring(delimIndex + 1); 82 Object newValue = value; 83 if (value != null) { 84 Class type = bubh.getPropertyType(component, propertyName); 85 newValue = IntrospectionHelper.valueFor(value, type); 86 } 87 bubh.setProperty(component, propertyName, newValue); 88 } 89 90 public Object getProperty(String fullName) { 91 int delimIndex = fullName.indexOf(PropertyUtils.NESTED_DELIM); 92 93 String propertyName; 94 String compName; 95 96 if ( delimIndex < 0) { 97 compName = fullName; 98 propertyName = null; 99 } 100 else { 101 compName = fullName.substring(0, delimIndex); 102 propertyName = fullName.substring(delimIndex + 1); 103 } 104 105 Object component = registry.objectForPath(new Path(compName)); 106 if (propertyName == null) { 107 return component; 108 } 109 if (component == null) { 110 if (strictSubstituation) { 111 throw new ArooaException("No such component [" + compName + "]"); 112 } 113 else { 114 return null; 115 } 116 } 117 return bubh.getProperty(component, propertyName); 118 } 119 120 121 } 122 | Popular Tags |