Home » geronimo-2.2-source-release » org.apache.geronimo.deployment.service » [javadoc | source]

    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;
   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.geronimo.crypto.EncryptionManager;
   34   import org.apache.xmlbeans.XmlObject;
   35   
   36   /**
   37    * @version $Rev: 617588 $ $Date: 2008-02-01 10:20:07 -0800 (Fri, 01 Feb 2008) $
   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 type, ClassLoader cl) throws DeploymentException {
   53           String className = type;
   54           if (javabean.isSetClass1()) {
   55               className = javabean.getClass1();
   56           }
   57           Class clazz = null;
   58           try {
   59               clazz = cl.loadClass(className);
   60               if (!type.equals(className) && !cl.loadClass(type).isAssignableFrom(clazz)) {
   61                   throw new DeploymentException("javabean class " + className + " is not of the expected type " + type);
   62               }
   63           } catch (ClassNotFoundException e) {
   64               throw new DeploymentException("Could not load alleged javabean class " + className, e);
   65           }
   66           Object instance = null;
   67           try {
   68               instance = clazz.newInstance();
   69           } catch (Exception e) {
   70               throw new DeploymentException("Could not create java bean instance", e);
   71           }
   72           
   73           PropertyDescriptor[] propertyDescriptors;
   74           try {
   75               BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
   76               propertyDescriptors = beanInfo.getPropertyDescriptors();
   77           } catch (IntrospectionException e) {
   78               throw new DeploymentException("Could not analyze java bean class", e);
   79           }
   80   
   81           PropertyType[] properties = javabean.getPropertyArray();
   82           for (int i = 0; i < properties.length; i++) {
   83               PropertyType property = properties[i];
   84               String propertyName = Introspector.decapitalize(property.getName());
   85               String propertyString = property.getStringValue().trim();
   86               for (int j = 0; j < propertyDescriptors.length; j++) {
   87                   PropertyDescriptor propertyDescriptor = propertyDescriptors[j];
   88                   if (propertyName.equals(propertyDescriptor.getName())) {
   89                       Method writeMethod = propertyDescriptor.getWriteMethod();
   90                       if (writeMethod.isAnnotationPresent(EncryptOnPersist.class)) {
   91                           propertyString = (String) EncryptionManager.decrypt(propertyString);
   92                       }
   93   
   94                       String protertyType = propertyDescriptor.getPropertyType().getName();
   95                       
   96                       PropertyEditor propertyEditor = null;
   97                       try {
   98                           propertyEditor = PropertyEditors.findEditor(protertyType, cl);
   99                       } catch (ClassNotFoundException e) {
  100                           throw new DeploymentException("Could not load editor for type " + protertyType, e);
  101                       }
  102                       if (propertyEditor == null) {
  103                           throw new DeploymentException("Unable to find PropertyEditor for " + protertyType);
  104                       }
  105                       propertyEditor.setAsText(propertyString);
  106                       Object value = propertyEditor.getValue();
  107                       
  108                       try {
  109                           writeMethod.invoke(instance, new Object[] {value});
  110                       } catch (Exception e) {
  111                           throw new DeploymentException("Could not set property value for property named " + propertyName, e);
  112                       }
  113                       break;
  114                   }
  115               }
  116           }
  117   
  118           BeanPropertyType[] beanProperties = javabean.getBeanPropertyArray();
  119           for (int i = 0; i < beanProperties.length; i++) {
  120               BeanPropertyType beanProperty = beanProperties[i];
  121               String propertyName = Introspector.decapitalize(beanProperty.getName().trim());
  122               JavabeanType innerBean = beanProperty.getJavabean();
  123               for (int j = 0; j < propertyDescriptors.length; j++) {
  124                   PropertyDescriptor propertyDescriptor = propertyDescriptors[j];
  125                   if (propertyName.equals(propertyDescriptor.getName())) {
  126                       String propertyType = propertyDescriptor.getPropertyType().getName();
  127                       Object value = getValue(innerBean, propertyType, cl);
  128                       Method m = propertyDescriptor.getWriteMethod();
  129                       try {
  130                           m.invoke(instance, new Object[] {value});
  131                       } catch (Exception e) {
  132                           throw new DeploymentException("Could not set property value for property named " + propertyName, e);
  133                       }
  134                       break;
  135                   }
  136               }
  137           }
  138           return instance;
  139       }
  140   
  141       public static final GBeanInfo GBEAN_INFO;
  142   
  143       static {
  144           GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(JavaBeanXmlAttributeBuilder.class, "XmlAttributeBuilder");
  145           infoBuilder.addInterface(XmlAttributeBuilder.class);
  146           GBEAN_INFO = infoBuilder.getBeanInfo();
  147       }
  148   
  149       public static GBeanInfo getGBeanInfo() {
  150           return GBEAN_INFO;
  151       }
  152   }

Home » geronimo-2.2-source-release » org.apache.geronimo.deployment.service » [javadoc | source]