KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > convert > BeanConverter


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.convert;
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.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.directwebremoting.extend.Converter;
26 import org.directwebremoting.extend.InboundContext;
27 import org.directwebremoting.extend.MarshallException;
28 import org.directwebremoting.extend.Property;
29 import org.directwebremoting.extend.TypeHintContext;
30 import org.directwebremoting.impl.PropertyDescriptorProperty;
31
32 /**
33  * Convert a Javascript associative array into a JavaBean
34  * @author Joe Walker [joe at getahead dot ltd dot uk]
35  */

36 public class BeanConverter extends BasicObjectConverter 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         return getPropertyMapFromClass(example.getClass(), readRequired, writeRequired);
44     }
45
46     /* (non-Javadoc)
47      * @see org.directwebremoting.extend.NamedConverter#getPropertyMap(java.lang.Class, boolean, boolean)
48      */

49     public Map JavaDoc getPropertyMapFromClass(Class JavaDoc type, boolean readRequired, boolean writeRequired) throws MarshallException
50     {
51         try
52         {
53             BeanInfo JavaDoc info = Introspector.getBeanInfo(type);
54             PropertyDescriptor JavaDoc[] descriptors = info.getPropertyDescriptors();
55
56             Map JavaDoc properties = new HashMap JavaDoc();
57             for (int i = 0; i < descriptors.length; i++)
58             {
59                 PropertyDescriptor JavaDoc descriptor = descriptors[i];
60                 String JavaDoc name = descriptor.getName();
61
62                 // We don't marshall getClass()
63
if (name.equals("class"))
64                 {
65                     continue;
66                 }
67
68                 // Access rules mean we might not want to do this one
69
if (!isAllowedByIncludeExcludeRules(name))
70                 {
71                     continue;
72                 }
73
74                 if (readRequired && descriptor.getReadMethod() == null)
75                 {
76                     continue;
77                 }
78
79                 if (writeRequired && descriptor.getWriteMethod() == null)
80                 {
81                     continue;
82                 }
83
84                 properties.put(name, new PropertyDescriptorProperty(descriptor));
85             }
86
87             return properties;
88         }
89         catch (IntrospectionException JavaDoc ex)
90         {
91             throw new MarshallException(type, ex);
92         }
93     }
94
95     /* (non-Javadoc)
96      * @see org.directwebremoting.convert.BasicObjectConverter#createTypeHintContext(org.directwebremoting.extend.InboundContext, org.directwebremoting.extend.Property)
97      */

98     protected TypeHintContext createTypeHintContext(InboundContext inctx, Property property)
99     {
100         return new TypeHintContext(converterManager, property.getSetter(), 0);
101     }
102 }
103
Popular Tags