KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.geronimo.axis.client;
18
19 import java.io.Serializable JavaDoc;
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21 import java.math.BigInteger JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.rmi.Remote JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import javax.xml.namespace.QName JavaDoc;
30 import javax.xml.rpc.ServiceException JavaDoc;
31 import javax.xml.rpc.handler.HandlerChain JavaDoc;
32
33 import net.sf.cglib.core.Signature;
34 import net.sf.cglib.proxy.Callback;
35 import net.sf.cglib.proxy.Enhancer;
36 import net.sf.cglib.proxy.MethodInterceptor;
37 import net.sf.cglib.proxy.MethodProxy;
38 import net.sf.cglib.proxy.NoOp;
39 import net.sf.cglib.reflect.FastClass;
40 import net.sf.cglib.reflect.FastConstructor;
41 import org.apache.axis.AxisEngine;
42 import org.apache.axis.Constants;
43 import org.apache.axis.client.Service;
44 import org.apache.axis.constants.Use;
45 import org.apache.axis.description.TypeDesc;
46 import org.apache.axis.encoding.TypeMapping;
47 import org.apache.axis.encoding.TypeMappingRegistry;
48 import org.apache.axis.encoding.ser.SimpleDeserializerFactory;
49 import org.apache.axis.encoding.ser.SimpleSerializerFactory;
50 import org.apache.axis.handlers.HandlerInfoChainFactory;
51
52 /**
53  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
54  */

