KickJava   Java API By Example, From Geeks To Geeks.

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


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.deployment.annotation.handlers;
24
25
26 import javax.jws.WebService;
27 import javax.xml.ws.WebServiceProvider;
28 import javax.xml.ws.WebServiceRef;
29 import javax.jws.HandlerChain;
30
31 import java.lang.annotation.ElementType JavaDoc;
32 import java.lang.reflect.Field JavaDoc;
33 import java.lang.reflect.Method JavaDoc;
34 import java.lang.reflect.AnnotatedElement JavaDoc;
35 import java.lang.annotation.Annotation JavaDoc;
36
37 import java.net.URL JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.io.InputStream JavaDoc;
42 import java.util.logging.Level JavaDoc;
43 import com.sun.enterprise.deployment.annotation.AnnotatedElementHandler;
44 import com.sun.enterprise.deployment.annotation.AnnotationInfo;
45 import com.sun.enterprise.deployment.annotation.ProcessingContext;
46 import com.sun.enterprise.deployment.annotation.ResultType;
47 import com.sun.enterprise.deployment.annotation.HandlerProcessingResult;
48 import com.sun.enterprise.deployment.annotation.AnnotationProcessorException;
49 import com.sun.enterprise.deployment.annotation.impl.HandlerProcessingResultImpl;
50 import com.sun.enterprise.deployment.annotation.context.AnnotationContext;
51 import com.sun.enterprise.deployment.annotation.context.ResourceContainerContextImpl;
52
53 import com.sun.enterprise.deployment.WebServiceHandlerChain;
54 import com.sun.enterprise.deployment.WebServiceHandler;
55 import com.sun.enterprise.deployment.WebServiceEndpoint;
56 import com.sun.enterprise.deployment.EjbDescriptor;
57 import com.sun.enterprise.deployment.EjbBundleDescriptor;
58 import com.sun.enterprise.deployment.Descriptor;
59 import com.sun.enterprise.deployment.ServiceReferenceDescriptor;
60
61 import com.sun.enterprise.deployment.xml.WebServicesTagNames;
62 import com.sun.enterprise.deployment.types.HandlerChainContainer;
63 import com.sun.enterprise.deployment.annotation.context.EjbContext;
64 import com.sun.enterprise.deployment.annotation.context.EjbBundleContext;
65 import com.sun.enterprise.deployment.annotation.context.WebBundleContext;
66 import com.sun.enterprise.deployment.annotation.context.HandlerContext;
67 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
68
69 import javax.xml.parsers.DocumentBuilder JavaDoc;
70 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
71 import org.xml.sax.SAXException JavaDoc;
72 import org.xml.sax.SAXParseException JavaDoc;
73 import org.w3c.dom.Document JavaDoc;
74 import org.w3c.dom.Node JavaDoc;
75 import org.w3c.dom.NodeList JavaDoc;
76
77 /**
78  * This handler takes care of the javax.jws.HandlerChain
79  *
80  * @author Jerome Dochez
81  */

