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

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