001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.geronimo.deployment.service;
018
019 import java.beans.BeanInfo;
020 import java.beans.IntrospectionException;
021 import java.beans.Introspector;
022 import java.beans.PropertyDescriptor;
023 import java.beans.PropertyEditor;
024 import java.lang.reflect.Method;
025
026 import org.apache.geronimo.common.DeploymentException;
027 import org.apache.geronimo.common.propertyeditor.PropertyEditors;
028 import org.apache.geronimo.deployment.javabean.xbeans.JavabeanType;
029 import org.apache.geronimo.deployment.javabean.xbeans.PropertyType;
030 import org.apache.geronimo.deployment.javabean.xbeans.BeanPropertyType;
031 import org.apache.geronimo.gbean.GBeanInfo;
032 import org.apache.geronimo.gbean.GBeanInfoBuilder;
033 import org.apache.xmlbeans.XmlObject;
034
035 /**
036 * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
037 */
038 public class JavaBeanXmlAttributeBuilder implements XmlAttributeBuilder {
039
040 private static final String NAMESPACE = "http://geronimo.apache.org/xml/ns/deployment/javabean-1.0";
041
042 public String getNamespace() {
043 return NAMESPACE;
044 }
045
046 public Object getValue(XmlObject xmlObject, String type, ClassLoader cl) throws DeploymentException {
047 JavabeanType javabean = (JavabeanType) xmlObject.copy().changeType(JavabeanType.type);
048 return getValue(javabean, type, cl);
049 }
050
051 private Object getValue(JavabeanType javabean, String className, ClassLoader cl) throws DeploymentException {
052 Class clazz = null;
053 try {
054 clazz = cl.loadClass(className);
055 } catch (ClassNotFoundException e) {
056 throw new DeploymentException("Could not load alleged javabean class " + className, e);
057 }
058 Object instance = null;
059 try {
060 instance = clazz.newInstance();
061 } catch (Exception e) {
062 throw new DeploymentException("Could not create java bean instance", e);
063 }
064 PropertyDescriptor[] propertyDescriptors;
065 try {
066 BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
067 propertyDescriptors = beanInfo.getPropertyDescriptors();
068 } catch (IntrospectionException e) {
069 throw new DeploymentException("Could not analyze java bean class", e);
070 }
071
072 PropertyType[] properties = javabean.getPropertyArray();
073 for (int i = 0; i < properties.length; i++) {
074 PropertyType property = properties[i];
075 String propertyName = Introspector.decapitalize(property.getName());
076 String propertyString = property.getStringValue().trim();
077 for (int j = 0; j < propertyDescriptors.length; j++) {
078 PropertyDescriptor propertyDescriptor = propertyDescriptors[j];
079 if (propertyName.equals(propertyDescriptor.getName())) {
080 String type = propertyDescriptor.getPropertyType().getName();
081 PropertyEditor propertyEditor = null;
082 try {
083 propertyEditor = PropertyEditors.findEditor(type, cl);
084 } catch (ClassNotFoundException e) {
085 throw new DeploymentException("Could not load editor for type " + type, e);
086 }
087 if (propertyEditor == null) {
088 throw new DeploymentException("Unable to find PropertyEditor for " + type);
089 }
090 propertyEditor.setAsText(propertyString);
091 Object value = propertyEditor.getValue();
092 Method m = propertyDescriptor.getWriteMethod();
093 try {
094 m.invoke(instance, new Object[] {value});
095 } catch (Exception e) {
096 throw new DeploymentException("Could not set property value for property named " + propertyName, e);
097 }
098 break;
099 }
100 }
101 }
102
103 BeanPropertyType[] beanProperties = javabean.getBeanPropertyArray();
104 for (int i = 0; i < beanProperties.length; i++) {
105 BeanPropertyType beanProperty = beanProperties[i];
106 String propertyName = Introspector.decapitalize(beanProperty.getName().trim());
107 JavabeanType innerBean = beanProperty.getJavabean();
108 for (int j = 0; j < propertyDescriptors.length; j++) {
109 PropertyDescriptor propertyDescriptor = propertyDescriptors[j];
110 if (propertyName.equals(propertyDescriptor.getName())) {
111 String propertyType = propertyDescriptor.getPropertyType().getName();
112 Object value = getValue(innerBean, propertyType, cl);
113 Method m = propertyDescriptor.getWriteMethod();
114 try {
115 m.invoke(instance, new Object[] {value});
116 } catch (Exception 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 }