1 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 ; 32 import java.lang.reflect.Field ; 33 import java.lang.reflect.Method ; 34 import java.lang.reflect.AnnotatedElement ; 35 import java.lang.annotation.Annotation ; 36 37 import java.net.URL ; 38 import java.util.List ; 39 import java.util.Iterator ; 40 import java.util.ArrayList ; 41 import java.io.InputStream ; 42 import java.util.logging.Level ; 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 ; 68 69 import javax.xml.parsers.DocumentBuilder ; 70 import javax.xml.parsers.DocumentBuilderFactory ; 71 import org.xml.sax.SAXException ; 72 import org.xml.sax.SAXParseException ; 73 import org.w3c.dom.Document ; 74 import org.w3c.dom.Node ; 75 import org.w3c.dom.NodeList ; 76 77 82 public class HandlerChainHandler extends AbstractHandler { 83 84 85 public HandlerChainHandler() { 86 } 87 88 public Class <? extends Annotation > getAnnotationType() { 89 return HandlerChain.class; 90 } 91 92 97 public Class <? extends Annotation >[] getTypeDependencies() { 98 Class <? extends Annotation >[] dependencies = new Class [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 annElem = annInfo.getAnnotatedElement(); 110 111 HandlerChain hChain = null; 112 Class declaringClass = null; 113 boolean serviceSideChain = 114 ((annElem.getAnnotation(WebService.class) != null) || 115 (annElem.getAnnotation(WebServiceProvider.class) != null)) ? true : false; 116 if(serviceSideChain) { 117 declaringClass = (Class ) annElem; 120 if (declaringClass.isInterface()) { 121 return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.PROCESSED); 122 } 123 124 hChain = (HandlerChain) annInfo.getAnnotation(); 128 if(hChain == null) { 129 WebService webService = (WebService) declaringClass.getAnnotation(WebService.class); 130 if (webService!=null) { 131 if (webService.endpointInterface()!=null && webService.endpointInterface().length()>0) { 133 134 Class endpointIntf; 135 try { 136 endpointIntf = declaringClass.getClassLoader().loadClass(webService.endpointInterface()); 137 } catch(java.lang.ClassNotFoundException 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 [] { 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 hChain = (HandlerChain) annInfo.getAnnotation(); 152 if (annInfo.getElementType().equals(ElementType.FIELD)) { 153 Field annotatedField = (Field ) annElem; 155 declaringClass = annotatedField.getDeclaringClass(); 156 } else if (annInfo.getElementType().equals(ElementType.METHOD)) { 157 158 Method annotatedMethod = (Method ) annElem; 160 declaringClass = annotatedMethod.getDeclaringClass(); 161 } else if (annInfo.getElementType().equals(ElementType.TYPE)) { 162 declaringClass = (Class ) 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 if(hChain == null) { 173 throw new AnnotationProcessorException( 174 localStrings.getLocalString("enterprise.deployment.annotation.handlers.handlerchainnotfound", 175 "Unable to get @HandlerChainin {0}", 176 new Object [] { annInfo.getClass().getCanonicalName() }), annInfo); 177 } 178 String 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 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 handlerFileURL=null; 197 try { 198 handlerFileURL = new URL (handlerFile); 199 } catch(java.net.MalformedURLException e) { 200 } 201 202 InputStream handlerFileStream; 203 if (handlerFileURL==null) { 204 ClassLoader 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 [] { handlerFile }), 220 annInfo); 221 } 222 Document document; 223 try { 224 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 225 factory.setNamespaceAware(true); 226 DocumentBuilder builder = factory.newDocumentBuilder(); 227 document = builder.parse(handlerFileStream); 228 } catch (SAXParseException 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 [] { 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 List <WebServiceHandlerChain> chains = container.getHandlerChain(); 250 ArrayList <Class > handlerClasses = new ArrayList <Class >(); 251 ClassLoader clo = annInfo.getProcessingContext().getProcessingInput().getClassLoader(); 252 for (WebServiceHandlerChain chain : chains) { 253 for (WebServiceHandler handler : chain.getHandlers()) { 254 String className = handler.getHandlerClass(); 255 try { 256 handlerClasses.add(clo.loadClass(className)); 257 } catch(ClassNotFoundException 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 [] {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 [] {className, handlerFile})); 268 } 269 } 270 } 271 } 272 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 <EjbDescriptor> ejbsIter = ejbBundle.getEjbs().iterator(); 287 while(ejbsIter.hasNext()) { 288 EjbDescriptor ejb = ejbsIter.next(); 289 try { 290 if(ejb.getServiceReferenceByName(ref.getName()) != null) { 291 jndiContainer = ejb; 293 break; 294 } 295 } catch (IllegalArgumentException illex) { 296 } 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 annInfo.getProcessingContext().getProcessor().process( 310 annInfo.getProcessingContext(), 311 handlerClasses.toArray(new Class [0])); 312 313 ctx.popHandler(); 314 } 315 } catch(Throwable t) { 316 throw new AnnotationProcessorException(t.getMessage(), annInfo); 317 } 318 return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.PROCESSED); 319 } 320 321 private void processHandlerFile(Document document, HandlerChainContainer ep) 322 throws SAXException { 323 324 NodeList handlerChainList = document.getElementsByTagNameNS("http://java.sun.com/xml/ns/javaee", 325 WebServicesTagNames.HANDLER_CHAIN); 326 if(handlerChainList.getLength() != 0) { 327 processHandlerChains(handlerChainList, ep); 329 return; 330 } 331 handlerChainList = document.getElementsByTagName(WebServicesTagNames.HANDLER_CHAIN); 333 processHandlerChains(handlerChainList, ep); 334 } 335 336 private void processHandlerChains(NodeList handlerChainList, HandlerChainContainer ep) 337 throws SAXException { 338 339 for(int i=0; i<handlerChainList.getLength(); i++) { 340 WebServiceHandlerChain hc = new WebServiceHandlerChain(); 341 Node handlerChain = handlerChainList.item(i); 342 Node 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 handler, WebServiceHandlerChain hc) 363 throws SAXException { 364 365 Node 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 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 |