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

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with 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,
   13    * software distributed under the License is distributed on an
   14    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    * KIND, either express or implied.  See the License for the
   16    * specific language governing permissions and limitations
   17    * under the License.
   18    */
   19   
   20   package org.apache.geronimo.deployment.service;
   21   
   22   import java.beans.BeanInfo;
   23   import java.beans.IntrospectionException;
   24   import java.beans.Introspector;
   25   import java.beans.PropertyDescriptor;
   26   import java.beans.PropertyEditorSupport;
   27   import java.lang.reflect.Method;
   28   
   29   import javax.xml.namespace.QName;
   30   
   31   import org.apache.geronimo.common.DeploymentException;
   32   import org.apache.geronimo.common.propertyeditor.PropertyEditorException;
   33   import org.apache.geronimo.deployment.javabean.xbeans.BeanPropertyType;
   34   import org.apache.geronimo.deployment.javabean.xbeans.JavabeanDocument;
   35   import org.apache.geronimo.deployment.javabean.xbeans.JavabeanType;
   36   import org.apache.geronimo.deployment.javabean.xbeans.PropertyType;
   37   import org.apache.geronimo.crypto.EncryptionManager;
   38   import org.apache.xmlbeans.XmlException;
   39   import org.apache.xmlbeans.XmlOptions;
   40   
   41   /**
   42    *
   43    * @version $Rev:$ $Date:$
   44    */
   45   public class JavaBeanXmlAttributeEditor extends PropertyEditorSupport {
   46       private static final QName QNAME = JavabeanDocument.type.getDocumentElementName();
   47       private static final Class[] PRIMITIVES_CLASSES = new Class[] {String.class,
   48           Boolean.class,
   49           Character.class,
   50           Byte.class,
   51           Short.class,
   52           Integer.class,
   53           Long.class,
   54           Float.class,
   55           Double.class
   56       };
   57   
   58       private final Class javaBeanClazz;
   59       private final XmlAttributeBuilder xmlAttributeBuilder;
   60       
   61       public JavaBeanXmlAttributeEditor(Class clazz) {
   62           if (null == clazz) {
   63               throw new IllegalArgumentException("clazz is required");
   64           }
   65           this.javaBeanClazz = clazz;
   66           
   67           xmlAttributeBuilder = newXmlAttributeBuilder();
   68       }
   69   
   70       protected JavaBeanXmlAttributeBuilder newXmlAttributeBuilder() {
   71           return new JavaBeanXmlAttributeBuilder();
   72       }
   73   
   74       @Override
   75       public void setAsText(String text) throws IllegalArgumentException {
   76           try {
   77               JavabeanDocument document = JavabeanDocument.Factory.parse(text);
   78               JavabeanType javaBeanType = document.getJavabean();
   79               
   80               Object javabean = xmlAttributeBuilder.getValue(javaBeanType,
   81                   javaBeanClazz.getName(),
   82                   getClass().getClassLoader());
   83   
   84               setValue(javabean);
   85           } catch (XmlException e) {
   86               throw new PropertyEditorException(e);
   87           } catch (DeploymentException e) {
   88               throw new PropertyEditorException(e);
   89           }
   90       }
   91   
   92       @Override
   93       public String getAsText() {
   94           JavabeanType javabeanType = getJavabeanType(getValue());
   95           
   96           XmlOptions xmlOptions = new XmlOptions();
   97           xmlOptions.setSaveSyntheticDocumentElement(QNAME);
   98           xmlOptions.setSavePrettyPrint();
   99           return javabeanType.xmlText(xmlOptions);
  100       }
  101   
  102       protected JavabeanType getJavabeanType(Object javaBean) {
  103           JavabeanType javabeanType = JavabeanType.Factory.newInstance();
  104   
  105           javabeanType.setClass1(javaBean.getClass().getName());
  106           
  107           PropertyDescriptor[] propertyDescriptors;
  108           try {
  109               BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass());
  110               propertyDescriptors = beanInfo.getPropertyDescriptors();
  111           } catch (IntrospectionException e) {
  112               throw new IllegalStateException("See nested", e);
  113           }
  114   
  115           for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
  116               handle(javaBean, propertyDescriptor, javabeanType);
  117           }
  118           
  119           return javabeanType;
  120       }
  121       
  122       protected void handle(Object javaBean, PropertyDescriptor propertyDescriptor, JavabeanType javabeanType) {
  123           Method readMethod = propertyDescriptor.getReadMethod();
  124           if (null == readMethod) {
  125               return;
  126           } else if (readMethod.isAnnotationPresent(DoNotPersist.class) || readMethod.getName().equals("getClass")) {
  127               return;
  128           }
  129           
  130           Object value;
  131           try {
  132               value = readMethod.invoke(javaBean, null);
  133           } catch (Exception e) {
  134               throw new IllegalStateException("See nested", e);
  135           }
  136           if (null == value) {
  137               return;
  138           }
  139           
  140           if (isPrimitive(value)) {
  141               PropertyType propertyType = javabeanType.addNewProperty();
  142               propertyType.setName(propertyDescriptor.getName());
  143               
  144               String valueAsString = value.toString();
  145               if (readMethod.isAnnotationPresent(EncryptOnPersist.class)) {
  146                   valueAsString = EncryptionManager.encrypt(valueAsString);
  147               }
  148               
  149               propertyType.setStringValue(valueAsString);
  150           } else {
  151               JavabeanType nestedJavabeanType = getJavabeanType(value);
  152               
  153               BeanPropertyType propertyType = javabeanType.addNewBeanProperty();
  154               propertyType.setName(propertyDescriptor.getName());
  155               propertyType.setJavabean(nestedJavabeanType);
  156           }
  157       }
  158       
  159       protected boolean isPrimitive(Object propertyValue) {
  160           Class valueClass = propertyValue.getClass();
  161           for (Class primitiveClass : PRIMITIVES_CLASSES) {
  162               if (valueClass.equals(primitiveClass)) {
  163                   return true;
  164               }
  165           }
  166           return false;
  167       }
  168       
  169   }

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