Home » openejb-3.1.2-src » org.apache » openejb » server » axis » [javadoc | source]

    1   /**
    2    *
    3    * Licensed to the Apache Software Foundation (ASF) under one or more
    4    * contributor license agreements.  See the NOTICE file distributed with
    5    * this work for additional information regarding copyright ownership.
    6    * The ASF licenses this file to You under the Apache License, Version 2.0
    7    * (the "License"); you may not use this file except in compliance with
    8    * 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, software
   13    *  distributed under the License is distributed on an "AS IS" BASIS,
   14    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15    *  See the License for the specific language governing permissions and
   16    *  limitations under the License.
   17    */
   18   package org.apache.openejb.server.axis;
   19   
   20   import org.apache.axis.constants.Style;
   21   import org.apache.axis.constants.Use;
   22   import org.apache.axis.description.FaultDesc;
   23   import org.apache.axis.description.JavaServiceDesc;
   24   import org.apache.axis.description.OperationDesc;
   25   import org.apache.axis.description.ParameterDesc;
   26   import org.apache.axis.encoding.DefaultJAXRPC11TypeMappingImpl;
   27   import org.apache.axis.encoding.DefaultSOAPEncodingTypeMappingImpl;
   28   import org.apache.axis.encoding.TypeMapping;
   29   import org.apache.axis.encoding.TypeMappingImpl;
   30   import org.apache.axis.encoding.TypeMappingRegistryImpl;
   31   import org.apache.axis.encoding.XMLType;
   32   import org.apache.axis.encoding.ser.ArrayDeserializerFactory;
   33   import org.apache.axis.encoding.ser.ArraySerializerFactory;
   34   import org.apache.axis.encoding.ser.BaseDeserializerFactory;
   35   import org.apache.axis.encoding.ser.BaseSerializerFactory;
   36   import org.apache.axis.encoding.ser.BeanDeserializerFactory;
   37   import org.apache.axis.encoding.ser.BeanSerializerFactory;
   38   import org.apache.axis.encoding.ser.EnumDeserializerFactory;
   39   import org.apache.axis.encoding.ser.EnumSerializerFactory;
   40   import org.apache.axis.encoding.ser.SimpleListDeserializerFactory;
   41   import org.apache.axis.encoding.ser.SimpleListSerializerFactory;
   42   import org.apache.openejb.OpenEJBException;
   43   import org.apache.openejb.server.axis.assembler.BindingStyle;
   44   import org.apache.openejb.server.axis.assembler.JaxRpcFaultInfo;
   45   import org.apache.openejb.server.axis.assembler.JaxRpcOperationInfo;
   46   import org.apache.openejb.server.axis.assembler.JaxRpcParameterInfo;
   47   import org.apache.openejb.server.axis.assembler.JaxRpcServiceInfo;
   48   import org.apache.openejb.server.axis.assembler.JaxRpcTypeInfo;
   49   
   50   import javax.wsdl.OperationType;
   51   import javax.xml.rpc.encoding.DeserializerFactory;
   52   import javax.xml.rpc.encoding.SerializerFactory;
   53   import java.lang.reflect.Method;
   54   import java.util.ArrayList;
   55   
   56   public class JavaServiceDescBuilder {
   57       private static final TypeMappingImpl SOAP_TYPE_MAPPING = DefaultSOAPEncodingTypeMappingImpl.getSingleton();
   58       private static final TypeMappingImpl JAXRPC_TYPE_MAPPING = DefaultJAXRPC11TypeMappingImpl.getSingleton();
   59   
   60       private final JaxRpcServiceInfo serviceInfo;
   61       private final ClassLoader classLoader;
   62   
   63       public JavaServiceDescBuilder(JaxRpcServiceInfo serviceInfo, ClassLoader classLoader) {
   64           this.serviceInfo = serviceInfo;
   65           this.classLoader = classLoader;
   66       }
   67   
   68       public JavaServiceDesc createServiceDesc() throws OpenEJBException {
   69           Class serviceEndpointInterface;
   70           try {
   71               serviceEndpointInterface = classLoader.loadClass(serviceInfo.serviceEndpointInterface);
   72           } catch (ClassNotFoundException e) {
   73               throw new OpenEJBException("Unable to load the service endpoint interface " + serviceInfo.serviceEndpointInterface, e);
   74           }
   75   
   76           JavaServiceDesc serviceDesc = new JavaServiceDesc();
   77           serviceDesc.setName(serviceInfo.name);
   78           serviceDesc.setEndpointURL(serviceInfo.endpointURL);
   79           serviceDesc.setWSDLFile(serviceInfo.wsdlFile);
   80   
   81           BindingStyle bindingStyle = serviceInfo.defaultBindingStyle;
   82           switch (bindingStyle) {
   83               case RPC_ENCODED:
   84                   serviceDesc.setStyle(Style.RPC);
   85                   serviceDesc.setUse(Use.ENCODED);
   86                   break;
   87               case RPC_LITERAL:
   88                   serviceDesc.setStyle(Style.RPC);
   89                   serviceDesc.setUse(Use.LITERAL);
   90                   break;
   91               case DOCUMENT_ENCODED:
   92                   serviceDesc.setStyle(Style.DOCUMENT);
   93                   serviceDesc.setUse(Use.ENCODED);
   94                   break;
   95               case DOCUMENT_LITERAL:
   96                   serviceDesc.setStyle(Style.DOCUMENT);
   97                   serviceDesc.setUse(Use.LITERAL);
   98                   break;
   99               case DOCUMENT_LITERAL_WRAPPED:
  100                   serviceDesc.setStyle(Style.WRAPPED);
  101                   serviceDesc.setUse(Use.LITERAL);
  102                   break;
  103           }
  104   
  105           // Operations
  106           for (JaxRpcOperationInfo operationInfo : serviceInfo.operations) {
  107               OperationDesc operationDesc = buildOperationDesc(operationInfo, serviceEndpointInterface);
  108               serviceDesc.addOperationDesc(operationDesc);
  109           }
  110   
  111           // Type mapping registry
  112           TypeMappingRegistryImpl typeMappingRegistry = new TypeMappingRegistryImpl();
  113           typeMappingRegistry.doRegisterFromVersion("1.3");
  114           serviceDesc.setTypeMappingRegistry(typeMappingRegistry);
  115   
  116           // Type mapping
  117           TypeMapping typeMapping = typeMappingRegistry.getOrMakeTypeMapping(serviceDesc.getUse().getEncoding());
  118           serviceDesc.setTypeMapping(typeMapping);
  119   
  120           // Types
  121           for (JaxRpcTypeInfo type : serviceInfo.types) {
  122               registerType(type, typeMapping);
  123           }
  124   
  125           return new ReadOnlyServiceDesc(serviceDesc);
  126       }
  127   
  128       private OperationDesc buildOperationDesc(JaxRpcOperationInfo operationInfo, Class serviceEndpointInterface) throws OpenEJBException {
  129           OperationDesc operationDesc = new OperationDesc();
  130           operationDesc.setName(operationInfo.name);
  131   
  132           // Binding type
  133           switch (operationInfo.bindingStyle) {
  134               case RPC_ENCODED:
  135                   operationDesc.setStyle(Style.RPC);
  136                   operationDesc.setUse(Use.ENCODED);
  137                   break;
  138               case RPC_LITERAL:
  139                   operationDesc.setStyle(Style.RPC);
  140                   operationDesc.setUse(Use.LITERAL);
  141                   break;
  142               case DOCUMENT_ENCODED:
  143                   operationDesc.setStyle(Style.DOCUMENT);
  144                   operationDesc.setUse(Use.ENCODED);
  145                   break;
  146               case DOCUMENT_LITERAL:
  147                   operationDesc.setStyle(Style.DOCUMENT);
  148                   operationDesc.setUse(Use.LITERAL);
  149                   break;
  150               case DOCUMENT_LITERAL_WRAPPED:
  151                   operationDesc.setStyle(Style.WRAPPED);
  152                   operationDesc.setUse(Use.LITERAL);
  153                   break;
  154           }
  155   
  156           // Operation style
  157           switch (operationInfo.operationStyle) {
  158               case NOTIFICATION:
  159                   operationDesc.setMep(OperationType.NOTIFICATION);
  160                   break;
  161               case ONE_WAY:
  162                   operationDesc.setMep(OperationType.ONE_WAY);
  163                   break;
  164               case REQUEST_RESPONSE:
  165                   operationDesc.setMep(OperationType.REQUEST_RESPONSE);
  166                   break;
  167               case SOLICIT_RESPONSE:
  168                   operationDesc.setMep(OperationType.SOLICIT_RESPONSE);
  169                   break;
  170           }
  171   
  172           // Build parameters
  173           Class[] paramTypes = new Class[operationInfo.parameters.size()];
  174           int i = 0;
  175           for (JaxRpcParameterInfo parameterInfo : operationInfo.parameters) {
  176               ParameterDesc parameterDesc = buildParameterDesc(parameterInfo);
  177               operationDesc.addParameter(parameterDesc);
  178               paramTypes[i++] = parameterDesc.getJavaType();
  179           }
  180   
  181           // Java method
  182           try {
  183               Method method = serviceEndpointInterface.getMethod(operationInfo.javaMethodName, paramTypes);
  184               operationDesc.setMethod(method);
  185           } catch (NoSuchMethodException e) {
  186               String args = "";
  187               for (Class paramType : paramTypes) {
  188                   if (args.length() > 0) {
  189                       args += ", ";
  190                   }
  191                   args += paramType.getName();
  192               }
  193               throw new OpenEJBException("Mapping references non-existent method in service-endpoint: " + operationInfo.javaMethodName + "(" + args + ")");
  194           }
  195   
  196           //
  197           // Set return
  198           //
  199           if (operationInfo.returnQName != null) {
  200               operationDesc.setReturnQName(operationInfo.returnQName);
  201               operationDesc.setReturnType(operationInfo.returnXmlType);
  202               try {
  203                   Class<?> returnClass = classLoader.loadClass(operationInfo.returnJavaType);
  204                   operationDesc.setReturnClass(returnClass);
  205               } catch (ClassNotFoundException e) {
  206                   throw new OpenEJBException();
  207               }
  208           } else if (operationInfo.operationStyle == JaxRpcOperationInfo.OperationStyle.REQUEST_RESPONSE) {
  209               operationDesc.setReturnQName(null);
  210               operationDesc.setReturnType(XMLType.AXIS_VOID);
  211               operationDesc.setReturnClass(void.class);
  212           }
  213   
  214           // Build faults
  215           for (JaxRpcFaultInfo faultInfo : operationInfo.faults) {
  216               FaultDesc faultDesc = buildFaultDesc(faultInfo);
  217               operationDesc.addFault(faultDesc);
  218           }
  219   
  220           return operationDesc;
  221       }
  222   
  223       private ParameterDesc buildParameterDesc(JaxRpcParameterInfo parameterInfo) throws OpenEJBException {
  224           byte mode = ParameterDesc.modeFromString(parameterInfo.mode.toString());
  225   
  226           boolean inHeader = parameterInfo.soapHeader && parameterInfo.mode.isIn();
  227           boolean outHeader = parameterInfo.soapHeader  && parameterInfo.mode.isOut();
  228   
  229           Class<?> javaType;
  230           try {
  231               javaType = classLoader.loadClass(parameterInfo.javaType);
  232           } catch (ClassNotFoundException e) {
  233               throw new OpenEJBException("Unable to load parameter type " + parameterInfo.javaType);
  234           }
  235   
  236           ParameterDesc parameterDesc = new ParameterDesc(parameterInfo.qname, mode, parameterInfo.xmlType, javaType, inHeader, outHeader);
  237           return parameterDesc;
  238       }
  239   
  240       private FaultDesc buildFaultDesc(JaxRpcFaultInfo faultInfo) throws OpenEJBException {
  241           FaultDesc faultDesc = new FaultDesc(faultInfo.qname, faultInfo.javaType, faultInfo.xmlType, faultInfo.complex);
  242   
  243           ArrayList<ParameterDesc> parameters = new ArrayList<ParameterDesc>();
  244           for (JaxRpcParameterInfo parameterInfo : faultInfo.parameters) {
  245               ParameterDesc parameterDesc = buildParameterDesc(parameterInfo);
  246               parameters.add(parameterDesc);
  247           }
  248           faultDesc.setParameters(parameters);
  249   
  250           return faultDesc;
  251       }
  252   
  253       private void registerType(JaxRpcTypeInfo type, TypeMapping typeMapping) throws OpenEJBException {
  254           Class javaType;
  255           try {
  256               javaType = classLoader.loadClass(type.javaType);
  257           } catch (ClassNotFoundException e) {
  258               throw new OpenEJBException("Could not load class for JaxRpc mapping " + type.javaType);
  259           }
  260   
  261           // Default uses the generic Java Beans serializer/deserializer
  262           Class serializerFactoryClass = BeanSerializerFactory.class;
  263           Class deserializerFactoryClass = BeanDeserializerFactory.class;
  264           switch (type.serializerType) {
  265               case ARRAY:
  266                   serializerFactoryClass = ArraySerializerFactory.class;
  267                   deserializerFactoryClass = ArrayDeserializerFactory.class;
  268                   break;
  269               case ENUM:
  270                   serializerFactoryClass = EnumSerializerFactory.class;
  271                   deserializerFactoryClass = EnumDeserializerFactory.class;
  272                   break;
  273               case LIST:
  274                   serializerFactoryClass = SimpleListSerializerFactory.class;
  275                   deserializerFactoryClass = SimpleListDeserializerFactory.class;
  276                   break;
  277               default:
  278                   if (type.simpleBaseType != null) {
  279                       Class clazz = SOAP_TYPE_MAPPING.getClassForQName(type.simpleBaseType, null, null);
  280                       if (null != clazz) {
  281                           // Built in SOAP type
  282                           serializerFactoryClass = SOAP_TYPE_MAPPING.getSerializer(clazz, type.simpleBaseType).getClass();
  283                           deserializerFactoryClass = SOAP_TYPE_MAPPING.getDeserializer(clazz, type.simpleBaseType, null).getClass();
  284                       } else {
  285                           clazz = JAXRPC_TYPE_MAPPING.getClassForQName(type.simpleBaseType, null, null);
  286                           if (null != clazz) {
  287                               // Built in XML schema type
  288                               serializerFactoryClass = JAXRPC_TYPE_MAPPING.getSerializer(clazz, type.simpleBaseType).getClass();
  289                               deserializerFactoryClass = JAXRPC_TYPE_MAPPING.getDeserializer(clazz, type.simpleBaseType, null).getClass();
  290                           }
  291                       }
  292                   }
  293                   break;
  294           }
  295   
  296           SerializerFactory serializerFactory = BaseSerializerFactory.createFactory(serializerFactoryClass, javaType, type.qname);
  297           DeserializerFactory deserializerFactory = BaseDeserializerFactory.createFactory(deserializerFactoryClass, javaType, type.qname);
  298   
  299           typeMapping.register(javaType, type.qname, serializerFactory, deserializerFactory);
  300       }
  301   }

Home » openejb-3.1.2-src » org.apache » openejb » server » axis » [javadoc | source]