1 19 20 package org.netbeans.modules.websvc.jaxwsruntimemodel; 21 22 import java.io.IOException ; 23 import java.net.URLEncoder ; 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import java.util.StringTokenizer ; 30 import javax.lang.model.element.AnnotationMirror; 31 import javax.lang.model.element.AnnotationValue; 32 import javax.lang.model.element.Element; 33 import javax.lang.model.element.ExecutableElement; 34 import javax.lang.model.element.Modifier; 35 import javax.lang.model.element.Name; 36 import javax.lang.model.element.TypeElement; 37 import javax.lang.model.type.ArrayType; 38 import javax.lang.model.type.TypeKind; 39 import javax.lang.model.type.TypeMirror; 40 import javax.lang.model.util.ElementFilter; 41 import javax.xml.namespace.QName ; 42 import org.netbeans.api.java.source.CompilationController; 43 import org.netbeans.api.java.source.JavaSource; 44 import org.netbeans.modules.websvc.wsitconf.util.AbstractTask; 45 import org.netbeans.modules.websvc.wsitconf.util.SourceUtils; 46 import org.openide.filesystems.FileObject; 47 import org.openide.util.Exceptions; 48 49 58 public class JavaWsdlMapper { 59 60 public static final String RESPONSE = "Response"; public static final String RETURN = "return"; public static final String BEAN = "Bean"; public static final String SERVICE = "Service"; public static final String PORT = "Port"; public static final String PORT_TYPE = "PortType"; public static final String BINDING = "Binding"; 68 69 public JavaWsdlMapper() { } 70 71 76 public static String getNamespace(String packageName) { 77 if (packageName == null || packageName.length() == 0) 78 return null; 79 80 StringTokenizer tokenizer = new StringTokenizer (packageName, "."); 81 String [] tokens; 82 if (tokenizer.countTokens() == 0) { 83 tokens = new String [0]; 84 } else { 85 tokens = new String [tokenizer.countTokens()]; 86 for (int i=tokenizer.countTokens()-1; i >= 0; i--) { 87 tokens[i] = tokenizer.nextToken(); 88 } 89 } 90 StringBuilder namespace = new StringBuilder ("http://"); for (int i=0; i<tokens.length; i++) { 92 if (i!=0) { 93 namespace.append('.'); 94 } 95 namespace.append(tokens[i]); 96 } 97 namespace.append('/'); 98 return namespace.toString(); 99 } 100 101 106 public static QName getServiceName(FileObject implClass) { 107 108 final java.lang.String [] serviceNameQNameARR = new String [2]; 109 if (implClass == null) return null; 110 try { 111 JavaSource js = JavaSource.forFileObject(implClass); 112 js.runUserActionTask(new AbstractTask<CompilationController>() { 113 public void run(CompilationController controller) throws java.io.IOException { 114 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 115 SourceUtils sourceUtils = SourceUtils.newInstance(controller); 116 TypeElement te = sourceUtils.getTypeElement(); 117 118 serviceNameQNameARR[0] = te.getSimpleName().toString() + SERVICE; 119 String packageName = getPackageFromClass(te.getQualifiedName().toString()); 120 serviceNameQNameARR[1] = getNamespace(packageName); 121 122 List <? extends AnnotationMirror> annotations = te.getAnnotationMirrors(); 123 for (AnnotationMirror m : annotations) { 124 Name qualifiedName = ((TypeElement)m.getAnnotationType().asElement()).getQualifiedName(); 125 if (qualifiedName.contentEquals("javax.jws.WebService")) { String serviceNameAnnot = null; 127 String targetNamespaceAnnot = null; 128 129 Map <? extends ExecutableElement, ? extends AnnotationValue> expressions = m.getElementValues(); 130 for(ExecutableElement ex:expressions.keySet()) { 131 if (ex.getSimpleName().contentEquals("serviceName")) { serviceNameAnnot = (String )expressions.get(ex).getValue(); 133 if (serviceNameAnnot!=null) serviceNameAnnot = URLEncoder.encode(serviceNameAnnot,"UTF-8"); } else if (ex.getSimpleName().contentEquals("targetNamespace")) { targetNamespaceAnnot = (String )expressions.get(ex).getValue(); 136 if (targetNamespaceAnnot!=null) targetNamespaceAnnot = URLEncoder.encode(targetNamespaceAnnot,"UTF-8"); } 138 if (targetNamespaceAnnot!=null && serviceNameAnnot!=null) break; 139 } 140 if (serviceNameAnnot != null) { 141 serviceNameQNameARR[0] = serviceNameAnnot; 142 } 143 if (targetNamespaceAnnot != null) { 144 serviceNameQNameARR[1] = targetNamespaceAnnot; 145 } 146 } 147 } 148 } 149 }, true); 150 } catch (IOException ex) { 151 Exceptions.printStackTrace(ex); 152 } 153 return new QName (serviceNameQNameARR[1], serviceNameQNameARR[0]); 154 } 155 156 162 public static String getBindingName(FileObject implClass, String targetNamespace) { 163 QName portName = getPortName(implClass, targetNamespace); 164 if (portName != null) { 165 return (portName.getLocalPart() + BINDING); 166 } 167 return null; 168 } 169 170 public static final int UNKNOWN = -1; 171 public static final int OUTPUTINPUT = 0; 172 public static final int OUTPUT = 1; 173 public static final int INPUT = 2; 174 175 public static List <String > getOperationFaults(FileObject implClass, final String operationName) { 176 if ((implClass == null) || (operationName == null)) { 177 return Collections.EMPTY_LIST; 178 } 179 180 final List <String > faults = new ArrayList (); 181 if (implClass == null) return null; 182 try { 183 JavaSource js = JavaSource.forFileObject(implClass); 184 js.runUserActionTask(new AbstractTask<CompilationController>() { 185 public void run(CompilationController controller) throws java.io.IOException { 186 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 187 SourceUtils sourceUtils = SourceUtils.newInstance(controller); 188 TypeElement te = sourceUtils.getTypeElement(); 189 List <? extends Element> members = te.getEnclosedElements(); 190 List <ExecutableElement> methods = ElementFilter.methodsIn(members); 191 for (ExecutableElement method:methods) { 192 Set <Modifier> modifiers = method.getModifiers(); 193 if (modifiers.contains(Modifier.PUBLIC)) { 194 List <? extends AnnotationMirror> annotations = method.getAnnotationMirrors(); 195 boolean hasWebMethodAnnotation=false; 196 String nameAnnot = null; 197 for (AnnotationMirror an:annotations) { 198 TypeElement webMethodEl = controller.getElements().getTypeElement("javax.jws.WebMethod"); if (webMethodEl!=null && controller.getTypes().isSameType(webMethodEl.asType(), an.getAnnotationType())) { 200 hasWebMethodAnnotation=true; 201 Map <? extends ExecutableElement, ? extends AnnotationValue> expressions = an.getElementValues(); 202 for(ExecutableElement ex:expressions.keySet()) { 203 if (ex.getSimpleName().contentEquals("operationName")) { nameAnnot = (String )expressions.get(ex).getValue(); 205 if (nameAnnot!=null) nameAnnot = URLEncoder.encode(nameAnnot,"UTF-8"); } 207 if (nameAnnot!=null) break; 208 } 209 break; 210 } 211 } 212 String opName = method.getSimpleName().toString(); 213 if ((hasWebMethodAnnotation) && (nameAnnot != null)) { 214 opName = nameAnnot; 215 } 216 if (operationName.equals(opName)) { 217 List <? extends TypeMirror> excs = method.getThrownTypes(); 218 for (TypeMirror ex:excs) { 219 String tName = getTypeName(controller, ex); 220 if (tName != null){ 221 faults.add(tName); 222 } 223 } 224 } 225 } 226 } 227 } 228 }, true); 229 } catch (IOException ex) { 230 Exceptions.printStackTrace(ex); 231 } 232 return faults; 233 } 234 235 static String getTypeName(CompilationController controller, TypeMirror typeMirror) { 236 TypeKind typeKind = typeMirror.getKind(); 237 switch (typeKind) { 238 case BOOLEAN : return "boolean"; case BYTE : return "byte"; case CHAR : return "char"; case DOUBLE : return "double"; case FLOAT : return "float"; case INT : return "int"; case LONG : return "long"; case SHORT : return "short"; case VOID : return "void"; case DECLARED : 248 Element element = controller.getTypes().asElement(typeMirror); 249 return ((TypeElement) element).getSimpleName().toString(); 250 case ARRAY : 251 ArrayType arrayType = (ArrayType) typeMirror; 252 Element componentTypeElement = controller.getTypes().asElement(arrayType.getComponentType()); 253 return ((TypeElement) componentTypeElement).getSimpleName().toString() + "[]"; 254 case ERROR : 255 case EXECUTABLE : 256 case NONE : 257 case NULL : 258 case OTHER : 259 case PACKAGE : 260 case TYPEVAR : 261 case WILDCARD : 262 default:break; 263 } 264 return null; 265 } 266 267 public static List <String > getOperationNames(FileObject implClass) { 268 final List <String > operations = new ArrayList (); 269 if (implClass == null) return null; 270 try { 271 JavaSource js = JavaSource.forFileObject(implClass); 272 js.runUserActionTask(new AbstractTask<CompilationController>() { 273 public void run(CompilationController controller) throws java.io.IOException { 274 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 275 SourceUtils sourceUtils = SourceUtils.newInstance(controller); 276 TypeElement te = sourceUtils.getTypeElement(); 277 278 List <? extends Element> members = te.getEnclosedElements(); 279 List <ExecutableElement> methods = ElementFilter.methodsIn(members); 280 boolean foundWebMethodAnnotation=false; 281 for (ExecutableElement method:methods) { 282 Set <Modifier> modifiers = method.getModifiers(); 283 if (modifiers.contains(Modifier.PUBLIC)) { 284 List <? extends AnnotationMirror> annotations = method.getAnnotationMirrors(); 285 boolean hasWebMethodAnnotation=false; 286 String nameAnnot = null; 287 for (AnnotationMirror an:annotations) { 288 TypeElement webMethodEl = controller.getElements().getTypeElement("javax.jws.WebMethod"); if (webMethodEl!=null && controller.getTypes().isSameType(webMethodEl.asType(), an.getAnnotationType())) { 290 hasWebMethodAnnotation=true; 291 Map <? extends ExecutableElement, ? extends AnnotationValue> expressions = an.getElementValues(); 292 for(ExecutableElement ex:expressions.keySet()) { 293 if (ex.getSimpleName().contentEquals("operationName")) { nameAnnot = (String )expressions.get(ex).getValue(); 295 if (nameAnnot!=null) nameAnnot = URLEncoder.encode(nameAnnot,"UTF-8"); } 297 if (nameAnnot!=null) break; 298 } 299 break; 300 } 301 } 302 if (hasWebMethodAnnotation) { 303 if (!foundWebMethodAnnotation) { 304 foundWebMethodAnnotation=true; 305 if (operations.size()>0) operations.clear(); 307 } 308 if (nameAnnot != null) { 309 operations.add(nameAnnot); 310 } else { 311 operations.add(method.getSimpleName().toString()); 312 } 313 } else { 314 operations.add(method.getSimpleName().toString()); 316 } 317 } 318 } 319 } 320 }, true); 321 } catch (IOException ex) { 322 Exceptions.printStackTrace(ex); 323 } 324 return operations; 325 } 326 327 public static QName getPortTypeName(FileObject implClass) { 328 final java.lang.String [] portTypeQNameARR = new String [2]; 329 if (implClass == null) return null; 330 try { 331 JavaSource js = JavaSource.forFileObject(implClass); 332 js.runUserActionTask(new AbstractTask<CompilationController>() { 333 public void run(CompilationController controller) throws java.io.IOException { 334 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 335 SourceUtils sourceUtils = SourceUtils.newInstance(controller); 336 TypeElement te = sourceUtils.getTypeElement(); 337 portTypeQNameARR[0] = te.getSimpleName().toString(); 338 339 List <? extends AnnotationMirror> annotations = te.getAnnotationMirrors(); 340 for (AnnotationMirror m : annotations) { 341 Name qualifiedName = ((TypeElement)m.getAnnotationType().asElement()).getQualifiedName(); 342 if (qualifiedName.contentEquals("javax.jws.WebService")) { String nameAnnot = null; 344 String targetNamespaceAnnot = null; 345 346 Map <? extends ExecutableElement, ? extends AnnotationValue> expressions = m.getElementValues(); 347 for(ExecutableElement ex:expressions.keySet()) { 348 if (ex.getSimpleName().contentEquals("name")) { nameAnnot = (String )expressions.get(ex).getValue(); 350 if (nameAnnot!=null) nameAnnot = URLEncoder.encode(nameAnnot,"UTF-8"); } else if (ex.getSimpleName().contentEquals("targetNamespace")) { targetNamespaceAnnot = (String )expressions.get(ex).getValue(); 353 if (targetNamespaceAnnot!=null) targetNamespaceAnnot = URLEncoder.encode(targetNamespaceAnnot,"UTF-8"); } 355 if (targetNamespaceAnnot!=null && nameAnnot!=null) break; 356 } 357 358 if ((nameAnnot != null) && (nameAnnot.length() > 0)) { 359 portTypeQNameARR[0] = nameAnnot; 360 } 361 362 String pkg = getPackageFromClass(te.getQualifiedName().toString()); 363 364 String targetNamespace = targetNamespaceAnnot; 365 if ((targetNamespace == null) || (targetNamespace.length() == 0)) { 366 targetNamespace = getNamespace(pkg); 367 } 368 369 portTypeQNameARR[1] = targetNamespace; 370 } 371 } 372 } 373 }, true); 374 } catch (IOException ex) { 375 Exceptions.printStackTrace(ex); 376 } 377 return new QName (portTypeQNameARR[1], portTypeQNameARR[0]); 378 } 379 380 public static String getWsdlLocation(FileObject implClass) { 381 final java.lang.String [] wsdlLocARR = new String [1]; 382 try { 383 if (implClass == null) return null; 384 385 JavaSource js = JavaSource.forFileObject(implClass); 386 js.runUserActionTask(new AbstractTask<CompilationController>() { 387 public void run(CompilationController controller) throws java.io.IOException { 388 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 389 SourceUtils sourceUtils = SourceUtils.newInstance(controller); 390 TypeElement te = sourceUtils.getTypeElement(); 391 List <? extends AnnotationMirror> annotations = te.getAnnotationMirrors(); 392 for (AnnotationMirror m : annotations) { 393 Name qualifiedName = ((TypeElement)m.getAnnotationType().asElement()).getQualifiedName(); 394 if (qualifiedName.contentEquals("javax.jws.WebService")) { String wsdlLoc = null; 396 Map <? extends ExecutableElement, ? extends AnnotationValue> expressions = m.getElementValues(); 397 for(ExecutableElement ex:expressions.keySet()) { 398 if (ex.getSimpleName().contentEquals("wsdlLocation")) { wsdlLoc = (String )expressions.get(ex).getValue(); 400 if (wsdlLoc!=null) wsdlLoc = URLEncoder.encode(wsdlLoc,"UTF-8"); } 402 if (wsdlLoc != null) break; 403 } 404 wsdlLocARR[1] = wsdlLoc; 405 } 406 } 407 } 408 }, true); 409 } catch (IOException ex) { 410 Exceptions.printStackTrace(ex); 411 } 412 return wsdlLocARR[0]; 413 } 414 415 421 public static QName getPortName(FileObject implClass, final String targetNamespace) { 422 423 final java.lang.String [] portNameQNameARR = new String [2]; 424 if (implClass == null) return null; 425 try { 426 JavaSource js = JavaSource.forFileObject(implClass); 427 js.runUserActionTask(new AbstractTask<CompilationController>() { 428 public void run(CompilationController controller) throws java.io.IOException { 429 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 430 SourceUtils sourceUtils = SourceUtils.newInstance(controller); 431 TypeElement te = sourceUtils.getTypeElement(); 432 String className = te.getSimpleName().toString(); 433 List <? extends AnnotationMirror> annotations = te.getAnnotationMirrors(); 434 for (AnnotationMirror m : annotations) { 435 Name qualifiedName = ((TypeElement)m.getAnnotationType().asElement()).getQualifiedName(); 436 if (qualifiedName.contentEquals("javax.jws.WebService")) { String portNameAnnot = null; 438 String nameAnnot = null; 439 String targetNamespaceAnnot = null; 440 441 Map <? extends ExecutableElement, ? extends AnnotationValue> expressions = m.getElementValues(); 442 for(ExecutableElement ex:expressions.keySet()) { 443 if (ex.getSimpleName().contentEquals("name")) { nameAnnot = (String )expressions.get(ex).getValue(); 445 if (nameAnnot!=null) nameAnnot = URLEncoder.encode(nameAnnot,"UTF-8"); } else if (ex.getSimpleName().contentEquals("portName")) { portNameAnnot = (String )expressions.get(ex).getValue(); 448 if (portNameAnnot!=null) portNameAnnot = URLEncoder.encode(portNameAnnot,"UTF-8"); } else if (ex.getSimpleName().contentEquals("targetNamespace")) { targetNamespaceAnnot = (String )expressions.get(ex).getValue(); 451 if (targetNamespaceAnnot!=null) targetNamespaceAnnot = URLEncoder.encode(targetNamespaceAnnot,"UTF-8"); } 453 if (targetNamespaceAnnot!=null && nameAnnot!=null && portNameAnnot != null) break; 454 } 455 456 if ((portNameAnnot != null) && (portNameAnnot.length() > 0)) { 457 portNameQNameARR[0] = portNameAnnot; 458 } else if ((nameAnnot != null) && (nameAnnot.length() > 0)) { 459 portNameQNameARR[0] = nameAnnot + PORT; 460 } else { 461 portNameQNameARR[0] = className + PORT; 462 } 463 464 if (targetNamespace == null) { 465 if ((targetNamespaceAnnot != null) && (targetNamespaceAnnot.length() > 0)) { 466 portNameQNameARR[1] = targetNamespaceAnnot; 467 } else { 468 String packageName = getPackageFromClass(className); 469 portNameQNameARR[1] = getNamespace(packageName); 470 } 471 } 472 } 473 } 474 } 475 }, true); 476 } catch (IOException ex) { 477 Exceptions.printStackTrace(ex); 478 } 479 return new QName (portNameQNameARR[1], portNameQNameARR[0]); 480 } 481 482 private static String getPackageFromClass(String fqClassName) { 483 return fqClassName.substring(0, fqClassName.lastIndexOf('.')); 484 } 485 486 } | Popular Tags |