KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > arooa > reflect > DefaultRegistryLookup


1 /*
2  * (c) Rob Gordon 2005
3  */

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 JavaDoc;
15
16 /**
17  *
18  */

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 JavaDoc elementTag, ArooaContext context) {
26         if (context == null) {
27             throw new NullPointerException JavaDoc("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 JavaDoc 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     /**
61      * Set a property on a Component in the registry. This method
62      * will resolve the valueFor before setting the property. This might
63      * not be the desired result for things like variables. The
64      * way round this would be to create a dummy type ProxyType which
65      * the DynaClass of VariablesJob, SetJob etc would return and which
66      * valueFor methods would recognize and would return themselves as the
67      * valueFor - but that's a lot of work and propbably isn't requied
68      * much.
69      * <p>
70      * This method should really be a lot better at resolving the types of
71      * nested beans for property conversion. This method only goes as far as
72      * resolving past component id so the correct type of component is used.
73      */

74     public void setProperty(String JavaDoc property, Object JavaDoc 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 JavaDoc compName = property.substring(0, delimIndex);
80         Object JavaDoc component = registry.objectForPath(new Path(compName));
81         String JavaDoc propertyName = property.substring(delimIndex + 1);
82         Object JavaDoc newValue = value;
83         if (value != null) {
84             Class JavaDoc type = bubh.getPropertyType(component, propertyName);
85             newValue = IntrospectionHelper.valueFor(value, type);
86         }
87         bubh.setProperty(component, propertyName, newValue);
88     }
89     
90     public Object JavaDoc getProperty(String JavaDoc fullName) {
91         int delimIndex = fullName.indexOf(PropertyUtils.NESTED_DELIM);
92         
93         String JavaDoc propertyName;
94         String JavaDoc 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 JavaDoc 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