KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > kernel > plugins > config > xml > JavaBeanSchemaInitializer


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * 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.jboss.kernel.plugins.config.xml;
23
24 import java.security.AccessController JavaDoc;
25 import java.security.PrivilegedExceptionAction JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import javax.xml.namespace.NamespaceContext JavaDoc;
30 import javax.xml.namespace.QName JavaDoc;
31
32 import org.jboss.beans.info.spi.BeanInfo;
33 import org.jboss.beans.info.spi.PropertyInfo;
34 import org.jboss.kernel.plugins.config.Configurator;
35 import org.jboss.kernel.plugins.config.property.PropertyKernelConfig;
36 import org.jboss.kernel.spi.config.KernelConfig;
37 import org.jboss.reflect.plugins.introspection.IntrospectionTypeInfoFactory;
38 import org.jboss.reflect.spi.MethodInfo;
39 import org.jboss.reflect.spi.TypeInfo;
40 import org.jboss.reflect.spi.TypeInfoFactory;
41 import org.jboss.util.propertyeditor.PropertyEditors;
42 import org.jboss.xb.binding.sunday.unmarshalling.DefaultElementHandler;
43 import org.jboss.xb.binding.sunday.unmarshalling.DefaultElementInterceptor;
44 import org.jboss.xb.binding.sunday.unmarshalling.ElementBinding;
45 import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
46 import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingInitializer;
47 import org.jboss.xb.binding.sunday.unmarshalling.TypeBinding;
48 import org.xml.sax.Attributes JavaDoc;
49
50 /**
51  * JavaBeanSchemaInitializer.
52  *
53  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
54  * @version $Revision: 57307 $
55  */

