KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > jaxwsruntimemodel > JavaWsdlMapper


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.websvc.jaxwsruntimemodel;
21
22 import java.io.IOException JavaDoc;
23 import java.net.URLEncoder JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
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 JavaDoc;
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 /**
50  * This class is compiled from:
51  * com.sun.xml.ws.model.RuntimeModeler
52  * com.sun.xml.ws.model.SEIModel
53  * com.sun.xml.ws.model.AbstractSEIModelImpl
54  * com.sun.xml.ws.wsdl.writer.WSDLGenerator
55  * in JAX-WS component.
56  * @author Martin Grebac
57  */

58 public class JavaWsdlMapper {
59     
60     public static final String JavaDoc RESPONSE = "Response"; //NOI18N
61
public static final String JavaDoc RETURN = "return"; //NOI18N
62
public static final String JavaDoc BEAN = "Bean"; //NOI18N
63
public static final String JavaDoc SERVICE = "Service"; //NOI18N
64
public static final String JavaDoc PORT = "Port"; //NOI18N
65
public static final String JavaDoc PORT_TYPE = "PortType"; //NOI18N
66
public static final String JavaDoc BINDING = "Binding"; //NOI18N
67

68     /** Creates a new instance of JavaWsdlMapper */
69     public JavaWsdlMapper() { }
70     
71     /**
72      * gets the namespace <code>String</code> for a given <code>packageName</code>
73      * @param packageName the name of the package used to find a namespace
74      * @return the namespace for the specified <code>packageName</code>
75      */

76     public static String JavaDoc getNamespace(String JavaDoc packageName) {
77         if (packageName == null || packageName.length() == 0)
78             return null;
79
80         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(packageName, ".");
81         String JavaDoc[] tokens;
82         if (tokenizer.countTokens() == 0) {
83             tokens = new String JavaDoc[0];
84         } else {
85             tokens = new String JavaDoc[tokenizer.countTokens()];
86             for (int i=tokenizer.countTokens()-1; i >= 0; i--) {
87                 tokens[i] = tokenizer.nextToken();
88             }
89         }
90         StringBuilder JavaDoc namespace = new StringBuilder JavaDoc("http://"); //NOI18N
91
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     /**
102      * gets the <code>wsdl:serviceName</code> for a given implementation class
103      * @param implClass the implementation class
104      * @return the <code>wsdl:serviceName</code> for the <code>implClass</code>
105      */

106     public static QName JavaDoc getServiceName(FileObject implClass) {
107
108         final java.lang.String JavaDoc[] serviceNameQNameARR = new String JavaDoc[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 JavaDoc {
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 JavaDoc packageName = getPackageFromClass(te.getQualifiedName().toString());
120                      serviceNameQNameARR[1] = getNamespace(packageName);
121
122                      List JavaDoc<? 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")) { //NOI18N
126
String JavaDoc serviceNameAnnot = null;
127                             String JavaDoc targetNamespaceAnnot = null;
128
129                             Map JavaDoc<? extends ExecutableElement, ? extends AnnotationValue> expressions = m.getElementValues();
130                             for(ExecutableElement ex:expressions.keySet()) {
131                                 if (ex.getSimpleName().contentEquals("serviceName")) { //NOI18N
132
serviceNameAnnot = (String JavaDoc)expressions.get(ex).getValue();
133                                     if (serviceNameAnnot!=null) serviceNameAnnot = URLEncoder.encode(serviceNameAnnot,"UTF-8"); //NOI18N
134
} else if (ex.getSimpleName().contentEquals("targetNamespace")) { //NOI18N
135
targetNamespaceAnnot = (String JavaDoc)expressions.get(ex).getValue();
136                                     if (targetNamespaceAnnot!=null) targetNamespaceAnnot = URLEncoder.encode(targetNamespaceAnnot,"UTF-8"); //NOI18N
137
}
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 JavaDoc ex) {
151             Exceptions.printStackTrace(ex);
152         }
153         return new QName JavaDoc(serviceNameQNameARR[1], serviceNameQNameARR[0]);
154     }
155
156     /**
157      * gets the <code>wsdl:portName</code> for a given implementation class
158      * @param implClass the implementation class
159      * @param targetNamespace Namespace URI for service name
160      * @return the <code>wsdl:portName</code> for the <code>implClass</code>
161      */

162     public static String JavaDoc getBindingName(FileObject implClass, String JavaDoc targetNamespace) {
163         QName JavaDoc 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 JavaDoc<String JavaDoc> getOperationFaults(FileObject implClass, final String JavaDoc operationName) {
176         if ((implClass == null) || (operationName == null)) {
177              return Collections.EMPTY_LIST;
178         }
179                 
180         final List JavaDoc<String JavaDoc> faults = new ArrayList JavaDoc();
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 JavaDoc {
186                      controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
187                      SourceUtils sourceUtils = SourceUtils.newInstance(controller);
188                      TypeElement te = sourceUtils.getTypeElement();
189                      List JavaDoc<? extends Element> members = te.getEnclosedElements();
190                      List JavaDoc<ExecutableElement> methods = ElementFilter.methodsIn(members);
191                      for (ExecutableElement method:methods) {
192                         Set JavaDoc<Modifier> modifiers = method.getModifiers();
193                         if (modifiers.contains(Modifier.PUBLIC)) {
194                             List JavaDoc<? extends AnnotationMirror> annotations = method.getAnnotationMirrors();
195                             boolean hasWebMethodAnnotation=false;
196                             String JavaDoc nameAnnot = null;
197                             for (AnnotationMirror an:annotations) {
198                                 TypeElement webMethodEl = controller.getElements().getTypeElement("javax.jws.WebMethod"); //NOI18N
199
if (webMethodEl!=null && controller.getTypes().isSameType(webMethodEl.asType(), an.getAnnotationType())) {
200                                     hasWebMethodAnnotation=true;
201                                     Map JavaDoc<? extends ExecutableElement, ? extends AnnotationValue> expressions = an.getElementValues();
202                                     for(ExecutableElement ex:expressions.keySet()) {
203                                         if (ex.getSimpleName().contentEquals("operationName")) { //NOI18N
204
nameAnnot = (String JavaDoc)expressions.get(ex).getValue();
205                                             if (nameAnnot!=null) nameAnnot = URLEncoder.encode(nameAnnot,"UTF-8"); //NOI18N
206
}
207                                         if (nameAnnot!=null) break;
208                                     }
209                                     break;
210                                 }
211                             }
212                             String JavaDoc opName = method.getSimpleName().toString();
213                             if ((hasWebMethodAnnotation) && (nameAnnot != null)) {
214                                 opName = nameAnnot;
215                             }
216                             if (operationName.equals(opName)) {
217                                 List JavaDoc<? extends TypeMirror> excs = method.getThrownTypes();
218                                 for (TypeMirror ex:excs) {
219                                     String JavaDoc tName = getTypeName(controller, ex);
220                                     if (tName != null){
221                                         faults.add(tName);
222                                     }
223                                 }
224                             }
225                         }
226                      }
227                 }
228             }, true);
229         } catch (IOException JavaDoc ex) {
230             Exceptions.printStackTrace(ex);
231         }
232         return faults;
233     }
234
235     static String JavaDoc getTypeName(CompilationController controller, TypeMirror typeMirror) {
236         TypeKind typeKind = typeMirror.getKind();
237         switch (typeKind) {
238             case BOOLEAN : return "boolean"; // NOI18N
239
case BYTE : return "byte"; // NOI18N
240
case CHAR : return "char"; // NOI18N
241
case DOUBLE : return "double"; // NOI18N
242
case FLOAT : return "float"; // NOI18N
243
case INT : return "int"; // NOI18N
244
case LONG : return "long"; // NOI18N
245
case SHORT : return "short"; // NOI18N
246
case VOID : return "void"; // NOI18N
247
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 JavaDoc<String JavaDoc> getOperationNames(FileObject implClass) {
268         final List JavaDoc<String JavaDoc> operations = new ArrayList JavaDoc();
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 JavaDoc {
274                      controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
275                      SourceUtils sourceUtils = SourceUtils.newInstance(controller);
276                      TypeElement te = sourceUtils.getTypeElement();
277
278                      List JavaDoc<? extends Element> members = te.getEnclosedElements();
279                      List JavaDoc<ExecutableElement> methods = ElementFilter.methodsIn(members);
280                      boolean foundWebMethodAnnotation=false;
281                      for (ExecutableElement method:methods) {
282                         Set JavaDoc<Modifier> modifiers = method.getModifiers();
283                         if (modifiers.contains(Modifier.PUBLIC)) {
284                             List JavaDoc<? extends AnnotationMirror> annotations = method.getAnnotationMirrors();
285                             boolean hasWebMethodAnnotation=false;
286                             String JavaDoc nameAnnot = null;
287                             for (AnnotationMirror an:annotations) {
288                                 TypeElement webMethodEl = controller.getElements().getTypeElement("javax.jws.WebMethod"); //NOI18N
289
if (webMethodEl!=null && controller.getTypes().isSameType(webMethodEl.asType(), an.getAnnotationType())) {
290                                     hasWebMethodAnnotation=true;
291                                     Map JavaDoc<? extends ExecutableElement, ? extends AnnotationValue> expressions = an.getElementValues();
292                                     for(ExecutableElement ex:expressions.keySet()) {
293                                         if (ex.getSimpleName().contentEquals("operationName")) { //NOI18N
294
nameAnnot = (String JavaDoc)expressions.get(ex).getValue();
295                                             if (nameAnnot!=null) nameAnnot = URLEncoder.encode(nameAnnot,"UTF-8"); //NOI18N
296
}
297                                         if (nameAnnot!=null) break;
298                                     }
299                                     break;
300                                 }
301                             }
302                             if (hasWebMethodAnnotation) {
303                                 if (!foundWebMethodAnnotation) {
304                                     foundWebMethodAnnotation=true;
305                                     // remove all methods added before because only annotated methods should be added
306
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                                 // there are only non-annotated methods present until now
315
operations.add(method.getSimpleName().toString());
316                             }
317                         }
318                      }
319                 }
320             }, true);
321         } catch (IOException JavaDoc ex) {
322             Exceptions.printStackTrace(ex);
323         }
324         return operations;
325     }
326     
327     public static QName JavaDoc getPortTypeName(FileObject implClass) {
328         final java.lang.String JavaDoc[] portTypeQNameARR = new String JavaDoc[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 JavaDoc {
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 JavaDoc<? 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")) { //NOI18N
343
String JavaDoc nameAnnot = null;
344                             String JavaDoc targetNamespaceAnnot = null;
345
346                             Map JavaDoc<? extends ExecutableElement, ? extends AnnotationValue> expressions = m.getElementValues();
347                             for(ExecutableElement ex:expressions.keySet()) {
348                                 if (ex.getSimpleName().contentEquals("name")) { //NOI18N
349
nameAnnot = (String JavaDoc)expressions.get(ex).getValue();
350                                     if (nameAnnot!=null) nameAnnot = URLEncoder.encode(nameAnnot,"UTF-8"); //NOI18N
351
} else if (ex.getSimpleName().contentEquals("targetNamespace")) { //NOI18N
352
targetNamespaceAnnot = (String JavaDoc)expressions.get(ex).getValue();
353                                     if (targetNamespaceAnnot!=null) targetNamespaceAnnot = URLEncoder.encode(targetNamespaceAnnot,"UTF-8"); //NOI18N
354
}
355                                 if (targetNamespaceAnnot!=null && nameAnnot!=null) break;
356                             }
357
358                             if ((nameAnnot != null) && (nameAnnot.length() > 0)) {
359                                 portTypeQNameARR[0] = nameAnnot;
360                             }
361
362                             String JavaDoc pkg = getPackageFromClass(te.getQualifiedName().toString());
363
364                             String JavaDoc 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 JavaDoc ex) {
375             Exceptions.printStackTrace(ex);
376         }
377         return new QName JavaDoc(portTypeQNameARR[1], portTypeQNameARR[0]);
378     }
379     
380     public static String JavaDoc getWsdlLocation(FileObject implClass) {
381         final java.lang.String JavaDoc[] wsdlLocARR = new String JavaDoc[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 JavaDoc {
388                      controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
389                      SourceUtils sourceUtils = SourceUtils.newInstance(controller);
390                      TypeElement te = sourceUtils.getTypeElement();
391                      List JavaDoc<? 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")) { //NOI18N
395
String JavaDoc wsdlLoc = null;
396                             Map JavaDoc<? extends ExecutableElement, ? extends AnnotationValue> expressions = m.getElementValues();
397                             for(ExecutableElement ex:expressions.keySet()) {
398                                 if (ex.getSimpleName().contentEquals("wsdlLocation")) { //NOI18N
399
wsdlLoc = (String JavaDoc)expressions.get(ex).getValue();
400                                     if (wsdlLoc!=null) wsdlLoc = URLEncoder.encode(wsdlLoc,"UTF-8"); //NOI18N
401
}
402                                 if (wsdlLoc != null) break;
403                             }
404                             wsdlLocARR[1] = wsdlLoc;
405                         }
406                     }
407                 }
408             }, true);
409         } catch (IOException JavaDoc ex) {
410             Exceptions.printStackTrace(ex);
411         }
412         return wsdlLocARR[0];
413     }
414     
415     /**
416      * gets the <code>wsdl:portName</code> for a given implementation class
417      * @param implClass the implementation class
418      * @param targetNamespace Namespace URI for service name
419      * @return the <code>wsdl:portName</code> for the <code>implClass</code>
420      */

