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.configuration;
023:
024: import java.util.ArrayList;
025: import java.util.HashMap;
026: import java.util.Iterator;
027: import java.util.List;
028: import java.util.Map;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.jbpm.JbpmContext;
033: import org.jbpm.svc.Services;
034: import org.jbpm.util.XmlUtil;
035: import org.w3c.dom.Element;
036:
037: public class JbpmContextInfo extends AbstractObjectInfo {
038:
039: private static final long serialVersionUID = 1L;
040:
041: Map serviceFactoryObjectInfos = null;
042: Map serviceFactories = null;
043: List serviceNames = null;
044:
045: ObjectInfo[] saveOperationObjectInfos = null;
046: List saveOperations = null;
047:
048: public JbpmContextInfo(Element jbpmContextElement,
049: ObjectFactoryParser objectFactoryParser) {
050: super (verifyDefaultName(jbpmContextElement),
051: objectFactoryParser);
052: if (jbpmContextElement.hasAttribute("singleton")) {
053: throw new ConfigurationException(
054: "attribute 'singleton' is not allowed in element 'jbpm-context'");
055: }
056:
057: // parse the services
058: serviceFactoryObjectInfos = new HashMap();
059: List serviceElements = XmlUtil.elements(jbpmContextElement,
060: "service");
061: serviceNames = new ArrayList();
062: Iterator iter = serviceElements.iterator();
063: while (iter.hasNext()) {
064: Element serviceElement = (Element) iter.next();
065: if (!serviceElement.hasAttribute("name")) {
066: throw new ConfigurationException(
067: "name is required in service element "
068: + XmlUtil.toString(serviceElement));
069: }
070: String serviceName = serviceElement.getAttribute("name");
071: serviceNames.add(serviceName);
072: ObjectInfo serviceFactoryObjectInfo = null;
073: Element factoryElement = XmlUtil.element(serviceElement,
074: "factory");
075: if (factoryElement != null) {
076: Element factoryBeanElement = XmlUtil
077: .element(factoryElement);
078: if (factoryBeanElement == null) {
079: throw new ConfigurationException(
080: "element 'factory' requires either a bean or ref element");
081: }
082: serviceFactoryObjectInfo = objectFactoryParser
083: .parse(factoryBeanElement);
084:
085: if (serviceElement.hasAttribute("factory")) {
086: log
087: .warn("duplicate factory specification for service "
088: + serviceName
089: + ", using the factory element");
090: }
091: } else if (serviceElement.hasAttribute("factory")) {
092: String factoryClassName = serviceElement
093: .getAttribute("factory");
094: BeanInfo beanInfo = new BeanInfo();
095: beanInfo.setClassName(factoryClassName);
096: serviceFactoryObjectInfo = beanInfo;
097: } else {
098: throw new ConfigurationException(
099: "element 'service' requires either a factory attribute or a factory element");
100: }
101:
102: serviceFactoryObjectInfos.put(serviceName,
103: serviceFactoryObjectInfo);
104: }
105:
106: // parse the save operations
107: Element saveOperationsElement = XmlUtil.element(
108: jbpmContextElement, "save-operations");
109: if (saveOperationsElement != null) {
110: List saveOperationElements = XmlUtil.elements(
111: saveOperationsElement, "save-operation");
112: saveOperationObjectInfos = new ObjectInfo[saveOperationElements
113: .size()];
114: for (int i = 0; i < saveOperationElements.size(); i++) {
115: Element saveOperationElement = (Element) saveOperationElements
116: .get(i);
117:
118: if (saveOperationElement.hasAttribute("class")) {
119: String saveOperationClassName = saveOperationElement
120: .getAttribute("class");
121: BeanInfo beanInfo = new BeanInfo();
122: beanInfo.setClassName(saveOperationClassName);
123: saveOperationObjectInfos[i] = beanInfo;
124: } else {
125: Element saveOperationBeanElement = XmlUtil
126: .element(saveOperationElement);
127: if (saveOperationBeanElement == null) {
128: throw new ConfigurationException(
129: "element 'save-operation' requires either a class attribute or an element of type 'bean' or 'ref'");
130: }
131: saveOperationObjectInfos[i] = objectFactoryParser
132: .parse(saveOperationBeanElement);
133: }
134: }
135: }
136: }
137:
138: static Element verifyDefaultName(Element jbpmContextElement) {
139: if (!jbpmContextElement.hasAttribute("name")) {
140: jbpmContextElement.setAttribute("name",
141: JbpmContext.DEFAULT_JBPM_CONTEXT_NAME);
142: }
143: return jbpmContextElement;
144: }
145:
146: public synchronized Object createObject(
147: ObjectFactoryImpl objectFactory) {
148: if (serviceFactories == null) {
149: serviceFactories = new HashMap();
150: Iterator iter = serviceFactoryObjectInfos.entrySet()
151: .iterator();
152: while (iter.hasNext()) {
153: Map.Entry entry = (Map.Entry) iter.next();
154: String serviceName = (String) entry.getKey();
155: ObjectInfo serviceFactoryObjectInfo = (ObjectInfo) entry
156: .getValue();
157: serviceFactories.put(serviceName,
158: serviceFactoryObjectInfo
159: .createObject(objectFactory));
160: }
161: }
162:
163: if ((saveOperations == null)
164: && (saveOperationObjectInfos != null)) {
165: saveOperations = new ArrayList();
166: for (int i = 0; i < saveOperationObjectInfos.length; i++) {
167: Object saveOperation = saveOperationObjectInfos[i]
168: .createObject(objectFactory);
169: saveOperations.add(saveOperation);
170: }
171: }
172:
173: Services services = new Services(serviceFactories,
174: serviceNames, saveOperations);
175:
176: if (log.isDebugEnabled())
177: log.debug("creating jbpm context with service factories '"
178: + serviceFactories.keySet() + "'");
179: return new JbpmContext(services, objectFactory);
180: }
181:
182: private static Log log = LogFactory.getLog(JbpmContextInfo.class);
183: }
|