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.configuration;
23:
24: import java.lang.reflect.Field;
25:
26: import org.apache.commons.logging.Log;
27: import org.apache.commons.logging.LogFactory;
28: import org.jbpm.JbpmException;
29: import org.w3c.dom.Element;
30:
31: public class FieldInfo extends PropertyInfo {
32:
33: private static final long serialVersionUID = 1L;
34:
35: public FieldInfo(Element fieldElement,
36: ObjectFactoryParser configParser) {
37: super (fieldElement, configParser);
38: }
39:
40: public void injectProperty(Object object,
41: ObjectFactoryImpl objectFactory) {
42:
43: Object propertyValue = objectFactory
44: .getObject(propertyValueInfo);
45: Field propertyField = findField(object.getClass());
46: propertyField.setAccessible(true);
47: try {
48: propertyField.set(object, propertyValue);
49: } catch (Exception e) {
50: throw new JbpmException("couldn't set field '"
51: + propertyName + "' on class '" + object.getClass()
52: + "' to value '" + propertyValue + "'", e);
53: }
54: }
55:
56: Field findField(Class clazz) {
57: Field field = null;
58:
59: Class candidateClass = clazz;
60: while ((candidateClass != null) && (field == null)) {
61:
62: try {
63: field = candidateClass.getDeclaredField(propertyName);
64: } catch (Exception e) {
65: candidateClass = candidateClass.getSuperclass();
66: }
67: }
68:
69: if (field == null) {
70: JbpmException e = new JbpmException("couldn't find field '"
71: + propertyName + "' in class '" + clazz.getName()
72: + "'");
73: log.error(e);
74: throw e;
75: }
76:
77: return field;
78: }
79:
80: private static Log log = LogFactory.getLog(FieldInfo.class);
81: }
|