421     public static QName JavaDoc getPortName(FileObject implClass, final String JavaDoc targetNamespace) {
422
423         final java.lang.String JavaDoc[] portNameQNameARR = new String JavaDoc[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 JavaDoc {
429                      controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
430                      SourceUtils sourceUtils = SourceUtils.newInstance(controller);
431                      TypeElement te = sourceUtils.getTypeElement();
432                      String JavaDoc className = te.getSimpleName().toString();
433                      List JavaDoc<? 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")) { //NOI18N
437
String JavaDoc portNameAnnot = null;
438                             String JavaDoc nameAnnot = null;
439                             String JavaDoc targetNamespaceAnnot = null;
440
441                             Map JavaDoc<? extends ExecutableElement, ? extends AnnotationValue> expressions = m.getElementValues();
442                             for(ExecutableElement ex:expressions.keySet()) {
443                                 if (ex.getSimpleName().contentEquals("name")) { //NOI18N
444
nameAnnot = (String JavaDoc)expressions.get(ex).getValue();
445                                     if (nameAnnot!=null) nameAnnot = URLEncoder.encode(nameAnnot,"UTF-8"); //NOI18N
446
} else if (ex.getSimpleName().contentEquals("portName")) { //NOI18N
447
portNameAnnot = (String JavaDoc)expressions.get(ex).getValue();
448                                     if (portNameAnnot!=null) portNameAnnot = URLEncoder.encode(portNameAnnot,"UTF-8"); //NOI18N
449
} else if (ex.getSimpleName().contentEquals("targetNamespace")) { //NOI18N
450
targetNamespaceAnnot = (String JavaDoc)expressions.get(ex).getValue();
451                                     if (targetNamespaceAnnot!=null) targetNamespaceAnnot = URLEncoder.encode(targetNamespaceAnnot,"UTF-8"); //NOI18N
452
}
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 JavaDoc packageName = getPackageFromClass(className);
469                                     portNameQNameARR[1] = getNamespace(packageName);
470                                 }
471                             }
472                         }
473                     }
474                 }
475             }, true);
476         } catch (IOException JavaDoc ex) {
477             Exceptions.printStackTrace(ex);
478         }
479         return new QName JavaDoc(portNameQNameARR[1], portNameQNameARR[0]);
480     }
481     
482     private static String JavaDoc getPackageFromClass(String JavaDoc fqClassName) {
483         return fqClassName.substring(0, fqClassName.lastIndexOf('.'));
484     }
485
486 }
Popular Tags