KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > service > JavaBeanXmlAttributeBuilder


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

17 package org.apache.geronimo.deployment.service;
18
19 import java.beans.BeanInfo JavaDoc;
20 import java.beans.IntrospectionException JavaDoc;
21 import java.beans.Introspector JavaDoc;
22 import java.beans.PropertyDescriptor JavaDoc;
23 import java.beans.PropertyEditor JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25
26 import org.apache.geronimo.common.DeploymentException;
27 import org.apache.geronimo.common.propertyeditor.PropertyEditors;
28 import org.apache.geronimo.deployment.javabean.xbeans.JavabeanType;
29 import org.apache.geronimo.deployment.javabean.xbeans.PropertyType;
30 import org.apache.geronimo.deployment.javabean.xbeans.BeanPropertyType;
31 import org.apache.geronimo.gbean.GBeanInfo;
32 import org.apache.geronimo.gbean.GBeanInfoBuilder;
33 import org.apache.xmlbeans.XmlObject;
34
35 /**
36  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
37  */

38 public class JavaBeanXmlAttributeBuilder implements XmlAttributeBuilder {
39
40     private static final String JavaDoc NAMESPACE = "http://geronimo.apache.org/xml/ns/deployment/javabean-1.0";
41
42     public String JavaDoc getNamespace() {
43         return NAMESPACE;
44     }
45
46     public Object JavaDoc getValue(XmlObject xmlObject, String JavaDoc type, ClassLoader JavaDoc cl) throws DeploymentException {
47         JavabeanType javabean = (JavabeanType) xmlObject.copy().changeType(JavabeanType.type);
48         return getValue(javabean, type, cl);
49     }
50
51     private Object JavaDoc getValue(JavabeanType javabean, String JavaDoc className, ClassLoader JavaDoc cl) throws DeploymentException {
52         Class JavaDoc clazz = null;
53         try {
54             clazz = cl.loadClass(className);
55         } catch (ClassNotFoundException JavaDoc e) {
56             throw new DeploymentException("Could not load alleged javabean class " + className, e);
57         }
58         Object JavaDoc instance = null;
59         try {
60             instance = clazz.newInstance();
61         } catch (Exception JavaDoc e) {
62             throw new DeploymentException("Could not create java bean instance", e);
63         }
64         PropertyDescriptor JavaDoc[] propertyDescriptors;
65         try {
66             BeanInfo JavaDoc beanInfo = Introspector.getBeanInfo(clazz);
67             propertyDescriptors = beanInfo.getPropertyDescriptors();
68         } catch (IntrospectionException JavaDoc e) {
69             throw new DeploymentException("Could not analyze java bean class", e);
70         }
71
72         PropertyType[] properties = javabean.getPropertyArray();
73         for (int i = 0; i < properties.length; i++) {
74             PropertyType property = properties[i];
75             String JavaDoc propertyName = Introspector.decapitalize(property.getName());
76             String JavaDoc propertyString = property.getStringValue().trim();
77             for (int j = 0; j < propertyDescriptors.length; j++) {
78                 PropertyDescriptor JavaDoc propertyDescriptor = propertyDescriptors[j];
79                 if (propertyName.equals(propertyDescriptor.getName())) {
80                     String JavaDoc type = propertyDescriptor.getPropertyType().getName();
81                     PropertyEditor JavaDoc propertyEditor = null;
82                     try {
83                         propertyEditor = PropertyEditors.findEditor(type, cl);
84                     } catch (ClassNotFoundException JavaDoc e) {
85                         throw new DeploymentException("Could not load editor for type " + type, e);
86                     }
87                     if (propertyEditor == null) {
88                         throw new DeploymentException("Unable to find PropertyEditor for " + type);
89                     }
90                     propertyEditor.setAsText(propertyString);
91                     Object JavaDoc value = propertyEditor.getValue();
92                     Method JavaDoc m = propertyDescriptor.getWriteMethod();
93                     try {
94                         m.invoke(instance, new Object JavaDoc[] {value});
95                     } catch (Exception JavaDoc e) {
96                         throw new DeploymentException("Could not set property value for property named " + propertyName, e);
97                     }
98                     break;
99                 }
100             }
101         }
102
103         BeanPropertyType[] beanProperties = javabean.getBeanPropertyArray();
104         for (int i = 0; i < beanProperties.length; i++) {
105             BeanPropertyType beanProperty = beanProperties[i];
106             String JavaDoc propertyName = Introspector.decapitalize(beanProperty.getName().trim());
107             JavabeanType innerBean = beanProperty.getJavabean();
108             for (int j = 0; j < propertyDescriptors.length; j++) {
109                 PropertyDescriptor JavaDoc propertyDescriptor = propertyDescriptors[j];
110                 if (propertyName.equals(propertyDescriptor.getName())) {
111                     String JavaDoc propertyType = propertyDescriptor.getPropertyType().getName();
112                     Object JavaDoc value = getValue(innerBean, propertyType, cl);
113                     Method JavaDoc m = propertyDescriptor.getWriteMethod();
114                     try {
115                         m.invoke(instance, new Object JavaDoc[] {value});
116                     } catch (Exception JavaDoc e) {
117                         throw new DeploymentException("Could not set property value for property named " + propertyName, e);
118                     }
119                     break;
120                 }
121             }
122         }
123         return instance;
124     }
125
126     public static final GBeanInfo GBEAN_INFO;
127
128     static {
129         GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(JavaBeanXmlAttributeBuilder.class, "XmlAttributeBuilder");
130         infoBuilder.addInterface(XmlAttributeBuilder.class);
131         GBEAN_INFO = infoBuilder.getBeanInfo();
132     }
133
134     public static GBeanInfo getGBeanInfo() {
135         return GBEAN_INFO;
136     }
137 }
138
Popular Tags