001    /**
002     *
003     * Copyright 2006 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.axis.client;
019    
020    import java.lang.reflect.InvocationTargetException;
021    import java.util.Iterator;
022    import java.util.Map;
023    
024    import javax.naming.NamingException;
025    
026    import net.sf.cglib.proxy.Callback;
027    import net.sf.cglib.proxy.Enhancer;
028    import net.sf.cglib.proxy.MethodInterceptor;
029    import net.sf.cglib.proxy.NoOp;
030    import net.sf.cglib.reflect.FastClass;
031    import net.sf.cglib.reflect.FastConstructor;
032    import org.apache.axis.client.Service;
033    import org.apache.geronimo.naming.reference.ClassLoaderAwareReference;
034    import org.apache.geronimo.naming.reference.SimpleReference;
035    
036    /**
037     * @version $Rev: 430508 $ $Date: 2006-08-10 12:56:47 -0700 (Thu, 10 Aug 2006) $
038     */
039    public class AxisServiceReference extends SimpleReference implements ClassLoaderAwareReference {
040        private static final Class[] SERVICE_CONSTRUCTOR_TYPES = new Class[]{Map.class, Map.class};
041    
042        private String serviceInterfaceClassName;
043        private Map seiPortNameToFactoryMap;
044        private Map seiClassNameToFactoryMap;
045        private ClassLoader classLoader;
046    
047        public AxisServiceReference(String serviceInterfaceClassName, Map seiPortNameToFactoryMap, Map seiClassNameToFactoryMap) {
048            this.serviceInterfaceClassName = serviceInterfaceClassName;
049            this.seiPortNameToFactoryMap = seiPortNameToFactoryMap;
050            this.seiClassNameToFactoryMap = seiClassNameToFactoryMap;
051        }
052    
053        public void setClassLoader(ClassLoader classLoader) {
054            this.classLoader = classLoader;
055        }
056    
057        public Object getContent() throws NamingException {
058            Class serviceInterface;
059            try {
060                serviceInterface = classLoader.loadClass(serviceInterfaceClassName);
061            } catch (ClassNotFoundException e) {
062                throw new NamingException("Could not load service interface class " + serviceInterfaceClassName);
063            }
064            Object serviceInstance = createServiceInterfaceProxy(serviceInterface, seiPortNameToFactoryMap, seiClassNameToFactoryMap, classLoader);
065            for (Iterator iterator = seiPortNameToFactoryMap.values().iterator(); iterator.hasNext();) {
066                SEIFactoryImpl seiFactory = (SEIFactoryImpl) iterator.next();
067                try {
068                    seiFactory.initialize(serviceInstance, classLoader);
069                } catch (ClassNotFoundException e) {
070                    throw new NamingException("Could not load service interface class; " + e.getMessage());
071                }
072            }
073    
074            return serviceInstance;
075        }
076    
077        private Object createServiceInterfaceProxy(Class serviceInterface, Map seiPortNameToFactoryMap, Map seiClassNameToFactoryMap, ClassLoader classLoader) throws NamingException {
078    
079            Callback callback = new ServiceMethodInterceptor(seiPortNameToFactoryMap);
080            Callback[] methodInterceptors = new Callback[]{SerializableNoOp.INSTANCE, callback};
081    
082            Enhancer enhancer = new Enhancer();
083            enhancer.setClassLoader(classLoader);
084            enhancer.setSuperclass(ServiceImpl.class);
085            enhancer.setInterfaces(new Class[]{serviceInterface});
086            enhancer.setCallbackFilter(new NoOverrideCallbackFilter(Service.class));
087            enhancer.setCallbackTypes(new Class[]{NoOp.class, MethodInterceptor.class});
088            enhancer.setUseFactory(false);
089            enhancer.setUseCache(false);
090            Class serviceClass = enhancer.createClass();
091    
092            Enhancer.registerCallbacks(serviceClass, methodInterceptors);
093            FastConstructor constructor = FastClass.create(serviceClass).getConstructor(SERVICE_CONSTRUCTOR_TYPES);
094            try {
095                return constructor.newInstance(new Object[]{seiPortNameToFactoryMap, seiClassNameToFactoryMap});
096            } catch (InvocationTargetException e) {
097                throw (NamingException)new NamingException("Could not construct service instance").initCause(e.getTargetException());
098            }
099        }
100    
101    }