KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > instantiation > FieldInstantiator


1 package org.jbpm.instantiation;
2
3 import java.lang.reflect.*;
4 import java.util.*;
5
6 import org.apache.commons.logging.*;
7 import org.dom4j.*;
8
9 public class FieldInstantiator implements Instantiator {
10
11   public Object JavaDoc instantiate(Class JavaDoc clazz, String JavaDoc configuration) {
12
13     // create a new instance with the default constructor
14
Object JavaDoc newInstance = newInstance(clazz);
15
16     // parse the bean configuration
17
Element configurationElement = parseConfiguration(configuration);
18     
19     // loop over the configured properties
20
Iterator iter = configurationElement.elements().iterator();
21     while( iter.hasNext() ) {
22       Element propertyElement = (Element) iter.next();
23       String JavaDoc propertyName = propertyElement.getName();
24       setPropertyValue(clazz, newInstance, propertyName, propertyElement);
25     }
26     return newInstance;
27   }
28   
29   protected void setPropertyValue(Class JavaDoc clazz, Object JavaDoc newInstance, String JavaDoc propertyName, Element propertyElement) {
30     try {
31       Field f = clazz.getDeclaredField(propertyName);
32       f.setAccessible(true);
33       f.set(newInstance, getValue(f.getType(), propertyElement));
34     } catch (Exception JavaDoc e) {
35       log.error( "couldn't parse set field '"+propertyName+"' to value '"+propertyElement.asXML()+"'", e );
36     }
37   }
38
39   protected Element parseConfiguration(String JavaDoc configuration) {
40     Element element = null;
41     try {
42       element = DocumentHelper.parseText( "<action>"+configuration+"</action>" ).getRootElement();
43     } catch (DocumentException e) {
44       log.error( "couldn't parse bean configuration : " + configuration, e );
45       throw new RuntimeException JavaDoc(e);
46     }
47     return element;
48   }
49
50   protected Object JavaDoc newInstance(Class JavaDoc clazz) {
51     Object JavaDoc newInstance = null;
52     try {
53       newInstance = clazz.newInstance();
54     } catch (Exception JavaDoc e) {
55       log.error( "couldn't instantiate type '" + clazz.getName() + "' with the default constructor" );
56       throw new RuntimeException JavaDoc(e);
57     }
58     return newInstance;
59   }
60
61   public static Object JavaDoc getValue(Class JavaDoc type, Element propertyElement) {
62     // parse the value
63
Object JavaDoc value = null;
64     try {
65       
66       if ( type == String JavaDoc.class ) {
67         value = propertyElement.getTextTrim();
68       } else if ( (type==Integer JavaDoc.class) || (type==int.class) ) {
69         value = new Integer JavaDoc( propertyElement.getTextTrim() );
70       } else if ( (type==Long JavaDoc.class) || (type==long.class) ) {
71         value = new Long JavaDoc( propertyElement.getTextTrim() );
72       } else if ( (type==Float JavaDoc.class ) || (type==float.class) ) {
73         value = new Float JavaDoc( propertyElement.getTextTrim() );
74       } else if ( (type==Double JavaDoc.class ) || (type==double.class) ) {
75         value = new Double JavaDoc( propertyElement.getTextTrim() );
76       } else if ( (type==Boolean JavaDoc.class ) || (type==boolean.class) ) {
77         value = Boolean.valueOf( propertyElement.getTextTrim() );
78       } else if ( (type==Character JavaDoc.class ) || (type==char.class) ) {
79         value = new Character JavaDoc( propertyElement.getTextTrim().charAt(0) );
80       } else if ( (type==Short JavaDoc.class ) || (type==short.class) ) {
81         value = new Short JavaDoc( propertyElement.getTextTrim() );
82       } else if ( (type==Byte JavaDoc.class ) || (type==byte.class) ) {
83         value = new Byte JavaDoc( propertyElement.getTextTrim() );
84       } else if (type.isAssignableFrom(List.class)) {
85         value = getCollection(propertyElement, new ArrayList());
86       } else if (type.isAssignableFrom(Set.class)) {
87         value = getCollection(propertyElement, new HashSet());
88       } else if (type.isAssignableFrom(Collection.class)) {
89         value = getCollection(propertyElement, new ArrayList());
90       } else if (type.isAssignableFrom(Map.class)) {
91         value = getMap(propertyElement, new HashMap());
92       } else if ( type==Element.class ) {
93         value = propertyElement;
94       } else {
95         Constructor constructor = type.getConstructor(new Class JavaDoc[]{String JavaDoc.class});
96         if ( (propertyElement.isTextOnly())
97              && (constructor!=null) ) {
98           value = constructor.newInstance(new Object JavaDoc[]{propertyElement.getTextTrim()});
99         }
100       }
101     } catch (Exception JavaDoc e) {
102       log.error("couldn't parse the bean property value '" + propertyElement.asXML() + "' to a '" + type.getName() + "'" );
103       throw new RuntimeException JavaDoc( e );
104     }
105     return value;
106   }
107
108   private static Object JavaDoc getMap(Element mapElement, Map map) {
109     Class JavaDoc keyClass = String JavaDoc.class;
110     String JavaDoc keyType = mapElement.attributeValue("key-type");
111     if (keyType!=null) {
112       keyClass = ClassLoaderUtil.loadClass(keyType);
113     }
114
115     Class JavaDoc valueClass = String JavaDoc.class;
116     String JavaDoc valueType = mapElement.attributeValue("value-type");
117     if (keyType!=null) {
118       valueClass = ClassLoaderUtil.loadClass(valueType);
119     }
120
121     Iterator iter = mapElement.elementIterator();
122     while (iter.hasNext()) {
123       Element element = (Element) iter.next();
124       Element keyElement = element.element("key");
125       Element valueElement = element.element("value");
126       
127       map.put(getValue(keyClass, keyElement), getValue(valueClass, valueElement));
128     }
129     return map;
130   }
131
132   private static Object JavaDoc getCollection(Element collectionElement, Collection collection) {
133     Class JavaDoc elementClass = String JavaDoc.class;
134     String JavaDoc elementType = collectionElement.attributeValue("element-type");
135     if (elementType!=null) {
136       elementClass = ClassLoaderUtil.loadClass(elementType);
137     }
138     Iterator iter = collectionElement.elementIterator();
139     while (iter.hasNext()) {
140       Element element = (Element) iter.next();
141       collection.add(getValue(elementClass, element));
142     }
143     return collection;
144   }
145
146   private static final Log log = LogFactory.getLog(FieldInstantiator.class);
147 }
148
Popular Tags