82 public class HandlerChainHandler extends AbstractHandler {
83     
84     /** Creates a new instance of HandlerChainHandler */
85     public HandlerChainHandler() {
86     }
87     
88     public Class JavaDoc<? extends Annotation JavaDoc> getAnnotationType() {
89         return HandlerChain.class;
90     }
91     
92     /**
93      * @return an array of annotation types this annotation handler would
94      * require to be processed (if present) before it processes it's own
95      * annotation type.
96      */

97     public Class JavaDoc<? extends Annotation JavaDoc>[] getTypeDependencies() {
98         Class JavaDoc<? extends Annotation JavaDoc>[] dependencies = new Class JavaDoc[3];
99         dependencies[0] = WebService.class;
100         dependencies[1] = WebServiceRef.class;
101         dependencies[2] = WebServiceProvider.class;
102         return dependencies;
103     }
104     
105     public HandlerProcessingResult processAnnotation(AnnotationInfo annInfo)
106         throws AnnotationProcessorException {
107         
108         AnnotatedElementHandler annCtx = annInfo.getProcessingContext().getHandler();
109         AnnotatedElement JavaDoc annElem = annInfo.getAnnotatedElement();
110         
111         HandlerChain hChain = null;
112         Class JavaDoc declaringClass = null;
113         boolean serviceSideChain =
114                 ((annElem.getAnnotation(WebService.class) != null) ||
115                  (annElem.getAnnotation(WebServiceProvider.class) != null)) ? true : false;
116         if(serviceSideChain) {
117             // This is a service handler chain
118
// Ignore @WebService annotation on an interface; process only those in an actual service impl class
119
declaringClass = (Class JavaDoc) annElem;
120             if (declaringClass.isInterface()) {
121                 return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.PROCESSED);
122             }
123         
124             // The @HandlerChain can be in the impl class (typically in the java-wsdl case) or
125
// can be in the SEI also. Check if there is handler chain in the impl class.
126
// If so, the @HandlerChain in impl class gets precedence
127
hChain = (HandlerChain) annInfo.getAnnotation();
128             if(hChain == null) {
129                 WebService webService = (WebService) declaringClass.getAnnotation(WebService.class);
130                 if (webService!=null) {
131                 // check that it is not overrident by SEI..
132
if (webService.endpointInterface()!=null && webService.endpointInterface().length()>0) {
133
134                         Class JavaDoc endpointIntf;
135                         try {
136                             endpointIntf = declaringClass.getClassLoader().loadClass(webService.endpointInterface());
137                         } catch(java.lang.ClassNotFoundException JavaDoc cfne) {
138                             throw new AnnotationProcessorException(
139                                     localStrings.getLocalString("enterprise.deployment.annotation.handlers.classnotfound",
140                                         "class {0} referenced from annotation symbol cannot be loaded",
141                                         new Object JavaDoc[] { webService.endpointInterface() }), annInfo);
142                         }
143                         if (endpointIntf.getAnnotation(HandlerChain.class)!=null) {
144                             hChain = (HandlerChain) endpointIntf.getAnnotation(HandlerChain.class);
145                         }
146                     }
147                 }
148             }
149         } else {
150             // this is a client side handler chain
151
hChain = (HandlerChain) annInfo.getAnnotation();
152             if (annInfo.getElementType().equals(ElementType.FIELD)) {
153                 // this is a field injection
154
Field JavaDoc annotatedField = (Field JavaDoc) annElem;
155                 declaringClass = annotatedField.getDeclaringClass();
156             } else if (annInfo.getElementType().equals(ElementType.METHOD)) {
157
158                 // this is a method injection
159
Method JavaDoc annotatedMethod = (Method JavaDoc) annElem;
160                 declaringClass = annotatedMethod.getDeclaringClass();
161             } else if (annInfo.getElementType().equals(ElementType.TYPE)) {
162                 declaringClass = (Class JavaDoc) annElem;
163             } else {
164                 throw new AnnotationProcessorException(
165                         localStrings.getLocalString(
166                         "enterprise.deployment.annotation.handlers.invalidtype",
167                         "annotation not allowed on this element."), annInfo);
168             }
169         }
170         
171         // At this point the hChain should be the annotation to use.
172
if(hChain == null) {
173             throw new AnnotationProcessorException(
174                     localStrings.getLocalString("enterprise.deployment.annotation.handlers.handlerchainnotfound",
175                         "Unable to get @HandlerChainin {0}",
176                         new Object JavaDoc[] { annInfo.getClass().getCanonicalName() }), annInfo);
177         }
178         // At this point the hChain should be the annotation to use.
179
String JavaDoc handlerFile = hChain.file();
180         
181         HandlerChainContainer[] containers=null;
182         if (annCtx instanceof HandlerContext) {
183             containers = ((HandlerContext) annCtx).getHandlerChainContainers(serviceSideChain, declaringClass);
184         }
185
186         if (containers==null || containers.length==0) {
187             // could not find my web service...
188
throw new AnnotationProcessorException(
189                     localStrings.getLocalString(
190                         "enterprise.deployment.annotation.handlers.componentnotfound",
191                         "component referenced from annotation symbol cannot be found"),
192                     annInfo);
193         }
194         
195         try {
196             URL JavaDoc handlerFileURL=null;
197             try {
198                 handlerFileURL = new URL JavaDoc(handlerFile);
199             } catch(java.net.MalformedURLException JavaDoc e) {
200             }
201                 
202             InputStream JavaDoc handlerFileStream;
203             if (handlerFileURL==null) {
204                 ClassLoader JavaDoc clo = annInfo.getProcessingContext().getProcessingInput().getClassLoader();
205                 handlerFileStream = clo.getResourceAsStream(handlerFile);
206                 if (handlerFileStream==null) {
207                     handlerFileStream = clo.getResourceAsStream(
208                             declaringClass.getPackage().getName().replaceAll("\\.","/") + "/" + handlerFile);
209                     
210                 }
211             } else {
212                 handlerFileStream = handlerFileURL.openConnection().getInputStream();
213             }
214             if (handlerFileStream==null) {
215                 throw new AnnotationProcessorException(
216                         localStrings.getLocalString(
217                             "enterprise.deployment.annotation.handlers.handlerfilenotfound",
218                             "handler file {0} not found",
219                             new Object JavaDoc[] { handlerFile }),
220                          annInfo);
221             }
222             Document JavaDoc document;
223             try {
224                 DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
225                 factory.setNamespaceAware(true);
226                 DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
227                 document = builder.parse(handlerFileStream);
228             } catch (SAXParseException JavaDoc spe) {
229                 throw new AnnotationProcessorException(
230                         localStrings.getLocalString(
231                             "enterprise.deployment.annotation.handlers.parserexception",
232                             "{0} XML Parsing error : line {1} ; Error = {2}",
233                             new Object JavaDoc[] { handlerFile, spe.getLineNumber(), spe.getMessage()}));
234             } finally {
235                 if (handlerFileStream!=null) {
236                     handlerFileStream.close();
237                 }
238             }
239             for (HandlerChainContainer container : containers) {
240                 boolean fromDD=true;
241                 if (!container.hasHandlerChain()) {
242                     fromDD = false;
243                     processHandlerFile(document, container);
244                 }
245                 
246                 // we now need to create the right context to push on the stack
247
// and manually invoke the handlers annotation processing since
248
// we know they are Jax-ws handlers.
249
List JavaDoc<WebServiceHandlerChain> chains = container.getHandlerChain();
250                 ArrayList JavaDoc<Class JavaDoc> handlerClasses = new ArrayList JavaDoc<Class JavaDoc>();
251                 ClassLoader JavaDoc clo = annInfo.getProcessingContext().getProcessingInput().getClassLoader();
252                 for (WebServiceHandlerChain chain : chains) {
253                     for (WebServiceHandler handler : chain.getHandlers()) {
254                         String JavaDoc className = handler.getHandlerClass();
255                         try {
256                             handlerClasses.add(clo.loadClass(className));
257                         } catch(ClassNotFoundException JavaDoc e) {
258                             if (fromDD) {
259                                 logger.log(Level.WARNING, localStrings.getLocalString(
260                                     "enterprise.deployment.annotation.handlers.ddhandlernotfound",
261                                     "handler class {0} specified in deployment descriptors",
262                                     new Object JavaDoc[] {className}));
263                             } else {
264                                 logger.log(Level.WARNING, localStrings.getLocalString(
265                                     "enterprise.deployment.annotation.handlers.handlerfilehandlernotfound",
266                                     "handler class {0} specified in handler file {1} cannot be loaded",
267                                     new Object JavaDoc[] {className, handlerFile}));
268                             }
269                         }
270                     }
271                 }
272                 // we have the list of handler classes, we can now
273
// push the context and call back annotation processing.
274
Descriptor jndiContainer=null;
275                 if (serviceSideChain) {
276                     WebServiceEndpoint endpoint = (WebServiceEndpoint) container;
277                     if (ModuleType.WAR.equals(endpoint.getBundleDescriptor().getModuleType())) {
278                         jndiContainer = endpoint.getBundleDescriptor();
279                     } else {
280                         jndiContainer = endpoint.getEjbComponentImpl();
281                     }
282                 } else {
283                     ServiceReferenceDescriptor ref = (ServiceReferenceDescriptor) container;
284                     if(ModuleType.EJB.equals(ref.getBundleDescriptor().getModuleType())) {
285                         EjbBundleDescriptor ejbBundle = (EjbBundleDescriptor) ref.getBundleDescriptor();
286                         Iterator JavaDoc<EjbDescriptor> ejbsIter = ejbBundle.getEjbs().iterator();
287                         while(ejbsIter.hasNext()) {
288                             EjbDescriptor ejb = ejbsIter.next();
289                             try {
290                                 if(ejb.getServiceReferenceByName(ref.getName()) != null) {
291                                     // found the ejb; break out of the loop
292
jndiContainer = ejb;
293                                     break;
294                                 }
295                             } catch (IllegalArgumentException JavaDoc illex) {
296                                 // this ejb does not have a service-ref by this name;
297
// swallow this exception and go to next
298
}
299                         }
300                     } else {
301                         jndiContainer = ref.getBundleDescriptor();
302                     }
303                 }
304                 ResourceContainerContextImpl newContext = new ResourceContainerContextImpl(jndiContainer);
305                 ProcessingContext ctx = annInfo.getProcessingContext();
306                 
307                 ctx.pushHandler(newContext);
308                 // process the classes
309
annInfo.getProcessingContext().getProcessor().process(
310                         annInfo.getProcessingContext(),
311                         handlerClasses.toArray(new Class JavaDoc[0]));
312
313                 ctx.popHandler();
314             }
315         } catch(Throwable JavaDoc t) {
316             throw new AnnotationProcessorException(t.getMessage(), annInfo);
317         }
318         return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.PROCESSED);
319     }
320
321     private void processHandlerFile(Document JavaDoc document, HandlerChainContainer ep)
322         throws SAXException JavaDoc {
323         
324         NodeList JavaDoc handlerChainList = document.getElementsByTagNameNS("http://java.sun.com/xml/ns/javaee",
325                 WebServicesTagNames.HANDLER_CHAIN);
326         if(handlerChainList.getLength() != 0) {
327             // this is a namespace aware handler-config file - process and return
328
processHandlerChains(handlerChainList, ep);
329             return;
330         }
331         // No Handlers found !!!! this could be namespace-unaware handler file
332
handlerChainList = document.getElementsByTagName(WebServicesTagNames.HANDLER_CHAIN);
333         processHandlerChains(handlerChainList, ep);
334     }
335     
336     private void processHandlerChains(NodeList JavaDoc handlerChainList, HandlerChainContainer ep)
337         throws SAXException JavaDoc {
338         
339         for(int i=0; i<handlerChainList.getLength(); i++) {
340             WebServiceHandlerChain hc = new WebServiceHandlerChain();
341             Node JavaDoc handlerChain = handlerChainList.item(i);
342             Node JavaDoc child = handlerChain.getFirstChild();
343             while(child != null) {
344                 if(WebServicesTagNames.SERVICE_NAME_PATTERN.equals(child.getLocalName())) {
345                     hc.setServiceNamePattern(child.getTextContent());
346                 }
347                 if(WebServicesTagNames.PORT_NAME_PATTERN.equals(child.getLocalName())) {
348                     hc.setPortNamePattern(child.getTextContent());
349                 }
350                 if(WebServicesTagNames.PROTOCOL_BINDINGS.equals(child.getLocalName())) {
351                     hc.setProtocolBindings(child.getTextContent());
352                 }
353                 if(WebServicesTagNames.HANDLER.equals(child.getLocalName())) {
354                     processHandlers(child, hc);
355                 }
356                 child = child.getNextSibling();
357             }
358             ep.addHandlerChain(hc);
359         }
360     }
361
362     private void processHandlers(Node JavaDoc handler, WebServiceHandlerChain hc)
363         throws SAXException JavaDoc {
364         
365         Node JavaDoc child = handler.getFirstChild();
366         com.sun.enterprise.deployment.WebServiceHandler h =
367                     new com.sun.enterprise.deployment.WebServiceHandler();
368         while(child != null) {
369             if(WebServicesTagNames.HANDLER_NAME.equals(child.getLocalName())) {
370                 h.setHandlerName(child.getTextContent());
371             }
372             if(WebServicesTagNames.HANDLER_CLASS.equals(child.getLocalName())) {
373                 h.setHandlerClass(child.getTextContent());
374             }
375             // Ignore init-params and soap-header; not applicable for JAXWS
376
if(WebServicesTagNames.HANDLER_PORT_NAME.equals(child.getLocalName())) {
377                 h.addPortName(child.getTextContent());
378             }
379             if(WebServicesTagNames.SOAP_ROLE.equals(child.getLocalName())) {
380                 h.addSoapRole(child.getTextContent());
381             }
382             child = child.getNextSibling();
383         }
384         hc.addHandler(h);
385     }
386 }
387
Popular Tags