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 instantiate(Class clazz, String configuration) { 12 13 Object newInstance = newInstance(clazz); 15 16 Element configurationElement = parseConfiguration(configuration); 18 19 Iterator iter = configurationElement.elements().iterator(); 21 while( iter.hasNext() ) { 22 Element propertyElement = (Element) iter.next(); 23 String propertyName = propertyElement.getName(); 24 setPropertyValue(clazz, newInstance, propertyName, propertyElement); 25 } 26 return newInstance; 27 } 28 29 protected void setPropertyValue(Class clazz, Object newInstance, String 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 e) { 35 log.error( "couldn't parse set field '"+propertyName+"' to value '"+propertyElement.asXML()+"'", e ); 36 } 37 } 38 39 protected Element parseConfiguration(String 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 (e); 46 } 47 return element; 48 } 49 50 protected Object newInstance(Class clazz) { 51 Object newInstance = null; 52 try { 53 newInstance = clazz.newInstance(); 54 } catch (Exception e) { 55 log.error( "couldn't instantiate type '" + clazz.getName() + "' with the default constructor" ); 56 throw new RuntimeException (e); 57 } 58 return newInstance; 59 } 60 61 public static Object getValue(Class type, Element propertyElement) { 62 Object value = null; 64 try { 65 66 if ( type == String .class ) { 67 value = propertyElement.getTextTrim(); 68 } else if ( (type==Integer .class) || (type==int.class) ) { 69 value = new Integer ( propertyElement.getTextTrim() ); 70 } else if ( (type==Long .class) || (type==long.class) ) { 71 value = new Long ( propertyElement.getTextTrim() ); 72 } else if ( (type==Float .class ) || (type==float.class) ) { 73 value = new Float ( propertyElement.getTextTrim() ); 74 } else if ( (type==Double .class ) || (type==double.class) ) { 75 value = new Double ( propertyElement.getTextTrim() ); 76 } else if ( (type==Boolean .class ) || (type==boolean.class) ) { 77 value = Boolean.valueOf( propertyElement.getTextTrim() ); 78 } else if ( (type==Character .class ) || (type==char.class) ) { 79 value = new Character ( propertyElement.getTextTrim().charAt(0) ); 80 } else if ( (type==Short .class ) || (type==short.class) ) { 81 value = new Short ( propertyElement.getTextTrim() ); 82 } else if ( (type==Byte .class ) || (type==byte.class) ) { 83 value = new Byte ( 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 []{String .class}); 96 if ( (propertyElement.isTextOnly()) 97 && (constructor!=null) ) { 98 value = constructor.newInstance(new Object []{propertyElement.getTextTrim()}); 99 } 100 } 101 } catch (Exception e) { 102 log.error("couldn't parse the bean property value '" + propertyElement.asXML() + "' to a '" + type.getName() + "'" ); 103 throw new RuntimeException ( e ); 104 } 105 return value; 106 } 107 108 private static Object getMap(Element mapElement, Map map) { 109 Class keyClass = String .class; 110 String keyType = mapElement.attributeValue("key-type"); 111 if (keyType!=null) { 112 keyClass = ClassLoaderUtil.loadClass(keyType); 113 } 114 115 Class valueClass = String .class; 116 String 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 getCollection(Element collectionElement, Collection collection) { 133 Class elementClass = String .class; 134 String 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 |