KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > registry > wsdl > WSDLInfo


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 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.websvc.registry.wsdl;
21
22 import org.openide.ErrorManager;
23 import org.netbeans.modules.websvc.registry.jaxrpc.Wsdl2Java;
24 import org.netbeans.modules.websvc.registry.model.WebServiceData;
25 import org.netbeans.modules.websvc.registry.nodes.WebServicesPortNode;
26 import org.netbeans.modules.websvc.registry.util.Util;
27 import com.sun.xml.rpc.processor.ProcessorOptions;
28 import com.sun.xml.rpc.processor.config.Configuration;
29 import com.sun.xml.rpc.processor.config.WSDLModelInfo;
30 import com.sun.xml.rpc.processor.model.Operation;
31 import com.sun.xml.rpc.processor.model.Port;
32 import com.sun.xml.rpc.processor.model.java.JavaMethod;
33 import com.sun.xml.rpc.processor.model.java.JavaParameter;
34 import com.sun.xml.rpc.processor.modeler.ModelerException;
35 import com.sun.xml.rpc.processor.modeler.wsdl.WSDLModelerBase;
36 import com.sun.xml.rpc.processor.util.ClientProcessorEnvironment;
37 import com.sun.xml.rpc.spi.model.JavaInterface;
38 import com.sun.xml.rpc.spi.model.Model;
39 import com.sun.xml.rpc.spi.model.Service;
40 import com.sun.xml.rpc.util.JAXRPCClassFactory;
41 import java.io.ByteArrayOutputStream JavaDoc;
42 import java.io.File JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.StringWriter JavaDoc;
45 import java.net.URL JavaDoc;
46 import java.util.HashSet JavaDoc;
47 import java.util.Iterator JavaDoc;
48 import java.util.Properties JavaDoc;
49 import java.util.Set JavaDoc;
50 import org.openide.util.NbBundle;
51
52
53
54
55
56
57 /**
58  *
59  * @author Winston Prakash
60  */

