KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > arooa > PropertyProxyResolver


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

4 package org.oddjob.arooa;
5
6 import java.io.File JavaDoc;
7 import java.io.FileInputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.InputStream JavaDoc;
10 import java.util.Enumeration JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.Properties JavaDoc;
14
15 import org.apache.log4j.Logger;
16 import org.oddjob.arooa.reflect.IntrospectionHelper;
17
18 /**
19  * Provide a PropertyProxy Object which will be used to set a property with it's
20  * valueOf method. The proxy object is used during configuration to process the
21  * XML.
22  *
23  * @author Rob Gordon.
24  */

25 public class PropertyProxyResolver {
26     private static final Logger logger = Logger.getLogger(PropertyProxyResolver.class);
27
28     /** Class definitions */
29     private final Map JavaDoc classDefinitions = new HashMap JavaDoc();
30     private Class JavaDoc[] definitionsAsArray = new Class JavaDoc[0];
31     
32     private PropertyProxyResolver parent;
33     
34     public PropertyProxyResolver(PropertyProxyResolver parent) {
35         this.parent = parent;
36     }
37     
38     public PropertyProxyResolver() {
39         this(null);
40     }
41     
42     /**
43      * Add definitions fom a resource property file, i.e. one
44      * that is on the class path.
45      *
46      * @param classDefFileName The file name.
47      */

48     public void addResource(String JavaDoc classDefFileName) {
49
50         Properties JavaDoc props = new Properties JavaDoc();
51         try {
52             InputStream JavaDoc in = this.getClass().getResourceAsStream(classDefFileName);
53     
54             if (in == null) {
55                     throw new ArooaException("Can't load class def list");
56             }
57             props.load(in);
58             in.close();
59         }
60         catch (IOException JavaDoc e) {
61             throw new ArooaException("Failed to load class def list.", e);
62         }
63
64         addProperties(props);
65     }
66             
67     /**
68      * Add definitions fom a property file.
69      *
70      * @param classDefFileName The file name.
71      */

72     public void addFile(File JavaDoc file) {
73         
74         Properties JavaDoc props = new Properties JavaDoc();
75
76         try {
77             InputStream JavaDoc in = new FileInputStream JavaDoc(file);
78     
79             if (in == null) {
80                     throw new ArooaException("Can't load class def list");
81             }
82             props.load(in);
83             in.close();
84         }
85         catch (IOException JavaDoc e) {
86             throw new ArooaException("Failed to load class def list.", e);
87         }
88
89         addProperties(props);
90     }
91     
92     /**
93      * Add class definitions from properties.
94      *
95      * @param props Class definition properties.
96      */

97     public void addProperties(Properties JavaDoc props) {
98         
99         Enumeration JavaDoc enumuration = props.propertyNames();
100         while (enumuration.hasMoreElements()) {
101             String JavaDoc key = (String JavaDoc) enumuration.nextElement();
102             String JavaDoc value = props.getProperty(key);
103             setProxy(key, value);
104         }
105     }
106
107     public void setProxy(String JavaDoc key, String JavaDoc value) {
108         Class JavaDoc classKey = SimpleObjectFactory.loadClass(key);
109         Class JavaDoc classValue = SimpleObjectFactory.loadClass(value);
110         synchronized (classDefinitions) {
111             classDefinitions.put(classKey, classValue);
112             definitionsAsArray = (Class JavaDoc[]) classDefinitions.keySet().toArray(new Class JavaDoc[0]);
113         }
114     }
115     
116     Class JavaDoc proxyClassFor(Class JavaDoc clazz) {
117         Class JavaDoc key = IntrospectionHelper.selectBestMatchingClass(clazz, definitionsAsArray);
118         if (key == null) {
119             if (parent == null) {
120                 return null;
121             }
122             else {
123                 return parent.proxyClassFor(clazz);
124             }
125         }
126         Class JavaDoc result = null;
127         synchronized (classDefinitions) {
128             result = (Class JavaDoc) classDefinitions.get(key);
129         }
130         logger.debug("Providing " + result.getName() + " as proxy for " + clazz.getName());
131         return result;
132     }
133     
134     /**
135      * Return a Proxy Type which is capable of resolving an XML definition into
136      * a Class of the given type. The XML could be elements, attributes or
137      * text so the Type should provide approprate add/set methods.
138      * <p>
139      *
140      * @param bean The bean with the property.
141      * @param The property.
142      *
143      * @return An object which can resolve the class or null if one doesn't exist.
144      */

145     public Object JavaDoc proxyFor(Object JavaDoc bean, String JavaDoc property) {
146         // look at the attribute class to find the correct type to create.
147
IntrospectionHelper pih = IntrospectionHelper.getHelper(bean.getClass());
148         Class JavaDoc clazz = pih.getAttributeType(property);
149         if (clazz == null) {
150             throw new ArooaException("No such property [" + property
151                     + "] for class " + bean.getClass());
152         }
153         Class JavaDoc c = proxyClassFor(clazz);
154         if (c == null) {
155             return null;
156         }
157         try {
158             return c.newInstance();
159         } catch (IllegalAccessException JavaDoc e) {
160             throw new ArooaException("Illegal access attempting to create object of class "
161                 + c, e);
162         } catch (InstantiationException JavaDoc e) {
163             throw new ArooaException("InstantiationException attempting to create object of class "
164                 + c, e);
165         }
166     }
167     
168 }
169
Popular Tags