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.io.Serializable;
025: import java.lang.reflect.Method;
026:
027: import org.jbpm.JbpmException;
028: import org.jbpm.util.XmlUtil;
029: import org.w3c.dom.Element;
030:
031: public class PropertyInfo implements Serializable {
032:
033: private static final long serialVersionUID = 1L;
034:
035: String propertyName = null;
036: String setterMethodName = null;
037: ObjectInfo propertyValueInfo = null;
038:
039: public PropertyInfo(Element propertyElement,
040: ObjectFactoryParser configParser) {
041: // propertyName or setterMethodName
042: if (propertyElement.hasAttribute("name")) {
043: propertyName = propertyElement.getAttribute("name");
044: } else if (propertyElement.hasAttribute("setter")) {
045: setterMethodName = propertyElement.getAttribute("setter");
046: } else {
047: throw new JbpmException(
048: "property must have a 'name' or 'setter' attribute: "
049: + XmlUtil.toString(propertyElement));
050: }
051:
052: // propertyValueInfo
053: Element propertyValueElement = XmlUtil.element(propertyElement);
054: propertyValueInfo = configParser.parse(propertyValueElement);
055: }
056:
057: public void injectProperty(Object object,
058: ObjectFactoryImpl objectFactory) {
059: Object propertyValue = objectFactory
060: .getObject(propertyValueInfo);
061: Method setterMethod = findSetter(object.getClass());
062: setterMethod.setAccessible(true);
063: try {
064: setterMethod.invoke(object, new Object[] { propertyValue });
065: } catch (Exception e) {
066: throw new JbpmException("couldn't set property '"
067: + propertyName + "' on class '" + object.getClass()
068: + "' to value '" + propertyValue + "'", e);
069: }
070: }
071:
072: public Method findSetter(Class clazz) {
073: Method method = null;
074:
075: if (setterMethodName == null) {
076: if ((propertyName.startsWith("is"))
077: && (propertyName.length() > 3)
078: && (Character.isUpperCase(propertyName.charAt(2)))) {
079: setterMethodName = "set" + propertyName.substring(2);
080: } else {
081: setterMethodName = "set"
082: + propertyName.substring(0, 1).toUpperCase()
083: + propertyName.substring(1);
084: }
085: }
086:
087: Class candidateClass = clazz;
088: while ((candidateClass != null) && (method == null)) {
089:
090: Method[] methods = candidateClass.getDeclaredMethods();
091: if (methods != null) {
092: for (int i = 0; ((i < methods.length) && (method == null)); i++) {
093: if ((methods[i].getName().equals(setterMethodName))
094: && (methods[i].getParameterTypes() != null)
095: && (methods[i].getParameterTypes().length == 1)) {
096: method = methods[i];
097: }
098: }
099: }
100:
101: if (method == null) {
102: candidateClass = candidateClass.getSuperclass();
103: }
104: }
105:
106: if (method == null) {
107: throw new JbpmException("couldn't find setter '"
108: + setterMethodName + "' in class '"
109: + clazz.getName() + "'");
110: }
111:
112: return method;
113: }
114: }
|