1 16 package org.apache.axis.tools.ant.wsdl; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.net.Authenticator ; 21 import java.util.ArrayList ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 25 import org.apache.axis.constants.Scope; 26 import org.apache.axis.utils.DefaultAuthenticator; 27 import org.apache.axis.utils.ClassUtils; 28 import org.apache.axis.wsdl.toJava.Emitter; 29 import org.apache.axis.wsdl.toJava.FactoryProperty; 30 import org.apache.axis.wsdl.toJava.NamespaceSelector; 31 import org.apache.tools.ant.BuildException; 32 import org.apache.tools.ant.Project; 33 import org.apache.tools.ant.Task; 34 import org.apache.tools.ant.AntClassLoader; 35 import org.apache.tools.ant.types.Path; 36 import org.apache.tools.ant.types.CommandlineJava; 37 import org.apache.tools.ant.types.Environment; 38 39 44 45 79 public class Wsdl2javaAntTask extends Task 80 { 81 private boolean verbose = false; 82 private boolean debug = false; 83 private boolean quiet = false; 84 private boolean server = false; 85 private boolean skeletonDeploy = false; 86 private boolean testCase = false; 87 private boolean noImports = false; 88 private boolean all = false; 89 private boolean helperGen = false; 90 private boolean noWrapped = false; 91 private boolean allowInvalidURL = false; 92 private String factory = null; 93 private HashMap namespaceMap = new HashMap (); 94 private String output = "."; 95 private String protocolHandlerPkgs = ""; 96 private String deployScope = ""; 97 private String url = ""; 98 private String typeMappingVersion = "1.2"; 99 private long timeout = 45000; 100 private File namespaceMappingFile = null; 101 private MappingSet mappings = new MappingSet(); 102 private String username = null; 103 private String password = null; 104 private Path classpath = null; 105 private List nsIncludes = new ArrayList (); 106 private List nsExcludes = new ArrayList (); 107 private List properties = new ArrayList (); 108 private String implementationClassName = null; 109 private CommandlineJava commandline = new CommandlineJava(); 110 111 114 private boolean printStackTraceOnFailure = true; 115 119 private boolean failOnNetworkErrors = false; 120 121 private boolean wrapArrays = false; 122 123 public Wsdl2javaAntTask() { 124 } 125 126 130 protected void validate() 131 throws BuildException { 132 if (url == null || url.length() == 0) { 133 throw new BuildException("No url specified"); 134 } 135 if (timeout < -1) { 136 throw new BuildException("negative timeout supplied"); 137 } 138 File outdir = new File (output); 139 if (!outdir.isDirectory() || !outdir.exists()) { 140 throw new BuildException("output directory is not valid"); 141 } 142 if (quiet) { 143 if (verbose) { 144 throw new BuildException("quiet and verbose options are " + 145 "exclusive"); 146 } 147 if (debug) { 148 throw new BuildException("quiet and debug options are " + 149 "exclusive"); 150 } 151 } 152 } 153 154 159 public void traceParams(int logLevel) { 160 log("Running Wsdl2javaAntTask with parameters:", logLevel); 161 log("\tverbose:" + verbose, logLevel); 162 log("\tdebug:" + debug, logLevel); 163 log("\tquiet:" + quiet, logLevel); 164 log("\tserver-side:" + server, logLevel); 165 log("\tskeletonDeploy:" + skeletonDeploy, logLevel); 166 log("\thelperGen:" + helperGen, logLevel); 167 log("\tfactory:" + factory, logLevel); 168 log("\tnsIncludes:" + nsIncludes, logLevel); 169 log("\tnsExcludes:" + nsExcludes, logLevel); 170 log("\tfactoryProps:" + properties, logLevel); 171 log("\ttestCase:" + testCase, logLevel); 172 log("\tnoImports:" + noImports, logLevel); 173 log("\tNStoPkg:" + namespaceMap, logLevel); 174 log("\toutput:" + output, logLevel); 175 log("\tprotocolHandlerPkgs:" + protocolHandlerPkgs, logLevel); 176 log("\tdeployScope:" + deployScope, logLevel); 177 log("\tURL:" + url, logLevel); 178 log("\tall:" + all, logLevel); 179 log("\ttypeMappingVersion:" + typeMappingVersion, logLevel); 180 log("\ttimeout:" + timeout, logLevel); 181 log("\tfailOnNetworkErrors:" + failOnNetworkErrors, logLevel); 182 log("\tprintStackTraceOnFailure:" + printStackTraceOnFailure, logLevel); 183 log("\tnamespaceMappingFile:" + namespaceMappingFile, logLevel); 184 log("\tusername:" + username, logLevel); 185 log("\t:password" + password, logLevel); 186 log("\t:noWrapped" + noWrapped, logLevel); 187 log("\t:allowInvalidURL" + allowInvalidURL, logLevel); 188 log("\t:implementationClassName" + implementationClassName, logLevel); 189 log("\t:classpath" + classpath, logLevel); 190 traceNetworkSettings(logLevel); 191 } 192 193 197 public void execute() throws BuildException { 198 201 traceParams(Project.MSG_VERBOSE); 202 validate(); 203 CommandlineJava.SysProperties sysProperties = 204 commandline.getSystemProperties(); 205 if (sysProperties != null) { 206 sysProperties.setSystem(); 207 } 208 209 try { 210 Emitter emitter = createEmitter(); 212 213 Scope scope = Scope.getScope(deployScope, null); 215 if (scope != null) { 216 emitter.setScope(scope); 217 } else if (deployScope.length() == 0 218 || "none".equalsIgnoreCase(deployScope)) { 219 ; 220 } else { 221 log("Unrecognized scope: " + deployScope + ". Ignoring it.", Project.MSG_VERBOSE); 222 } 223 224 mappings.execute(this, namespaceMap, false); 226 if (!namespaceMap.isEmpty()) { 227 emitter.setNamespaceMap(namespaceMap); 228 } 229 emitter.setTestCaseWanted(testCase); 230 emitter.setHelperWanted(helperGen); 231 if (factory != null) { 232 emitter.setFactory(factory); 233 } 234 emitter.setNamespaceIncludes(nsIncludes); 235 emitter.setNamespaceExcludes(nsExcludes); 236 emitter.setProperties(properties); 237 emitter.setImports(!noImports); 238 emitter.setAllWanted(all); 239 emitter.setOutputDir(output); 240 emitter.setServerSide(server); 241 emitter.setSkeletonWanted(skeletonDeploy); 242 emitter.setVerbose(verbose); 243 emitter.setDebug(debug); 244 emitter.setQuiet(quiet); 245 emitter.setTypeMappingVersion(typeMappingVersion); 246 emitter.setNowrap(noWrapped); 247 emitter.setAllowInvalidURL(allowInvalidURL); 248 emitter.setWrapArrays(wrapArrays); 249 if (namespaceMappingFile != null) { 250 emitter.setNStoPkg(namespaceMappingFile.toString()); 251 } 252 emitter.setTimeout(timeout); 253 emitter.setImplementationClassName(implementationClassName); 254 255 Authenticator.setDefault(new DefaultAuthenticator(username, password)); 256 if (classpath != null) { 257 AntClassLoader cl = new AntClassLoader( 258 getClass().getClassLoader(), 259 getProject(), 260 classpath, 261 false); 262 log("Using CLASSPATH " + cl.getClasspath(), 263 Project.MSG_VERBOSE); 264 ClassUtils.setDefaultClassLoader(cl); 265 } 266 267 try { 268 if(url.indexOf(':') == -1) 269 url = getProject().resolveFile(url).getAbsolutePath(); 270 } catch (Throwable t){ 271 } 272 273 log("WSDL2Java " + url, Project.MSG_INFO); 274 try { 275 emitter.run(url); 276 } catch (Throwable e) { 277 if (url.startsWith("http://")) { 278 if (!failOnNetworkErrors) { 282 log(e.toString(), Project.MSG_WARN); 285 return; 286 } else { 287 throw new BuildException("Could not build " + url, e); 289 } 290 } else { 291 throw e; 292 } 293 } 294 } catch (BuildException b) { 295 throw b; 298 } catch (Throwable t) { 299 if (printStackTraceOnFailure) { 300 traceParams(Project.MSG_INFO); 301 t.printStackTrace(); 302 } 303 throw new BuildException("WSDL processing error for " 305 + url +" :\n "+t.getMessage() , t); 306 } finally { 307 if (sysProperties != null) { 308 sysProperties.restoreSystem(); 309 } 310 } 311 312 } 313 314 319 public void setVerbose(boolean verbose) { 320 this.verbose = verbose; 321 } 322 323 328 public void setDebug(boolean debug) { 329 this.debug = debug; 330 } 331 332 337 public void setQuiet(boolean quiet) { 338 this.quiet = quiet; 339 } 340 341 344 public void setServerSide(boolean parameter) { 345 this.server = parameter; 346 } 347 348 352 public void setSkeletonDeploy(boolean parameter) { 353 this.skeletonDeploy = parameter; 354 } 355 356 360 public void setTestCase(boolean parameter) { 361 this.testCase = parameter; 362 } 363 364 368 public void setHelperGen(boolean parameter) { 369 this.helperGen = parameter; 370 } 371 372 376 public void setFactory(String parameter) { 377 this.factory = parameter; 378 } 379 380 384 public void setNoImports(boolean parameter) { 385 this.noImports = parameter; 386 } 387 388 391 public void setOutput(File parameter) throws BuildException { 392 try { 393 this.output = parameter.getCanonicalPath(); 394 } catch (IOException ioe) { 395 throw new BuildException(ioe); 396 } 397 } 398 399 402 public void setProtocolHandlerPkgs(String handlerPkgs) { 403 String currentPkgs = System.getProperty("java.protocol.handler.pkgs"); 404 String newPkgs = null; 405 406 if (currentPkgs == null) 407 newPkgs = handlerPkgs; 408 else 409 newPkgs = currentPkgs + "|" + handlerPkgs; 411 412 System.setProperty("java.protocol.handler.pkgs", newPkgs); 413 } 414 415 419 public void setDeployScope(String scope) { 420 this.deployScope = scope; 421 } 422 428 432 public void setURL(String parameter) { 433 this.url = parameter; 434 } 435 436 440 public void setAll(boolean parameter) { 441 this.all = parameter; 442 } 443 444 449 public void setTypeMappingVersion(TypeMappingVersionEnum parameter) { 450 this.typeMappingVersion = parameter.getValue(); 451 } 452 453 458 public void setTimeout(long parameter) { 459 this.timeout = parameter; 460 } 461 462 465 public void addMapping(NamespaceMapping mapping) { 466 mappings.addMapping(mapping); 467 } 468 469 472 public void addMappingSet(MappingSet mappingset) { 473 mappings.addMappingSet(mappingset); 474 } 475 476 482 public void setNamespaceMappingFile(File namespaceMappingFile) { 483 this.namespaceMappingFile = namespaceMappingFile; 484 } 485 486 489 497 498 503 public void setFailOnNetworkErrors(boolean failOnNetworkErrors) { 504 this.failOnNetworkErrors = failOnNetworkErrors; 505 } 506 507 512 public void setPrintStackTraceOnFailure(boolean printStackTraceOnFailure) { 513 this.printStackTraceOnFailure = printStackTraceOnFailure; 514 } 515 516 521 public void setUsername(String username) { 522 this.username = username; 523 } 524 525 531 public void setPassword(String password) { 532 this.password = password; 533 } 534 535 539 public void setNoWrapped(boolean noWrapped) { 540 this.noWrapped = noWrapped; 541 } 542 543 546 public void setAllowInvalidUrl(boolean b) { 547 this.allowInvalidURL = b; 548 } 549 550 557 public void setImplementationClassName(String implementationClassName) { 558 this.implementationClassName = implementationClassName; 559 } 560 561 562 569 public void setWrapArrays(boolean wrapArrays) { 570 this.wrapArrays = wrapArrays; 571 } 572 573 577 public Path createClasspath() { 578 if (classpath == null) { 579 classpath = new Path(getProject()); 580 } 581 return classpath.createPath(); 582 } 583 584 587 public NamespaceSelector createNsInclude() { 588 NamespaceSelector selector = new NamespaceSelector(); 589 nsIncludes.add(selector); 590 return selector; 591 } 592 593 596 public NamespaceSelector createNsExclude() { 597 NamespaceSelector selector = new NamespaceSelector(); 598 nsExcludes.add(selector); 599 return selector; 600 } 601 602 605 public FactoryProperty createProperty() { 606 FactoryProperty property = new FactoryProperty(); 607 properties.add(property); 608 return property; 609 } 610 611 614 protected Emitter createEmitter() { 615 return new Emitter(); 616 } 617 618 protected NamespaceSelector createSelector() { 619 return new NamespaceSelector(); 620 } 621 622 private void traceSystemSetting(String setting, int logLevel) { 623 String value = System.getProperty(setting); 624 log("\t" + setting + "=" + value, logLevel); 625 } 626 627 private void traceNetworkSettings(int logLevel) { 628 traceSystemSetting("http.proxyHost", logLevel); 629 traceSystemSetting("http.proxyPort", logLevel); 630 traceSystemSetting("http.proxyUser", logLevel); 631 traceSystemSetting("http.proxyPassword", logLevel); 632 traceSystemSetting("socks.proxyHost", logLevel); 633 traceSystemSetting("socks.proxyPort", logLevel); 634 } 635 636 640 public void addSysproperty(Environment.Variable sysp) { 641 commandline.addSysproperty(sysp); 642 } 643 } 644 645 | Popular Tags |