KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > registry > jaxrpc > Wsdl2Java


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.jaxrpc;
21
22 import org.netbeans.modules.websvc.registry.util.Util;
23 import org.netbeans.modules.websvc.registry.util.WebProxySetter;
24 import org.netbeans.modules.websvc.registry.model.WebServiceData;
25 import org.netbeans.modules.websvc.registry.WebServiceException;
26 import com.sun.xml.rpc.spi.JaxRpcObjectFactory;
27
28 import com.sun.xml.rpc.processor.model.Operation;
29 import com.sun.xml.rpc.processor.model.Port;
30 import com.sun.xml.rpc.processor.model.java.JavaException;
31 import com.sun.xml.rpc.processor.model.java.JavaMethod;
32 import com.sun.xml.rpc.spi.model.JavaInterface;
33 import com.sun.xml.rpc.spi.model.Model;
34 import com.sun.xml.rpc.spi.model.Service;
35 import com.sun.xml.rpc.spi.tools.CompileTool;
36 import com.sun.xml.rpc.spi.tools.CompileToolDelegate;
37 import com.sun.xml.rpc.spi.tools.GeneratedFileInfo;
38 import com.sun.xml.rpc.tools.wscompile.ActionConstants;
39 import java.io.BufferedOutputStream JavaDoc;
40 import java.io.File JavaDoc;
41 import java.io.FileOutputStream JavaDoc;
42 import java.io.FileWriter JavaDoc;
43 import java.io.OutputStreamWriter JavaDoc;
44 import java.io.OutputStream JavaDoc;
45 import java.io.PrintStream JavaDoc;
46 import java.io.IOException JavaDoc;
47
48 import java.net.URI JavaDoc;
49 import java.net.URL JavaDoc;
50 import java.util.ArrayList JavaDoc;
51 import java.util.HashSet JavaDoc;
52 import java.util.Iterator JavaDoc;
53 import java.util.List JavaDoc;
54 import java.util.Set JavaDoc;
55 import java.util.StringTokenizer JavaDoc;
56 import org.openide.ErrorManager;
57 import org.openide.util.NbBundle;
58 import org.openide.awt.StatusDisplayer;
59
60
61
62 /**
63  * @author Winston Prakash
64  */

