1 17 package org.apache.geronimo.deployment.service; 18 19 import java.beans.BeanInfo ; 20 import java.beans.IntrospectionException ; 21 import java.beans.Introspector ; 22 import java.beans.PropertyDescriptor ; 23 import java.beans.PropertyEditor ; 24 import java.lang.reflect.Method ; 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 38 public class JavaBeanXmlAttributeBuilder implements XmlAttributeBuilder { 39 40 private static final String NAMESPACE = "http://geronimo.apache.org/xml/ns/deployment/javabean-1.0"; 41 42 public String getNamespace() { 43 return NAMESPACE; 44 } 45 46 public Object getValue(XmlObject xmlObject, String type, ClassLoader cl) throws DeploymentException { 47 JavabeanType javabean = (JavabeanType) xmlObject.copy().changeType(JavabeanType.type); 48 return getValue(javabean, type, cl); 49 } 50 51 private Object getValue(JavabeanType javabean, String className, ClassLoader cl) throws DeploymentException { 52 Class clazz = null; 53 try { 54 clazz = cl.loadClass(className); 55 } catch (ClassNotFoundException e) { 56 throw new DeploymentException("Could not load alleged javabean class " + className, e); 57 } 58 Object instance = null; 59 try { 60 instance = clazz.newInstance(); 61 } catch (Exception e) { 62 throw new DeploymentException("Could not create java bean instance", e); 63 } 64 PropertyDescriptor [] propertyDescriptors; 65 try { 66 BeanInfo beanInfo = Introspector.getBeanInfo(clazz); 67 propertyDescriptors = beanInfo.getPropertyDescriptors(); 68 } catch (IntrospectionException 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 propertyName = Introspector.decapitalize(property.getName()); 76 String propertyString = property.getStringValue().trim(); 77 for (int j = 0; j < propertyDescriptors.length; j++) { 78 PropertyDescriptor propertyDescriptor = propertyDescriptors[j]; 79 if (propertyName.equals(propertyDescriptor.getName())) { 80 String type = propertyDescriptor.getPropertyType().getName(); 81 PropertyEditor propertyEditor = null; 82 try { 83 propertyEditor = PropertyEditors.findEditor(type, cl); 84 } catch (ClassNotFoundException 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 value = propertyEditor.getValue(); 92 Method m = propertyDescriptor.getWriteMethod(); 93 try { 94 m.invoke(instance, new Object [] {value}); 95 } catch (Exception 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 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 } 138 | Popular Tags |