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: */package org.apache.geronimo.deployment.service;
017:
018: import java.beans.BeanInfo;
019: import java.beans.IntrospectionException;
020: import java.beans.Introspector;
021: import java.beans.PropertyDescriptor;
022: import java.beans.PropertyEditor;
023: import java.lang.reflect.Method;
024:
025: import org.apache.geronimo.common.DeploymentException;
026: import org.apache.geronimo.common.propertyeditor.PropertyEditors;
027: import org.apache.geronimo.deployment.javabean.xbeans.JavabeanType;
028: import org.apache.geronimo.deployment.javabean.xbeans.PropertyType;
029: import org.apache.geronimo.deployment.javabean.xbeans.BeanPropertyType;
030: import org.apache.geronimo.gbean.GBeanInfo;
031: import org.apache.geronimo.gbean.GBeanInfoBuilder;
032: import org.apache.geronimo.crypto.EncryptionManager;
033: import org.apache.xmlbeans.XmlObject;
034:
035: /**
036: * @version $Rev: 617588 $ $Date: 2008-02-01 10:20:07 -0800 (Fri, 01 Feb 2008) $
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,
047: ClassLoader cl) throws DeploymentException {
048: JavabeanType javabean = (JavabeanType) xmlObject.copy()
049: .changeType(JavabeanType.type);
050: return getValue(javabean, type, cl);
051: }
052:
053: private Object getValue(JavabeanType javabean, String type,
054: ClassLoader cl) throws DeploymentException {
055: String className = type;
056: if (javabean.isSetClass1()) {
057: className = javabean.getClass1();
058: }
059: Class clazz = null;
060: try {
061: clazz = cl.loadClass(className);
062: if (!type.equals(className)
063: && !cl.loadClass(type).isAssignableFrom(clazz)) {
064: throw new DeploymentException("javabean class "
065: + className + " is not of the expected type "
066: + type);
067: }
068: } catch (ClassNotFoundException e) {
069: throw new DeploymentException(
070: "Could not load alleged javabean class "
071: + className, e);
072: }
073: Object instance = null;
074: try {
075: instance = clazz.newInstance();
076: } catch (Exception e) {
077: throw new DeploymentException(
078: "Could not create java bean instance", e);
079: }
080:
081: PropertyDescriptor[] propertyDescriptors;
082: try {
083: BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
084: propertyDescriptors = beanInfo.getPropertyDescriptors();
085: } catch (IntrospectionException e) {
086: throw new DeploymentException(
087: "Could not analyze java bean class", e);
088: }
089:
090: PropertyType[] properties = javabean.getPropertyArray();
091: for (int i = 0; i < properties.length; i++) {
092: PropertyType property = properties[i];
093: String propertyName = Introspector.decapitalize(property
094: .getName());
095: String propertyString = property.getStringValue().trim();
096: for (int j = 0; j < propertyDescriptors.length; j++) {
097: PropertyDescriptor propertyDescriptor = propertyDescriptors[j];
098: if (propertyName.equals(propertyDescriptor.getName())) {
099: Method writeMethod = propertyDescriptor
100: .getWriteMethod();
101: if (writeMethod
102: .isAnnotationPresent(EncryptOnPersist.class)) {
103: propertyString = (String) EncryptionManager
104: .decrypt(propertyString);
105: }
106:
107: String protertyType = propertyDescriptor
108: .getPropertyType().getName();
109:
110: PropertyEditor propertyEditor = null;
111: try {
112: propertyEditor = PropertyEditors.findEditor(
113: protertyType, cl);
114: } catch (ClassNotFoundException e) {
115: throw new DeploymentException(
116: "Could not load editor for type "
117: + protertyType, e);
118: }
119: if (propertyEditor == null) {
120: throw new DeploymentException(
121: "Unable to find PropertyEditor for "
122: + protertyType);
123: }
124: propertyEditor.setAsText(propertyString);
125: Object value = propertyEditor.getValue();
126:
127: try {
128: writeMethod.invoke(instance,
129: new Object[] { value });
130: } catch (Exception e) {
131: throw new DeploymentException(
132: "Could not set property value for property named "
133: + propertyName, e);
134: }
135: break;
136: }
137: }
138: }
139:
140: BeanPropertyType[] beanProperties = javabean
141: .getBeanPropertyArray();
142: for (int i = 0; i < beanProperties.length; i++) {
143: BeanPropertyType beanProperty = beanProperties[i];
144: String propertyName = Introspector
145: .decapitalize(beanProperty.getName().trim());
146: JavabeanType innerBean = beanProperty.getJavabean();
147: for (int j = 0; j < propertyDescriptors.length; j++) {
148: PropertyDescriptor propertyDescriptor = propertyDescriptors[j];
149: if (propertyName.equals(propertyDescriptor.getName())) {
150: String propertyType = propertyDescriptor
151: .getPropertyType().getName();
152: Object value = getValue(innerBean, propertyType, cl);
153: Method m = propertyDescriptor.getWriteMethod();
154: try {
155: m.invoke(instance, new Object[] { value });
156: } catch (Exception e) {
157: throw new DeploymentException(
158: "Could not set property value for property named "
159: + propertyName, e);
160: }
161: break;
162: }
163: }
164: }
165: return instance;
166: }
167:
168: public static final GBeanInfo GBEAN_INFO;
169:
170: static {
171: GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(
172: JavaBeanXmlAttributeBuilder.class,
173: "XmlAttributeBuilder");
174: infoBuilder.addInterface(XmlAttributeBuilder.class);
175: GBEAN_INFO = infoBuilder.getBeanInfo();
176: }
177:
178: public static GBeanInfo getGBeanInfo() {
179: return GBEAN_INFO;
180: }
181: }
|