1 23 24 package com.sun.enterprise.deployment.annotation.handlers; 25 26 import javax.xml.ws.WebServiceRef; 27 28 import java.lang.reflect.AnnotatedElement ; 29 import java.lang.annotation.Annotation ; 30 import java.lang.annotation.ElementType ; 31 import java.lang.reflect.Field ; 32 import java.lang.reflect.Method ; 33 import java.lang.reflect.Modifier ; 34 35 import java.util.Iterator ; 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 66 public class WebServiceRefHandler extends AbstractHandler { 67 68 69 public WebServiceRefHandler() { 70 } 71 72 public Class <? extends Annotation > getAnnotationType() { 73 return javax.xml.ws.WebServiceRef.class; 74 } 75 76 81 public Class <? extends Annotation >[] getTypeDependencies() { 82 Class <? extends Annotation >[] dependencies = new Class [3]; 89 dependencies[0] = javax.ejb.Stateless .class; 90 dependencies[1] = javax.ejb.Stateful .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 annElem = annInfo.getAnnotatedElement(); 99 100 Class annotatedType = null; 101 Class declaringClass = null; 102 String serviceRefName = annotation.name(); 103 if (annInfo.getElementType().equals(ElementType.FIELD)) { 104 Field annotatedField = (Field ) annElem; 106 107 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 if (serviceRefName.equals("")) { 122 serviceRefName = declaringClass.getName() 123 + "/" + annotatedField.getName(); 124 } 125 } else if (annInfo.getElementType().equals(ElementType.METHOD)) { 126 127 Method annotatedMethod = (Method ) 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 String propertyName = 146 getInjectionMethodPropertyName(annotatedMethod, annInfo); 147 serviceRefName = declaringClass.getName() 149 + "/" + propertyName; 150 } 151 } else if (annInfo.getElementType().equals(ElementType.TYPE)) 152 { 153 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 annotatedType = annotation.type(); 163 if (annotatedType==null || annotatedType==Object .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 ) 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 for (ServiceReferenceContainer container : containers) { 197 try { 198 aRef =container.getServiceReferenceByName(serviceRefName); 199 } catch(Throwable t) {}; 201 if (aRef==null) { 202 aRef = new ServiceReferenceDescriptor(); 204 aRef.setName(serviceRefName); 205 container.addServiceReferenceDescriptor(aRef); 206 } 207 208 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 Field annotatedField = (Field ) annElem; 222 target.setFieldName(annotatedField.getName()); 223 target.setClassName(annotatedField.getDeclaringClass().getName()); 224 } else { 225 if (annInfo.getElementType().equals(ElementType.METHOD)) { 226 Method annotatedMethod = (Method ) annElem; 228 target.setMethodName(annotatedMethod.getName()); 229 target.setClassName(annotatedMethod.getDeclaringClass().getName()); 230 } 231 } 232 aRef.addInjectionTarget(target); 233 } 234 235 if (!Object .class.equals(annotation.value())) { 236 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 if (aRef.getInjectionTargetType()==null) { 250 aRef.setInjectionTargetType(annotatedType.getName()); 251 } 252 } 253 254 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 WebServiceClient wsclientAnn; 266 if (Object .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 [] { annotatedType.toString(), WebServiceClient.class.toString() })); 277 } 278 279 if (aRef.getWsdlFileUri()==null) { 282 aRef.setWsdlFileUri(wsclientAnn.wsdlLocation()); 283 } 284 285 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 |