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   
   18   package org.apache.openejb.server.axis.client;
   19   
   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.NoOp;
   24   import net.sf.cglib.reflect.FastClass;
   25   import net.sf.cglib.reflect.FastConstructor;
   26   import org.apache.axis.client.Service;
   27   import org.apache.openejb.core.ivm.naming.Reference;
   28   
   29   import javax.naming.NamingException;
   30   import java.lang.reflect.InvocationTargetException;
   31   import java.util.Iterator;
   32   import java.util.Map;
   33   
   34   public class AxisServiceReference extends Reference {
   35       private static final Class[] SERVICE_CONSTRUCTOR_TYPES = new Class[]{Map.class, Map.class};
   36   
   37       private String serviceInterfaceClassName;
   38       private Map seiPortNameToFactoryMap;
   39       private Map seiClassNameToFactoryMap;
   40       private ClassLoader classLoader;
   41   
   42       private FastConstructor serviceConstructor;
   43       private Callback[] methodInterceptors;
   44       private Class enhancedServiceClass;
   45   
   46       public AxisServiceReference(String serviceInterfaceClassName, Map seiPortNameToFactoryMap, Map seiClassNameToFactoryMap) {
   47           this.serviceInterfaceClassName = serviceInterfaceClassName;
   48           this.seiPortNameToFactoryMap = seiPortNameToFactoryMap;
   49           this.seiClassNameToFactoryMap = seiClassNameToFactoryMap;
   50       }
   51   
   52       public void setClassLoader(ClassLoader classLoader) {
   53           this.classLoader = classLoader;
   54       }
   55   
   56       public Object getObject() throws NamingException {
   57           Object serviceInstance = createServiceInterfaceProxy(serviceInterfaceClassName, seiPortNameToFactoryMap, seiClassNameToFactoryMap, classLoader);
   58           return serviceInstance;
   59       }
   60   
   61       private Object createServiceInterfaceProxy(String serviceInterfaceClassName, Map seiPortNameToFactoryMap, Map seiClassNameToFactoryMap, ClassLoader classLoader) throws NamingException {
   62           boolean initialize = (this.serviceConstructor == null);
   63   
   64           if (initialize) {
   65               Class serviceInterface;
   66               try {
   67                   serviceInterface = classLoader.loadClass(serviceInterfaceClassName);
   68               } catch (ClassNotFoundException e) {
   69                   throw (NamingException) new NamingException("Could not load service interface class " + serviceInterfaceClassName).initCause(e);
   70               }
   71   
   72               // create method interceptors
   73               Callback callback = new ServiceMethodInterceptor(seiPortNameToFactoryMap);
   74               this.methodInterceptors = new Callback[]{NoOp.INSTANCE, callback};
   75   
   76               // create service class
   77               Enhancer enhancer = new Enhancer();
   78               enhancer.setClassLoader(classLoader);
   79               enhancer.setSuperclass(ServiceImpl.class);
   80               enhancer.setInterfaces(new Class[]{serviceInterface});
   81               enhancer.setCallbackFilter(new NoOverrideCallbackFilter(Service.class));
   82               enhancer.setCallbackTypes(new Class[]{NoOp.class, MethodInterceptor.class});
   83               enhancer.setUseFactory(false);
   84               enhancer.setUseCache(false);
   85               this.enhancedServiceClass = enhancer.createClass();
   86   
   87               // get constructor
   88               this.serviceConstructor = FastClass.create(this.enhancedServiceClass).getConstructor(SERVICE_CONSTRUCTOR_TYPES);
   89           }
   90   
   91           // associate the method interceptors with the generated service class on the current thread
   92           Enhancer.registerCallbacks(this.enhancedServiceClass, this.methodInterceptors);
   93   
   94           Object[] arguments = new Object[]{seiPortNameToFactoryMap, seiClassNameToFactoryMap};
   95   
   96           Object serviceInstance = null;
   97   
   98           try {
   99               serviceInstance = this.serviceConstructor.newInstance(arguments);
  100           } catch (InvocationTargetException e) {
  101               throw (NamingException) new NamingException("Could not construct service instance").initCause(e.getTargetException());
  102           }
  103   
  104           if (initialize) {
  105               for (Iterator iterator = seiPortNameToFactoryMap.values().iterator(); iterator.hasNext();) {
  106                   SeiFactoryImpl seiFactory = (SeiFactoryImpl) iterator.next();
  107                   try {
  108                       seiFactory.initialize(serviceInstance, classLoader);
  109                   } catch (ClassNotFoundException e) {
  110                       throw (NamingException) new NamingException("Could not load service interface class; " + e.getMessage()).initCause(e);
  111                   }
  112               }
  113           }
  114   
  115           return serviceInstance;
  116       }
  117   }

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