001: /* $Id: DefaultBeanData.java 812 2007-01-15 20:48:12Z hengels $ */
002: package org.conform;
003:
004: import org.apache.commons.logging.LogFactory;
005:
006: import java.beans.BeanInfo;
007: import java.beans.IntrospectionException;
008: import java.beans.Introspector;
009: import java.beans.PropertyDescriptor;
010: import java.lang.reflect.Method;
011: import java.lang.reflect.Constructor;
012: import java.util.*;
013:
014: public class DefaultBeanData extends AbstractBeanData {
015: private static org.apache.commons.logging.Log LOG = LogFactory
016: .getLog(Data.class);
017:
018: public static final String PROPERTY_DATA_CLASS = "property.data";
019:
020: private transient Map descriptorMap = new HashMap();
021: Object bean;
022:
023: public DefaultBeanData(BeanMeta beanMeta) {
024: super (beanMeta);
025: }
026:
027: protected PropertyData doGetPropertyData(PropertyMeta propertyMeta) {
028: String propertyDataClassName = (String) propertyMeta
029: .getAttribute(PROPERTY_DATA_CLASS);
030: PropertyData propertyData;
031: if (propertyDataClassName != null) {
032: try {
033: ClassLoader classLoader = Thread.currentThread()
034: .getContextClassLoader();
035: if (classLoader == null)
036: classLoader = getClass().getClassLoader();
037: Class propertyDataClass = classLoader
038: .loadClass(propertyDataClassName);
039: Constructor constructor = propertyDataClass
040: .getConstructor(new Class[] {
041: DefaultBeanData.class,
042: PropertyMeta.class });
043: propertyData = (PropertyData) constructor
044: .newInstance(new Object[] { this , propertyMeta });
045: } catch (Exception e) {
046: e.printStackTrace();
047: propertyData = new DefaultPropertyData(this ,
048: propertyMeta);
049: }
050: } else
051: propertyData = new DefaultPropertyData(this , propertyMeta);
052: return propertyData;
053: }
054:
055: private void reflect() {
056: try {
057: List list = descriptors();
058: for (Iterator iterator = list.iterator(); iterator
059: .hasNext();) {
060: PropertyDescriptor descriptor = (PropertyDescriptor) iterator
061: .next();
062: String qualifiedName = beanMeta.getType().getName()
063: + "." + descriptor.getName();
064: descriptorMap.put(qualifiedName, descriptor);
065: }
066: } catch (Exception e) {
067: e.printStackTrace();
068: }
069: }
070:
071: private List descriptors() throws IntrospectionException {
072: Class beanClass = beanMeta.getType();
073: BeanInfo info = Introspector.getBeanInfo(beanClass,
074: Object.class);
075: PropertyDescriptor[] descriptors = info
076: .getPropertyDescriptors();
077: List list = new ArrayList(descriptors.length);
078: for (int i = 0; i < descriptors.length; i++) {
079: PropertyDescriptor descriptor = descriptors[i];
080: Method getter = descriptor.getReadMethod();
081:
082: if (getter == null)
083: continue;
084: list.add(descriptor);
085: }
086: return list;
087: }
088:
089: private PropertyDescriptor getDescriptor(String name) {
090: String qualifiedName = beanMeta.getType().getName() + "."
091: + name;
092: PropertyDescriptor descriptor = (PropertyDescriptor) descriptorMap
093: .get(qualifiedName);
094: if (descriptor == null)
095: reflect();
096: descriptor = (PropertyDescriptor) descriptorMap
097: .get(qualifiedName);
098: if (descriptor == null)
099: throw new RuntimeException("no descriptor for "
100: + qualifiedName);
101: return descriptor;
102: }
103:
104: void doSetValue(PropertyMeta property, Object value) {
105: String propertyName = property.getName();
106:
107: PropertyDescriptor descriptor = getDescriptor(propertyName);
108: Method setter = descriptor.getWriteMethod();
109: if (setter == null)
110: throw new RuntimeException("No setter for: " + propertyName);
111:
112: try {
113: if (value != null
114: || !setter.getParameterTypes()[0].isPrimitive())
115: setter.invoke(bean, value);
116:
117: if (LOG.isDebugEnabled())
118: LOG.debug(setter.getName() + " = "
119: + System.identityHashCode(value));
120:
121: } catch (Exception e) {
122: LOG.error(setter.getName() + " (" + value + ")", e);
123: throw new RuntimeException(e);
124: }
125: }
126:
127: Object doGetValue(PropertyMeta property) {
128: String propertyName = property.getName();
129:
130: PropertyDescriptor descriptor = getDescriptor(propertyName);
131: Method getter = descriptor.getReadMethod();
132: if (getter == null)
133: throw new RuntimeException("No getter for: " + propertyName);
134: try {
135: Object value = getter.invoke(bean);
136:
137: if (LOG.isDebugEnabled())
138: LOG.debug(getter.getName() + " = "
139: + System.identityHashCode(value));
140:
141: return value;
142: } catch (Exception e) {
143: LOG.error(getter.getName() + " ()", e);
144: throw new RuntimeException(e);
145: }
146: }
147:
148: public Object doGetValue() {
149: return bean;
150: }
151:
152: public void doSetValue(Object bean) {
153: this .bean = bean;
154: }
155:
156: public void setPropertyData(PropertyMeta property, PropertyData data) {
157: data.setIssuePublishingActive(true);
158: propertyDatas.put(property, data);
159: }
160:
161: public void update() {
162: super .update();
163: }
164:
165: public void flush() {
166: super .flush();
167: }
168:
169: public void clear() {
170: super .clear();
171: bean = null;
172: }
173:
174: public Object createInstance() {
175: try {
176: return beanMeta.getType().newInstance();
177: } catch (Exception e) {
178: throw new RuntimeException(e);
179: }
180: }
181: }
|