KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > defaults > BeanPropertyComponentAdapter


1 package org.picocontainer.defaults;
2
3 import org.picocontainer.ComponentAdapter;
4 import org.picocontainer.PicoContainer;
5 import org.picocontainer.PicoInitializationException;
6 import org.picocontainer.PicoIntrospectionException;
7
8 import java.io.File JavaDoc;
9 import java.lang.reflect.Method JavaDoc;
10 import java.net.MalformedURLException JavaDoc;
11 import java.net.URL JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.Set JavaDoc;
15
16 /**
17  * Decorating component adapter that can be used to set additional properties
18  * on a component in a bean style. These properties must be managed manually
19  * by the user of the API, and will not be managed by PicoContainer. This class
20  * is therefore <em>not</em> the same as {@link SetterInjectionComponentAdapter},
21  * which is a true Setter Injection adapter.
22  * <p/>
23  * This adapter is mostly handy for setting various primitive properties via setters.
24  * <p/>
25  * <em>
26  * Note that this class doesn't cache instances. If you want caching,
27  * use a {@link CachingComponentAdapter} around this one.
28  * </em>
29  *
30  * @author Aslak Helles&oslash;y
31  * @version $Revision: 1812 $
32  * @since 1.0
33  */

34 public class BeanPropertyComponentAdapter extends DecoratingComponentAdapter {
35     private Map JavaDoc properties;
36     private transient Map JavaDoc setters = null;
37
38     /**
39      * Construct a BeanProeprtyComponentAdapter.
40      *
41      * @param delegate the wrapped {@link ComponentAdapter}
42      * @throws PicoInitializationException {@inheritDoc}
43      */

44     public BeanPropertyComponentAdapter(ComponentAdapter delegate) throws PicoInitializationException {
45         super(delegate);
46     }
47
48     /**
49      * Get a component instance and set given property values.
50      *
51      * @return the component instance with any properties of the properties map set.
52      * @throws PicoInitializationException {@inheritDoc}
53      * @throws PicoIntrospectionException {@inheritDoc}
54      * @throws AssignabilityRegistrationException
55      * {@inheritDoc}
56      * @throws NotConcreteRegistrationException
57      * {@inheritDoc}
58      * @see #setProperties(Map)
59      */

60     public Object JavaDoc getComponentInstance(PicoContainer container) throws PicoInitializationException, PicoIntrospectionException, AssignabilityRegistrationException, NotConcreteRegistrationException {
61         final Object JavaDoc componentInstance = super.getComponentInstance(container);
62         if (setters == null) {
63             setters = new SetterIntrospector().getSetters(getComponentImplementation());
64         }
65
66         if (properties != null) {
67             Set JavaDoc propertyNames = properties.keySet();
68             for (Iterator JavaDoc iterator = propertyNames.iterator(); iterator.hasNext();) {
69                 final String JavaDoc propertyName = (String JavaDoc) iterator.next();
70                 final String JavaDoc propertyValue = (String JavaDoc) properties.get(propertyName);
71                 Method JavaDoc setter = (Method JavaDoc) setters.get(propertyName);
72                 Object JavaDoc value = null;
73                 try {
74                     value = convertType(container, setter, propertyValue);
75                 } catch (ClassNotFoundException JavaDoc e) {
76                     throw new PicoInvocationTargetInitializationException(e);
77                 }
78                 try {
79                     setter.invoke(componentInstance, new Object JavaDoc[]{value});
80                 } catch (final Exception JavaDoc e) {
81                     throw new PicoInitializationException("Failed to set property " + propertyName + " to " + propertyValue + ": " + e.getMessage(), e);
82                 }
83             }
84         }
85         return componentInstance;
86     }
87
88     private Object JavaDoc convertType(PicoContainer container, Method JavaDoc setter, String JavaDoc propertyValue) throws ClassNotFoundException JavaDoc {
89         if (propertyValue == null) {
90             return null;
91         }
92         Class JavaDoc type = setter.getParameterTypes()[0];
93         String JavaDoc typeName = type.getName();
94
95         Object JavaDoc result = convert(typeName, propertyValue, Thread.currentThread().getContextClassLoader());
96
97         if (result == null) {
98
99             // check if the propertyValue is a key of a component in the container
100
// if so, the typeName of the component and the setters parameter typeName
101
// have to be compatible
102

103             // TODO: null check only because of test-case, otherwise null is impossible
104
if (container != null) {
105                 Object JavaDoc component = container.getComponentInstance(propertyValue);
106                 if (component != null && type.isAssignableFrom(component.getClass())) {
107                     return component;
108                 }
109             }
110         }
111         return result;
112     }
113
114     /**
115      * Converts a type name to an object.
116      *
117      * @param typeName name of the type
118      * @param value its value
119      * @param classLoader used to load a class if typeName is "class" or "java.lang.Class" (ignored otherwise)
120      * @return instantiated object or null if the type was unknown/unsupported
121      * @throws ClassNotFoundException if typeName is "class" or "java.lang.Class" and class couldn't be loaded.
122      */

123     public static Object JavaDoc convert(String JavaDoc typeName, String JavaDoc value, ClassLoader JavaDoc classLoader) throws ClassNotFoundException JavaDoc {
124         if (typeName.equals(Boolean JavaDoc.class.getName()) || typeName.equals(boolean.class.getName())) {
125             return Boolean.valueOf(value);
126         } else if (typeName.equals(Byte JavaDoc.class.getName()) || typeName.equals(byte.class.getName())) {
127             return Byte.valueOf(value);
128         } else if (typeName.equals(Short JavaDoc.class.getName()) || typeName.equals(short.class.getName())) {
129             return Short.valueOf(value);
130         } else if (typeName.equals(Integer JavaDoc.class.getName()) || typeName.equals(int.class.getName())) {
131             return Integer.valueOf(value);
132         } else if (typeName.equals(Long JavaDoc.class.getName()) || typeName.equals(long.class.getName())) {
133             return Long.valueOf(value);
134         } else if (typeName.equals(Float JavaDoc.class.getName()) || typeName.equals(float.class.getName())) {
135             return Float.valueOf(value);
136         } else if (typeName.equals(Double JavaDoc.class.getName()) || typeName.equals(double.class.getName())) {
137             return Double.valueOf(value);
138         } else if (typeName.equals(Character JavaDoc.class.getName()) || typeName.equals(char.class.getName())) {
139             return new Character JavaDoc(value.toCharArray()[0]);
140         } else if (typeName.equals(String JavaDoc.class.getName()) || typeName.equals("string")) {
141             return value;
142         } else if (typeName.equals(File JavaDoc.class.getName()) || typeName.equals("file")) {
143             return new File JavaDoc(value);
144         } else if (typeName.equals(URL JavaDoc.class.getName()) || typeName.equals("url")) {
145             try {
146                 return new URL JavaDoc(value);
147             } catch (MalformedURLException JavaDoc e) {
148                 throw new PicoInitializationException(e);
149             }
150         } else if (typeName.equals(Class JavaDoc.class.getName()) || typeName.equals("class")) {
151             return classLoader.loadClass(value);
152         }
153         return null;
154     }
155
156     /**
157      * Sets the bean property values that should be set upon creation.
158      *
159      * @param properties bean properties
160      */

161     public void setProperties(Map JavaDoc properties) {
162         this.properties = properties;
163     }
164 }
Popular Tags