65 public class Wsdl2Java {
66     
67     private String JavaDoc program ="wscompile";
68     private String JavaDoc outputDir;
69     private String JavaDoc nonClassOutputDir;
70     private String JavaDoc sourceOutputDir;
71     private URL JavaDoc wsdlUrl;
72     private String JavaDoc packageName;
73     private JaxRpcObjectFactory jaxprObjectFactory ;
74     private CompileTool compTool;
75     private File JavaDoc webserviceClient;
76     private File JavaDoc webserviceClientBeanInfo;
77     
78     
79     public final static String JavaDoc DEFAULT_TARGET_PACKAGE = "webservice"; // the default location to generate the java source
80

81     public Wsdl2Java() {
82         
83         System.setProperty("http.proxyHost", WebProxySetter.getInstance().getProxyHost());
84         System.setProperty("http.proxyPort", WebProxySetter.getInstance().getProxyPort());
85         
86         jaxprObjectFactory = JaxRpcObjectFactory.newInstance();
87     }
88     
89     public void setOutputDirectory(String JavaDoc path){
90         outputDir = path;
91     }
92     
93     public String JavaDoc getOutputDirectory(){
94         if(outputDir == null) {
95             outputDir = System.getProperty("user.home");
96         }
97         return outputDir;
98     }
99     
100     public void setNonClassOutputDirectory(String JavaDoc path){
101         nonClassOutputDir = path;
102     }
103     
104     public String JavaDoc getNonClassOutputDirectory(){
105         if(nonClassOutputDir == null) {
106             nonClassOutputDir = getOutputDirectory();
107         }
108         return nonClassOutputDir;
109     }
110     
111     public void setSourceOutputDirectory(String JavaDoc path){
112         sourceOutputDir = path;
113     }
114     
115     public String JavaDoc getSourceOutputDirectory(){
116         if(sourceOutputDir == null) {
117             sourceOutputDir = getOutputDirectory();
118         }
119         return sourceOutputDir;
120     }
121     
122     public File JavaDoc getWebserviceClient() {
123         return webserviceClient;
124     }
125     public File JavaDoc getWebserviceClientBeanInfo() {
126         return webserviceClientBeanInfo;
127     }
128     
129     public void setWsdlUrl(URL JavaDoc url){
130         wsdlUrl = url;
131     }
132     
133     public URL JavaDoc getWsdlUrl(){
134         return wsdlUrl;
135     }
136     
137     public void setPackageName(String JavaDoc pkgName){
138         packageName = pkgName;
139     }
140     
141     public String JavaDoc getPackageName(){
142         if(packageName == null) {
143             packageName = this.DEFAULT_TARGET_PACKAGE;
144         }
145         return packageName;
146     }
147     
148     public boolean execute(WebServiceData inWSData, File JavaDoc errorFile) {
149         PrintStream JavaDoc ps = null;
150         try{
151             FileOutputStream JavaDoc fdout = new FileOutputStream JavaDoc(errorFile);
152             BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(fdout, 2048);
153             ps = new PrintStream JavaDoc(bos, false);
154         }catch(Exception JavaDoc exc){
155             ps = System.out;
156         }
157         return execute(inWSData,ps);
158     }
159     
160     public boolean execute(WebServiceData inWSData,OutputStream JavaDoc outputStream) {
161         jaxprObjectFactory = JaxRpcObjectFactory.newInstance();
162         compTool = jaxprObjectFactory.createCompileTool(outputStream, program);
163         WSCompileArguments wsCompileArgs = new WSCompileArguments();
164         
165         wsCompileArgs.setClasspath(Util.getRuntimeClassPath());
166         File JavaDoc outDir = new File JavaDoc(getOutputDirectory());
167         if(!outDir.exists()) {
168             outDir.mkdirs();
169         }
170         wsCompileArgs.setOutputDirectory(outDir.getAbsolutePath());
171         wsCompileArgs.setGen("client");
172         //wsCompileArgs.setImportGen(true);
173
wsCompileArgs.setVerbose(false);
174         wsCompileArgs.setKeep(false);
175         /**
176          * If the wsi feature is turned on, I've found some of the client code to be genereated incorrectly
177          * with JAX-RPC 1.1.
178          * -David Botterill 4/22/2004
179          */

180         //wsCompileArgs.addFeature("wsi");
181
wsCompileArgs.addFeature("searchschema");
182         File JavaDoc nonClassDir = new File JavaDoc(getNonClassOutputDirectory());
183         if(!nonClassDir.exists()) nonClassDir.mkdirs();
184         wsCompileArgs.setNonclassOutputDirectory(nonClassDir.getAbsolutePath());
185         
186         File JavaDoc srcDir = new File JavaDoc(getSourceOutputDirectory());
187         if(!srcDir.exists()) srcDir.mkdirs();
188         wsCompileArgs.setSourceOutputDirectory(srcDir.getAbsolutePath());
189         
190         Configuration config = new Configuration();
191         WsdlType wsdl = new WsdlType();
192         config.setWsdl(wsdl);
193         try{
194             wsdl.setLocation( getWsdlUrl());
195         }catch(Exception JavaDoc exc){
196             exc.printStackTrace();
197             return false;
198         }
199         wsdl.setPackageName(getPackageName());
200         wsCompileArgs.setConfiguration(config);
201         
202         boolean ret = false;
203         try{
204             //System.out.println("WSCOMPILE ARGS=" + wsCompileArgs.toString());
205
ret = compTool.run(wsCompileArgs.toArgs());
206             if(ret) {
207                 createWrapperClients(inWSData);
208             } else {
209                 ErrorManager.getDefault().log(this.getClass().getName() + NbBundle.getMessage(this.getClass(), "ERROR_WSCOMPILE_INTERNAL"));
210 // StatusDisplayer.getDefault().displayError(NbBundle.getMessage(this.getClass(), "ERROR_WSCOMPILE_EXTERNAL"),2);
211
StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(this.getClass(), "ERROR_WSCOMPILE_EXTERNAL"));
212             }
213         }catch(Exception JavaDoc exc){
214             ErrorManager.getDefault().notify(exc);
215             ErrorManager.getDefault().log(this.getClass().getName() + NbBundle.getMessage(this.getClass(), "ERROR_WSCOMPILE_INTERNAL"));
216 // StatusDisplayer.getDefault().displayError(NbBundle.getMessage(this.getClass(), "ERROR_WSCOMPILE_EXTERNAL"),2);
217
StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(this.getClass(), "ERROR_WSCOMPILE_EXTERNAL"));
218         }
219         return ret;
220     }
221     
222     /**
223      * Create client wrapper for all the ports.
224      */

