1 16 package org.apache.axis.wsdl; 17 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; 27 28 import java.io.File ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 40 public class Java2WSDL { 41 42 44 45 protected static final int INHERITED_CLASS_OPT = 'a'; 46 47 48 protected static final int SOAPACTION_OPT = 'A'; 49 50 51 protected static final int BINDING_NAME_OPT = 'b'; 52 53 54 protected static final int STOP_CLASSES_OPT = 'c'; 55 56 57 protected static final int IMPORT_SCHEMA_OPT = 'C'; 58 59 60 protected static final int EXTRA_CLASSES_OPT = 'e'; 61 62 63 protected static final int HELP_OPT = 'h'; 64 65 66 protected static final int IMPL_CLASS_OPT = 'i'; 67 68 69 protected static final int INPUT_OPT = 'I'; 70 71 72 protected static final int LOCATION_OPT = 'l'; 73 74 75 protected static final int LOCATION_IMPORT_OPT = 'L'; 76 77 78 protected static final int METHODS_ALLOWED_OPT = 'm'; 79 80 81 protected static final int NAMESPACE_OPT = 'n'; 82 83 84 protected static final int NAMESPACE_IMPL_OPT = 'N'; 85 86 87 protected static final int OUTPUT_OPT = 'o'; 88 89 90 protected static final int OUTPUT_IMPL_OPT = 'O'; 91 92 93 protected static final int PACKAGE_OPT = 'p'; 94 95 96 protected static final int PORTTYPE_NAME_OPT = 'P'; 97 98 99 protected static final int SERVICE_PORT_NAME_OPT = 's'; 100 101 102 protected static final int SERVICE_ELEMENT_NAME_OPT = 'S'; 103 104 105 protected static final int TYPEMAPPING_OPT = 'T'; 106 107 108 protected static final int USE_OPT = 'u'; 109 110 111 protected static final int OUTPUT_WSDL_MODE_OPT = 'w'; 112 113 114 protected static final int METHODS_NOTALLOWED_OPT = 'x'; 115 116 protected static final int CLASSPATH_OPT = 'X'; 117 118 119 protected static final int STYLE_OPT = 'y'; 120 121 122 protected static final int DEPLOY_OPT = 'd'; 123 124 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, 139 OUTPUT_OPT, 140 Messages.getMessage("j2woptoutput00")), 141 new CLOptionDescriptor("location", 142 CLOptionDescriptor.ARGUMENT_REQUIRED, 143 LOCATION_OPT, 144 Messages.getMessage("j2woptlocation00")), 145 new CLOptionDescriptor("portTypeName", 146 CLOptionDescriptor.ARGUMENT_REQUIRED, 147 PORTTYPE_NAME_OPT, 148 Messages.getMessage("j2woptportTypeName00")), 149 new CLOptionDescriptor("bindingName", 150 CLOptionDescriptor.ARGUMENT_REQUIRED, 151 BINDING_NAME_OPT, 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, 163 NAMESPACE_OPT, 164 Messages.getMessage("j2woptnamespace00")), 165 new CLOptionDescriptor("PkgtoNS", 166 CLOptionDescriptor.DUPLICATES_ALLOWED 167 + CLOptionDescriptor.ARGUMENTS_REQUIRED_2, 168 PACKAGE_OPT, 169 Messages.getMessage("j2woptPkgtoNS00")), 170 new CLOptionDescriptor("methods", 171 CLOptionDescriptor.DUPLICATES_ALLOWED 172 + CLOptionDescriptor.ARGUMENT_REQUIRED, 173 METHODS_ALLOWED_OPT, 174 Messages.getMessage("j2woptmethods00")), 175 new CLOptionDescriptor("all", CLOptionDescriptor.ARGUMENT_DISALLOWED, 176 INHERITED_CLASS_OPT, 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, 184 LOCATION_IMPORT_OPT, 185 Messages.getMessage("j2woptlocationImport00")), 186 new CLOptionDescriptor("namespaceImpl", 187 CLOptionDescriptor.ARGUMENT_REQUIRED, 188 NAMESPACE_IMPL_OPT, 189 Messages.getMessage("j2woptnamespaceImpl00")), 190 new CLOptionDescriptor("outputImpl", 191 CLOptionDescriptor.ARGUMENT_REQUIRED, 192 OUTPUT_IMPL_OPT, 193 Messages.getMessage("j2woptoutputImpl00")), 194 new CLOptionDescriptor("implClass", 195 CLOptionDescriptor.ARGUMENT_REQUIRED, 196 IMPL_CLASS_OPT, 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, 206 STOP_CLASSES_OPT, 207 Messages.getMessage("j2woptstopClass00")), 208 new CLOptionDescriptor("typeMappingVersion", 209 CLOptionDescriptor.ARGUMENT_REQUIRED, 210 TYPEMAPPING_OPT, 211 Messages.getMessage("j2wopttypeMapping00")), 212 new CLOptionDescriptor("soapAction", 213 CLOptionDescriptor.ARGUMENT_REQUIRED, 214 SOAPACTION_OPT, 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, 223 EXTRA_CLASSES_OPT, 224 Messages.getMessage("j2woptExtraClasses00")), 225 new CLOptionDescriptor("importSchema", 226 CLOptionDescriptor.ARGUMENT_OPTIONAL, 227 IMPORT_SCHEMA_OPT, 228 Messages.getMessage("j2woptImportSchema00")), 229 new CLOptionDescriptor("classpath", 230 CLOptionDescriptor.ARGUMENT_OPTIONAL, 231 CLASSPATH_OPT, 232 Messages.getMessage("optionClasspath")), 233 new CLOptionDescriptor("deploy", 234 CLOptionDescriptor.ARGUMENT_DISALLOWED, 235 DEPLOY_OPT, 236 Messages.getMessage("j2woptDeploy00")), 237 }; 238 239 240 protected Emitter emitter; 241 242 243 protected String className = null; 244 245 246 protected String wsdlFilename = null; 247 248 249 protected String wsdlImplFilename = null; 250 251 252 protected HashMap namespaceMap = new HashMap (); 253 254 255 protected int mode = Emitter.MODE_ALL; 256 257 258 boolean locationSet = false; 259 260 261 protected String typeMappingVersion = "1.2"; 262 263 264 protected boolean isDeploy = false; 265 266 269 protected Java2WSDL() { 270 emitter = createEmitter(); 271 } 273 278 protected Emitter createEmitter() { 279 return new Emitter(); 280 } 282 289 protected void addOptions(CLOptionDescriptor[] newOptions) { 290 291 if ((newOptions != null) && (newOptions.length > 0)) { 292 CLOptionDescriptor[] allOptions = 293 new CLOptionDescriptor[options.length + newOptions.length]; 294 295 System.arraycopy(options, 0, allOptions, 0, options.length); 296 System.arraycopy(newOptions, 0, allOptions, options.length, 297 newOptions.length); 298 299 options = allOptions; 300 } 301 } 302 303 309 protected boolean parseOption(CLOption option) { 310 311 String value; 312 boolean status = true; 313 314 switch (option.getId()) { 315 316 case CLOption.TEXT_ARGUMENT: 317 if (className != null) { 318 System.out.println( 319 Messages.getMessage( 320 "j2wDuplicateClass00", className, 321 option.getArgument())); 322 printUsage(); 323 324 status = false; } 326 327 className = option.getArgument(); 328 break; 329 330 case METHODS_ALLOWED_OPT: 331 emitter.setAllowedMethods(option.getArgument()); 332 break; 333 334 case INHERITED_CLASS_OPT: 335 emitter.setUseInheritedMethods(true); 336 break; 337 338 case IMPL_CLASS_OPT: 339 emitter.setImplCls(option.getArgument()); 340 break; 341 342 case HELP_OPT: 343 printUsage(); 344 345 status = false; 346 break; 347 348 case OUTPUT_WSDL_MODE_OPT: 349 String modeArg = option.getArgument(); 350 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; 357 } else { 358 mode = Emitter.MODE_ALL; 359 360 System.err.println(Messages.getMessage("j2wmodeerror", 361 modeArg)); 362 } 363 break; 364 365 case OUTPUT_OPT: 366 wsdlFilename = option.getArgument(); 367 break; 368 369 case INPUT_OPT: 370 emitter.setInputWSDL(option.getArgument()); 371 break; 372 373 case OUTPUT_IMPL_OPT: 374 wsdlImplFilename = option.getArgument(); 375 break; 376 377 case PACKAGE_OPT: 378 String packageName = option.getArgument(0); 379 String namespace = option.getArgument(1); 380 381 namespaceMap.put(packageName, namespace); 382 break; 383 384 case NAMESPACE_OPT: 385 emitter.setIntfNamespace(option.getArgument()); 386 break; 387 388 case NAMESPACE_IMPL_OPT: 389 emitter.setImplNamespace(option.getArgument()); 390 break; 391 392 case SERVICE_ELEMENT_NAME_OPT: 393 emitter.setServiceElementName(option.getArgument()); 394 break; 395 396 case SERVICE_PORT_NAME_OPT: 397 emitter.setServicePortName(option.getArgument()); 398 break; 399 400 case LOCATION_OPT: 401 emitter.setLocationUrl(option.getArgument()); 402 403 locationSet = true; 404 break; 405 406 case LOCATION_IMPORT_OPT: 407 emitter.setImportUrl(option.getArgument()); 408 break; 409 410 case METHODS_NOTALLOWED_OPT: 411 emitter.setDisallowedMethods(option.getArgument()); 412 break; 413 414 case PORTTYPE_NAME_OPT: 415 emitter.setPortTypeName(option.getArgument()); 416 break; 417 418 case BINDING_NAME_OPT: 419 emitter.setBindingName(option.getArgument()); 420 break; 421 422 case STOP_CLASSES_OPT: 423 emitter.setStopClasses(option.getArgument()); 424 break; 425 426 case TYPEMAPPING_OPT: 427 value = option.getArgument(); 428 typeMappingVersion = value; 429 break; 430 431 case SOAPACTION_OPT: 432 value = option.getArgument(); 433 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"); 440 } else { 441 System.out.println( 442 Messages.getMessage("j2wBadSoapAction00")); 443 444 status = false; 445 } 446 break; 447 448 case STYLE_OPT: 449 value = option.getArgument(); 450 451 if (value.equalsIgnoreCase("DOCUMENT") 452 || value.equalsIgnoreCase("RPC") 453 || value.equalsIgnoreCase("WRAPPED")) { 454 emitter.setStyle(value); 455 } else { 456 System.out.println(Messages.getMessage("j2woptBadStyle00")); 457 458 status = false; 459 } 460 break; 461 462 case USE_OPT: 463 value = option.getArgument(); 464 465 if (value.equalsIgnoreCase("LITERAL") 466 || value.equalsIgnoreCase("ENCODED")) { 467 emitter.setUse(value); 468 } else { 469 System.out.println(Messages.getMessage("j2woptBadUse00")); 470 471 status = false; 472 } 473 break; 474 475 case EXTRA_CLASSES_OPT: 476 try { 477 emitter.setExtraClasses(option.getArgument()); 478 } catch (ClassNotFoundException e) { 479 System.out.println(Messages.getMessage("j2woptBadClass00", 480 e.toString())); 481 482 status = false; 483 } 484 break; 485 486 case IMPORT_SCHEMA_OPT: 487 emitter.setInputSchema(option.getArgument()); 488 break; 489 490 case CLASSPATH_OPT: 491 ClassUtils.setDefaultClassLoader(ClassUtils.createClassLoader( 492 option.getArgument(), 493 this.getClass().getClassLoader())); 494 break; 495 496 case DEPLOY_OPT: 497 isDeploy = true; 498 break; 499 500 default : 501 break; 502 } 503 504 return status; 505 } 506 507 514 protected boolean validateOptions() { 515 516 if ((className == null)) { 518 System.out.println(Messages.getMessage("j2wMissingClass00")); 519 printUsage(); 520 521 return false; 522 } 523 524 if (!locationSet 525 && ((mode == Emitter.MODE_ALL) 526 || (mode == Emitter.MODE_IMPLEMENTATION))) { 527 System.out.println(Messages.getMessage("j2wMissingLocation00")); 528 printUsage(); 529 530 return false; 531 } 532 return true; } 534 535 542 protected int run(String [] args) { 543 544 CLArgsParser argsParser = new CLArgsParser(args, options); 546 547 if (null != argsParser.getErrorString()) { 549 System.err.println( 550 Messages.getMessage("j2werror00", argsParser.getErrorString())); 551 printUsage(); 552 553 return (1); 554 } 555 556 List clOptions = argsParser.getArguments(); 558 int size = clOptions.size(); 559 560 try { 561 562 for (int i = 0; i < size; i++) { 564 if (parseOption((CLOption) clOptions.get(i)) == false) { 565 return (1); 566 } 567 } 568 569 if (validateOptions() == false) { 571 return (1); 572 } 573 574 if (!namespaceMap.isEmpty()) { 576 emitter.setNamespaceMap(namespaceMap); 577 } 578 579 TypeMappingRegistryImpl tmr = new TypeMappingRegistryImpl(); 580 tmr.doRegisterFromVersion(typeMappingVersion); 581 emitter.setTypeMappingRegistry(tmr); 582 583 emitter.setCls(className); 585 586 if (wsdlImplFilename == null) { 588 emitter.emit(wsdlFilename, mode); 589 } else { 590 emitter.emit(wsdlFilename, wsdlImplFilename); 591 } 592 593 if (isDeploy) { 594 generateServerSide(emitter, (wsdlImplFilename != null) ? wsdlImplFilename : wsdlFilename); 595 } 596 return (0); 598 } catch (Throwable t) { 599 t.printStackTrace(); 600 601 return (1); 602 } 603 } 605 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); 620 621 String ns = j2w.getIntfNamespace(); 623 String pkg = j2w.getCls().getPackage().getName(); 624 w2j.getNamespaceMap().put(ns, pkg); 625 626 Map nsmap = j2w.getNamespaceMap(); 627 if (nsmap != null) { 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); 632 } 633 } 634 635 w2j.setDeploy(true); 637 638 if (j2w.getImplCls() != null) { 639 w2j.setImplementationClassName(j2w.getImplCls().getName()); 640 } else { 641 if (!j2w.getCls().isInterface()) { 642 w2j.setImplementationClassName(j2w.getCls().getName()); 643 } else { 644 throw new Exception ("implementation class is not specified."); 645 } 646 } 647 648 w2j.run(wsdlFileName); 649 } 650 654 protected void printUsage() { 655 656 String lSep = System.getProperty("line.separator"); 657 StringBuffer msg = new StringBuffer (); 658 659 msg.append("Java2WSDL " 660 + Messages.getMessage("j2wemitter00")).append(lSep); 661 msg.append( 662 Messages.getMessage( 663 "j2wusage00", 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()); 670 } 671 672 678 public static void main(String args[]) { 679 680 Java2WSDL java2wsdl = new Java2WSDL(); 681 682 System.exit(java2wsdl.run(args)); 683 } 684 } 685 | Popular Tags |