001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jbpm.instantiation;
023:
024: import java.io.IOException;
025: import java.io.Serializable;
026: import java.io.StringWriter;
027: import java.util.ArrayList;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.Map;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.dom4j.DocumentException;
035: import org.dom4j.DocumentHelper;
036: import org.dom4j.Element;
037: import org.dom4j.Node;
038: import org.dom4j.io.OutputFormat;
039: import org.dom4j.io.XMLWriter;
040: import org.jbpm.JbpmException;
041: import org.jbpm.graph.def.ProcessDefinition;
042: import org.jbpm.jpdl.xml.JpdlXmlReader;
043: import org.jbpm.jpdl.xml.Parsable;
044: import org.jbpm.util.ClassLoaderUtil;
045: import org.jbpm.util.EqualsUtil;
046:
047: public class Delegation implements Parsable, Serializable {
048:
049: private static final long serialVersionUID = 1L;
050:
051: protected static Map instantiatorCache = new HashMap();
052: static {
053: instantiatorCache.put(null, new FieldInstantiator());
054: instantiatorCache.put("field", new FieldInstantiator());
055: instantiatorCache.put("bean", new BeanInstantiator());
056: instantiatorCache.put("constructor",
057: new ConstructorInstantiator());
058: instantiatorCache.put("configuration-property",
059: new ConfigurationPropertyInstantiator());
060: }
061:
062: long id = 0;
063: protected String className = null;
064: protected String configuration = null;
065: protected String configType = null;
066: protected ProcessDefinition processDefinition = null;
067: transient Object instance = null;
068:
069: public Delegation() {
070: }
071:
072: public Delegation(Object instance) {
073: this .instance = instance;
074: }
075:
076: public Delegation(String className) {
077: this .className = className;
078: }
079:
080: public void read(Element delegateElement, JpdlXmlReader jpdlReader) {
081: processDefinition = jpdlReader.getProcessDefinition();
082: className = delegateElement.attributeValue("class");
083: if (className == null) {
084: jpdlReader.addWarning("no class specified in "
085: + delegateElement.asXML());
086: }
087:
088: configType = delegateElement.attributeValue("config-type");
089: if (delegateElement.hasContent()) {
090: try {
091: StringWriter stringWriter = new StringWriter();
092: // when parsing, it could be to store the config in the database, so we want to make the configuration compact
093: XMLWriter xmlWriter = new XMLWriter(stringWriter,
094: OutputFormat.createCompactFormat());
095: Iterator iter = delegateElement.content().iterator();
096: while (iter.hasNext()) {
097: Object node = iter.next();
098: xmlWriter.write(node);
099: }
100: xmlWriter.flush();
101: configuration = stringWriter.toString();
102: } catch (IOException e) {
103: jpdlReader
104: .addWarning("io problem while parsing the configuration of "
105: + delegateElement.asXML());
106: }
107: }
108: }
109:
110: public void write(Element element) {
111: element.addAttribute("class", className);
112: element.addAttribute("config-type", configType);
113: String configuration = this .configuration;
114: if (configuration != null) {
115: try {
116: Element actionElement = DocumentHelper.parseText(
117: "<action>" + configuration + "</action>")
118: .getRootElement();
119: Iterator iter = new ArrayList(actionElement.content())
120: .iterator();
121: while (iter.hasNext()) {
122: Node node = (Node) iter.next();
123: node.setParent(null);
124: element.add(node);
125: }
126: } catch (DocumentException e) {
127: log.error(
128: "couldn't create dom-tree for action configuration '"
129: + configuration + "'", e);
130: }
131: }
132: }
133:
134: public Object getInstance() {
135: if (instance == null) {
136: instance = instantiate();
137: }
138: return instance;
139: }
140:
141: public Object instantiate() {
142:
143: Object newInstance = null;
144:
145: // find the classloader to use
146: ClassLoader classLoader = ClassLoaderUtil
147: .getProcessClassLoader(processDefinition);
148:
149: // load the class that needs to be instantiated
150: Class clazz = null;
151: try {
152: clazz = classLoader.loadClass(className);
153: } catch (ClassNotFoundException e) {
154: log.error("couldn't load delegation class '" + className
155: + "'", e);
156: }
157:
158: Instantiator instantiator = null;
159: try {
160: // find the instantiator
161: instantiator = (Instantiator) instantiatorCache
162: .get(configType);
163: if (instantiator == null) {
164: // load the instantiator class
165: Class instantiatorClass = classLoader
166: .loadClass(configType);
167: // instantiate the instantiator with the default constructor
168: instantiator = (Instantiator) instantiatorClass
169: .newInstance();
170: instantiatorCache.put(configType, instantiator);
171: }
172: } catch (Exception e) {
173: log.error(e);
174: throw new JbpmException(
175: "couldn't instantiate custom instantiator '"
176: + configType + "'", e);
177: }
178:
179: try {
180: // instantiate the object
181: newInstance = instantiator
182: .instantiate(clazz, configuration);
183: } catch (RuntimeException e) {
184: log.error("couldn't instantiate delegation class '"
185: + className + "'", e);
186: }
187:
188: return newInstance;
189: }
190:
191: // equals ///////////////////////////////////////////////////////////////////
192: // hack to support comparing hibernate proxies against the real objects
193: // since this always falls back to ==, we don't need to overwrite the hashcode
194: public boolean equals(Object o) {
195: return EqualsUtil.equals(this , o);
196: }
197:
198: // getters and setters //////////////////////////////////////////////////////
199:
200: public String getClassName() {
201: return className;
202: }
203:
204: public void setClassName(String className) {
205: this .className = className;
206: }
207:
208: public String getConfiguration() {
209: return configuration;
210: }
211:
212: public void setConfiguration(String configuration) {
213: this .configuration = configuration;
214: }
215:
216: public String getConfigType() {
217: return configType;
218: }
219:
220: public void setConfigType(String instantiatorType) {
221: this .configType = instantiatorType;
222: }
223:
224: public long getId() {
225: return id;
226: }
227:
228: public void setId(long id) {
229: this .id = id;
230: }
231:
232: public ProcessDefinition getProcessDefinition() {
233: return processDefinition;
234: }
235:
236: public void setProcessDefinition(ProcessDefinition processDefinition) {
237: this .processDefinition = processDefinition;
238: }
239:
240: private static final Log log = LogFactory.getLog(Delegation.class);
241: }
|