KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.xml.ws.WebServiceRef;
27
28 import java.lang.reflect.AnnotatedElement JavaDoc;
29 import java.lang.annotation.Annotation JavaDoc;
30 import java.lang.annotation.ElementType JavaDoc;
31 import java.lang.reflect.Field JavaDoc;
32 import java.lang.reflect.Method JavaDoc;
33 import java.lang.reflect.Modifier JavaDoc;
34
35 import java.util.Iterator JavaDoc;
36
37 import javax.xml.ws.*;
38
39 import com.sun.enterprise.deployment.annotation.AnnotationHandler;
40 import com.sun.enterprise.deployment.annotation.AnnotatedElementHandler;
41 import com.sun.enterprise.deployment.annotation.AnnotationInfo;
42 import com.sun.enterprise.deployment.annotation.ProcessingContext;
43 import com.sun.enterprise.deployment.annotation.ResultType;
44 import com.sun.enterprise.deployment.annotation.HandlerProcessingResult;
45 import com.sun.enterprise.deployment.annotation.AnnotationProcessorException;
46 import com.sun.enterprise.deployment.annotation.impl.HandlerProcessingResultImpl;
47
48 import com.sun.enterprise.deployment.annotation.context.AppClientContext;
49 import com.sun.enterprise.deployment.annotation.context.WebBundleContext;
50 import com.sun.enterprise.deployment.annotation.context.EjbContext;
51 import com.sun.enterprise.deployment.annotation.context.EjbBundleContext;
52 import com.sun.enterprise.deployment.annotation.context.ServiceReferenceContainerContext;
53 import com.sun.enterprise.deployment.EjbBundleDescriptor;
54 import com.sun.enterprise.deployment.EjbDescriptor;
55 import com.sun.enterprise.deployment.WebComponentDescriptor;
56 import com.sun.enterprise.deployment.ServiceReferenceDescriptor;
57 import com.sun.enterprise.deployment.ServiceRefPortInfo;
58 import com.sun.enterprise.deployment.InjectionTarget;
59 import com.sun.enterprise.deployment.types.ServiceReferenceContainer;
60
61 /**
62  * This annotation handler is responsible for processing the javax.jws.WebServiceRef annotation type.
63  *
64  * @author Jerome Dochez
65  */

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

