KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > xslt > BeanAdapter


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.views.xslt;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9
10 import java.beans.IntrospectionException JavaDoc;
11 import java.beans.Introspector JavaDoc;
12 import java.beans.PropertyDescriptor JavaDoc;
13 import java.lang.reflect.Method JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19
20 /**
21  * @author <a HREF="mailto:meier@meisterbohne.de">Philipp Meier</a>
22  * Date: 10.10.2003
23  * Time: 20:08:17
24  */

25 public class BeanAdapter extends DefaultElementAdapter {
26     //~ Static fields/initializers /////////////////////////////////////////////
27

28     private static final Object JavaDoc[] NULLPARAMS = new Object JavaDoc[0];
29
30     /**
31      * Cache can savely be static because the cached information is
32      * the same for all instances of this class.
33      */

34     private static Map JavaDoc propertyDescriptorCache;
35
36     //~ Instance fields ////////////////////////////////////////////////////////
37

38     private Log log = LogFactory.getLog(this.getClass());
39
40     //~ Constructors ///////////////////////////////////////////////////////////
41

42     public BeanAdapter(DOMAdapter rootAdapter, AdapterNode parent, String JavaDoc propertyName, Object JavaDoc value) {
43         super(rootAdapter, parent, propertyName, value);
44     }
45
46     //~ Methods ////////////////////////////////////////////////////////////////
47

48     public String JavaDoc getTagName() {
49         return getPropertyName();
50     }
51
52     protected List JavaDoc buildChildrenAdapters() {
53         List JavaDoc newAdapters = new ArrayList JavaDoc();
54         Class JavaDoc type = getValue().getClass();
55         PropertyDescriptor JavaDoc[] props = getPropertyDescriptors(getValue());
56
57         if (props.length > 0) {
58             for (int i = 0; i < props.length; i++) {
59                 Method JavaDoc m = props[i].getReadMethod();
60
61                 if (m == null) {
62                     //FIXME: write only property or indexed access
63
continue;
64                 }
65
66                 String JavaDoc propertyName = props[i].getName();
67                 Object JavaDoc propertyValue;
68
69                 /** 999 white magic hack start 999 **
70                  * some property accessors will throw exceptions, e.g. getLocale() in webwork.ActionSupport *grrr*
71                  * IMHO property accessors should not have those side effects - meier@meisterbohne.de
72                  */

73                 try {
74                     propertyValue = m.invoke(getValue(), NULLPARAMS);
75                 } catch (Exception JavaDoc e) {
76                     continue;
77                 }
78
79                 /** 999 white magic hack end 999 **/
80                 AdapterNode childAdapter;
81
82                 if (propertyValue == null) {
83                     childAdapter = getRootAdapter().adaptNullValue(getRootAdapter(), this, propertyName);
84                 } else {
85                     childAdapter = getRootAdapter().adapt(getRootAdapter(), this, propertyName, propertyValue);
86                 }
87
88                 newAdapters.add(childAdapter);
89
90                 if (log.isDebugEnabled()) {
91                     log.debug(this + " adding adapter: " + childAdapter);
92                 }
93             }
94         } else {
95             // No properties found
96
log.info("Class " + type.getName() + " has no readable properties, " + " trying to adapt " + getPropertyName() + " with ToStringAdapter...");
97
98             //newAdapters.add(new ToStringAdapter(getRootAdapter(), this, getPropertyName(), getValue()));
99
}
100
101         return newAdapters;
102     }
103
104     /**
105      * Caching facade method to Introspector.getBeanInfo(Class, Class).getPropertyDescriptors();
106      */

107     private synchronized PropertyDescriptor JavaDoc[] getPropertyDescriptors(Object JavaDoc bean) {
108         try {
109             if (propertyDescriptorCache == null) {
110                 propertyDescriptorCache = new HashMap JavaDoc();
111             }
112
113             PropertyDescriptor JavaDoc[] props = (PropertyDescriptor JavaDoc[]) propertyDescriptorCache.get(bean.getClass());
114
115             if (props == null) {
116                 log.debug("Caching property descriptor for " + bean.getClass().getName());
117                 props = Introspector.getBeanInfo(bean.getClass(), Object JavaDoc.class).getPropertyDescriptors();
118                 propertyDescriptorCache.put(bean.getClass(), props);
119             }
120
121             return props;
122         } catch (IntrospectionException JavaDoc e) {
123             e.printStackTrace();
124             throw new RuntimeException JavaDoc("Error getting property descriptors for " + bean + " : " + e.getMessage());
125         }
126     }
127 }
128
Popular Tags