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.Iterator;
026: import java.util.List;
027:
028: import org.jbpm.JbpmException;
029: import org.jbpm.util.XmlUtil;
030: import org.w3c.dom.Element;
031:
032: public class BeanInfo extends AbstractObjectInfo {
033:
034: private static final long serialVersionUID = 1L;
035:
036: String className = null;
037: ConstructorInfo constructorInfo = null;
038: PropertyInfo[] propertyInfos = null;
039:
040: public BeanInfo() {
041: }
042:
043: public BeanInfo(Element beanElement,
044: ObjectFactoryParser objectFactoryParser) {
045: super (beanElement, objectFactoryParser);
046:
047: // parse constructor or factory
048: Element constructorElement = XmlUtil.element(beanElement,
049: "constructor");
050: if (constructorElement != null) {
051: constructorInfo = new ConstructorInfo(constructorElement,
052: objectFactoryParser);
053: constructorInfo.beanInfo = this ;
054: }
055:
056: if (beanElement.hasAttribute("class")) {
057: className = beanElement.getAttribute("class");
058: } else if ((constructorInfo.factoryRefName == null)
059: && (constructorInfo.factoryClassName == null)) {
060: throw new JbpmException(
061: "bean element must have a class attribute: "
062: + XmlUtil.toString(beanElement));
063: }
064:
065: // parse fields
066: List propertyInfoList = new ArrayList();
067: Iterator iter = XmlUtil.elementIterator(beanElement, "field");
068: while (iter.hasNext()) {
069: Element fieldElement = (Element) iter.next();
070: propertyInfoList.add(new FieldInfo(fieldElement,
071: objectFactoryParser));
072: }
073:
074: // parse properties
075: iter = XmlUtil.elementIterator(beanElement, "property");
076: while (iter.hasNext()) {
077: Element propertyElement = (Element) iter.next();
078: propertyInfoList.add(new PropertyInfo(propertyElement,
079: objectFactoryParser));
080: }
081:
082: propertyInfos = (PropertyInfo[]) propertyInfoList
083: .toArray(new PropertyInfo[propertyInfoList.size()]);
084: }
085:
086: public Object createObject(ObjectFactoryImpl objectFactory) {
087: Object object = null;
088:
089: if (constructorInfo == null) {
090: if (className == null)
091: throw new JbpmException(
092: "bean '"
093: + getName()
094: + "' doesn't have a class or constructor specified");
095: try {
096: Class clazz = objectFactory.loadClass(className);
097: object = clazz.newInstance();
098: } catch (Exception e) {
099: throw new JbpmException("couldn't instantiate bean '"
100: + getName() + "' of type '" + className + "'",
101: e);
102: }
103: } else {
104: object = constructorInfo.createObject(objectFactory);
105: }
106:
107: if (className == null)
108: className = object.getClass().getName();
109:
110: if (propertyInfos != null) {
111: for (int i = 0; i < propertyInfos.length; i++) {
112: propertyInfos[i].injectProperty(object, objectFactory);
113: }
114: }
115:
116: return object;
117: }
118:
119: public String getClassName() {
120: return className;
121: }
122:
123: public void setClassName(String className) {
124: this.className = className;
125: }
126: }
|