KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > wsdl > Java2WSDL


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.jboss.axis.wsdl;
56
57 import org.jboss.axis.encoding.DefaultSOAPEncodingTypeMappingImpl;
58 import org.jboss.axis.encoding.DefaultTypeMappingImpl;
59 import org.jboss.axis.utils.CLArgsParser;
60 import org.jboss.axis.utils.CLOption;
61 import org.jboss.axis.utils.CLOptionDescriptor;
62 import org.jboss.axis.utils.CLUtil;
63 import org.jboss.axis.utils.Messages;
64 import org.jboss.axis.wsdl.fromJava.Emitter;
65
66 import java.util.HashMap JavaDoc;
67 import java.util.List JavaDoc;
68
69 /**
70  * Command line interface to the java2wsdl utility
71  *
72  * @author Ravi Kumar (rkumar@borland.com)
73  * @author Rich Scheuerle (scheu@us.ibm.com)
74  */

75
76 public class Java2WSDL
77 {
78    // Define our short one-letter option identifiers.
79
protected static final int INHERITED_CLASS_OPT = 'a';
80    protected static final int SOAPACTION_OPT = 'A';
81    protected static final int BINDING_NAME_OPT = 'b';
82    protected static final int STOP_CLASSES_OPT = 'c';
83    protected static final int IMPORT_SCHEMA_OPT = 'C';
84    protected static final int EXTRA_CLASSES_OPT = 'e';
85    protected static final int HELP_OPT = 'h';
86    protected static final int IMPL_CLASS_OPT = 'i';
87    protected static final int INPUT_OPT = 'I';
88    protected static final int LOCATION_OPT = 'l';
89    protected static final int LOCATION_IMPORT_OPT = 'L';
90    protected static final int METHODS_ALLOWED_OPT = 'm';
91    protected static final int NAMESPACE_OPT = 'n';
92    protected static final int NAMESPACE_IMPL_OPT = 'N';
93    protected static final int OUTPUT_OPT = 'o';
94    protected static final int OUTPUT_IMPL_OPT = 'O';
95    protected static final int PACKAGE_OPT = 'p';
96    protected static final int PORTTYPE_NAME_OPT = 'P';
97    protected static final int SERVICE_PORT_NAME_OPT = 's';
98    protected static final int SERVICE_ELEMENT_NAME_OPT = 'S';
99    protected static final int TYPEMAPPING_OPT = 'T';
100    protected static final int USE_OPT = 'u';
101    protected static final int OUTPUT_WSDL_MODE_OPT = 'w';
102    protected static final int METHODS_NOTALLOWED_OPT = 'x';
103    protected static final int STYLE_OPT = 'y';
104
105    /**
106     * Define the understood options. Each CLOptionDescriptor contains:
107     * - The "long" version of the option. Eg, "help" means that "--help" will
108     * be recognised.
109     * - The option flags, governing the option's argument(s).
110     * - The "short" version of the option. Eg, 'h' means that "-h" will be
111     * recognised.
112     * - A description of the option for the usage message
113     */

114    protected CLOptionDescriptor[] options = new CLOptionDescriptor[]{
115       new CLOptionDescriptor("help",
116               CLOptionDescriptor.ARGUMENT_DISALLOWED,
117               HELP_OPT,
118               Messages.getMessage("j2wopthelp00")),
119       new CLOptionDescriptor("input",
120               CLOptionDescriptor.ARGUMENT_REQUIRED,
121               INPUT_OPT,
122               Messages.getMessage("j2woptinput00")),
123       new CLOptionDescriptor("output",
124               CLOptionDescriptor.ARGUMENT_REQUIRED,
125               OUTPUT_OPT,
126               Messages.getMessage("j2woptoutput00")),
127       new CLOptionDescriptor("location",
128               CLOptionDescriptor.ARGUMENT_REQUIRED,
129               LOCATION_OPT,
130               Messages.getMessage("j2woptlocation00")),
131       new CLOptionDescriptor("portTypeName",
132               CLOptionDescriptor.ARGUMENT_REQUIRED,
133               PORTTYPE_NAME_OPT,
134               Messages.getMessage("j2woptportTypeName00")),
135       new CLOptionDescriptor("bindingName",
136               CLOptionDescriptor.ARGUMENT_REQUIRED,
137               BINDING_NAME_OPT,
138               Messages.getMessage("j2woptbindingName00")),
139       new CLOptionDescriptor("serviceElementName",
140               CLOptionDescriptor.ARGUMENT_REQUIRED,
141               SERVICE_ELEMENT_NAME_OPT,
142               Messages.getMessage("j2woptserviceElementName00")),
143       new CLOptionDescriptor("servicePortName",
144               CLOptionDescriptor.ARGUMENT_REQUIRED,
145               SERVICE_PORT_NAME_OPT,
146               Messages.getMessage("j2woptservicePortName00")),
147       new CLOptionDescriptor("namespace",
148               CLOptionDescriptor.ARGUMENT_REQUIRED,
149               NAMESPACE_OPT,
150               Messages.getMessage("j2woptnamespace00")),
151       new CLOptionDescriptor("PkgtoNS",
152               CLOptionDescriptor.DUPLICATES_ALLOWED + CLOptionDescriptor.ARGUMENTS_REQUIRED_2,
153               PACKAGE_OPT,
154               Messages.getMessage("j2woptPkgtoNS00")),
155       new CLOptionDescriptor("methods",
156               CLOptionDescriptor.DUPLICATES_ALLOWED + CLOptionDescriptor.ARGUMENT_REQUIRED,
157               METHODS_ALLOWED_OPT,
158               Messages.getMessage("j2woptmethods00")),
159       new CLOptionDescriptor("all",
160               CLOptionDescriptor.ARGUMENT_DISALLOWED,
161               INHERITED_CLASS_OPT,
162               Messages.getMessage("j2woptall00")),
163       new CLOptionDescriptor("outputWsdlMode",
164               CLOptionDescriptor.ARGUMENT_REQUIRED,
165               OUTPUT_WSDL_MODE_OPT,
166               Messages.getMessage("j2woptoutputWsdlMode00")),
167       new CLOptionDescriptor("locationImport",
168               CLOptionDescriptor.ARGUMENT_REQUIRED,
169               LOCATION_IMPORT_OPT,
170               Messages.getMessage("j2woptlocationImport00")),
171       new CLOptionDescriptor("namespaceImpl",
172               CLOptionDescriptor.ARGUMENT_REQUIRED,
173               NAMESPACE_IMPL_OPT,
174               Messages.getMessage("j2woptnamespaceImpl00")),
175       new CLOptionDescriptor("outputImpl",
176               CLOptionDescriptor.ARGUMENT_REQUIRED,
177               OUTPUT_IMPL_OPT,
178               Messages.getMessage("j2woptoutputImpl00")),
179       new CLOptionDescriptor("implClass",
180               CLOptionDescriptor.ARGUMENT_REQUIRED,
181               IMPL_CLASS_OPT,
182               Messages.getMessage("j2woptimplClass00")),
183       new CLOptionDescriptor("exclude",
184               CLOptionDescriptor.DUPLICATES_ALLOWED + CLOptionDescriptor.ARGUMENT_REQUIRED,
185               METHODS_NOTALLOWED_OPT,
186               Messages.getMessage("j2woptexclude00")),
187       new CLOptionDescriptor("stopClasses",
188               CLOptionDescriptor.DUPLICATES_ALLOWED + CLOptionDescriptor.ARGUMENT_REQUIRED,
189               STOP_CLASSES_OPT,
190               Messages.getMessage("j2woptstopClass00")),
191       new CLOptionDescriptor("typeMappingVersion",
192               CLOptionDescriptor.ARGUMENT_REQUIRED,
193               TYPEMAPPING_OPT,
194               Messages.getMessage("j2wopttypeMapping00")),
195       new CLOptionDescriptor("soapAction",
196               CLOptionDescriptor.ARGUMENT_REQUIRED,
197               SOAPACTION_OPT,
198               Messages.getMessage("j2woptsoapAction00")),
199       new CLOptionDescriptor("style",
200               CLOptionDescriptor.ARGUMENT_REQUIRED,
201               STYLE_OPT,
202               Messages.getMessage("j2woptStyle00")),
203       new CLOptionDescriptor("use",
204               CLOptionDescriptor.ARGUMENT_REQUIRED,
205               USE_OPT,
206               Messages.getMessage("j2woptUse00")),
207       new CLOptionDescriptor("extraClasses",
208               CLOptionDescriptor.DUPLICATES_ALLOWED + CLOptionDescriptor.ARGUMENT_REQUIRED,
209               EXTRA_CLASSES_OPT,
210               Messages.getMessage("j2woptExtraClasses00")),
211       new CLOptionDescriptor("importSchema",
212               CLOptionDescriptor.ARGUMENT_OPTIONAL,
213               IMPORT_SCHEMA_OPT,
214               Messages.getMessage("j2woptImportSchema00"))
215    };
216
217    protected Emitter emitter;
218    protected String JavaDoc className = null;
219    protected String JavaDoc wsdlFilename = null;
220    protected String JavaDoc wsdlImplFilename = null;
221    protected HashMap JavaDoc namespaceMap = new HashMap JavaDoc();
222    protected int mode = Emitter.MODE_ALL;
223    boolean locationSet = false;
224
225    /**
226     * Instantiate a Java2WSDL emitter.
227     */

228    protected Java2WSDL()
229    {
230       emitter = createEmitter();
231    } // ctor
232

233
234    /**
235     * Instantiate an Emitter
236     */

237    protected Emitter createEmitter()
238    {
239       return new Emitter();
240    } // createEmitter
241

242    /**
243     * addOptions
244     * Add option descriptions to the tool. Allows
245     * extended classes to add additional options.
246     *
247     * @param newOptions CLOptionDescriptor[] the options
248     */

249    protected void addOptions(CLOptionDescriptor[] newOptions)
250    {
251       if (newOptions != null && newOptions.length > 0)
252       {
253          CLOptionDescriptor[] allOptions = new CLOptionDescriptor[
254                  options.length + newOptions.length];
255          System.arraycopy(options, 0, allOptions, 0, options.length);
256          System.arraycopy(newOptions, 0, allOptions, options.length, newOptions.length);
257          options = allOptions;
258       }
259    }
260
261    /**
262     * Parse an option
263     *
264     * @param option CLOption is the option
265     */

266    protected boolean parseOption(CLOption option)
267    {
268       String JavaDoc value;
269       boolean status = true;
270
271       switch (option.getId())
272       {
273          case CLOption.TEXT_ARGUMENT:
274             if (className != null)
275             {
276                System.out.println(Messages.getMessage("j2wDuplicateClass00",
277                        className,
278                        option.getArgument()));
279                printUsage();
280                status = false; // error
281
}
282             className = option.getArgument();
283             break;
284
285          case METHODS_ALLOWED_OPT:
286             emitter.setAllowedMethods(option.getArgument());
287             break;
288
289          case INHERITED_CLASS_OPT:
290             emitter.setUseInheritedMethods(true);
291             break;
292
293          case IMPL_CLASS_OPT:
294             emitter.setImplCls(option.getArgument());
295             break;
296
297          case HELP_OPT:
298             printUsage();
299             status = false;
300             break;
301
302          case OUTPUT_WSDL_MODE_OPT:
303             String JavaDoc modeArg = option.getArgument();
304             if ("All".equalsIgnoreCase(modeArg))
305                mode = Emitter.MODE_ALL;
306             else if ("Interface".equalsIgnoreCase(modeArg))
307                mode = Emitter.MODE_INTERFACE;
308             else if ("Implementation".equalsIgnoreCase(modeArg))
309                mode = Emitter.MODE_IMPLEMENTATION;
310             else
311             {
312                mode = Emitter.MODE_ALL;
313                System.err.println(Messages.getMessage("j2wmodeerror", modeArg));
314             }
315             break;
316
317          case OUTPUT_OPT:
318             wsdlFilename = option.getArgument();
319             break;
320
321          case INPUT_OPT:
322             emitter.setInputWSDL(option.getArgument());
323             break;
324
325          case OUTPUT_IMPL_OPT:
326             wsdlImplFilename = option.getArgument();
327             break;
328
329          case PACKAGE_OPT:
330             String JavaDoc packageName = option.getArgument(0);
331             String JavaDoc namespace = option.getArgument(1);
332             namespaceMap.put(packageName, namespace);
333             break;
334
335          case NAMESPACE_OPT:
336             emitter.setIntfNamespace(option.getArgument());
337             break;
338
339          case NAMESPACE_IMPL_OPT:
340             emitter.setImplNamespace(option.getArgument());
341             break;
342
343          case SERVICE_ELEMENT_NAME_OPT:
344             emitter.setServiceElementName(option.getArgument());
345             break;
346
347          case SERVICE_PORT_NAME_OPT:
348             emitter.setServicePortName(option.getArgument());
349             break;
350
351          case LOCATION_OPT:
352             emitter.setLocationUrl(option.getArgument());
353             locationSet = true;
354             break;
355
356          case LOCATION_IMPORT_OPT:
357             emitter.setImportUrl(option.getArgument());
358             break;
359
360          case METHODS_NOTALLOWED_OPT:
361             emitter.setDisallowedMethods(option.getArgument());
362             break;
363
364          case PORTTYPE_NAME_OPT:
365             emitter.setPortTypeName(option.getArgument());
366             break;
367
368          case BINDING_NAME_OPT:
369             emitter.setBindingName(option.getArgument());
370             break;
371
372          case STOP_CLASSES_OPT:
373             emitter.setStopClasses(option.getArgument());
374             break;
375
376          case TYPEMAPPING_OPT:
377             value = option.getArgument();
378             if (value.equals("1.1"))
379             {
380                emitter.setDefaultTypeMapping(DefaultTypeMappingImpl.getSingleton());
381             }
382             else if (value.equals("1.2"))
383             {
384                emitter.setDefaultTypeMapping(DefaultSOAPEncodingTypeMappingImpl.create());
385             }
386             else
387             {
388                System.out.println(Messages.getMessage("j2wBadTypeMapping00"));
389                status = false;
390             }
391             break;
392
393          case SOAPACTION_OPT:
394             value = option.getArgument();
395             if (value.equalsIgnoreCase("DEFAULT"))
396             {
397                emitter.setSoapAction("DEFAULT");
398             }
399             else if (value.equalsIgnoreCase("OPERATION"))
400             {
401                emitter.setSoapAction("OPERATION");
402             }
403             else if (value.equalsIgnoreCase("NONE"))
404             {
405                emitter.setSoapAction("NONE");
406             }
407             else
408             {
409                System.out.println(Messages.getMessage("j2wBadSoapAction00"));
410                status = false;
411             }
412             break;
413
414          case STYLE_OPT:
415             value = option.getArgument();
416             if (value.equalsIgnoreCase("DOCUMENT") ||
417                     value.equalsIgnoreCase("RPC") ||
418                     value.equalsIgnoreCase("WRAPPED"))
419             {
420                emitter.setStyle(value);
421             }
422             else
423             {
424                System.out.println(Messages.getMessage("j2woptBadStyle00"));
425                status = false;
426             }
427             break;
428          case USE_OPT:
429             value = option.getArgument();
430             if (value.equalsIgnoreCase("LITERAL") ||
431                     value.equalsIgnoreCase("ENCODED"))
432             {
433                emitter.setUse(value);
434             }
435             else
436             {
437                System.out.println(Messages.getMessage("j2woptBadUse00"));
438                status = false;
439             }
440             break;
441          case EXTRA_CLASSES_OPT:
442             try
443             {
444                emitter.setExtraClasses(option.getArgument());
445             }
446             catch (ClassNotFoundException JavaDoc e)
447             {
448                System.out.println(Messages.getMessage("j2woptBadClass00", e.toString()));
449                status = false;
450             }
451             break;
452
453          case IMPORT_SCHEMA_OPT:
454             emitter.setInputSchema(option.getArgument());
455             break;
456
457          default:
458             break;
459       }
460       return status;
461    }
462
463    /**
464     * validateOptions
465     * This method is invoked after the options are set to validate
466     * the option settings.
467     */

468    protected boolean validateOptions()
469    {
470       // Can't proceed without a class name
471
if ((className == null))
472       {
473          System.out.println(Messages.getMessage("j2wMissingClass00"));
474          printUsage();
475          return false;
476       }
477
478       if (!locationSet && (mode == Emitter.MODE_ALL ||
479               mode == Emitter.MODE_IMPLEMENTATION))
480       {
481          System.out.println(Messages.getMessage("j2wMissingLocation00"));
482          printUsage();
483          return false;
484       }
485
486       // Default to SOAP 1.2 JAX-RPC mapping
487
if (emitter.getDefaultTypeMapping() == null)
488       {
489          emitter.setDefaultTypeMapping(DefaultTypeMappingImpl.getSingleton());
490       }
491
492       return true; // a-OK
493
}
494
495    /**
496     * run
497     * checks the command-line arguments and runs the tool.
498     *
499     * @param args String[] command-line arguments.
500     */

501    protected int run(String JavaDoc[] args)
502    {
503       // Parse the arguments
504
CLArgsParser argsParser = new CLArgsParser(args, options);
505
506       // Print parser errors, if any
507
if (null != argsParser.getErrorString())
508       {
509          System.err.println(Messages.getMessage("j2werror00", argsParser.getErrorString()));
510          printUsage();
511          return (1);
512       }
513
514       // Get a list of parsed options
515
List JavaDoc clOptions = argsParser.getArguments();
516       int size = clOptions.size();
517
518       try
519       {
520          // Parse the options and configure the emitter as appropriate.
521
for (int i = 0; i < size; i++)
522          {
523             if (parseOption((CLOption)clOptions.get(i)) == false)
524             {
525                return (1);
526             }
527          }
528
529          // validate argument combinations
530
if (validateOptions() == false)
531             return (1);
532
533          // Set the namespace map
534
if (!namespaceMap.isEmpty())
535          {
536             emitter.setNamespaceMap(namespaceMap);
537          }
538
539          // Find the class using the name
540
emitter.setCls(className);
541
542          // Generate a full wsdl, or interface & implementation wsdls
543
if (wsdlImplFilename == null)
544          {
545             emitter.emit(wsdlFilename, mode);
546          }
547          else
548          {
549             emitter.emit(wsdlFilename, wsdlImplFilename);
550          }
551
552          // everything is good
553
return (0);
554       }
555       catch (Throwable JavaDoc t)
556       {
557          t.printStackTrace();
558          return (1);
559       }
560    } // run
561

562    /**
563     * printUsage
564     * print usage information and quit.
565     */

566    protected void printUsage()
567    {
568       String JavaDoc lSep = System.getProperty("line.separator");
569       StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
570       msg.append("Java2WSDL "
571               + Messages.getMessage("j2wemitter00")).append(lSep);
572       msg.append(Messages.getMessage("j2wusage00",
573               "java " + getClass().getName() + " [options] class-of-portType")).append(lSep);
574       msg.append(Messages.getMessage("j2woptions00")).append(lSep);
575       msg.append(CLUtil.describeOptions(options).toString());
576       msg.append(Messages.getMessage("j2wdetails00")).append(lSep);
577       System.out.println(msg.toString());
578    }
579
580    /**
581     * Main
582     * Run the Java2WSDL emitter with the specified command-line arguments
583     *
584     * @param args String[] command-line arguments
585     */

586    public static void main(String JavaDoc args[])
587    {
588       Java2WSDL java2wsdl = new Java2WSDL();
589       System.exit(java2wsdl.run(args));
590    }
591 }
592
Popular Tags