01: /*
02: * JBoss, Home of Professional Open Source
03: * Copyright 2005, JBoss Inc., and individual contributors as indicated
04: * by the @authors tag. See the copyright.txt in the distribution for a
05: * full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jbpm.instantiation;
23:
24: import java.lang.reflect.Method;
25:
26: import org.apache.commons.logging.Log;
27: import org.apache.commons.logging.LogFactory;
28: import org.dom4j.Element;
29:
30: public class BeanInstantiator extends FieldInstantiator implements
31: Instantiator {
32:
33: protected void setPropertyValue(Class clazz, Object newInstance,
34: String propertyName, Element propertyElement) {
35: try {
36: // create the setter method name from the property name
37: String setterMethodName = "set"
38: + propertyName.substring(0, 1).toUpperCase()
39: + propertyName.substring(1);
40:
41: // find the setter method
42: Method method = findSetter(clazz, setterMethodName);
43: Class propertyType = method.getParameterTypes()[0];
44:
45: // if the setter method was found
46: if (method != null) {
47: // make it accessible
48: method.setAccessible(true);
49: // invoke it
50: method.invoke(newInstance, new Object[] { getValue(
51: propertyType, propertyElement) });
52: } else {
53: log.error("couldn't set property '" + propertyName
54: + "' to value '" + propertyElement.asXML()
55: + "'");
56: }
57: } catch (Exception e) {
58: log
59: .error("couldn't parse property '" + propertyName
60: + "' to value '" + propertyElement.asXML()
61: + "'", e);
62: }
63: }
64:
65: private Method findSetter(Class clazz, String setterMethodName) {
66: Method method = null;
67: Method[] methods = clazz.getDeclaredMethods();
68: for (int i = 0; ((i < methods.length) && (method == null)); i++) {
69: if ((setterMethodName.equals(methods[i].getName()))
70: && (methods[i].getParameterTypes() != null)
71: && (methods[i].getParameterTypes().length == 1)) {
72: method = methods[i];
73: }
74: }
75: if ((method == null) && (clazz != Object.class)) {
76: method = findSetter(clazz.getSuperclass(), setterMethodName);
77: }
78: return method;
79: }
80:
81: private static final Log log = LogFactory
82: .getLog(BeanInstantiator.class);
83: }
|