KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > webservice > JAXWSServiceDelegate


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.webservice;
24
25 import java.lang.UnsupportedOperationException JavaDoc;
26
27 import java.net.URL JavaDoc;
28 import java.net.URI JavaDoc;
29
30 import java.util.HashSet JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.concurrent.Executor JavaDoc;
37
38 import java.lang.reflect.Method JavaDoc;
39
40 import javax.xml.namespace.QName JavaDoc;
41 import javax.xml.bind.JAXBContext;
42
43 import javax.xml.ws.Service;
44 import javax.xml.ws.Dispatch;
45 import javax.xml.ws.handler.HandlerResolver;
46
47 import com.sun.enterprise.deployment.ServiceReferenceDescriptor;
48 import com.sun.enterprise.deployment.ServiceRefPortInfo;
49
50 /**
51  * Used as a delegate to concrete JAXWS
52  * Service implementation.
53  */

54 public class JAXWSServiceDelegate extends Service {
55
56     private ServiceReferenceDescriptor serviceRef;
57
58     // real service instance
59
private Service serviceDelegate;
60
61     private ClassLoader JavaDoc classLoader;
62
63     // location of full wsdl associated with service-ref
64
private URL JavaDoc wsdlLocation;
65
66     private boolean fullWsdl = false;
67     private boolean noWsdl = false;
68
69     // Service method types
70
private static final int ADD_PORT = 1;
71     private static final int CREATE_DISPATCH_CLASS = 2;
72     private static final int CREATE_DISPATCH_CONTEXT = 3;
73     private static final int GET_EXECUTOR = 4;
74     private static final int SET_EXECUTOR = 5;
75     private static final int GET_HANDLER_RESOLVER = 6;
76     private static final int GET_PORT_CONTAINER_MANAGED = 7;
77     private static final int GET_PORT_CLIENT_MANAGED = 8;
78     private static final int GET_PORTS = 9;
79     private static final int GET_SERVICE_NAME = 10;
80     private static final int SET_HANDLER_RESOLVER = 11;
81     private static final int GET_WSDL_LOCATION = 12;
82     private static final int GENERATED_SERVICE_METHOD = 13;
83
84     private static Map JavaDoc serviceMethodTypes;
85     private static Set JavaDoc fullWsdlIllegalMethods;
86     private static Set JavaDoc noWsdlIllegalMethods;
87
88     static {
89         Init();
90     }
91
92     public JAXWSServiceDelegate(ServiceReferenceDescriptor descriptor,
93                     Service delegate, ClassLoader JavaDoc loader) throws Exception JavaDoc {
94         super((new WsUtil()).privilegedGetServiceRefWsdl(descriptor),
95                 descriptor.getServiceName());
96         serviceRef = descriptor;
97         serviceDelegate = delegate;
98         classLoader = loader;
99         if( serviceRef.hasWsdlFile() ) {
100             wsdlLocation = (new WsUtil()).privilegedGetServiceRefWsdl(serviceRef);
101             fullWsdl = true;
102         } else {
103             noWsdl = true;
104         }
105     }
106
107     public void addPort(QName JavaDoc q, String JavaDoc id, String JavaDoc addr) {
108         checkUnsupportedMethods(ADD_PORT);
109         serviceDelegate.addPort(q, id, addr);
110         return;
111     }
112     
113     // TODO : To be implemented
114
public <T> Dispatch<T> createDispatch(QName JavaDoc qName, Class JavaDoc<T> aClass, Service.Mode mode) {
115         checkUnsupportedMethods(CREATE_DISPATCH_CLASS);
116         return null;
117     }
118                                                                                 
119     // TODO : To be implemented
120
public Dispatch<Object JavaDoc> createDispatch(QName JavaDoc qName, JAXBContext jaxbContext, Service.Mode mode) {
121         checkUnsupportedMethods(CREATE_DISPATCH_CONTEXT);
122         return null;
123     }
124
125     public Executor JavaDoc getExecutor() {
126         checkUnsupportedMethods(GET_EXECUTOR);
127         return serviceDelegate.getExecutor();
128     }
129     
130     public void setExecutor(Executor JavaDoc obj) {
131         checkUnsupportedMethods(SET_EXECUTOR);
132         serviceDelegate.setExecutor(obj);
133         return;
134     }
135     
136     public HandlerResolver getHandlerResolver() {
137         checkUnsupportedMethods(GET_HANDLER_RESOLVER);
138         return serviceDelegate.getHandlerResolver();
139     }
140        
141     public Object JavaDoc getPort(QName JavaDoc q, Class JavaDoc sei) {
142         checkUnsupportedMethods(GET_PORT_CLIENT_MANAGED);
143         return serviceDelegate.getPort(q, sei);
144     }
145     
146     public Object JavaDoc getPort(Class JavaDoc sei) {
147         checkUnsupportedMethods(GET_PORT_CONTAINER_MANAGED);
148         String JavaDoc serviceEndpointInterface = sei.getName();
149         ServiceRefPortInfo portInfo =
150                 serviceRef.getPortInfo(serviceEndpointInterface);
151         Object JavaDoc retVal;
152         if( (portInfo != null) && portInfo.hasWsdlPort() ) {
153             retVal = getPort(portInfo.getWsdlPort(), sei);
154         } else {
155             retVal = serviceDelegate.getPort(sei);
156         }
157         return retVal;
158     }
159     
160     public Iterator JavaDoc getPorts() {
161         checkUnsupportedMethods(GET_PORTS);
162         return serviceDelegate.getPorts();
163     }
164     
165     public QName JavaDoc getServiceName() {
166         checkUnsupportedMethods(GET_SERVICE_NAME);
167         return serviceRef.getServiceName();
168     }
169     
170     public void setHandlerResolver(HandlerResolver resolver) {
171         checkUnsupportedMethods(SET_HANDLER_RESOLVER);
172         serviceDelegate.setHandlerResolver(resolver);
173         return;
174     }
175     
176     public URL JavaDoc getWSDLDocumentLocation() {
177         checkUnsupportedMethods(SET_HANDLER_RESOLVER);
178         return wsdlLocation;
179     }
180
181     /**
182      * Convert invocation method to a constant for easier processing.
183      */

184     private static void Init() {
185
186         serviceMethodTypes = new HashMap JavaDoc();
187         fullWsdlIllegalMethods = new HashSet JavaDoc();
188         noWsdlIllegalMethods = new HashSet JavaDoc();
189
190         try {
191
192             Class JavaDoc noParams[] = new Class JavaDoc[0];
193             Class JavaDoc serviceClass = javax.xml.ws.Service.class;
194
195             //
196
// Map Service method to method type.
197
//
198

199             Method JavaDoc addPort = serviceClass.getDeclaredMethod
200                 ("addPort", new Class JavaDoc[] {QName JavaDoc.class, URI JavaDoc.class, String JavaDoc.class});
201             serviceMethodTypes.put(addPort,
202                                    new Integer JavaDoc(ADD_PORT));
203
204             Method JavaDoc createDispatchClass = serviceClass.getDeclaredMethod
205                 ("createDispatch", new Class JavaDoc[] {QName JavaDoc.class, Class JavaDoc.class, Service.Mode.class});
206             serviceMethodTypes.put(createDispatchClass,
207                                    new Integer JavaDoc(CREATE_DISPATCH_CLASS));
208
209             Method JavaDoc createDispatchContext = serviceClass.getDeclaredMethod
210                 ("createDispatch", new Class JavaDoc[] {QName JavaDoc.class, JAXBContext.class, Service.Mode.class});
211             serviceMethodTypes.put(createDispatchContext,
212                                    new Integer JavaDoc(CREATE_DISPATCH_CONTEXT));
213
214             Method JavaDoc getExecutor = serviceClass.getDeclaredMethod
215                 ("getExecutor", noParams);
216             serviceMethodTypes.put(getExecutor,
217                                    new Integer JavaDoc(GET_EXECUTOR));
218
219             Method JavaDoc setExecutor = serviceClass.getDeclaredMethod
220                 ("setExecutor", new Class JavaDoc[] {Executor JavaDoc.class});
221             serviceMethodTypes.put(setExecutor,
222                                    new Integer JavaDoc(SET_EXECUTOR));
223
224             Method JavaDoc getHandlerResolver = serviceClass.getDeclaredMethod
225                 ("getHandlerResolver", noParams);
226             serviceMethodTypes.put(getHandlerResolver,
227                                    new Integer JavaDoc(GET_HANDLER_RESOLVER));
228
229             Method JavaDoc getPortContainerManaged = serviceClass.getDeclaredMethod
230                 ("getPort", new Class JavaDoc[] { Class JavaDoc.class });
231             serviceMethodTypes.put(getPortContainerManaged,
232                                    new Integer JavaDoc(GET_PORT_CONTAINER_MANAGED));
233
234             Method JavaDoc getPortClientManaged = serviceClass.getDeclaredMethod
235                 ("getPort", new Class JavaDoc[] { QName JavaDoc.class, Class JavaDoc.class });
236             serviceMethodTypes.put(getPortClientManaged,
237                                    new Integer JavaDoc(GET_PORT_CLIENT_MANAGED));
238             
239             Method JavaDoc getPorts = serviceClass.getDeclaredMethod
240                 ("getPorts", noParams);
241             serviceMethodTypes.put(getPorts, new Integer JavaDoc(GET_PORTS));
242
243             Method JavaDoc getServiceName = serviceClass.getDeclaredMethod
244                 ("getServiceName", noParams);
245             serviceMethodTypes.put(getServiceName,
246                                    new Integer JavaDoc(GET_SERVICE_NAME));
247
248             Method JavaDoc setHandlerResolver = serviceClass.getDeclaredMethod
249                 ("setHandlerResolver", new Class JavaDoc[] {HandlerResolver.class});
250             serviceMethodTypes.put(setHandlerResolver,
251                                    new Integer JavaDoc(SET_HANDLER_RESOLVER));
252
253             Method JavaDoc getWsdlLocation = serviceClass.getDeclaredMethod
254                 ("getWSDLDocumentLocation", noParams);
255             serviceMethodTypes.put(getWsdlLocation,
256                                    new Integer JavaDoc(GET_WSDL_LOCATION));
257         } catch(NoSuchMethodException JavaDoc nsme) {}
258
259         noWsdlIllegalMethods.add(new Integer JavaDoc(GET_PORT_CONTAINER_MANAGED));
260         noWsdlIllegalMethods.add(new Integer JavaDoc(GET_PORT_CLIENT_MANAGED));
261         noWsdlIllegalMethods.add(new Integer JavaDoc(GET_PORTS));
262         noWsdlIllegalMethods.add(new Integer JavaDoc(GET_SERVICE_NAME));
263         noWsdlIllegalMethods.add(new Integer JavaDoc(GET_WSDL_LOCATION));
264
265         // This case shouldn't happen since if service-ref has generated
266
// service and no WSDL it won't get past deployment, but it's here
267
// for completeness.
268
noWsdlIllegalMethods.add(new Integer JavaDoc(GENERATED_SERVICE_METHOD));
269     }
270
271     private void checkUnsupportedMethods(int methodType)
272         throws UnsupportedOperationException JavaDoc {
273
274         Set JavaDoc illegalMethods = fullWsdl ?
275             fullWsdlIllegalMethods : noWsdlIllegalMethods;
276
277         if( illegalMethods.contains(new Integer JavaDoc(methodType)) ) {
278             throw new UnsupportedOperationException JavaDoc();
279         }
280
281         return;
282     }
283 }
284
Popular Tags