61 public class WSDLInfo {
62     
63     File JavaDoc errorFile;
64     private String JavaDoc program ="wscompile";
65     private String JavaDoc outputDir;
66     private String JavaDoc nonClassOutputDir;
67     private String JavaDoc sourceOutputDir;
68     private URL JavaDoc wsdlUrl;
69     private String JavaDoc packageName="";
70     private String JavaDoc errorMessage;
71     private boolean hasOperations;
72     
73     /**
74      * The following is taken from com.sun.xml.rpc.processor.modeler.wsdl.WSDLModelerBase where it's used to set
75      * the documentation property on Model.
76      */

77     private static final String JavaDoc WSDL_DOCUMENTATION = "com.sun.xml.rpc.processor.modeler.wsdl.documentation";
78     
79     public static final String JavaDoc SERVICE_DISPLAYNAME_TOKEN = "%%DISPLAY_NAME%%";
80     public static final String JavaDoc TOKEN_SEPARATOR = "@";
81     public static final String JavaDoc SIG_SEPARATOR = "|";
82     public static final String JavaDoc RETURN_SEPARATOR = "!";
83     public static final String JavaDoc PARAM_SEPARATOR = ":";
84     
85     private Model wsdlModel;
86     Wsdl2Java wsdl2Java;
87     
88     boolean removeGeneratedFiles = true;
89     
90     public WSDLInfo() {
91         try{
92             errorFile = File.createTempFile("wscompile","error");
93         }catch(IOException JavaDoc exc){
94             ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, exc);
95         }
96     }
97     
98     public boolean hasOperations() {
99         return hasOperations;
100     }
101     
102     public void setWsdlUrl(URL JavaDoc url){
103         wsdlUrl = url;
104     }
105     
106     public URL JavaDoc getWsdlUrl(){
107         return wsdlUrl;
108     }
109     
110     public void setPackageName(String JavaDoc pkgName){
111         packageName = pkgName;
112     }
113     
114     public String JavaDoc getPackageName(){
115         if(packageName == null) {
116             packageName = Wsdl2Java.DEFAULT_TARGET_PACKAGE;
117         }
118         return packageName;
119     }
120     
121     public String JavaDoc getErrorMessage(){
122         StringWriter JavaDoc errorWriter = new StringWriter JavaDoc();
123         StringBuffer JavaDoc strBuffer = new StringBuffer JavaDoc();
124         errorWriter.write("<B>" + NbBundle.getMessage(this.getClass(), "WSDLINFO_ERROR_PARSING") + " </B>");
125         errorWriter.write("<BR><B><I>" + errorMessage + "</I></B>");
126         return errorWriter.toString();
127     }
128     
129     public void setOutputDirectory(String JavaDoc dir){
130         outputDir = dir;
131     }
132     
133     public String JavaDoc getOutputDirectory(){
134         if (outputDir == null){
135             File JavaDoc outDir = new File JavaDoc(System.getProperty("user.home"));
136             return outDir.getAbsolutePath();
137         }
138         return outputDir;
139     }
140     
141     public void setRemoveGeneratedFiles(boolean remove){
142         removeGeneratedFiles = remove;
143     }
144     
145     public boolean create() {
146         /*
147         wsdl2Java = new Wsdl2Java();
148         wsdl2Java.setOutputDirectory(getOutputDirectory());
149         wsdl2Java.setWsdlUrl( getWsdlUrl());
150         if(!wsdl2Java.execute(errorFile)) {
151             return false;
152         }
153         //if (removeGeneratedFiles) removeTemporaryFiles()
154          
155          */

156         
157         /**
158          * First we need to get create a JAXRPC WSDLModeler
159          */

160         
161         WSDLModelInfo modelInfo = new WSDLModelInfo();
162         ClientProcessorEnvironment env = new ClientProcessorEnvironment(new ByteArrayOutputStream JavaDoc(), null, null);
163         
164         Configuration config = new Configuration(env);
165         
166         modelInfo = new WSDLModelInfo();
167         
168         config.setModelInfo(modelInfo);
169         modelInfo.setParent(config);
170         
171         
172         modelInfo.setLocation(getWsdlUrl().toExternalForm());
173         Properties JavaDoc options = new Properties JavaDoc();
174         options.put(ProcessorOptions.VALIDATE_WSDL_PROPERTY, "true");
175         options.put(ProcessorOptions.SEARCH_SCHEMA_FOR_SUBTYPES, "true");
176         options.put(ProcessorOptions.USE_WSI_BASIC_PROFILE, "false");
177         
178         
179         WSDLModelerBase modeler = JAXRPCClassFactory.newInstance().createWSDLModeler(modelInfo, options);
180         
181         /**
182          * Now that we have the modeler, we need to build a model.
183          */

184         Model tmpModel = null;
185         try {
186             tmpModel = modeler.buildModel();
187         } catch(/*Modeler*/Exception JavaDoc me) {
188             /**
189              * try to find a ParseException in the nested exceptions to get the cause
190              */

191             errorMessage = " Exception = " + me.getLocalizedMessage();;
192             return false;
193         }
194         
195         this.setWSDLModel(tmpModel);
196         
197         return true;
198     }
199     
200     private void setWSDLModel(Model inModel) {
201         wsdlModel = inModel;
202     }
203     
204     public Set JavaDoc getWebServices(){
205         Set JavaDoc webServices = new HashSet JavaDoc();
206         Model model = getWSDLModel();
207         if(null != model) {
208             for (Iterator JavaDoc services = model.getServices(); services.hasNext(); ) {
209                 WebServiceData wsData = new WebServiceData() ;
210                 wsData.setURL(getWsdlUrl().toString());
211                 Service service = (Service) services.next();
212                 
213                 String JavaDoc serviceName = service.getJavaIntf().getName();
214                 wsData.setName(serviceName);
215                 serviceName = serviceName.substring(serviceName.lastIndexOf('.') + 1, serviceName.length());
216                 String JavaDoc hotsName = getWsdlUrl().getHost();
217                 String JavaDoc displayName = serviceName;
218                 wsData.setDisplayName(displayName);
219                 
220                 Iterator JavaDoc ports = service.getPorts();
221                 while (ports.hasNext()) {
222                     Port port = (Port)ports.next();
223                     /**
224                      * We need to set a property on the Port for the name because QNAME is not a persistable
225                      * as a JavaBean.
226                      */

227                     port.setProperty(WebServiceData.PORT_PROPERTY_NAME, port.getName().getLocalPart());
228                     wsData.addPort(port);
229                     wsData.setWebServiceAddress(port.getAddress());
230                     String JavaDoc portName = port.getJavaInterface().getRealName();
231                     portName = portName.substring(portName.lastIndexOf('.') + 1, portName.length());
232                     wsData.setProxy(portName);
233                     /**
234                      * Iterator operations = port.getOperations();
235                      * int methodCount=0;
236                      * while (operations.hasNext()) {
237                      * Operation operation = (Operation) operations.next();
238                      * JavaMethod method = operation.getJavaMethod();
239                      * wsData.addMethod(method.getName());
240                      * }
241                      */

242                 }
243                 webServices.add(wsData);
244             }
245         }
246         
247         return webServices;
248     }
249     
250     private Model getWSDLModel() {
251         return wsdlModel;
252     }
253     /**
254      * This method will use the Model to generated service information HTML. The service DISPLAY_NAME
255      * will be a token that expected to ge set by the calling class to allow the caller of this method to
256      * control the display name.
257      */

