Save This Page
Home » geronimo-2.2-source-release » org.apache.geronimo.axis.builder » [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.axis.builder;
   18   
   19   import java.beans.IntrospectionException;
   20   import java.beans.Introspector;
   21   import java.beans.PropertyDescriptor;
   22   import java.util.ArrayList;
   23   import java.util.HashMap;
   24   import java.util.Iterator;
   25   import java.util.List;
   26   import java.util.Map;
   27   import java.util.Set;
   28   
   29   import javax.xml.namespace.QName;
   30   
   31   import org.apache.axis.description.ElementDesc;
   32   import org.apache.axis.description.FieldDesc;
   33   import org.apache.axis.encoding.ser.ArrayDeserializerFactory;
   34   import org.apache.axis.encoding.ser.ArraySerializerFactory;
   35   import org.apache.axis.encoding.ser.BeanDeserializerFactory;
   36   import org.apache.axis.encoding.ser.BeanSerializerFactory;
   37   import org.apache.geronimo.axis.client.TypeInfo;
   38   import org.apache.geronimo.common.DeploymentException;
   39   import org.apache.geronimo.kernel.ClassLoading;
   40   import org.apache.geronimo.xbeans.j2ee.JavaWsdlMappingType;
   41   import org.apache.geronimo.webservices.builder.SchemaTypeKey;
   42   import org.apache.geronimo.webservices.builder.WSDescriptorParser;
   43   import org.apache.xmlbeans.SchemaParticle;
   44   import org.apache.xmlbeans.SchemaType;
   45   
   46   /**
   47    * @version $Rev: 776717 $ $Date: 2009-05-20 07:54:13 -0700 (Wed, 20 May 2009) $
   48    */
   49   public class LightweightTypeInfoBuilder implements TypeInfoBuilder {
   50       private final ClassLoader cl;
   51       private final Map schemaTypeKeyToSchemaTypeMap;
   52       private final Set wrapperElementQNames;
   53   
   54       public LightweightTypeInfoBuilder(ClassLoader cl, Map schemaTypeKeyToSchemaTypeMap, Set wrapperElementQNames) {
   55           this.cl = cl;
   56           this.schemaTypeKeyToSchemaTypeMap = schemaTypeKeyToSchemaTypeMap;
   57           this.wrapperElementQNames = wrapperElementQNames;
   58       }
   59   
   60       public List buildTypeInfo(JavaWsdlMappingType mapping) throws DeploymentException {
   61           List typeInfoList = new ArrayList();
   62   
   63           for (Iterator iterator = schemaTypeKeyToSchemaTypeMap.keySet().iterator(); iterator.hasNext();) {
   64               SchemaTypeKey key = (SchemaTypeKey) iterator.next();
   65               if (!key.isElement() && !key.isAnonymous()) {
   66                   //default settings
   67                   QName typeQName = key.getqName();
   68                   String namespace = typeQName.getNamespaceURI();
   69                   String packageName = WSDescriptorParser.getPackageFromNamespace(namespace, mapping);
   70                   String classShortName = typeQName.getLocalPart();
   71                   String className = packageName + "." + classShortName;
   72   
   73                   Class clazz = null;
   74                   try {
   75                       clazz = ClassLoading.loadClass(className, cl);
   76                   } catch (ClassNotFoundException e) {
   77                       throw new DeploymentException("Could not load java type", e);
   78                   }
   79   
   80                   Class serializerFactoryClass = BeanSerializerFactory.class;
   81                   Class deserializerFactoryClass = BeanDeserializerFactory.class;
   82   
   83                   if (clazz.isArray()) {
   84                       serializerFactoryClass = ArraySerializerFactory.class;
   85                       deserializerFactoryClass = ArrayDeserializerFactory.class;
   86                   }
   87   
   88                   TypeInfo.UpdatableTypeInfo internalTypeInfo = new TypeInfo.UpdatableTypeInfo();
   89                   internalTypeInfo.setClazz(clazz);
   90                   internalTypeInfo.setQName(typeQName);
   91                   internalTypeInfo.setSerializerClass(serializerFactoryClass);
   92                   internalTypeInfo.setDeserializerClass(deserializerFactoryClass);
   93   
   94                   populateInternalTypeInfo(clazz, typeQName, key, internalTypeInfo);
   95   
   96                   typeInfoList.add(internalTypeInfo.buildTypeInfo());
   97               }
   98           }
   99   
  100           return typeInfoList;
  101       }
  102   
  103       private void populateInternalTypeInfo(Class javaClass, QName typeQName, SchemaTypeKey key, TypeInfo.UpdatableTypeInfo typeInfo) throws DeploymentException {
  104           SchemaType schemaType = (SchemaType) schemaTypeKeyToSchemaTypeMap.get(key);
  105           if (schemaType == null) {
  106               throw new DeploymentException("Schema type key " + key + " not found in analyzed schema: " + schemaTypeKeyToSchemaTypeMap);
  107           }
  108           typeInfo.setCanSearchParents(schemaType.getDerivationType() == SchemaType.DT_RESTRICTION);
  109   
  110           Map nameToType = new HashMap();
  111           if (null  == schemaType.getContentModel()) {
  112   
  113           } else if (SchemaParticle.SEQUENCE == schemaType.getContentModel().getParticleType()
  114                   || SchemaParticle.ALL == schemaType.getContentModel().getParticleType()) {
  115               SchemaParticle[] properties = schemaType.getContentModel().getParticleChildren();
  116               for (int i = 0; i < properties.length; i++) {
  117                   SchemaParticle parameter = properties[i];
  118   //                if (SchemaParticle.ELEMENT != parameter.getType().getContentModel().getParticleType()) {
  119   //                    throw new DeploymentException(parameter.getName() + " is not an element in schema " + schemaType.getName());
  120   //                }
  121                   nameToType.put(parameter.getName(), parameter);
  122               }
  123           } else if (SchemaParticle.ELEMENT == schemaType.getContentModel().getParticleType()) {
  124               SchemaParticle parameter = schemaType.getContentModel();
  125               nameToType.put(parameter.getName(), parameter);
  126           } else {
  127               throw new DeploymentException("Only all, choice and sequence particle types are supported." +
  128                       " SchemaType name =" + schemaType.getName());
  129           }
  130   
  131           PropertyDescriptor[] descriptors;
  132           try {
  133               descriptors = Introspector.getBeanInfo(javaClass).getPropertyDescriptors();
  134           } catch (IntrospectionException e) {
  135               throw new DeploymentException("Class " + javaClass + " is not a valid javabean", e);
  136           }
  137           Map nameToClass = new HashMap();
  138           for (int i = 0; i < descriptors.length; i++) {
  139               nameToClass.put(descriptors[i].getName(), descriptors[i].getPropertyType());
  140           }
  141   
  142           int idx = 0;
  143           FieldDesc[] fields = new FieldDesc[nameToType.size()];
  144           typeInfo.setFields(fields);
  145           for (Iterator iter = nameToType.entrySet().iterator(); iter.hasNext();) {
  146               Map.Entry entry = (Map.Entry) iter.next();
  147               QName fieldQName = (QName) entry.getKey();
  148               String fieldName = fieldQName.getLocalPart();
  149               SchemaParticle particle = (SchemaParticle) entry.getValue();
  150   
  151               ElementDesc elementDesc = new ElementDesc();
  152               elementDesc.setFieldName(fieldName);
  153   
  154               Class javaType = (Class) nameToClass.get(fieldName);
  155               if (null == javaType) {
  156                   throw new DeploymentException("Field " + fieldName + " is not defined by class " + javaClass.getName());
  157               }
  158               elementDesc.setNillable(particle.isNillable());
  159               elementDesc.setXmlName(fieldQName);
  160               elementDesc.setXmlType(particle.getType().getName());
  161   
  162               if (javaType.isArray()) {
  163                   elementDesc.setMinOccurs(particle.getIntMinOccurs());
  164                   elementDesc.setMaxOccurs(particle.getIntMaxOccurs());
  165                   //TODO axis seems to have the wrong name for this property based on how it is used
  166                   elementDesc.setMaxOccursUnbounded(particle.getIntMaxOccurs() > 1);
  167               }
  168   
  169               fields[idx++] = elementDesc;
  170           }
  171       }
  172   }

Save This Page
Home » geronimo-2.2-source-release » org.apache.geronimo.axis.builder » [javadoc | source]