2
* Copyright 2001-2004 The Apache Software Foundation.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
16
package org.apache.axis.wsdl;
18
import org.apache.axis.utils.CLArgsParser;
19
import org.apache.axis.utils.CLOption;
20
import org.apache.axis.utils.CLOptionDescriptor;
21
import org.apache.axis.utils.CLUtil;
22
import org.apache.axis.utils.ClassUtils;
23
import org.apache.axis.utils.Messages;
24
import org.apache.axis.wsdl.fromJava.Emitter;
25
import org.apache.axis.encoding.TypeMappingRegistryImpl;
26
import org.apache.axis.encoding.TypeMappingImpl;
29
import java.util.HashMap;
30
import java.util.Iterator;
31
import java.util.List;
35
* Command line interface to the java2wsdl utility
37
* @author Ravi Kumar (rkumar@borland.com)
38
* @author Rich Scheuerle (scheu@us.ibm.com)
40
public class Java2WSDL {
42
// Define our short one-letter option identifiers.
44
/** Field INHERITED_CLASS_OPT */
45
protected static final int INHERITED_CLASS_OPT = 'a';
47
/** Field SOAPACTION_OPT */
48
protected static final int SOAPACTION_OPT = 'A';
50
/** Field BINDING_NAME_OPT */
51
protected static final int BINDING_NAME_OPT = 'b';
53
/** Field STOP_CLASSES_OPT */
54
protected static final int STOP_CLASSES_OPT = 'c';
56
/** Field IMPORT_SCHEMA_OPT */
57
protected static final int IMPORT_SCHEMA_OPT = 'C';
59
/** Field EXTRA_CLASSES_OPT */
60
protected static final int EXTRA_CLASSES_OPT = 'e';
63
protected static final int HELP_OPT = 'h';
65
/** Field IMPL_CLASS_OPT */
66
protected static final int IMPL_CLASS_OPT = 'i';
68
/** Field INPUT_OPT */
69
protected static final int INPUT_OPT = 'I';
71
/** Field LOCATION_OPT */
72
protected static final int LOCATION_OPT = 'l';
74
/** Field LOCATION_IMPORT_OPT */
75
protected static final int LOCATION_IMPORT_OPT = 'L';
77
/** Field METHODS_ALLOWED_OPT */
78
protected static final int METHODS_ALLOWED_OPT = 'm';
80
/** Field NAMESPACE_OPT */
81
protected static final int NAMESPACE_OPT = 'n';
83
/** Field NAMESPACE_IMPL_OPT */
84
protected static final int NAMESPACE_IMPL_OPT = 'N';
86
/** Field OUTPUT_OPT */
87
protected static final int OUTPUT_OPT = 'o';
89
/** Field OUTPUT_IMPL_OPT */
90
protected static final int OUTPUT_IMPL_OPT = 'O';
92
/** Field PACKAGE_OPT */
93
protected static final int PACKAGE_OPT = 'p';
95
/** Field PORTTYPE_NAME_OPT */
96
protected static final int PORTTYPE_NAME_OPT = 'P';
98
/** Field SERVICE_PORT_NAME_OPT */
99
protected static final int SERVICE_PORT_NAME_OPT = 's';
101
/** Field SERVICE_ELEMENT_NAME_OPT */
102
protected static final int SERVICE_ELEMENT_NAME_OPT = 'S';
104
/** Field TYPEMAPPING_OPT */
105
protected static final int TYPEMAPPING_OPT = 'T';
108
protected static final int USE_OPT = 'u';
110
/** Field OUTPUT_WSDL_MODE_OPT */
111
protected static final int OUTPUT_WSDL_MODE_OPT = 'w';
113
/** Field METHODS_NOTALLOWED_OPT */
114
protected static final int METHODS_NOTALLOWED_OPT = 'x';
116
protected static final int CLASSPATH_OPT = 'X';
118
/** Field STYLE_OPT */
119
protected static final int STYLE_OPT = 'y';
121
/** Field DEPLOY_OPT */
122
protected static final int DEPLOY_OPT = 'd';
125
* Define the understood options. Each CLOptionDescriptor contains:
126
* - The "long" version of the option. Eg, "help" means that "--help" will
128
* - The option flags, governing the option's argument(s).
129
* - The "short" version of the option. Eg, 'h' means that "-h" will be
131
* - A description of the option for the usage message
133
protected CLOptionDescriptor[] options = new CLOptionDescriptor[]{
134
new CLOptionDescriptor("help", CLOptionDescriptor.ARGUMENT_DISALLOWED,
135
HELP_OPT, Messages.getMessage("j2wopthelp00")),
136
new CLOptionDescriptor("input", CLOptionDescriptor.ARGUMENT_REQUIRED,
137
INPUT_OPT, Messages.getMessage("j2woptinput00")),
138
new CLOptionDescriptor("output", CLOptionDescriptor.ARGUMENT_REQUIRED,
140
Messages.getMessage("j2woptoutput00")),
141
new CLOptionDescriptor("location",
142
CLOptionDescriptor.ARGUMENT_REQUIRED,
144
Messages.getMessage("j2woptlocation00")),
145
new CLOptionDescriptor("portTypeName",
146
CLOptionDescriptor.ARGUMENT_REQUIRED,
148
Messages.getMessage("j2woptportTypeName00")),
149
new CLOptionDescriptor("bindingName",
150
CLOptionDescriptor.ARGUMENT_REQUIRED,
152
Messages.getMessage("j2woptbindingName00")),
153
new CLOptionDescriptor(
154
"serviceElementName", CLOptionDescriptor.ARGUMENT_REQUIRED,
155
SERVICE_ELEMENT_NAME_OPT,
156
Messages.getMessage("j2woptserviceElementName00")),
157
new CLOptionDescriptor("servicePortName",
158
CLOptionDescriptor.ARGUMENT_REQUIRED,
159
SERVICE_PORT_NAME_OPT,
160
Messages.getMessage("j2woptservicePortName00")),
161
new CLOptionDescriptor("namespace",
162
CLOptionDescriptor.ARGUMENT_REQUIRED,
164
Messages.getMessage("j2woptnamespace00")),
165
new CLOptionDescriptor("PkgtoNS",
166
CLOptionDescriptor.DUPLICATES_ALLOWED
167
+ CLOptionDescriptor.ARGUMENTS_REQUIRED_2,
169
Messages.getMessage("j2woptPkgtoNS00")),
170
new CLOptionDescriptor("methods",
171
CLOptionDescriptor.DUPLICATES_ALLOWED
172
+ CLOptionDescriptor.ARGUMENT_REQUIRED,
174
Messages.getMessage("j2woptmethods00")),
175
new CLOptionDescriptor("all", CLOptionDescriptor.ARGUMENT_DISALLOWED,
177
Messages.getMessage("j2woptall00")),
178
new CLOptionDescriptor("outputWsdlMode",
179
CLOptionDescriptor.ARGUMENT_REQUIRED,
180
OUTPUT_WSDL_MODE_OPT,
181
Messages.getMessage("j2woptoutputWsdlMode00")),
182
new CLOptionDescriptor("locationImport",
183
CLOptionDescriptor.ARGUMENT_REQUIRED,
185
Messages.getMessage("j2woptlocationImport00")),
186
new CLOptionDescriptor("namespaceImpl",
187
CLOptionDescriptor.ARGUMENT_REQUIRED,
189
Messages.getMessage("j2woptnamespaceImpl00")),
190
new CLOptionDescriptor("outputImpl",
191
CLOptionDescriptor.ARGUMENT_REQUIRED,
193
Messages.getMessage("j2woptoutputImpl00")),
194
new CLOptionDescriptor("implClass",
195
CLOptionDescriptor.ARGUMENT_REQUIRED,
197
Messages.getMessage("j2woptimplClass00")),
198
new CLOptionDescriptor("exclude",
199
CLOptionDescriptor.DUPLICATES_ALLOWED
200
+ CLOptionDescriptor.ARGUMENT_REQUIRED,
201
METHODS_NOTALLOWED_OPT,
202
Messages.getMessage("j2woptexclude00")),
203
new CLOptionDescriptor("stopClasses",
204
CLOptionDescriptor.DUPLICATES_ALLOWED
205
+ CLOptionDescriptor.ARGUMENT_REQUIRED,
207
Messages.getMessage("j2woptstopClass00")),
208
new CLOptionDescriptor("typeMappingVersion",
209
CLOptionDescriptor.ARGUMENT_REQUIRED,
211
Messages.getMessage("j2wopttypeMapping00")),
212
new CLOptionDescriptor("soapAction",
213
CLOptionDescriptor.ARGUMENT_REQUIRED,
215
Messages.getMessage("j2woptsoapAction00")),
216
new CLOptionDescriptor("style", CLOptionDescriptor.ARGUMENT_REQUIRED,
217
STYLE_OPT, Messages.getMessage("j2woptStyle00")),
218
new CLOptionDescriptor("use", CLOptionDescriptor.ARGUMENT_REQUIRED,
219
USE_OPT, Messages.getMessage("j2woptUse00")),
220
new CLOptionDescriptor("extraClasses",
221
CLOptionDescriptor.DUPLICATES_ALLOWED
222
+ CLOptionDescriptor.ARGUMENT_REQUIRED,
224
Messages.getMessage("j2woptExtraClasses00")),
225
new CLOptionDescriptor("importSchema",
226
CLOptionDescriptor.ARGUMENT_OPTIONAL,
228
Messages.getMessage("j2woptImportSchema00")),
229
new CLOptionDescriptor("classpath",
230
CLOptionDescriptor.ARGUMENT_OPTIONAL,
232
Messages.getMessage("optionClasspath")),
233
new CLOptionDescriptor("deploy",
234
CLOptionDescriptor.ARGUMENT_DISALLOWED,
236
Messages.getMessage("j2woptDeploy00")),
240
protected Emitter emitter;
242
/** Field className */
243
protected String className = null;
245
/** Field wsdlFilename */
246
protected String wsdlFilename = null;
248
/** Field wsdlImplFilename */
249
protected String wsdlImplFilename = null;
251
/** Field namespaceMap */
252
protected HashMap namespaceMap = new HashMap();
255
protected int mode = Emitter.MODE_ALL;
257
/** Field locationSet */
258
boolean locationSet = false;
260
/** Field typeMappingVersion */
261
protected String typeMappingVersion = "1.2";
263
/** Field isDeplpy */
264
protected boolean isDeploy = false;
267
* Instantiate a Java2WSDL emitter.
269
protected Java2WSDL() {
270
emitter = createEmitter();
274
* Instantiate an Emitter
278
protected Emitter createEmitter() {
279
return new Emitter();
284
* Add option descriptions to the tool. Allows
285
* extended classes to add additional options.
287
* @param newOptions CLOptionDescriptor[] the options
289
protected void addOptions(CLOptionDescriptor[] newOptions) {
291
if ((newOptions != null) && (newOptions.length > 0)) {
292
CLOptionDescriptor[] allOptions =
293
new CLOptionDescriptor[options.length + newOptions.length];
295
System.arraycopy(options, 0, allOptions, 0, options.length);
296
System.arraycopy(newOptions, 0, allOptions, options.length,
299
options = allOptions;
306
* @param option CLOption is the option
309
protected boolean parseOption(CLOption option) {
312
boolean status = true;
314
switch (option.getId()) {
316
case CLOption.TEXT_ARGUMENT:
317
if (className != null) {
320
"j2wDuplicateClass00", className,
321
option.getArgument()));
324
status = false; // error
327
className = option.getArgument();
330
case METHODS_ALLOWED_OPT:
331
emitter.setAllowedMethods(option.getArgument());
334
case INHERITED_CLASS_OPT:
335
emitter.setUseInheritedMethods(true);
339
emitter.setImplCls(option.getArgument());
348
case OUTPUT_WSDL_MODE_OPT:
349
String modeArg = option.getArgument();
351
if ("All".equalsIgnoreCase(modeArg)) {
352
mode = Emitter.MODE_ALL;
353
} else if ("Interface".equalsIgnoreCase(modeArg)) {
354
mode = Emitter.MODE_INTERFACE;
355
} else if ("Implementation".equalsIgnoreCase(modeArg)) {
356
mode = Emitter.MODE_IMPLEMENTATION;
358
mode = Emitter.MODE_ALL;
360
System.err.println(Messages.getMessage("j2wmodeerror",
366
wsdlFilename = option.getArgument();
370
emitter.setInputWSDL(option.getArgument());
373
case OUTPUT_IMPL_OPT:
374
wsdlImplFilename = option.getArgument();
378
String packageName = option.getArgument(0);
379
String namespace = option.getArgument(1);
381
namespaceMap.put(packageName, namespace);
385
emitter.setIntfNamespace(option.getArgument());
388
case NAMESPACE_IMPL_OPT:
389
emitter.setImplNamespace(option.getArgument());
392
case SERVICE_ELEMENT_NAME_OPT:
393
emitter.setServiceElementName(option.getArgument());
396
case SERVICE_PORT_NAME_OPT:
397
emitter.setServicePortName(option.getArgument());
401
emitter.setLocationUrl(option.getArgument());
406
case LOCATION_IMPORT_OPT:
407
emitter.setImportUrl(option.getArgument());
410
case METHODS_NOTALLOWED_OPT:
411
emitter.setDisallowedMethods(option.getArgument());
414
case PORTTYPE_NAME_OPT:
415
emitter.setPortTypeName(option.getArgument());
418
case BINDING_NAME_OPT:
419
emitter.setBindingName(option.getArgument());
422
case STOP_CLASSES_OPT:
423
emitter.setStopClasses(option.getArgument());
426
case TYPEMAPPING_OPT:
427
value = option.getArgument();
428
typeMappingVersion = value;
432
value = option.getArgument();
434
if (value.equalsIgnoreCase("DEFAULT")) {
435
emitter.setSoapAction("DEFAULT");
436
} else if (value.equalsIgnoreCase("OPERATION")) {
437
emitter.setSoapAction("OPERATION");
438
} else if (value.equalsIgnoreCase("NONE")) {
439
emitter.setSoapAction("NONE");
442
Messages.getMessage("j2wBadSoapAction00"));
449
value = option.getArgument();
451
if (value.equalsIgnoreCase("DOCUMENT")
452
|| value.equalsIgnoreCase("RPC")
453
|| value.equalsIgnoreCase("WRAPPED")) {
454
emitter.setStyle(value);
456
System.out.println(Messages.getMessage("j2woptBadStyle00"));
463
value = option.getArgument();
465
if (value.equalsIgnoreCase("LITERAL")
466
|| value.equalsIgnoreCase("ENCODED")) {
467
emitter.setUse(value);
469
System.out.println(Messages.getMessage("j2woptBadUse00"));
475
case EXTRA_CLASSES_OPT:
477
emitter.setExtraClasses(option.getArgument());
478
} catch (ClassNotFoundException e) {
479
System.out.println(Messages.getMessage("j2woptBadClass00",
486
case IMPORT_SCHEMA_OPT:
487
emitter.setInputSchema(option.getArgument());
491
ClassUtils.setDefaultClassLoader(ClassUtils.createClassLoader(
492
option.getArgument(),
493
this.getClass().getClassLoader()));
509
* This method is invoked after the options are set to validate
510
* the option settings.
514
protected boolean validateOptions() {
516
// Can't proceed without a class name
517
if ((className == null)) {
518
System.out.println(Messages.getMessage("j2wMissingClass00"));
525
&& ((mode == Emitter.MODE_ALL)
526
|| (mode == Emitter.MODE_IMPLEMENTATION))) {
527
System.out.println(Messages.getMessage("j2wMissingLocation00"));
537
* checks the command-line arguments and runs the tool.
539
* @param args String[] command-line arguments.
542
protected int run(String[] args) {
544
// Parse the arguments
545
CLArgsParser argsParser = new CLArgsParser(args, options);
547
// Print parser errors, if any
548
if (null != argsParser.getErrorString()) {
550
Messages.getMessage("j2werror00", argsParser.getErrorString()));
556
// Get a list of parsed options
557
List clOptions = argsParser.getArguments();
558
int size = clOptions.size();
562
// Parse the options and configure the emitter as appropriate.
563
for (int i = 0; i < size; i++) {
564
if (parseOption((CLOption) clOptions.get(i)) == false) {
569
// validate argument combinations
570
if (validateOptions() == false) {
574
// Set the namespace map
575
if (!namespaceMap.isEmpty()) {
576
emitter.setNamespaceMap(namespaceMap);
579
TypeMappingRegistryImpl tmr = new TypeMappingRegistryImpl();
580
tmr.doRegisterFromVersion(typeMappingVersion);
581
emitter.setTypeMappingRegistry(tmr);
583
// Find the class using the name
584
emitter.setCls(className);
586
// Generate a full wsdl, or interface & implementation wsdls
587
if (wsdlImplFilename == null) {
588
emitter.emit(wsdlFilename, mode);
590
emitter.emit(wsdlFilename, wsdlImplFilename);
594
generateServerSide(emitter, (wsdlImplFilename != null) ? wsdlImplFilename : wsdlFilename);
596
// everything is good
598
} catch (Throwable t) {
606
* Generate the server side artifacts from the generated WSDL
608
* @param j2w the Java2WSDL emitter
609
* @param wsdlFileName the generated WSDL file
612
protected void generateServerSide(Emitter j2w, String wsdlFileName) throws Exception {
613
org.apache.axis.wsdl.toJava.Emitter w2j = new org.apache.axis.wsdl.toJava.Emitter();
614
File wsdlFile = new File(wsdlFileName);
615
w2j.setServiceDesc(j2w.getServiceDesc());
616
w2j.setQName2ClassMap(j2w.getQName2ClassMap());
617
w2j.setOutputDir(wsdlFile.getParent());
618
w2j.setServerSide(true);
619
w2j.setHelperWanted(true);
621
// setup namespace-to-package mapping
622
String ns = j2w.getIntfNamespace();
623
String pkg = j2w.getCls().getPackage().getName();
624
w2j.getNamespaceMap().put(ns, pkg);
626
Map nsmap = j2w.getNamespaceMap();
628
for (Iterator i = nsmap.keySet().iterator(); i.hasNext(); ) {
629
pkg = (String) i.next();
630
ns = (String) nsmap.get(pkg);
631
w2j.getNamespaceMap().put(ns, pkg);
638
if (j2w.getImplCls() != null) {
639
w2j.setImplementationClassName(j2w.getImplCls().getName());
641
if (!j2w.getCls().isInterface()) {
642
w2j.setImplementationClassName(j2w.getCls().getName());
644
throw new Exception("implementation class is not specified.");
648
w2j.run(wsdlFileName);
652
* print usage information and quit.
654
protected void printUsage() {
656
String lSep = System.getProperty("line.separator");
657
StringBuffer msg = new StringBuffer();
659
msg.append("Java2WSDL "
660
+ Messages.getMessage("j2wemitter00")).append(lSep);
664
"java " + getClass().getName()
665
+ " [options] class-of-portType")).append(lSep);
666
msg.append(Messages.getMessage("j2woptions00")).append(lSep);
667
msg.append(CLUtil.describeOptions(options).toString());
668
msg.append(Messages.getMessage("j2wdetails00")).append(lSep);
669
System.out.println(msg.toString());
674
* Run the Java2WSDL emitter with the specified command-line arguments
676
* @param args String[] command-line arguments
678
public static void main(String args[]) {
680
Java2WSDL java2wsdl = new Java2WSDL();
682
System.exit(java2wsdl.run(args));