1 package org.jbpm.instantiation; 2 3 import java.lang.reflect.*; 4 import org.apache.commons.logging.*; 5 import org.dom4j.*; 6 7 public class XmlInstantiator implements Instantiator { 8 9 private static final Class [] parameterTypes = new Class [] {Element.class}; 10 11 public Object instantiate(Class clazz, String configuration) { 12 Object newInstance = null; 13 try { 14 Element configurationElement = parseConfiguration(configuration); 16 17 Constructor constructor = clazz.getDeclaredConstructor( parameterTypes ); 18 constructor.setAccessible(true); 19 newInstance = constructor.newInstance( new Object [] { configurationElement } ); 20 } catch (Exception e) { 21 log.error( "couldn't instantiate '" + clazz.getName() + "'", e ); 22 throw new RuntimeException ( e ); 23 } 24 return newInstance; 25 } 26 27 protected Element parseConfiguration(String configuration) { 28 Element element = null; 29 try { 30 element = DocumentHelper.parseText( "<action>"+configuration+"</action>" ).getRootElement(); 31 } catch (DocumentException e) { 32 log.error( "couldn't parse bean configuration : " + configuration, e ); 33 throw new RuntimeException (e); 34 } 35 return element; 36 } 37 38 private static final Log log = LogFactory.getLog(XmlInstantiator.class); 39 } 40 | Popular Tags |