1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.geronimo.deployment.service;
19
20 import java.beans.BeanInfo;
21 import java.beans.IntrospectionException;
22 import java.beans.Introspector;
23 import java.beans.PropertyDescriptor;
24 import java.beans.PropertyEditor;
25 import java.lang.reflect.Method;
26
27 import org.apache.geronimo.common.DeploymentException;
28 import org.apache.geronimo.common.propertyeditor.PropertyEditors;
29 import org.apache.geronimo.deployment.javabean.xbeans.JavabeanType;
30 import org.apache.geronimo.deployment.javabean.xbeans.PropertyType;
31 import org.apache.geronimo.deployment.javabean.xbeans.BeanPropertyType;
32 import org.apache.geronimo.gbean.GBeanInfo;
33 import org.apache.geronimo.gbean.GBeanInfoBuilder;
34 import org.apache.xmlbeans.XmlObject;
35
36 /**
37 * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
38 */
39 public class JavaBeanXmlAttributeBuilder implements XmlAttributeBuilder {
40
41 private static final String NAMESPACE = "http://geronimo.apache.org/xml/ns/deployment/javabean-1.0";
42
43 public String getNamespace() {
44 return NAMESPACE;
45 }
46
47 public Object getValue(XmlObject xmlObject, String type, ClassLoader cl) throws DeploymentException {
48 JavabeanType javabean = (JavabeanType) xmlObject.copy().changeType(JavabeanType.type);
49 return getValue(javabean, type, cl);
50 }
51
52 private Object getValue(JavabeanType javabean, String className, ClassLoader cl) throws DeploymentException {
53 Class clazz = null;
54 try {
55 clazz = cl.loadClass(className);
56 } catch (ClassNotFoundException e) {
57 throw new DeploymentException("Could not load alleged javabean class " + className, e);
58 }
59 Object instance = null;
60 try {
61 instance = clazz.newInstance();
62 } catch (Exception e) {
63 throw new DeploymentException("Could not create java bean instance", e);
64 }
65 PropertyDescriptor[] propertyDescriptors;
66 try {
67 BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
68 propertyDescriptors = beanInfo.getPropertyDescriptors();
69 } catch (IntrospectionException e) {
70 throw new DeploymentException("Could not analyze java bean class", e);
71 }
72
73 PropertyType[] properties = javabean.getPropertyArray();
74 for (int i = 0; i < properties.length; i++) {
75 PropertyType property = properties[i];
76 String propertyName = Introspector.decapitalize(property.getName());
77 String propertyString = property.getStringValue().trim();
78 for (int j = 0; j < propertyDescriptors.length; j++) {
79 PropertyDescriptor propertyDescriptor = propertyDescriptors[j];
80 if (propertyName.equals(propertyDescriptor.getName())) {
81 String type = propertyDescriptor.getPropertyType().getName();
82 PropertyEditor propertyEditor = null;
83 try {
84 propertyEditor = PropertyEditors.findEditor(type, cl);
85 } catch (ClassNotFoundException e) {
86 throw new DeploymentException("Could not load editor for type " + type, e);
87 }
88 if (propertyEditor == null) {
89 throw new DeploymentException("Unable to find PropertyEditor for " + type);
90 }
91 propertyEditor.setAsText(propertyString);
92 Object value = propertyEditor.getValue();
93 Method m = propertyDescriptor.getWriteMethod();
94 try {
95 m.invoke(instance, new Object[] {value});
96 } catch (Exception e) {
97 throw new DeploymentException("Could not set property value for property named " + propertyName, e);
98 }
99 break;
100 }
101 }
102 }
103
104 BeanPropertyType[] beanProperties = javabean.getBeanPropertyArray();
105 for (int i = 0; i < beanProperties.length; i++) {
106 BeanPropertyType beanProperty = beanProperties[i];
107 String propertyName = Introspector.decapitalize(beanProperty.getName().trim());
108 JavabeanType innerBean = beanProperty.getJavabean();
109 for (int j = 0; j < propertyDescriptors.length; j++) {
110 PropertyDescriptor propertyDescriptor = propertyDescriptors[j];
111 if (propertyName.equals(propertyDescriptor.getName())) {
112 String propertyType = propertyDescriptor.getPropertyType().getName();
113 Object value = getValue(innerBean, propertyType, cl);
114 Method m = propertyDescriptor.getWriteMethod();
115 try {
116 m.invoke(instance, new Object[] {value});
117 } catch (Exception e) {
118 throw new DeploymentException("Could not set property value for property named " + propertyName, e);
119 }
120 break;
121 }
122 }
123 }
124 return instance;
125 }
126
127 public static final GBeanInfo GBEAN_INFO;
128
129 static {
130 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(JavaBeanXmlAttributeBuilder.class, "XmlAttributeBuilder");
131 infoBuilder.addInterface(XmlAttributeBuilder.class);
132 GBEAN_INFO = infoBuilder.getBeanInfo();
133 }
134
135 public static GBeanInfo getGBeanInfo() {
136 return GBEAN_INFO;
137 }
138 }