1 23 24 package com.sun.enterprise.deployment.annotation.handlers; 25 26 import java.util.Set ; 27 import java.util.StringTokenizer ; 28 29 import java.lang.reflect.AnnotatedElement ; 30 import java.lang.annotation.Annotation ; 31 32 import javax.enterprise.deploy.shared.ModuleType ; 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.WebComponentContext; 49 import com.sun.enterprise.deployment.annotation.context.EjbContext; 50 import com.sun.enterprise.deployment.annotation.context.EjbBundleContext; 51 52 import com.sun.enterprise.deployment.WebBundleDescriptor; 53 import com.sun.enterprise.deployment.EjbBundleDescriptor; 54 import com.sun.enterprise.deployment.BundleDescriptor; 55 import com.sun.enterprise.deployment.WebServicesDescriptor; 56 import com.sun.enterprise.deployment.WebService; 57 import com.sun.enterprise.deployment.WebServiceEndpoint; 58 import com.sun.enterprise.deployment.EjbDescriptor; 59 import com.sun.enterprise.deployment.WebComponentDescriptor; 60 61 import javax.xml.namespace.QName ; 62 63 69 70 public class WebServiceHandler extends AbstractHandler { 71 72 73 public WebServiceHandler() { 74 } 75 76 public Class <? extends Annotation > getAnnotationType() { 77 return javax.jws.WebService.class; 78 } 79 80 85 public Class <? extends Annotation >[] getTypeDependencies() { 86 Class dependencies[] = { javax.ejb.Stateless .class }; 87 return dependencies; 88 } 89 90 public HandlerProcessingResult processAnnotation(AnnotationInfo annInfo) 91 throws AnnotationProcessorException 92 { 93 AnnotatedElementHandler annCtx = annInfo.getProcessingContext().getHandler(); 94 AnnotatedElement annElem = annInfo.getAnnotatedElement(); 95 96 if (!(annElem instanceof Class )) { 98 AnnotationProcessorException ape = new AnnotationProcessorException( 99 localStrings.getLocalString("enterprise.deployment.annotation.handlers.wrongannotationlocation", 100 "symbol annotation can only be specified on TYPE"),annInfo); 101 annInfo.getProcessingContext().getErrorHandler().error(ape); 102 return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.FAILED); 103 } 104 105 106 if (((Class )annElem).isInterface()) { 108 return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.PROCESSED); 109 } 110 111 javax.jws.WebService ann = (javax.jws.WebService) annInfo.getAnnotation(); 113 114 BundleDescriptor bundleDesc; 115 116 if(annCtx instanceof EjbContext && 118 (annElem.getAnnotation(javax.ejb.Stateless .class) == null)) { 119 AnnotationProcessorException ape = new AnnotationProcessorException( 120 localStrings.getLocalString("enterprise.deployment.annotation.handlers.webeppkgwrong", 121 "Class {0} is annotated with @WebService and without @Stateless in an EJB-JAR." + 122 "If it is an EJB endpoint, it should have @Stateless annotation;" + 123 "If it is a servlet endpoint, it should be packaged in a WAR file", 124 new Object [] {((Class )annElem).getName()}),annInfo); 125 ape.setFatal(true); 126 throw ape; 127 } 128 if(annCtx instanceof EjbBundleContext && 129 (annElem.getAnnotation(javax.ejb.Stateless .class) == null)) { 130 AnnotationProcessorException ape = new AnnotationProcessorException( 131 localStrings.getLocalString("enterprise.deployment.annotation.handlers.webeppkgwrong", 132 "Class {0} is annotated with @WebService and without @Stateless in an EJB-JAR." + 133 "If it is an EJB endpoint, it should have @Stateless annotation;" + 134 "If it is a servlet endpoint, it should be packaged in a WAR file", 135 new Object [] {((Class )annElem).getName()}),annInfo); 136 ape.setFatal(true); 137 throw ape; 138 } 139 149 150 if (annElem.getAnnotation(javax.ejb.Stateless .class)!=null) { 152 EjbContext ctx = (EjbContext) annCtx; 154 bundleDesc = ctx.getDescriptor().getEjbBundleDescriptor(); 155 bundleDesc.setSpecVersion("3.0"); 156 } else { 157 if(annCtx instanceof WebComponentContext) { 159 bundleDesc = ((WebComponentContext)annCtx).getDescriptor().getWebBundleDescriptor(); 160 } else { 161 bundleDesc = ((WebBundleContext)annCtx).getDescriptor(); 162 } 163 bundleDesc.setSpecVersion("2.5"); 164 } 165 166 String portComponentName = ann.name(); 169 170 String svcNameFromImplClass = ann.serviceName(); 173 String implClassName = ((Class ) annElem).getSimpleName(); 174 String implClassFullName = ((Class )annElem).getName(); 175 176 String targetNameSpace = ann.targetNamespace(); 181 182 String portNameFromImplClass = ann.portName(); 186 if( (portNameFromImplClass == null) || 187 (portNameFromImplClass.length() == 0) ) { 188 if( (portComponentName != null) && (portComponentName.length() != 0) ) { 189 portNameFromImplClass = portComponentName + "Port"; 190 } else { 191 portNameFromImplClass = implClassName+"Port"; 192 } 193 } 194 195 String userSpecifiedBinding = null; 197 javax.xml.ws.BindingType bindingAnn = (javax.xml.ws.BindingType) 198 ((Class )annElem).getAnnotation(javax.xml.ws.BindingType.class); 199 if(bindingAnn != null) { 200 userSpecifiedBinding = bindingAnn.value(); 201 } 202 203 String wsdlLocation = null; 205 if (ann.wsdlLocation()!=null && ann.wsdlLocation().length()!=0) { 206 wsdlLocation = ann.wsdlLocation(); 207 } 208 209 boolean sibAnnotationOverriden=false; 213 if (ann.endpointInterface()!=null && ann.endpointInterface().length()>0) { 214 Class endpointIntf; 215 try { 216 endpointIntf = ((Class ) annElem).getClassLoader().loadClass(ann.endpointInterface()); 217 } catch(java.lang.ClassNotFoundException cfne) { 218 throw new AnnotationProcessorException( 219 localStrings.getLocalString("enterprise.deployment.annotation.handlers.classnotfound", 220 "class {0} referenced from annotation symbol cannot be loaded"), annInfo); 221 } 222 annElem = endpointIntf; 223 224 ann = annElem.getAnnotation(javax.jws.WebService.class); 225 if (ann==null) { 226 throw new AnnotationProcessorException("SEI " + ((javax.jws.WebService) annInfo.getAnnotation()).endpointInterface() 227 + " referenced from the @WebService annotation on " + ((Class ) annElem).getName() 228 + " does not contain a @WebService annotation"); 229 } 230 sibAnnotationOverriden = true; 231 232 if(annElem.getAnnotation(javax.xml.ws.BindingType.class) != null) { 234 throw new AnnotationProcessorException("SEI " + ((javax.jws.WebService) annInfo.getAnnotation()).endpointInterface() 235 + " cannot have @BindingType"); 236 } 237 } 238 239 WebServicesDescriptor wsDesc = bundleDesc.getWebServices(); 240 if(portComponentName == null || portComponentName.length() == 0) { 244 portComponentName = implClassName; 245 } 246 WebServiceEndpoint wep = wsDesc.getEndpointByName(portComponentName); 248 if(wep!=null) { 249 if((wep.getServiceEndpointInterface() != null) && 255 (wep.getServiceEndpointInterface().length() != 0) && 256 (!((Class )annElem).getName().equals(wep.getServiceEndpointInterface()))) { 257 portComponentName = implClassFullName; 258 } 259 } 260 261 WebServiceEndpoint endpoint = wsDesc.getEndpointByName(portComponentName); 265 WebService newWS; 266 if(endpoint == null) { 267 if (svcNameFromImplClass!=null && svcNameFromImplClass.length()!=0) { 270 newWS = wsDesc.getWebServiceByName(svcNameFromImplClass); 271 } else { 272 newWS = wsDesc.getWebServiceByName(implClassName+"Service"); 273 } 274 if(newWS==null) { 275 newWS = new WebService(); 276 if (svcNameFromImplClass!=null && svcNameFromImplClass.length()!=0) { 278 newWS.setName(svcNameFromImplClass); 279 } else { 280 newWS.setName(implClassName+"Service"); 281 } 282 wsDesc.addWebService(newWS); 283 } 284 endpoint = new WebServiceEndpoint(); 285 if (portComponentName!=null && portComponentName.length()!=0) { 286 endpoint.setEndpointName(portComponentName); 287 } else { 288 endpoint.setEndpointName(((Class ) annElem).getName()); 289 } 290 newWS.addEndpoint(endpoint); 291 wsDesc.setSpecVersion(com.sun.enterprise.deployment.node.WebServicesDescriptorNode.SPEC_VERSION); 292 } else { 293 newWS = endpoint.getWebService(); 294 } 295 296 if(endpoint.getWsdlService() != null) { 301 if( (targetNameSpace != null) && (targetNameSpace.length() != 0 ) && 302 (!endpoint.getWsdlService().getNamespaceURI().equals(targetNameSpace)) ) { 303 AnnotationProcessorException ape = new AnnotationProcessorException( 304 "Target Namespace in wsdl-service element does not match @WebService.targetNamespace", 305 annInfo); 306 annInfo.getProcessingContext().getErrorHandler().error(ape); 307 return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.FAILED); 308 } 309 targetNameSpace = endpoint.getWsdlService().getNamespaceURI(); 310 } 311 312 if( (endpoint.getWsdlService() != null) && 314 (endpoint.getWsdlPort() != null) ) { 315 if(!endpoint.getWsdlService().getNamespaceURI().equals( 316 endpoint.getWsdlPort().getNamespaceURI())) { 317 AnnotationProcessorException ape = new AnnotationProcessorException( 318 "Target Namespace for wsdl-service and wsdl-port should be the same", 319 annInfo); 320 annInfo.getProcessingContext().getErrorHandler().error(ape); 321 return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.FAILED); 322 } 323 } 324 325 327 if(newWS.getWsdlFileUri() == null) { 331 if(wsdlLocation != null) { 332 newWS.setWsdlFileUri(wsdlLocation); 333 } else { 334 if (ann.wsdlLocation()!=null && ann.wsdlLocation().length()!=0) { 335 newWS.setWsdlFileUri(ann.wsdlLocation()); 336 } 337 } 338 } 339 340 if((!endpoint.hasUserSpecifiedProtocolBinding()) && 342 (userSpecifiedBinding != null) && 343 (userSpecifiedBinding.length() != 0)){ 344 endpoint.setProtocolBinding(userSpecifiedBinding); 345 } 346 347 if(endpoint.getServiceEndpointInterface() == null) { 348 if (ann.endpointInterface()!=null && ann.endpointInterface().length()!=0) { 350 endpoint.setServiceEndpointInterface(ann.endpointInterface()); 351 } else { 352 endpoint.setServiceEndpointInterface(((Class )annElem).getName()); 353 } 354 } 355 356 annElem = annInfo.getAnnotatedElement(); 358 359 if (ModuleType.WAR.equals(bundleDesc.getModuleType())) { 360 if(endpoint.getServletImplClass() == null) { 361 endpoint.setServletImplClass(((Class )annElem).getName()); 363 } 364 365 WebBundleDescriptor webBundle = (WebBundleDescriptor) bundleDesc; 367 if(endpoint.getWebComponentLink() == null) { 368 endpoint.setWebComponentLink(endpoint.getEndpointName()); 370 } 371 if(endpoint.getWebComponentImpl() == null) { 372 WebComponentDescriptor webComponent = (WebComponentDescriptor) webBundle. 373 getWebComponentByCanonicalName(endpoint.getWebComponentLink()); 374 375 if (webComponent == null) { 377 webComponent = new WebComponentDescriptor(); 378 webComponent.setServlet(true); 379 webComponent.setWebComponentImplementation(((Class ) annElem).getCanonicalName()); 380 webComponent.setName(endpoint.getEndpointName()); 381 webComponent.addUrlPattern("/"+newWS.getName()); 382 webBundle.addWebComponentDescriptor(webComponent); 383 } 384 endpoint.setWebComponentImpl(webComponent); 385 } 386 } else { 387 if(endpoint.getEjbLink() == null) { 388 javax.ejb.Stateless stateless = annElem.getAnnotation(javax.ejb.Stateless .class); 389 String name; 390 if (stateless.name()==null || stateless.name().length()>0) { 391 name = stateless.name(); 392 } else { 393 name = ((Class ) annElem).getSimpleName(); 394 } 395 EjbDescriptor ejb = ((EjbBundleDescriptor) bundleDesc).getEjbByName(name); 396 endpoint.setEjbComponentImpl(ejb); 397 ejb.setWebServiceEndpointInterfaceName(endpoint.getServiceEndpointInterface()); 398 endpoint.setEjbLink(ejb.getName()); 399 } 400 } 401 402 if(endpoint.getWsdlPort() == null) { 403 if(targetNameSpace == null || targetNameSpace.length()==0) { 408 if (((Class ) annElem).getPackage()!=null) { 412 413 StringTokenizer tokens = new StringTokenizer ( 414 ((Class ) annElem).getPackage().getName(), ".", false); 415 416 if (tokens.hasMoreElements()) { 417 while (tokens.hasMoreElements()) { 418 if(targetNameSpace == null || targetNameSpace.length()==0) { 419 targetNameSpace=tokens.nextElement().toString(); 420 } else { 421 targetNameSpace=tokens.nextElement().toString()+"."+targetNameSpace; 422 } 423 } 424 } else { 425 targetNameSpace = ((Class ) annElem).getPackage().getName(); 426 } 427 } else { 428 throw new AnnotationProcessorException("JAX-WS 2.0 paragraph 3.2. " + 429 "The javax.jws.WebService annotation " 430 + "targetNamespace MUST be used for classes or interfaces in no package"); 431 } 432 targetNameSpace = "http://" + (targetNameSpace==null?"":targetNameSpace+"/"); 433 } 434 endpoint.setWsdlPort(new QName (targetNameSpace, portNameFromImplClass, "ns1")); 436 } 437 438 if(endpoint.getWsdlService() == null) { 439 String serviceNameSpace = endpoint.getWsdlPort().getNamespaceURI(); 442 String serviceName = newWS.getName(); 443 endpoint.setWsdlService(new QName (serviceNameSpace, serviceName)); 444 } 445 446 return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.PROCESSED); 447 } 448 } 449 | Popular Tags |