55 public class SEIFactoryImpl implements SEIFactory, Serializable JavaDoc {
56
57     private final QName JavaDoc serviceName;
58     private final QName JavaDoc portQName;
59     private final String JavaDoc serviceEndpointClassName;
60     private final OperationInfo[] operationInfos;
61     private transient FastConstructor constructor;
62     private Object JavaDoc serviceImpl;
63     private final List JavaDoc typeInfo;
64     private final URL JavaDoc location;
65     private final List JavaDoc handlerInfos;
66     private final String JavaDoc credentialsName;
67     private transient HandlerInfoChainFactory handlerInfoChainFactory;
68     private transient OperationInfo[] sortedOperationInfos;
69     private Class JavaDoc serviceEndpointClass;
70
71     public SEIFactoryImpl(QName JavaDoc serviceName, String JavaDoc portName, String JavaDoc serviceEndpointClassName, OperationInfo[] operationInfos, List JavaDoc typeInfo, URL JavaDoc location, List JavaDoc handlerInfos, String JavaDoc credentialsName) {
72         this.serviceName = serviceName;
73         this.portQName = new QName JavaDoc("", portName);
74         this.serviceEndpointClassName = serviceEndpointClassName;
75         this.operationInfos = operationInfos;
76         this.typeInfo = typeInfo;
77         this.location = location;
78         this.handlerInfos = handlerInfos;
79         this.credentialsName = credentialsName;
80     }
81
82     void initialize(Object JavaDoc serviceImpl, ClassLoader JavaDoc classLoader) throws ClassNotFoundException JavaDoc {
83         this.serviceImpl = serviceImpl;
84         Class JavaDoc serviceEndpointBaseClass = classLoader.loadClass(serviceEndpointClassName);
85         serviceEndpointClass = enhanceServiceEndpointInterface(serviceEndpointBaseClass, classLoader);
86         Class JavaDoc[] constructorTypes = new Class JavaDoc[]{classLoader.loadClass(GenericServiceEndpoint.class.getName())};
87         this.constructor = FastClass.create(serviceEndpointClass).getConstructor(constructorTypes);
88         this.handlerInfoChainFactory = new HandlerInfoChainFactory(handlerInfos);
89         sortedOperationInfos = new OperationInfo[FastClass.create(serviceEndpointClass).getMaxIndex() + 1];
90         String JavaDoc encodingStyle = "";
91         for (int i = 0; i < operationInfos.length; i++) {
92             OperationInfo operationInfo = operationInfos[i];
93             Signature signature = operationInfo.getSignature();
94             MethodProxy methodProxy = MethodProxy.find(serviceEndpointClass, signature);
95             if (methodProxy == null) {
96                 throw new RuntimeException JavaDoc("No method proxy for operationInfo " + signature);
97             }
98             int index = methodProxy.getSuperIndex();
99             sortedOperationInfos[index] = operationInfo;
100             if (operationInfo.getOperationDesc().getUse() == Use.ENCODED) {
101                 encodingStyle = org.apache.axis.Constants.URI_SOAP11_ENC;
102             }
103         }
104         //register our type descriptors
105
Service service = ((ServiceImpl) serviceImpl).getService();
106         AxisEngine axisEngine = service.getEngine();
107         TypeMappingRegistry typeMappingRegistry = axisEngine.getTypeMappingRegistry();
108         TypeMapping typeMapping = typeMappingRegistry.getOrMakeTypeMapping(encodingStyle);
109         typeMapping.register(BigInteger JavaDoc.class,
110                 Constants.XSD_UNSIGNEDLONG,
111                 new SimpleSerializerFactory(BigInteger JavaDoc.class, Constants.XSD_UNSIGNEDLONG),
112                 new SimpleDeserializerFactory(BigInteger JavaDoc.class, Constants.XSD_UNSIGNEDLONG));
113         typeMapping.register(URI JavaDoc.class,
114                 Constants.XSD_ANYURI,
115                 new SimpleSerializerFactory(URI JavaDoc.class, Constants.XSD_ANYURI),
116                 new SimpleDeserializerFactory(URI JavaDoc.class, Constants.XSD_ANYURI));
117         //It is essential that the types be registered before the typeInfos create the serializer/deserializers.
118
for (Iterator JavaDoc iter = typeInfo.iterator(); iter.hasNext();) {
119             TypeInfo info = (TypeInfo) iter.next();
120             TypeDesc.registerTypeDescForClass(info.getClazz(), info.buildTypeDesc());
121         }
122         TypeInfo.register(typeInfo, typeMapping);
123     }
124
125     private Class JavaDoc enhanceServiceEndpointInterface(Class JavaDoc serviceEndpointInterface, ClassLoader JavaDoc classLoader) {
126         Enhancer enhancer = new Enhancer();
127         enhancer.setClassLoader(classLoader);
128         enhancer.setSuperclass(GenericServiceEndpointWrapper.class);
129         enhancer.setInterfaces(new Class JavaDoc[]{serviceEndpointInterface});
130         enhancer.setCallbackFilter(new NoOverrideCallbackFilter(GenericServiceEndpointWrapper.class));
131         enhancer.setCallbackTypes(new Class JavaDoc[]{NoOp.class, MethodInterceptor.class});
132         enhancer.setUseFactory(false);
133         enhancer.setUseCache(false);
134
135         return enhancer.createClass();
136     }
137     public Remote JavaDoc createServiceEndpoint() throws ServiceException JavaDoc {
138         //TODO figure out why this can't be called in readResolve!
139
// synchronized (this) {
140
// if (!initialized) {
141
// initialize();
142
// initialized = true;
143
// }
144
// }
145
Service service = ((ServiceImpl) serviceImpl).getService();
146         GenericServiceEndpoint serviceEndpoint = new GenericServiceEndpoint(portQName, service, location);
147         Callback callback = new ServiceEndpointMethodInterceptor(serviceEndpoint, sortedOperationInfos, credentialsName);
148         Callback[] callbacks = new Callback[]{SerializableNoOp.INSTANCE, callback};
149         Enhancer.registerCallbacks(serviceEndpointClass, callbacks);
150         try {
151             return (Remote JavaDoc) constructor.newInstance(new Object JavaDoc[]{serviceEndpoint});
152         } catch (InvocationTargetException JavaDoc e) {
153             e.getTargetException().printStackTrace();
154             throw new ServiceException JavaDoc("Could not construct service instance", e.getTargetException());
155         }
156     }
157
158     public HandlerChain JavaDoc createHandlerChain() {
159         return handlerInfoChainFactory.createHandlerChain();
160     }
161
162 // private Object readResolve() throws ObjectStreamException {
163
// SEIFactoryImpl seiFactory = new SEIFactoryImpl(serviceName, portQName.getLocalPart(), serviceEndpointClassName, operationInfos, typeInfo, location, handlerInfos, credentialsName);
164
// seiFactory.initialize();
165
// return seiFactory;
166
// }
167

168     public OperationInfo[] getOperationInfos() {
169         return operationInfos;
170     }
171
172     public QName JavaDoc getPortQName() {
173         return portQName;
174     }
175
176     public QName JavaDoc getServiceName() {
177         return serviceName;
178     }
179
180     public URL JavaDoc getWSDLDocumentLocation() {
181         try {
182             return new URL JavaDoc(location.toExternalForm() + "?wsdl");
183         } catch (MalformedURLException JavaDoc e) {
184             return null;
185         }
186     }
187 }
188
Popular Tags