81     public Class JavaDoc<? extends Annotation JavaDoc>[] getTypeDependencies() {
82         // it is easier if we return the array of component type. That
83
// way, the @WebServiceRef is processed after the component
84
// has been added to the DOL and the right EjbContext is
85
// on the context stack. It won't hurt when @WebServiceRef
86
// is used in appclients or web app since references are
87
// declared at the bundle level.
88
Class JavaDoc<? extends Annotation JavaDoc>[] dependencies = new Class JavaDoc[3];
89         dependencies[0] = javax.ejb.Stateless JavaDoc.class;
90         dependencies[1] = javax.ejb.Stateful JavaDoc.class;
91         dependencies[2] = javax.persistence.Entity.class;
92         return dependencies;
93     }
94
95     protected HandlerProcessingResult processAWsRef(AnnotationInfo annInfo, WebServiceRef annotation)
96                 throws AnnotationProcessorException {
97         AnnotatedElementHandler annCtx = annInfo.getProcessingContext().getHandler();
98         AnnotatedElement JavaDoc annElem = annInfo.getAnnotatedElement();
99         
100         Class JavaDoc annotatedType = null;
101         Class JavaDoc declaringClass = null;
102         String JavaDoc serviceRefName = annotation.name();
103         if (annInfo.getElementType().equals(ElementType.FIELD)) {
104             // this is a field injection
105
Field JavaDoc annotatedField = (Field JavaDoc) annElem;
106             
107             // check this is a valid field
108
if (annCtx instanceof AppClientContext){
109                 if (!Modifier.isStatic(annotatedField.getModifiers())){
110                     throw new AnnotationProcessorException(
111                             localStrings.getLocalString(
112                             "enterprise.deployment.annotation.handlers.injectionfieldnotstatic",
113                             "Injection fields for application clients must be declared STATIC"),
114                             annInfo);
115                 }
116             }
117             
118             annotatedType = annotatedField.getType();
119             declaringClass = annotatedField.getDeclaringClass();
120             // applying with default
121
if (serviceRefName.equals("")) {
122                 serviceRefName = declaringClass.getName()
123                 + "/" + annotatedField.getName();
124             }
125         } else if (annInfo.getElementType().equals(ElementType.METHOD)) {
126             
127             // this is a method injection
128
Method JavaDoc annotatedMethod = (Method JavaDoc) annElem;
129             validateInjectionMethod(annotatedMethod, annInfo);
130             
131             if (annCtx instanceof AppClientContext){
132                 if (!Modifier.isStatic(annotatedMethod.getModifiers())){
133                     throw new AnnotationProcessorException(
134                             localStrings.getLocalString(
135                             "enterprise.deployment.annotation.handlers.injectionmethodnotstatic",
136                             "Injection methods for application clients must be declared STATIC"),
137                             annInfo);
138                 }
139             }
140             
141             annotatedType = annotatedMethod.getParameterTypes()[0];
142             declaringClass = annotatedMethod.getDeclaringClass();
143             if (serviceRefName == null || serviceRefName.equals("")) {
144                 // Derive javabean property name.
145
String JavaDoc propertyName =
146                     getInjectionMethodPropertyName(annotatedMethod, annInfo);
147                 // prefixing with fully qualified type name
148
serviceRefName = declaringClass.getName()
149                     + "/" + propertyName;
150             }
151         } else if (annInfo.getElementType().equals(ElementType.TYPE))
152         {
153             // name must be specified.
154
if (serviceRefName==null || serviceRefName.length()==0) {
155                 throw new AnnotationProcessorException(
156                         localStrings.getLocalString(
157                         "enterprise.deployment.annotation.handlers.nonametypelevel",
158                         "TYPE-Level annotation must specify name member."), annInfo);
159             }
160             // this is a dependency declaration, we need the service interface
161
// to be specified
162
annotatedType = annotation.type();
163             if (annotatedType==null || annotatedType==Object JavaDoc.class ) {
164                 throw new AnnotationProcessorException(
165                         localStrings.getLocalString(
166                         "enterprise.deployment.annotation.handlers.typenotfound",
167                         "TYPE-level annotation symbol must specify type member."),
168                          annInfo);
169             }
170             declaringClass = (Class JavaDoc) annElem;
171         } else {
172                 throw new AnnotationProcessorException(
173                         localStrings.getLocalString(
174                         "enterprise.deployment.annotation.handlers.invalidtype",
175                         "annotation not allowed on this element."), annInfo);
176             
177         }
178         
179         ServiceReferenceContainer[] containers = null;
180         ServiceReferenceDescriptor aRef =null;
181         if (annCtx instanceof ServiceReferenceContainerContext) {
182             containers = ((ServiceReferenceContainerContext) annCtx).getServiceRefContainers();
183         }
184
185         if (containers==null || containers.length==0) {
186             annInfo.getProcessingContext().getErrorHandler().warning(
187                     new AnnotationProcessorException(
188                     localStrings.getLocalString(
189                     "enterprise.deployment.annotation.handlers.invalidannotationforthisclass",
190                     "Illegal annotation symbol for this class will be ignored"),
191                     annInfo));
192             return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.PROCESSED);
193         }
194         
195         // now process the annotation for all the containers.
196
for (ServiceReferenceContainer container : containers) {
197             try {
198                 aRef =container.getServiceReferenceByName(serviceRefName);
199             } catch(Throwable JavaDoc t) {}; // ignore
200

201             if (aRef==null) {
202                 // time to create it...
203
aRef = new ServiceReferenceDescriptor();
204                 aRef.setName(serviceRefName);
205                 container.addServiceReferenceDescriptor(aRef);
206             }
207
208             // Store mapped name that is specified
209
if(aRef.getMappedName() == null) {
210                 if(annotation.mappedName() != null && annotation.mappedName().length() != 0) {
211                     aRef.setMappedName(annotation.mappedName());
212                 }
213             }
214
215             aRef.setInjectResourceType("javax.jws.WebServiceRef");
216             
217             if (!annInfo.getElementType().equals(ElementType.TYPE)) {
218                 InjectionTarget target = new InjectionTarget();
219                 if (annInfo.getElementType().equals(ElementType.FIELD)) {
220                     // this is a field injection
221
Field JavaDoc annotatedField = (Field JavaDoc) annElem;
222                     target.setFieldName(annotatedField.getName());
223                     target.setClassName(annotatedField.getDeclaringClass().getName());
224                 } else {
225                     if (annInfo.getElementType().equals(ElementType.METHOD)) {
226                         // this is a method injection
227
Method JavaDoc annotatedMethod = (Method JavaDoc) annElem;
228                         target.setMethodName(annotatedMethod.getName());
229                         target.setClassName(annotatedMethod.getDeclaringClass().getName());
230                     }
231                 }
232                 aRef.addInjectionTarget(target);
233             }
234             
235             if (!Object JavaDoc.class.equals(annotation.value())) {
236                 // a value was provided, which should be the Service
237
// interface, the requested injection is therefore on the
238
// port.
239
if (aRef.getServiceInterface()==null) {
240                     aRef.setServiceInterface(annotation.value().getName());
241                 }
242                 
243                 if (aRef.getPortInfoBySEI(annotatedType.getName())==null) {
244                     ServiceRefPortInfo portInfo = new ServiceRefPortInfo();
245                     portInfo.setServiceEndpointInterface(annotatedType.getName());
246                     aRef.addPortInfo(portInfo);
247                 }
248                 // set the port type requested for injection
249
if (aRef.getInjectionTargetType()==null) {
250                     aRef.setInjectionTargetType(annotatedType.getName());
251                 }
252             }
253             
254             // watch the override order
255
if(aRef.getName()==null || aRef.getName().length()==0) {
256                 aRef.setName(annotation.name());
257             }
258             if (aRef.getWsdlFileUri()==null) {
259                 if (annotation.wsdlLocation()==null || annotation.wsdlLocation().length()!=0) {
260                     aRef.setWsdlFileUri(annotation.wsdlLocation());
261                 }
262             }
263             
264             // Read the WebServiceClient annotation for the service name space uri and wsdl (if required)
265
WebServiceClient wsclientAnn;
266             if (Object JavaDoc.class.equals(annotation.value())) {
267                 wsclientAnn = (WebServiceClient) annotatedType.getAnnotation(WebServiceClient.class);
268             } else {
269                 wsclientAnn = (WebServiceClient) annotation.value().getAnnotation(WebServiceClient.class);
270             }
271             if (wsclientAnn==null) {
272                 throw new AnnotationProcessorException(
273                         localStrings.getLocalString(
274                         "enterprise.deployment.annotation.handlers.classnotannotated",
275                         "Class must be annotated with a {1} annotation\n symbol : {1}\n location: {0}",
276                         new Object JavaDoc[] { annotatedType.toString(), WebServiceClient.class.toString() }));
277             }
278             
279             // If wsdl file was not specified in a descriptor and not in the annotation, get it from WebServiceClient
280
// annotation
281
if (aRef.getWsdlFileUri()==null) {
282                 aRef.setWsdlFileUri(wsclientAnn.wsdlLocation());
283             }
284             
285             // Set service name space URI and service local part
286
if(aRef.getServiceName() == null) {
287                 aRef.setServiceNamespaceUri(wsclientAnn.targetNamespace());
288                 aRef.setServiceLocalPart(wsclientAnn.name());
289             }
290             
291             if (aRef.getServiceInterface()==null) {
292                 aRef.setServiceInterface(annotatedType.getName());
293             }
294         }
295         return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.PROCESSED);
296     }
297     
298     public HandlerProcessingResult processAnnotation(AnnotationInfo annInfo)
299             throws AnnotationProcessorException {
300         WebServiceRef annotation = (WebServiceRef) annInfo.getAnnotation();
301         return(processAWsRef(annInfo, annotation));
302     }
303 }
304
Popular Tags