001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. 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: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
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 private FastConstructor serviceConstructor;
048 private Callback[] methodInterceptors;
049 private Class enhancedServiceClass;
050
051 public AxisServiceReference(String serviceInterfaceClassName, Map seiPortNameToFactoryMap, Map seiClassNameToFactoryMap) {
052 this.serviceInterfaceClassName = serviceInterfaceClassName;
053 this.seiPortNameToFactoryMap = seiPortNameToFactoryMap;
054 this.seiClassNameToFactoryMap = seiClassNameToFactoryMap;
055 }
056
057 public void setClassLoader(ClassLoader classLoader) {
058 this.classLoader = classLoader;
059 }
060
061 public Object getContent() throws NamingException {
062 Object serviceInstance = createServiceInterfaceProxy(serviceInterfaceClassName, seiPortNameToFactoryMap, seiClassNameToFactoryMap, classLoader);
063 return serviceInstance;
064 }
065
066 private Object createServiceInterfaceProxy(String serviceInterfaceClassName, Map seiPortNameToFactoryMap, Map seiClassNameToFactoryMap, ClassLoader classLoader) throws NamingException {
067 boolean initialize = (this.serviceConstructor == null);
068
069 if (initialize) {
070 Class serviceInterface;
071 try {
072 serviceInterface = classLoader.loadClass(serviceInterfaceClassName);
073 } catch (ClassNotFoundException e) {
074 throw (NamingException)new NamingException("Could not load service interface class " + serviceInterfaceClassName).initCause(e);
075 }
076
077 // create method interceptors
078 Callback callback = new ServiceMethodInterceptor(seiPortNameToFactoryMap);
079 this.methodInterceptors = new Callback[]{SerializableNoOp.INSTANCE, callback};
080
081 // create service class
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 this.enhancedServiceClass = enhancer.createClass();
091
092 // get constructor
093 this.serviceConstructor =
094 FastClass.create(this.enhancedServiceClass).getConstructor(SERVICE_CONSTRUCTOR_TYPES);
095 }
096
097 // associate the method interceptors with the generated service class on the current thread
098 Enhancer.registerCallbacks(this.enhancedServiceClass, this.methodInterceptors);
099
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 }