KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > axis > client > AxisServiceReference


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 JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.naming.NamingException JavaDoc;
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: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
38  */

39 public class AxisServiceReference extends SimpleReference implements ClassLoaderAwareReference {
40     private static final Class JavaDoc[] SERVICE_CONSTRUCTOR_TYPES = new Class JavaDoc[]{Map JavaDoc.class, Map JavaDoc.class};
41
42     private String JavaDoc serviceInterfaceClassName;
43     private Map JavaDoc seiPortNameToFactoryMap;
44     private Map JavaDoc seiClassNameToFactoryMap;
45     private ClassLoader JavaDoc classLoader;
46
47     public AxisServiceReference(String JavaDoc serviceInterfaceClassName, Map JavaDoc seiPortNameToFactoryMap, Map JavaDoc seiClassNameToFactoryMap) {
48         this.serviceInterfaceClassName = serviceInterfaceClassName;
49         this.seiPortNameToFactoryMap = seiPortNameToFactoryMap;
50         this.seiClassNameToFactoryMap = seiClassNameToFactoryMap;
51     }
52
53     public void setClassLoader(ClassLoader JavaDoc classLoader) {
54         this.classLoader = classLoader;
55     }
56
57     public Object JavaDoc getContent() throws NamingException JavaDoc {
58         Class JavaDoc serviceInterface;
59         try {
60             serviceInterface = classLoader.loadClass(serviceInterfaceClassName);
61         } catch (ClassNotFoundException JavaDoc e) {
62             throw new NamingException JavaDoc("Could not load service interface class " + serviceInterfaceClassName);
63         }
64         Object JavaDoc serviceInstance = createServiceInterfaceProxy(serviceInterface, seiPortNameToFactoryMap, seiClassNameToFactoryMap, classLoader);
65         for (Iterator JavaDoc iterator = seiPortNameToFactoryMap.values().iterator(); iterator.hasNext();) {
66             SEIFactoryImpl seiFactory = (SEIFactoryImpl) iterator.next();
67             try {
68                 seiFactory.initialize(serviceInstance, classLoader);
69             } catch (ClassNotFoundException JavaDoc e) {
70                 throw new NamingException JavaDoc("Could not load service interface class; " + e.getMessage());
71             }
72         }
73
74         return serviceInstance;
75     }
76
77     private Object JavaDoc createServiceInterfaceProxy(Class JavaDoc serviceInterface, Map JavaDoc seiPortNameToFactoryMap, Map JavaDoc seiClassNameToFactoryMap, ClassLoader JavaDoc classLoader) throws NamingException JavaDoc {
78
79         Callback callback = new ServiceMethodInterceptor(seiPortNameToFactoryMap);
80         Callback[] methodInterceptors = new Callback[]{SerializableNoOp.INSTANCE, callback};
81
82         Enhancer enhancer = new Enhancer();
83         enhancer.setClassLoader(classLoader);
84         enhancer.setSuperclass(ServiceImpl.class);
85         enhancer.setInterfaces(new Class JavaDoc[]{serviceInterface});
86         enhancer.setCallbackFilter(new NoOverrideCallbackFilter(Service.class));
87         enhancer.setCallbackTypes(new Class JavaDoc[]{NoOp.class, MethodInterceptor.class});
88         enhancer.setUseFactory(false);
89         enhancer.setUseCache(false);
90         Class JavaDoc serviceClass = enhancer.createClass();
91
92         Enhancer.registerCallbacks(serviceClass, methodInterceptors);
93         FastConstructor constructor = FastClass.create(serviceClass).getConstructor(SERVICE_CONSTRUCTOR_TYPES);
94         try {
95             return constructor.newInstance(new Object JavaDoc[]{seiPortNameToFactoryMap, seiClassNameToFactoryMap});
96         } catch (InvocationTargetException JavaDoc e) {
97             throw (NamingException JavaDoc)new NamingException JavaDoc("Could not construct service instance").initCause(e.getTargetException());
98         }
99     }
100
101 }
102
Popular Tags