Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » server » axis » client » [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.openejb.server.axis.client;
   18   
   19   import net.sf.cglib.core.Signature;
   20   import net.sf.cglib.proxy.Callback;
   21   import net.sf.cglib.proxy.Enhancer;
   22   import net.sf.cglib.proxy.MethodInterceptor;
   23   import net.sf.cglib.proxy.MethodProxy;
   24   import net.sf.cglib.proxy.NoOp;
   25   import net.sf.cglib.reflect.FastClass;
   26   import net.sf.cglib.reflect.FastConstructor;
   27   import org.apache.axis.AxisEngine;
   28   import org.apache.axis.Constants;
   29   import org.apache.axis.client.Service;
   30   import org.apache.axis.constants.Use;
   31   import org.apache.axis.description.TypeDesc;
   32   import org.apache.axis.encoding.TypeMapping;
   33   import org.apache.axis.encoding.TypeMappingRegistry;
   34   import org.apache.axis.encoding.ser.SimpleDeserializerFactory;
   35   import org.apache.axis.encoding.ser.SimpleSerializerFactory;
   36   import org.apache.axis.handlers.HandlerInfoChainFactory;
   37   
   38   import javax.xml.namespace.QName;
   39   import javax.xml.rpc.ServiceException;
   40   import javax.xml.rpc.handler.HandlerChain;
   41   import java.lang.reflect.InvocationTargetException;
   42   import java.math.BigInteger;
   43   import java.net.MalformedURLException;
   44   import java.net.URI;
   45   import java.net.URL;
   46   import java.rmi.Remote;
   47   import java.util.Iterator;
   48   import java.util.List;
   49   
   50   public class SeiFactoryImpl implements SeiFactory {
   51       private final QName serviceName;
   52       private final QName portQName;
   53       private final String serviceEndpointClassName;
   54       private final OperationInfo[] operationInfos;
   55       private FastConstructor constructor;
   56       private Object serviceImpl;
   57       private final List typeInfo;
   58       private final URL location;
   59       private final List handlerInfos;
   60       private final String credentialsName;
   61       private HandlerInfoChainFactory handlerInfoChainFactory;
   62       private OperationInfo[] sortedOperationInfos;
   63       private Class serviceEndpointClass;
   64   
   65       public SeiFactoryImpl(QName serviceName,
   66               String portName,
   67               String serviceEndpointClassName,
   68               OperationInfo[] operationInfos,
   69               List typeInfo,
   70               URL location,
   71               List handlerInfos,
   72               String credentialsName) {
   73           this.serviceName = serviceName;
   74           this.portQName = new QName("", portName);
   75           this.serviceEndpointClassName = serviceEndpointClassName;
   76           this.operationInfos = operationInfos;
   77           this.typeInfo = typeInfo;
   78           this.location = location;
   79           this.handlerInfos = handlerInfos;
   80           this.credentialsName = credentialsName;
   81       }
   82   
   83       void initialize(Object serviceImpl, ClassLoader classLoader) throws ClassNotFoundException {
   84           this.serviceImpl = serviceImpl;
   85           Class serviceEndpointBaseClass = classLoader.loadClass(serviceEndpointClassName);
   86           serviceEndpointClass = enhanceServiceEndpointInterface(serviceEndpointBaseClass, classLoader);
   87           Class[] constructorTypes = new Class[]{classLoader.loadClass(GenericServiceEndpoint.class.getName())};
   88           this.constructor = FastClass.create(serviceEndpointClass).getConstructor(constructorTypes);
   89           this.handlerInfoChainFactory = new HandlerInfoChainFactory(handlerInfos);
   90           sortedOperationInfos = new OperationInfo[FastClass.create(serviceEndpointClass).getMaxIndex() + 1];
   91           String encodingStyle = "";
   92           for (int i = 0; i < operationInfos.length; i++) {
   93               OperationInfo operationInfo = operationInfos[i];
   94               Signature signature = operationInfo.getSignature();
   95               MethodProxy methodProxy = MethodProxy.find(serviceEndpointClass, signature);
   96               if (methodProxy == null) {
   97                   throw new RuntimeException("No method proxy for operationInfo " + signature);
   98               }
   99               int index = methodProxy.getSuperIndex();
  100               sortedOperationInfos[index] = operationInfo;
  101               if (operationInfo.getOperationDesc().getUse() == Use.ENCODED) {
  102                   encodingStyle = org.apache.axis.Constants.URI_SOAP11_ENC;
  103               }
  104           }
  105           //register our type descriptors
  106           Service service = ((ServiceImpl) serviceImpl).getService();
  107           AxisEngine axisEngine = service.getEngine();
  108           TypeMappingRegistry typeMappingRegistry = axisEngine.getTypeMappingRegistry();
  109           TypeMapping typeMapping = typeMappingRegistry.getOrMakeTypeMapping(encodingStyle);
  110           typeMapping.register(BigInteger.class, Constants.XSD_UNSIGNEDLONG, new SimpleSerializerFactory(BigInteger.class, Constants.XSD_UNSIGNEDLONG), new SimpleDeserializerFactory(BigInteger.class, Constants.XSD_UNSIGNEDLONG));
  111           typeMapping.register(URI.class, Constants.XSD_ANYURI, new SimpleSerializerFactory(URI.class, Constants.XSD_ANYURI), new SimpleDeserializerFactory(URI.class, Constants.XSD_ANYURI));
  112           //It is essential that the types be registered before the typeInfos create the serializer/deserializers.
  113           for (Iterator iter = typeInfo.iterator(); iter.hasNext();) {
  114               TypeInfo info = (TypeInfo) iter.next();
  115               TypeDesc.registerTypeDescForClass(info.getClazz(), info.buildTypeDesc());
  116           }
  117           TypeInfo.register(typeInfo, typeMapping);
  118       }
  119   
  120       private Class enhanceServiceEndpointInterface(Class serviceEndpointInterface, ClassLoader classLoader) {
  121           Enhancer enhancer = new Enhancer();
  122           enhancer.setClassLoader(classLoader);
  123           enhancer.setSuperclass(GenericServiceEndpointWrapper.class);
  124           enhancer.setInterfaces(new Class[]{serviceEndpointInterface});
  125           enhancer.setCallbackFilter(new NoOverrideCallbackFilter(GenericServiceEndpointWrapper.class));
  126           enhancer.setCallbackTypes(new Class[]{NoOp.class, MethodInterceptor.class});
  127           enhancer.setUseFactory(false);
  128           enhancer.setUseCache(false);
  129   
  130           return enhancer.createClass();
  131       }
  132   
  133       public Remote createServiceEndpoint() throws ServiceException {
  134           //TODO figure out why this can't be called in readResolve!
  135   //        synchronized (this) {
  136   //            if (!initialized) {
  137   //                initialize();
  138   //                initialized = true;
  139   //            }
  140   //        }
  141           Service service = ((ServiceImpl) serviceImpl).getService();
  142           GenericServiceEndpoint serviceEndpoint = new GenericServiceEndpoint(portQName, service, location);
  143           Callback callback = new ServiceEndpointMethodInterceptor(serviceEndpoint, sortedOperationInfos, credentialsName);
  144           Callback[] callbacks = new Callback[]{NoOp.INSTANCE, callback};
  145           Enhancer.registerCallbacks(serviceEndpointClass, callbacks);
  146           try {
  147               return (Remote) constructor.newInstance(new Object[]{serviceEndpoint});
  148           } catch (InvocationTargetException e) {
  149               throw (ServiceException) new ServiceException("Could not construct service instance", e.getTargetException()).initCause(e);
  150           }
  151       }
  152   
  153       public HandlerChain createHandlerChain() {
  154           return handlerInfoChainFactory.createHandlerChain();
  155       }
  156   
  157   //    private Object readResolve() throws ObjectStreamException {
  158   //            SEIFactoryImpl seiFactory =  new SEIFactoryImpl(serviceName, portQName.getLocalPart(), serviceEndpointClassName, operationInfos, typeInfo, location, handlerInfos, credentialsName);
  159   //            seiFactory.initialize();
  160   //            return seiFactory;
  161   //    }
  162   
  163       public OperationInfo[] getOperationInfos() {
  164           return operationInfos;
  165       }
  166   
  167       public QName getPortQName() {
  168           return portQName;
  169       }
  170   
  171       public QName getServiceName() {
  172           return serviceName;
  173       }
  174   
  175       public URL getWSDLDocumentLocation() {
  176           try {
  177               return new URL(location.toExternalForm() + "?wsdl");
  178           } catch (MalformedURLException e) {
  179               return null;
  180           }
  181       }
  182   }

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