KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > annotation > handlers > WebServiceProviderHandler


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
24 package com.sun.enterprise.deployment.annotation.handlers;
25
26 import java.util.Set JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 import java.lang.reflect.AnnotatedElement JavaDoc;
30 import java.lang.annotation.Annotation JavaDoc;
31
32 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
33
34 import com.sun.enterprise.deployment.annotation.AnnotationHandler;
35 import com.sun.enterprise.deployment.annotation.AnnotationProcessorException;
36 import com.sun.enterprise.deployment.annotation.AnnotatedElementHandler;
37 import com.sun.enterprise.deployment.annotation.AnnotationInfo;
38 import com.sun.enterprise.deployment.annotation.ProcessingContext;
39 import com.sun.enterprise.deployment.annotation.ResultType;
40 import com.sun.enterprise.deployment.annotation.HandlerProcessingResult;
41 import com.sun.enterprise.deployment.annotation.AnnotationProcessorException;
42
43 import com.sun.enterprise.deployment.annotation.impl.AnnotationUtils;
44 import com.sun.enterprise.deployment.annotation.impl.HandlerProcessingResultImpl;
45
46 import com.sun.enterprise.deployment.annotation.context.AnnotationContext;
47 import com.sun.enterprise.deployment.annotation.context.WebBundleContext;
48 import com.sun.enterprise.deployment.annotation.context.EjbContext;
49
50 import com.sun.enterprise.deployment.WebBundleDescriptor;
51 import com.sun.enterprise.deployment.EjbBundleDescriptor;
52 import com.sun.enterprise.deployment.BundleDescriptor;
53 import com.sun.enterprise.deployment.WebServicesDescriptor;
54 import com.sun.enterprise.deployment.WebService;
55 import com.sun.enterprise.deployment.WebServiceEndpoint;
56 import com.sun.enterprise.deployment.EjbDescriptor;
57 import com.sun.enterprise.deployment.WebComponentDescriptor;
58
59 import javax.xml.namespace.QName JavaDoc;
60
61 /**
62  * This annotation handler is responsible for processing the javax.jws.WebService
63  * annotation type.
64  *
65  * @author Jerome Dochez
66  */