258     public String JavaDoc getServiceInfo(String JavaDoc inServiceName) {
259         StringWriter JavaDoc strWriter = new StringWriter JavaDoc();
260         
261         
262         Model model = getWSDLModel();
263         if(null != model) {
264             Iterator JavaDoc services = model.getServices();
265             for (int ii=0; services.hasNext();ii++ ) {
266                 Service service = (Service) services.next();
267                 String JavaDoc serviceName = service.getJavaIntf().getName();
268                 if(serviceName.equals(inServiceName)) {
269                     serviceName = serviceName.substring(serviceName.lastIndexOf('.') + 1, serviceName.length());
270                     strWriter.write("<a name=\"TOPOFPAGE\">&nbsp</a>");
271 // !HIE strWriter.write("<FORM NAME=\"service_form." + serviceName + "\" BGCOLOR=\"#e6e6ff\"><P>");
272
strWriter.write("<FORM NAME=\"service_form." + serviceName + "\">");
273                     strWriter.write("<TABLE><TR>");
274                     strWriter.write("<TD VALIGN=TOP><B>" + NbBundle.getMessage(this.getClass(), "SERVICE_WSDL_NAME") + "</B></TD>");
275                     strWriter.write("<TD VALIGN=TOP>" + serviceName + "</TD>");
276                     strWriter.write("</TR><TR>");
277                     strWriter.write("<TD VALIGN=TOP><LABEL FOR=\"service_name." + serviceName +"\" ><B>"
278                     + NbBundle.getMessage(this.getClass(), "SERVICE_DISPLAY_NAME") + "</B></LABEL></TD>");
279                     strWriter.write("<TD VALIGN=TOP><INPUT TYPE=TEXT ID=\"service_name." + serviceName + "\" NAME=\"service_name."
280                     + serviceName + "\" VALUE=\"" +
281                     this.SERVICE_DISPLAYNAME_TOKEN +
282                     "\" STYLE=\"background: #ade7ff\"></TD>");
283                     strWriter.write("</TR><TR>");
284                     strWriter.write("<TD VALIGN=TOP><LABEL FOR=\"package_name." + serviceName +"\" ><B>"
285                     + NbBundle.getMessage(this.getClass(), "CLIENT_PACKAGE_NAME") + "</B></LABEL></TD>");
286                     strWriter.write("<TD VALIGN=TOP><INPUT TYPE=TEXT SIZE=30 ID=\"package_name." + serviceName
287                     + "\" NAME=\"package_name." + serviceName + "\" VALUE=\"" +
288                     Wsdl2Java.DEFAULT_TARGET_PACKAGE + "." + serviceName.toLowerCase() +
289                     "\" STYLE=\"background: #ade7ff\"></TD>");
290                     strWriter.write("</TR>");
291                     String JavaDoc serviceDocumentationContent = (String JavaDoc)service.getProperty(this.WSDL_DOCUMENTATION);
292                     
293                     if(null != serviceDocumentationContent) {
294                         strWriter.write("<TR>");
295                         strWriter.write("<TD VALIGN=TOP><LABEL><B>" + NbBundle.getMessage(this.getClass(), "SERVICE_DOCUMENTATION") + "</B></LABEL></TD>");
296                         strWriter.write("<TD VALIGN=TOP>" + serviceDocumentationContent + "</TD>");
297                         strWriter.write("</TR>");
298                     }
299                     Iterator JavaDoc ports = service.getPorts();
300                     if(!ports.hasNext()) {
301                         strWriter.write("</TABLE>");
302                     }
303                     while (ports.hasNext()) {
304                         Port port = (Port)ports.next();
305                         JavaInterface javaInterface = port.getJavaInterface();
306                         //strWriter.write("Port Name: " + javaInterface.getName() + "\n");
307
String JavaDoc proxy = javaInterface.getName();
308                         proxy = proxy.substring(proxy.lastIndexOf('.') + 1, proxy.length());
309                         //strWriter.write("Proxy: " + proxy + "_Stub" + "\n");
310
strWriter.write("<TR>");
311                         strWriter.write("<TD VALIGN=TOP><B>" + NbBundle.getMessage(this.getClass(), "WEB_SERVICE_PORTNNAME") + "</B></TD>");
312                         strWriter.write("<TD VALIGN=TOP>" +port.getName().toString() +"</TD>");
313                         strWriter.write("</TR>");
314                         strWriter.write("<TR>");
315                         strWriter.write("<TD VALIGN=TOP><B>" + NbBundle.getMessage(this.getClass(), "WEB_SERVICE_ADDRESS") + "</B></TD>");
316                         strWriter.write("<TD VALIGN=TOP>" +port.getAddress() +"</TD>");
317                         strWriter.write("</TR>");
318                         Iterator JavaDoc operations = port.getOperations();
319                         strWriter.write("<TR>");
320                         strWriter.write("<TD VALIGN=TOP><B>" + NbBundle.getMessage(this.getClass(), "METHODS") + "</B></TD>");
321                         strWriter.write("</TR>");
322                         int methodCount=0;
323                         while (operations.hasNext()) {
324                             hasOperations=true;
325                             Operation operation = (Operation) operations.next();
326                             JavaMethod method = operation.getJavaMethod();
327                             strWriter.write("<TR>");
328                             strWriter.write("<TD VALIGN=TOP>");
329                             /**
330                              * Since the neither JavaParameter nor Operation override "equals()", we need to jump through
331                              * some hoops to compare the method signatures for methods with the same name. The pattern is
332                              *
333                              * actionTOKEN_SEPARATORserviceTOKEN_SEPARATORportTOKEN_SEPARATORmethodTOKEN_SEPARATORSIG_SEPARATORreturntypePARAM_SEPARATORparametertypePARAM_SEPARATORparametertype:..:
334                              *
335                              * -David Botterill 4/22/2004
336                              */

337                             String JavaDoc signatureString = method.getReturnType().getRealName() + RETURN_SEPARATOR;
338                             Iterator JavaDoc paramameters = method.getParameters();
339                             while (paramameters.hasNext()) {
340                                 JavaParameter currentParam = (JavaParameter)paramameters.next();
341                                 signatureString += currentParam.getType().getRealName() + PARAM_SEPARATOR;
342                             }
343                             
344                             strWriter.write("<A HREF=\"" + "test" + TOKEN_SEPARATOR +
345                             serviceName + TOKEN_SEPARATOR +
346                             port.getName().getLocalPart() + TOKEN_SEPARATOR +
347                             method.getName() + TOKEN_SEPARATOR +
348                             SIG_SEPARATOR +
349                             signatureString +
350                             "\">" + NbBundle.getMessage(this.getClass(), "TEST_METHOD")+ "</A>");
351                             strWriter.write("</TD>");
352                             // strWriter.write("<TD COLSPAN=2 VALIGN=TOP>");
353
strWriter.write("<TD VALIGN=TOP>");
354 // !HIE strWriter.write("<UL><LI>");
355
strWriter.write("<I>" + method.getReturnType().getRealName() + "</I>&nbsp");
356                             strWriter.write("<B>" + method.getName() + "</B>(");
357                             Iterator JavaDoc params = method.getParameters();
358                             while (params.hasNext()) {
359                                 strWriter.write("<I>");
360                                 JavaParameter param = (JavaParameter)params.next();
361                                 String JavaDoc parameterType = Util.getParameterType(port,param);
362                                 strWriter.write(parameterType);
363                                 if(params.hasNext()) strWriter.write(", ");
364                                 strWriter.write("</I>");
365                             }
366                             strWriter.write(")");
367                             Iterator JavaDoc exceptions = method.getExceptionsList().iterator();
368                             while (exceptions.hasNext()) {
369                                 /**
370                                  * Bugid: 4970323 - The return type of an exceptions is a String not a JavaException. This
371                                  * can only be know for sure by reading the current JavaException code since the "JavaMethod.getExceptions()"
372                                  * method returns an Iterator and the javadoc says nothing of it being a String.
373                                  */

374                                 //JavaException exc = (JavaException)exceptions.next();
375
//strWriter.write(exc.getName());
376
/**
377                                  * Make sure we don't get back a null or an empty String. Check to make sure
378                                  * the Object is a string in case the API matures and JavaException turns back into a real object with
379                                  * a "getName" method.
380                                  */

381                                 Object JavaDoc currentException = exceptions.next();
382                                 if(null != currentException &&
383                                 currentException instanceof String JavaDoc &&
384                                 ((String JavaDoc)currentException).length() > 0) {
385                                     strWriter.write("<I> &nbsp throws &nbsp ");
386                                     strWriter.write((String JavaDoc)currentException);
387                                     if(params.hasNext()) strWriter.write(", &nbsp");
388                                     strWriter.write("</I>");
389                                 }
390                             }
391                             //strWriter.write("<BR>");
392
String JavaDoc operationDocumentationContent = (String JavaDoc)operation.getProperty(this.WSDL_DOCUMENTATION);
393                             
394                             if(null != operationDocumentationContent) {
395 // !HIE strWriter.write("<BR><B>" + NbBundle.getMessage(this.getClass(), "METHOD_DOCUMENTATION") + "</B>");
396
strWriter.write("<BR>" + operationDocumentationContent);
397                             }
398 // !HIE strWriter.write("</LI></UL>");
399
strWriter.write("</TD>");
400                             strWriter.write("</TR>");
401                         }
402                     }
403                     strWriter.write("</TABLE>");
404                     strWriter.write("</FORM>");
405                     if(services.hasNext()) {
406                         strWriter.write("<HR>");
407                     }
408                     /**
409                      * we've processed the requested service so break
410                      */

411                     break;
412                 }
413                 
414                 
415             }
416         } else {
417             strWriter.write("<BR><B>" + NbBundle.getMessage(this.getClass(), "WSDLINFO_ERROR_PARSING") +"</B><BR> ");
418             
419         }
420         
421         return strWriter.toString();
422     }
423     
424     public static void main(String JavaDoc[] args){
425         System.setProperty("http.proxyHost", "webcache.sfbay.sun.com");
426         System.setProperty("http.proxyPort", "8080");
427         
428         WSDLInfo wsdlInfo = new WSDLInfo();
429         File JavaDoc wsdlFile = new File JavaDoc("D:\\realtimeservice.wsdl");
430         
431         try{
432             wsdlInfo.setWsdlUrl(wsdlFile.toURL());
433             //wsdlInfo.setWsdlUrl(new URL("http://www.lixusnet.com/lixusnet/AddFinder.jws?wsdl"));
434
}catch (Exception JavaDoc exc){
435             exc.printStackTrace();
436             System.exit(-1);
437         }
438         
439         if(wsdlInfo.create()) {
440             wsdlInfo.getServiceInfo("RealTimeService");
441         }else{
442             System.out.println(wsdlInfo.getErrorMessage());
443         }
444     }
445 }
446
Popular Tags