1 package org.jbpm.instantiation; 2 3 import java.io.*; 4 import java.util.*; 5 6 import org.apache.commons.logging.*; 7 import org.dom4j.*; 8 import org.dom4j.Node; 9 import org.dom4j.io.*; 10 import org.jbpm.graph.def.*; 11 import org.jbpm.jpdl.xml.*; 12 13 public class Delegation implements Parsable, Serializable { 14 15 private static final long serialVersionUID = 1L; 16 17 protected static Map instantiatorCache = new HashMap(); 18 static { 19 instantiatorCache.put(null, new FieldInstantiator()); 20 instantiatorCache.put("field", new FieldInstantiator()); 21 instantiatorCache.put("bean", new BeanInstantiator()); 22 instantiatorCache.put("constructor", new ConstructorInstantiator()); 23 instantiatorCache.put("configuration-property", new ConfigurationPropertyInstantiator()); 24 } 25 26 long id = 0; 27 protected String className = null; 28 protected String configuration = null; 29 protected String configType = null; 30 protected ProcessDefinition processDefinition = null; 31 private transient Object instance = null; 32 33 public Delegation() { 34 } 35 36 public Delegation(Object instance) { 37 this.instance = instance; 38 } 39 40 public Delegation(String className) { 41 this.className = className; 42 } 43 44 public void read(Element delegateElement, JpdlXmlReader jpdlReader) { 45 processDefinition = jpdlReader.getProcessDefinition(); 46 className = delegateElement.attributeValue("class"); 47 if (className==null) { 48 jpdlReader.addWarning("no class specified in "+delegateElement.asXML()); 49 } 50 51 configType = delegateElement.attributeValue("config-type"); 52 if ( delegateElement.hasContent() ) { 53 try { 54 StringWriter stringWriter = new StringWriter(); 55 XMLWriter xmlWriter = new XMLWriter( stringWriter, OutputFormat.createCompactFormat() ); 57 Iterator iter = delegateElement.content().iterator(); 58 while (iter.hasNext()) { 59 xmlWriter.write( iter.next() ); 60 } 61 xmlWriter.flush(); 62 configuration = stringWriter.toString(); 63 } catch (IOException e) { 64 jpdlReader.addWarning("io problem while parsing the configuration of "+delegateElement.asXML()); 65 } 66 } 67 } 68 69 public void write(Element element) { 70 element.addAttribute("class", className); 71 element.addAttribute("config-type", configType); 72 String configuration = this.configuration; 73 if (configuration!=null) { 74 try { 75 Element actionElement = DocumentHelper.parseText( "<action>"+configuration+"</action>" ).getRootElement(); 76 Iterator iter = new ArrayList( actionElement.content() ).iterator(); 77 while (iter.hasNext()) { 78 Node node = (Node)iter.next(); 79 node.setParent(null); 80 element.add( node ); 81 } 82 } catch (DocumentException e) { 83 log.error("couldn't create dom-tree for action configuration '"+configuration+"'", e); 84 } 85 } 86 } 87 88 public Object getInstance() { 89 if (instance==null) { 90 instance = instantiate(); 91 } 92 return instance; 93 } 94 95 public Object instantiate() { 96 97 Object newInstance = null; 98 99 ClassLoader classLoader = ClassLoaderUtil.getProcessClassLoader(processDefinition); 101 102 Class clazz = null; 104 try { 105 clazz = classLoader.loadClass(className); 106 } catch (ClassNotFoundException e) { 107 log.error("couldn't load delegation class '"+className+"'", e); 108 } 109 110 Instantiator instantiator = null; 111 try { 112 instantiator = (Instantiator) instantiatorCache.get(configType); 114 if (instantiator == null) { 115 Class instantiatorClass = classLoader.loadClass(configType); 117 instantiator = (Instantiator) instantiatorClass.newInstance(); 119 instantiatorCache.put(configType, instantiator); 120 } 121 } catch (Exception e) { 122 log.error(e); 123 throw new RuntimeException ("couldn't instantiate custom instantiator '" + configType + "'", e); 124 } 125 126 try { 127 newInstance = instantiator.instantiate(clazz, configuration); 129 } catch (RuntimeException e) { 130 log.error("couldn't instantiate delegation class '"+className+"'", e); 131 } 132 133 return newInstance; 134 } 135 136 public String getClassName() { 137 return className; 138 } 139 140 public void setClassName(String className) { 141 this.className = className; 142 } 143 144 public String getConfiguration() { 145 return configuration; 146 } 147 148 public void setConfiguration(String configuration) { 149 this.configuration = configuration; 150 } 151 152 public String getConfigType() { 153 return configType; 154 } 155 156 public void setConfigType(String instantiatorType) { 157 this.configType = instantiatorType; 158 } 159 160 public long getId() { 161 return id; 162 } 163 164 public void setId(long id) { 165 this.id = id; 166 } 167 168 public ProcessDefinition getProcessDefinition() { 169 return processDefinition; 170 } 171 public void setProcessDefinition(ProcessDefinition processDefinition) { 172 this.processDefinition = processDefinition; 173 } 174 175 private static final Log log = LogFactory.getLog(Delegation.class); 176 177 } 178 | Popular Tags |