67
68 public class WebServiceProviderHandler implements AnnotationHandler {
69     
70     /** Creates a new instance of WebServiceHandler */
71     public WebServiceProviderHandler() {
72     }
73         
74     public Class JavaDoc<? extends Annotation JavaDoc> getAnnotationType() {
75         return javax.xml.ws.WebServiceProvider.class;
76     }
77
78     /**
79      * @return an array of annotation types this annotation handler would
80      * require to be processed (if present) before it processes it's own
81      * annotation type.
82      */

83     public Class JavaDoc<? extends Annotation JavaDoc>[] getTypeDependencies() {
84         Class JavaDoc dependencies[] = { javax.ejb.Stateless JavaDoc.class };
85         return dependencies;
86     }
87     
88     public HandlerProcessingResult processAnnotation(AnnotationInfo annInfo)
89         throws AnnotationProcessorException
90     {
91         AnnotatedElementHandler annCtx = annInfo.getProcessingContext().getHandler();
92         AnnotatedElement JavaDoc annElem = annInfo.getAnnotatedElement();
93         
94         // sanity check
95
if (!(annElem instanceof Class JavaDoc)) {
96             AnnotationProcessorException ape = new AnnotationProcessorException(
97                     "@WebServiceProvider can only be specified on TYPE", annInfo);
98             annInfo.getProcessingContext().getErrorHandler().error(ape);
99             return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.FAILED);
100         }
101         // WebServiceProvider MUST implement the provider interface, let's check this
102
if (!javax.xml.ws.Provider.class.isAssignableFrom((Class JavaDoc) annElem)) {
103             AnnotationProcessorException ape = new AnnotationProcessorException(
104                     annElem.toString() + "does not implement the javax.xml.ws.Provider interface", annInfo);
105             annInfo.getProcessingContext().getErrorHandler().error(ape);
106             return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.FAILED);
107         }
108     
109         // let's get the main annotation of interest.
110
javax.xml.ws.WebServiceProvider ann = (javax.xml.ws.WebServiceProvider) annInfo.getAnnotation();
111         
112         BundleDescriptor bundleDesc;
113         
114         // let's see the type of web service we are dealing with...
115
if (annElem.getAnnotation(javax.ejb.Stateless JavaDoc.class)!=null) {
116             // this is an ejb !
117
EjbContext ctx = (EjbContext) annCtx;
118             bundleDesc = ctx.getDescriptor().getEjbBundleDescriptor();
119             bundleDesc.setSpecVersion("3.0");
120         } else {
121             // this has to be a servlet since there is no @Servlet annotation yet
122
WebBundleContext ctx = (WebBundleContext)annCtx;
123             bundleDesc = ctx.getDescriptor();
124             bundleDesc.setSpecVersion("2.5");
125         }
126
127         // For WSProvider, portComponentName is the fully qualified class name
128
String JavaDoc portComponentName = ((Class JavaDoc) annElem).getName();
129         
130         // As per JSR181, the serviceName is either specified in the deployment descriptor
131
// or in @WebSErvice annotation in impl class; if neither service name implclass+Service
132
String JavaDoc svcName = ann.serviceName();
133         if(svcName == null) {
134             svcName = "";
135         }
136
137         // Store binding type specified in Impl class
138
String JavaDoc userSpecifiedBinding = null;
139         javax.xml.ws.BindingType bindingAnn = (javax.xml.ws.BindingType)
140                 ((Class JavaDoc)annElem).getAnnotation(javax.xml.ws.BindingType.class);
141         if(bindingAnn != null) {
142             userSpecifiedBinding = bindingAnn.value();
143         }
144         
145         // In case user gives targetNameSpace in the Impl class, that has to be used as
146
// the namespace for service, port; typically user will do this in cases where
147
// port_types reside in a different namespace than that of server/port.
148
// Store the targetNameSpace, if any, in the impl class for later use
149
String JavaDoc targetNameSpace = ann.targetNamespace();
150         if(targetNameSpace == null) {
151             targetNameSpace = "";
152         }
153
154         String JavaDoc portName = ann.portName();
155         if(portName == null) {
156             portName = "";
157         }
158         
159         // Check if the same endpoint is already defined in webservices.xml
160
WebServicesDescriptor wsDesc = bundleDesc.getWebServices();
161         WebServiceEndpoint endpoint = wsDesc.getEndpointByName(portComponentName);
162         WebService newWS;
163         if(endpoint == null) {
164             // Check if a service with the same name is already present
165
// If so, add this endpoint to the existing service
166
if (svcName.length()!=0) {
167                 newWS = wsDesc.getWebServiceByName(svcName);
168             } else {
169                 newWS = wsDesc.getWebServiceByName(((Class JavaDoc)annElem).getSimpleName()+"Service");
170             }
171             if(newWS==null) {
172                 newWS = new WebService();
173                 // service name from annotation
174
if (svcName.length()!=0) {
175                     newWS.setName(svcName);
176                 } else {
177                     newWS.setName(((Class JavaDoc)annElem).getSimpleName()+"Service");
178                 }
179                 wsDesc.addWebService(newWS);
180             }
181             endpoint = new WebServiceEndpoint();
182             // port-component-name is fully qualified class name
183
endpoint.setEndpointName(portComponentName);
184             newWS.addEndpoint(endpoint);
185             wsDesc.setSpecVersion(com.sun.enterprise.deployment.node.WebServicesDescriptorNode.SPEC_VERSION);
186         } else {
187             newWS = endpoint.getWebService();
188         }
189
190         // If wsdl-service is specified in the descriptor, then the targetnamespace
191
// in wsdl-service should match the @WebService.targetNameSpace, if any.
192
// make that assertion here - and the targetnamespace in wsdl-service, if
193
// present overrides everything else
194
if(endpoint.getWsdlService() != null) {
195             if( (targetNameSpace != null) && (targetNameSpace.length() != 0 ) &&
196                 (!endpoint.getWsdlService().getNamespaceURI().equals(targetNameSpace)) ) {
197                 throw new AnnotationProcessorException(
198                         "Target Namespace inwsdl-service element does not match @WebService.targetNamespace",
199                         annInfo);
200             }
201             targetNameSpace = endpoint.getWsdlService().getNamespaceURI();
202         }
203         
204         // Set binding id id @BindingType is specified by the user in the impl class
205
if((!endpoint.hasUserSpecifiedProtocolBinding()) &&
206                     (userSpecifiedBinding != null) &&
207                         (userSpecifiedBinding.length() != 0)){
208             endpoint.setProtocolBinding(userSpecifiedBinding);
209         }
210
211         // Use annotated values only if the deployment descriptor equivalent has not been specified
212
if(newWS.getWsdlFileUri() == null) {
213             // take wsdl location from annotation
214
if (ann.wsdlLocation()!=null && ann.wsdlLocation().length()!=0) {
215                 newWS.setWsdlFileUri(ann.wsdlLocation());
216             }
217         }
218
219         annElem = annInfo.getAnnotatedElement();
220         
221         // we checked that the endpoint implements the provider interface above
222
Class JavaDoc clz = (Class JavaDoc) annElem;
223         Class JavaDoc serviceEndpointIntf = null;
224         for (Class JavaDoc intf : clz.getInterfaces()) {
225             if (javax.xml.ws.Provider.class.isAssignableFrom(intf)) {
226                 serviceEndpointIntf = intf;
227                 break;
228             }
229         }
230         if (serviceEndpointIntf==null) {
231             endpoint.setServiceEndpointInterface("javax.xml.ws.Provider");
232         } else {
233             endpoint.setServiceEndpointInterface(serviceEndpointIntf.getName());
234         }
235
236         if (ModuleType.WAR.equals(bundleDesc.getModuleType())) {
237             if(endpoint.getServletImplClass() == null) {
238                 // Set servlet impl class here
239
endpoint.setServletImplClass(((Class JavaDoc)annElem).getName());
240             }
241
242             // Servlet link name
243
WebBundleDescriptor webBundle = (WebBundleDescriptor) bundleDesc;
244             if(endpoint.getWebComponentLink() == null) {
245                 endpoint.setWebComponentLink(portComponentName);
246             }
247             if(endpoint.getWebComponentImpl() == null) {
248                 WebComponentDescriptor webComponent = (WebComponentDescriptor) webBundle.
249                     getWebComponentByCanonicalName(endpoint.getWebComponentLink());
250
251                 // if servlet is not known, we should add it now
252
if (webComponent == null) {
253                     webComponent = new WebComponentDescriptor();
254                     webComponent.setServlet(true);
255                     webComponent.setWebComponentImplementation(((Class JavaDoc) annElem).getCanonicalName());
256                     webComponent.setName(endpoint.getEndpointName());
257                     webComponent.addUrlPattern("/"+newWS.getName());
258                     webBundle.addWebComponentDescriptor(webComponent);
259                 }
260                 endpoint.setWebComponentImpl(webComponent);
261             }
262         } else {
263             if(endpoint.getEjbLink() == null) {
264                 EjbDescriptor[] ejbDescs = ((EjbBundleDescriptor) bundleDesc).getEjbByClassName(((Class JavaDoc)annElem).getName());
265                 if(ejbDescs.length != 1) {
266                     throw new AnnotationProcessorException(
267                         "Unable to find matching descriptor for EJB endpoint",
268                         annInfo);
269                 }
270                 endpoint.setEjbComponentImpl(ejbDescs[0]);
271                 ejbDescs[0].setWebServiceEndpointInterfaceName(endpoint.getServiceEndpointInterface());
272                 endpoint.setEjbLink(ejbDescs[0].getName());
273             }
274         }
275
276         if(endpoint.getWsdlPort() == null) {
277             endpoint.setWsdlPort(new QName JavaDoc(targetNameSpace, portName, "ns1"));
278         }
279         
280         if(endpoint.getWsdlService() == null) {
281             endpoint.setWsdlService(new QName JavaDoc(targetNameSpace, svcName));
282         }
283                 
284         return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.PROCESSED);
285     }
286 }
287
Popular Tags