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 org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.jbpm.context.exe.Converter;
027: import org.jbpm.context.exe.JbpmType;
028: import org.jbpm.context.exe.JbpmTypeMatcher;
029: import org.jbpm.context.exe.VariableInstance;
030: import org.jbpm.db.hibernate.Converters;
031: import org.jbpm.util.ClassLoaderUtil;
032: import org.jbpm.util.XmlUtil;
033: import org.w3c.dom.Element;
034:
035: public class JbpmTypeObjectInfo extends AbstractObjectInfo {
036:
037: private static final long serialVersionUID = 1L;
038:
039: ObjectInfo typeMatcherObjectInfo = null;
040: Converter converter = null;
041: Class variableInstanceClass = null;
042:
043: public JbpmTypeObjectInfo(Element jbpmTypeElement,
044: ObjectFactoryParser objectFactoryParser) {
045: super (jbpmTypeElement, objectFactoryParser);
046:
047: try {
048: Element typeMatcherElement = XmlUtil.element(
049: jbpmTypeElement, "matcher");
050: if (typeMatcherElement == null) {
051: throw new ConfigurationException(
052: "matcher is a required element in a jbpm-type: "
053: + XmlUtil.toString(jbpmTypeElement));
054: }
055: Element typeMatcherBeanElement = XmlUtil
056: .element(typeMatcherElement);
057: typeMatcherObjectInfo = objectFactoryParser
058: .parse(typeMatcherBeanElement);
059:
060: Element converterElement = XmlUtil.element(jbpmTypeElement,
061: "converter");
062: if (converterElement != null) {
063: if (!converterElement.hasAttribute("class")) {
064: throw new ConfigurationException(
065: "class attribute is required in a converter element: "
066: + XmlUtil.toString(jbpmTypeElement));
067: }
068: String converterClassName = converterElement
069: .getAttribute("class");
070: converter = Converters
071: .getConverterByClassName(converterClassName);
072: }
073:
074: Element variableInstanceElement = XmlUtil.element(
075: jbpmTypeElement, "variable-instance");
076: if (!variableInstanceElement.hasAttribute("class")) {
077: throw new ConfigurationException(
078: "class is a required attribute in element variable-instance: "
079: + XmlUtil.toString(jbpmTypeElement));
080: }
081: String variableInstanceClassName = variableInstanceElement
082: .getAttribute("class");
083: variableInstanceClass = ClassLoaderUtil
084: .loadClass(variableInstanceClassName);
085: if (!VariableInstance.class
086: .isAssignableFrom(variableInstanceClass)) {
087: throw new ConfigurationException(
088: "variable instance class '"
089: + variableInstanceClassName
090: + "' is not a VariableInstance");
091: }
092: } catch (ConfigurationException e) {
093: throw e;
094: } catch (Exception e) {
095: // NOTE that Error's are not caught because that might halt the JVM and mask the original Error.
096: // Probably the user doesn't need support for this type and doesn't have a required library in the path.
097: // So let's log and ignore
098: log.debug("jbpm variables type "
099: + XmlUtil.toString(jbpmTypeElement)
100: + " couldn't be instantiated properly: "
101: + e.toString());
102: // now, let's make sure that this JbpmType is ignored by always returning false in the JbpmTypeMatcher
103: typeMatcherObjectInfo = new ObjectInfo() {
104: private static final long serialVersionUID = 1L;
105:
106: public boolean hasName() {
107: return false;
108: }
109:
110: public String getName() {
111: return null;
112: }
113:
114: public boolean isSingleton() {
115: return true;
116: }
117:
118: public Object createObject(
119: ObjectFactoryImpl objectFactory) {
120: return new JbpmTypeMatcher() {
121: private static final long serialVersionUID = 1L;
122:
123: public boolean matches(Object value) {
124: return false;
125: }
126: };
127: }
128: };
129: converter = null;
130: variableInstanceClass = null;
131: }
132: }
133:
134: public Object createObject(ObjectFactoryImpl objectFactory) {
135: JbpmTypeMatcher jbpmTypeMatcher = (JbpmTypeMatcher) objectFactory
136: .createObject(typeMatcherObjectInfo);
137: return new JbpmType(jbpmTypeMatcher, converter,
138: variableInstanceClass);
139: }
140:
141: private static Log log = LogFactory
142: .getLog(JbpmTypeObjectInfo.class);
143: }
|