KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > hibernate > H2BeanConverter


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.hibernate;
17
18 import java.beans.BeanInfo JavaDoc;
19 import java.beans.IntrospectionException JavaDoc;
20 import java.beans.Introspector JavaDoc;
21 import java.beans.PropertyDescriptor JavaDoc;
22 import java.lang.reflect.Method JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import net.sf.hibernate.Hibernate;
27
28 import org.directwebremoting.convert.BeanConverter;
29 import org.directwebremoting.extend.Converter;
30 import org.directwebremoting.extend.MarshallException;
31
32 /**
33  * BeanConverter that works with Hibernate to get BeanInfo.
34  * @author Joe Walker [joe at getahead dot ltd dot uk]
35  */

36 public class H2BeanConverter extends BeanConverter implements Converter
37 {
38     /* (non-Javadoc)
39      * @see org.directwebremoting.extend.NamedConverter#getPropertyMapFromObject(java.lang.Object, boolean, boolean)
40      */

41     public Map JavaDoc getPropertyMapFromObject(Object JavaDoc example, boolean readRequired, boolean writeRequired) throws MarshallException
42     {
43         Class JavaDoc clazz = Hibernate.getClass(example);
44         try
45         {
46             BeanInfo JavaDoc info = Introspector.getBeanInfo(clazz);
47             PropertyDescriptor JavaDoc[] descriptors = info.getPropertyDescriptors();
48
49             Map JavaDoc properties = new HashMap JavaDoc();
50             for (int i = 0; i < descriptors.length; i++)
51             {
52                 PropertyDescriptor JavaDoc descriptor = descriptors[i];
53                 String JavaDoc name = descriptor.getName();
54
55                 // We don't marshall getClass()
56
if (name.equals("class"))
57                 {
58                     continue;
59                 }
60
61                 // Access rules mean we might not want to do this one
62
if (!isAllowedByIncludeExcludeRules(name))
63                 {
64                     continue;
65                 }
66
67                 if (readRequired && descriptor.getReadMethod() == null)
68                 {
69                     continue;
70                 }
71
72                 if (writeRequired && descriptor.getWriteMethod() == null)
73                 {
74                     continue;
75                 }
76
77                 properties.put(name, new H2PropertyDescriptorProperty(descriptor));
78             }
79
80             return properties;
81         }
82         catch (Exception JavaDoc ex)
83         {
84             throw new MarshallException(clazz, ex);
85         }
86     }
87
88     /**
89      * Cache the method if possible, using the classname and property name to
90      * allow for similar named methods.
91      * @param data The bean to introspect
92      * @param property The property to get the accessor for
93      * @return The getter method
94      * @throws IntrospectionException
95      */

96     protected Method JavaDoc findGetter(Object JavaDoc data, String JavaDoc property) throws IntrospectionException JavaDoc
97     {
98         String JavaDoc key = data.getClass().getName() + ":" + property;
99
100         Method JavaDoc method = (Method JavaDoc) methods.get(key);
101         if (method == null)
102         {
103             PropertyDescriptor JavaDoc[] props = Introspector.getBeanInfo(data.getClass()).getPropertyDescriptors();
104             for (int i = 0; i < props.length; i++)
105             {
106                 if (props[i].getName().equalsIgnoreCase(property))
107                 {
108                     method = props[i].getReadMethod();
109                 }
110             }
111
112             methods.put(key, method);
113         }
114
115         return method;
116     }
117
118     /**
119      * The cache of method lookups that we've already done
120      */

121     protected static final Map JavaDoc methods = new HashMap JavaDoc();
122 }
123
Popular Tags