225     public void createWrapperClients(WebServiceData inWSData){
226         File JavaDoc sourceDir = new File JavaDoc(getSourceOutputDirectory());
227         String JavaDoc pkgName = getPackageName();
228         StringTokenizer JavaDoc strTokenizer = new StringTokenizer JavaDoc(getPackageName(),".");
229         while(strTokenizer.hasMoreTokens()) {
230             sourceDir = new File JavaDoc(sourceDir,strTokenizer.nextToken());
231         }
232         if(!sourceDir.exists()) {
233             ErrorManager.getDefault().log(this.getClass().getName() + NbBundle.getMessage(this.getClass(), "ERROR_CREATING_SOURCEDIR"));
234 // StatusDisplayer.getDefault().displayError(NbBundle.getMessage(this.getClass(), "ERROR_CREATING_SOURCEDIR"),2);
235
StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(this.getClass(), "ERROR_CREATING_SOURCEDIR"));
236         }
237         Model model = compTool.getProcessor().getModel();
238         if(model == null) {
239             ErrorManager.getDefault().log(this.getClass().getName() + NbBundle.getMessage(this.getClass(), "ERROR_WSCOMPILE_INTERNAL"));
240 // StatusDisplayer.getDefault().displayError(NbBundle.getMessage(this.getClass(), "ERROR_WSCOMPILE_EXTERNAL"),2);
241
StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(this.getClass(), "ERROR_WSCOMPILE_EXTERNAL"));
242             return;
243         }
244         for (Iterator JavaDoc services = model.getServices(); services.hasNext(); ) {
245             Service service = (Service) services.next();
246             String JavaDoc serviceFullName = service.getJavaIntf().getName();
247             /**
248              * We only want the name, not the fully qualified name.
249              */

250             String JavaDoc serviceName = serviceFullName.substring(serviceFullName.lastIndexOf(".") + 1);
251             Iterator JavaDoc ports = service.getPorts();
252             /**
253              * Since there will be one jar per web service client, even for a multi-service WSDL, we want the
254              * display name of the WebServiceData passed in to be used to name the client.
255              */

256             String JavaDoc displayName = inWSData.getDisplayName();
257             String JavaDoc clientNameRoot = displayName.substring(displayName.lastIndexOf('.') + 1, displayName.length());
258             
259             webserviceClient = new File JavaDoc(sourceDir, clientNameRoot + "Client.java");
260             webserviceClientBeanInfo = new File JavaDoc(sourceDir, clientNameRoot + "ClientBeanInfo.java");
261             WrapperClientWriter beanWriter = null;
262             WrapperClientBeanInfoWriter beanInfoWriter = null;
263             try {
264                 beanWriter = new WrapperClientWriter(new FileWriter JavaDoc(webserviceClient));
265                 beanInfoWriter = new WrapperClientBeanInfoWriter(new FileWriter JavaDoc(webserviceClientBeanInfo));
266             } catch(IOException JavaDoc ioe) {
267                 ErrorManager.getDefault().notify(ioe);
268 // StatusDisplayer.getDefault().displayError(NbBundle.getMessage(this.getClass(), "ERROR_WSCOMPILE_EXTERNAL"),2);
269
StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(this.getClass(), "ERROR_WSCOMPILE_EXTERNAL"));
270                 return;
271             }
272             
273             beanWriter.setPackage(getPackageName());
274             beanWriter.setName(clientNameRoot + "Client");
275             beanWriter.setContainedClassInfo(serviceName);
276             beanWriter.addImport(inWSData.getPackageName() + ".*");
277             beanInfoWriter.setPackage(getPackageName());
278             beanInfoWriter.setName(clientNameRoot + "Client");
279             while (ports.hasNext()) {
280                 Port port = (Port)ports.next();
281                 beanWriter.addPort(port);
282             }
283             beanWriter.writeClass();
284             beanWriter.flush();
285             beanWriter.close();
286             beanInfoWriter.writeBeanInfo();
287             beanInfoWriter.flush();
288             beanInfoWriter.close();
289             
290         }
291     }
292     
293     public Model getModel(){
294         return compTool.getProcessor().getModel();
295     }
296     
297     
298     public File JavaDoc[] getGeneratedFiles(){
299         return getGeneratedFiles(false);
300     }
301     
302     public File JavaDoc[] getGeneratedJavaFiles(){
303         return getGeneratedFiles(true);
304     }
305     
306     public File JavaDoc[] getGeneratedFiles(boolean onlyJava){
307         Set JavaDoc generatedFiles = new HashSet JavaDoc();
308         // I had to compile JAXRPC-RI from source file and add it to external - WJP
309
// com.sun.xml.rpc.processor.util.ClientProcessorEnvironment,shutDown sets
310
// generatedfiles to null. So compTool.getEnvironment().getGeneratedFiles()
311
// fails with NPE
312
for (Iterator JavaDoc iter = compTool.getEnvironment().getGeneratedFiles(); iter.hasNext();) {
313             Object JavaDoc obj = iter.next();
314             File JavaDoc file;
315             if (obj instanceof File JavaDoc) {
316                 file = (File JavaDoc) obj;
317             }else if (obj instanceof GeneratedFileInfo) {
318                 file = ((GeneratedFileInfo)obj).getFile();
319             }else {
320                 //System.out.println("Wsdl2Java.getGeneratedFile(): Found a nonFile: "+obj);
321
continue;
322             }
323             
324             if (file.exists()){
325                 if(onlyJava){
326                     if(file.getName().endsWith(".java")) generatedFiles.add(file);
327                 }else{
328                     generatedFiles.add(file);
329                 }
330             }
331         }
332         return (File JavaDoc[]) generatedFiles.toArray(new File JavaDoc[generatedFiles.size()]);
333     }
334     
335     public static void main(String JavaDoc[] args){
336         System.setProperty("http.proxyHost", "webcache.sfbay.sun.com");
337         System.setProperty("http.proxyPort", "8080");
338         
339         Wsdl2Java wsdl2java = new Wsdl2Java();
340         wsdl2java.setOutputDirectory("D:\\wsdl2java\\classes");
341         //wsdl2java.setNonClassOutputDirectory("D:\\wsdl2java\\non-classes");
342
//wsdl2java.setSourceOutputDirectory("D:\\wsdl2java\\sources");
343
File JavaDoc wsdlFile = new File JavaDoc("D:\\wsdl2java\\JavaTravellerService.wsdl");
344         //File wsdlFile = new File("D:\\wsdl2java\\AddressBook.wsdl");
345

346         try{
347             wsdl2java.setWsdlUrl(wsdlFile.toURI().toURL());
348             //wsdl2java.setWsdlUrl(new URL("http://www.lixusnet.com/lixusnet/AddFinder.jws?wsdl"));
349
//wsdl2java.setWsdlUrl(new URL("http://ws.digiposs.com/WorldTime.jws?wsdl"));
350
//wsdl2java.setWsdlUrl(new URL("http://www.xmethods.net/sd/2001/TemperatureService.wsdl"));
351
}catch (Exception JavaDoc exc){
352             exc.printStackTrace();
353             System.exit(-1);
354         }
355         
356         //wsdl2java.setPackageName("wsdl2java_pkg");
357
WebServiceData wsData = new WebServiceData();
358         wsData.setDisplayName("JavaTraveller_service");
359         if(wsdl2java.execute(wsData,System.out)) {
360             System.out.println("Client Proxy Successfully created");
361         }else{
362             System.out.println("Client Proxy creation failed");
363         }
364         
365         /*File[] generatedFiles = wsdl2java.getGeneratedJavaFiles();
366         for(int i=0; i< generatedFiles.length; i++){
367             System.out.println(generatedFiles[i].getAbsolutePath());
368         }*/

369     }
370     
371 }
372
Popular Tags