56 public class JavaBeanSchemaInitializer implements SchemaBindingInitializer
57 {
58    /** The kernel config */
59    private static final KernelConfig config;
60    /** The type info factory */
61    protected static final TypeInfoFactory typeInfoFactory = new IntrospectionTypeInfoFactory();
62
63    /** The namespace */
64    private static final String JavaDoc JAVABEAN_NS = "urn:jboss:javabean:1.0";
65
66    /** The javabean binding */
67    private static final QName JavaDoc javabeanTypeQName = new QName JavaDoc(JAVABEAN_NS, "javabeanType");
68
69    /** The property binding */
70    private static final QName JavaDoc propertyTypeQName = new QName JavaDoc(JAVABEAN_NS, "propertyType");
71
72    /** The property element name */
73    private static final QName JavaDoc propertyQName = new QName JavaDoc(JAVABEAN_NS, "property");
74
75    static
76    {
77       try
78       {
79          config = AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc<KernelConfig>()
80          {
81             public KernelConfig run() throws Exception JavaDoc
82             {
83                return new PropertyKernelConfig(System.getProperties());
84             }
85          });
86       }
87       catch (RuntimeException JavaDoc e)
88       {
89          throw e;
90       }
91       catch (Exception JavaDoc e)
92       {
93          throw new RuntimeException JavaDoc("Error getting configuration", e);
94       }
95       
96       PropertyEditors.init();
97    }
98
99    public SchemaBinding init(SchemaBinding schema)
100    {
101       // javabean binding
102
TypeBinding beanType = schema.getType(javabeanTypeQName);
103       beanType.setHandler(new DefaultElementHandler()
104       {
105          
106          public Object JavaDoc startElement(Object JavaDoc parent, QName JavaDoc name, ElementBinding element)
107          {
108             return new Holder();
109          }
110
111          public void attributes(Object JavaDoc o, QName JavaDoc elementName, ElementBinding element, Attributes JavaDoc attrs, NamespaceContext JavaDoc nsCtx)
112          {
113             Holder holder = (Holder) o;
114             String JavaDoc className = null;
115             for (int i = 0; i < attrs.getLength(); ++i)
116             {
117                String JavaDoc localName = attrs.getLocalName(i);
118                if ("class".equals(localName))
119                   className = attrs.getValue(i);
120             }
121             
122             if (className == null)
123                throw new IllegalArgumentException JavaDoc("No class attribute for " + elementName);
124             
125             try
126             {
127                BeanInfo beanInfo = config.getBeanInfo(className, Thread.currentThread().getContextClassLoader());
128                Object JavaDoc object = Configurator.instantiate(config, beanInfo, null);
129                holder.setValue(object);
130             }
131             catch (RuntimeException JavaDoc e)
132             {
133                throw e;
134             }
135             catch (Error JavaDoc e)
136             {
137                throw e;
138             }
139             catch (Throwable JavaDoc t)
140             {
141                throw new RuntimeException JavaDoc("Error instantiating class " + className, t);
142             }
143          }
144
145          public Object JavaDoc endElement(Object JavaDoc o, QName JavaDoc qName, ElementBinding element)
146          {
147             Holder holder = (Holder) o;
148             return holder.getValue();
149          }
150       });
151
152       // bean has properties
153
beanType.pushInterceptor(propertyQName, new DefaultElementInterceptor()
154       {
155          public void add(Object JavaDoc parent, Object JavaDoc child, QName JavaDoc name)
156          {
157             Holder holder = (Holder) parent;
158             Object JavaDoc parentValue = holder.getValue();
159             
160             Property prop = (Property) child;
161             String JavaDoc property = prop.getProperty();
162
163             MethodInfo method;
164             Object JavaDoc value = prop.getValue();
165             try
166             {
167                PropertyInfo info = getProperty(parentValue, property);
168                value = convertValue(info, prop.getType(), value);
169                method = info.getSetter();
170                method.invoke(parentValue, new Object JavaDoc[] { value });
171             }
172             catch (RuntimeException JavaDoc e)
173             {
174                throw e;
175             }
176             catch (Error JavaDoc e)
177             {
178                throw e;
179             }
180             catch (Throwable JavaDoc t)
181             {
182                throw new RuntimeException JavaDoc("Error setting property " + property + " on object" + parentValue + " with value " + value, t);
183             }
184          }
185       });
186
187       // property binding
188
TypeBinding propertyType = schema.getType(propertyTypeQName);
189       propertyType.setHandler(new DefaultElementHandler()
190       {
191          
192          public Object JavaDoc startElement(Object JavaDoc parent, QName JavaDoc name, ElementBinding element)
193          {
194             return new Property();
195          }
196
197          public void attributes(Object JavaDoc o, QName JavaDoc elementName, ElementBinding element, Attributes JavaDoc attrs, NamespaceContext JavaDoc nsCtx)
198          {
199             Property property = (Property) o;
200             for (int i = 0; i < attrs.getLength(); ++i)
201             {
202                String JavaDoc localName = attrs.getLocalName(i);
203                if ("name".equals(localName))
204                   property.setProperty(attrs.getValue(i));
205                else if ("class".equals(localName))
206                   property.setType(attrs.getValue(i));
207             }
208          }
209       });
210
211       return schema;
212    }
213    
214    private PropertyInfo getProperty(Object JavaDoc parent, String JavaDoc property) throws Throwable JavaDoc
215    {
216       BeanInfo beanInfo = config.getBeanInfo(parent.getClass());
217       Set JavaDoc properties = beanInfo.getProperties();
218       if (properties != null && properties.size() > 0)
219       {
220          for (Iterator JavaDoc i = properties.iterator(); i.hasNext();)
221          {
222             PropertyInfo prop = (PropertyInfo) i.next();
223             if (prop.getName().equals(property))
224             {
225                if (prop.getSetter() == null)
226                   throw new IllegalArgumentException JavaDoc("Property '" + property + "' is read only " + prop);
227                return prop;
228             }
229          }
230       }
231       throw new IllegalArgumentException JavaDoc("No property '" + property + "' for " + beanInfo);
232    }
233
234    /**
235     * Convert a value
236     *
237     * @param info the property info
238     * @param override the override class
239     * @param value the value
240     * @return the converted value
241     * @throws Throwable for any error
242     */

243    private Object JavaDoc convertValue(PropertyInfo info, String JavaDoc override, Object JavaDoc value) throws Throwable JavaDoc
244    {
245       TypeInfo type = info.getType();
246       if (override != null)
247          type = typeInfoFactory.getTypeInfo(override, null);
248       return type.convertValue(value);
249    }
250    
251    public static class Holder
252    {
253       private Object JavaDoc object;
254       
255       public Holder()
256       {
257       }
258       
259       public Object JavaDoc getValue()
260       {
261          return object;
262       }
263       
264       public void setValue(Object JavaDoc object)
265       {
266          this.object = object;
267       }
268    }
269    
270    public static class Property extends Holder
271    {
272       private String JavaDoc property;
273       
274       private String JavaDoc type;
275       
276       public Property()
277       {
278       }
279       
280       public String JavaDoc getProperty()
281       {
282          return property;
283       }
284       
285       public void setProperty(String JavaDoc property)
286       {
287          this.property = property;
288       }
289       
290       public String JavaDoc getType()
291       {
292          return type;
293       }
294       
295       public void setType(String JavaDoc type)
296       {
297          this.type = type;
298       }
299    }
300 }
301
Popular Tags