1 16 package org.apache.axis.tools.ant.wsdl; 17 18 import org.apache.axis.encoding.TypeMappingImpl; 19 import org.apache.axis.encoding.TypeMappingRegistryImpl; 20 import org.apache.axis.encoding.TypeMappingDelegate; 21 import org.apache.axis.utils.ClassUtils; 22 import org.apache.axis.wsdl.fromJava.Emitter; 23 import org.apache.tools.ant.AntClassLoader; 24 import org.apache.tools.ant.BuildException; 25 import org.apache.tools.ant.Project; 26 import org.apache.tools.ant.Task; 27 import org.apache.tools.ant.types.Path; 28 import org.apache.tools.ant.types.Reference; 29 import org.apache.tools.ant.types.Environment; 30 import org.apache.tools.ant.types.CommandlineJava; 31 32 import java.io.File ; 33 import java.io.PrintWriter ; 34 import java.io.StringWriter ; 35 import java.util.HashMap ; 36 import java.util.Iterator ; 37 import java.util.LinkedList ; 38 import java.util.List ; 39 import java.util.Map ; 40 import java.util.StringTokenizer ; 41 42 54 60 61 public class Java2WsdlAntTask extends Task 62 { 63 private String namespace = ""; 64 private String namespaceImpl = null; 65 private HashMap namespaceMap = new HashMap (); 66 private String location = ""; 67 private String locationImport = null; 68 private String output = "." ; 69 private String importSchema = null ; 70 private String input = null ; 71 private String outputImpl = null; 72 private String className = "." ; 73 private String servicePortName = null ; 74 private String portTypeName = null ; 75 private String bindingName = null ; 76 private String implClass = null; 77 private boolean useInheritedMethods = false; 78 private String exclude = null; 79 private String stopClasses = null; 80 private String typeMappingVersion = TypeMappingVersionEnum.DEFAULT_VERSION; 81 private String style = null; 82 private String serviceElementName=null; 83 private String methods=null; 84 private String use = null; 85 private MappingSet mappings=new MappingSet(); 86 private String extraClasses = null; 87 private Path classpath = null; 88 private String soapAction = null; 89 private List complexTypes = new LinkedList (); 90 private boolean isDeploy = false; 91 private CommandlineJava commandline = new CommandlineJava(); 92 93 98 public void traceParams(int logLevel) { 99 log("Running Java2WsdlAntTask with parameters:", logLevel); 100 log("\tnamespace:" + namespace, logLevel); 101 log("\tPkgtoNS:" + namespaceMap, logLevel); 102 log("\tlocation:" + location, logLevel); 103 log("\toutput:" + output, logLevel); 104 log("\timportSchema:" + importSchema, logLevel); 105 log("\tinput:" + input, logLevel); 106 log("\tclassName:" + className, logLevel); 107 log("\tservicePortName:" + servicePortName, logLevel); 108 log("\tportTypeName:" + portTypeName, logLevel); 109 log("\tbindingName:" + bindingName, logLevel); 110 log("\timplClass:" + implClass, logLevel); 111 log("\tinheritance:" + useInheritedMethods, logLevel); 112 log("\texcluded:" + exclude, logLevel); 113 log("\tstopClasses:" + stopClasses, logLevel); 114 log("\ttypeMappingVersion:" + typeMappingVersion, logLevel); 115 log("\tstyle:" + style, logLevel); 116 log("\toutputImpl:" + outputImpl, logLevel); 117 log("\tuse:" + use, logLevel); 118 log("\tnamespaceImpl:" + namespaceImpl, logLevel); 119 log("\tlocationImport:" + locationImport, logLevel); 120 log("\tserviceElementName:" + serviceElementName, logLevel); 121 log("\tmethods:" + methods, logLevel); 122 log("\textraClasses:" + extraClasses, logLevel); 123 log("\tsoapAction:" + soapAction, logLevel); 124 log("\tclasspath:" + classpath, logLevel); 125 126 } 127 128 132 protected void validate() 133 throws BuildException { 134 if(className==null || className.length() ==0) { 135 throw new BuildException("No classname was specified"); 136 } 137 if(location==null || location.length() == 0) { 138 throw new BuildException("No location was specified"); 139 } 140 } 141 142 146 public void execute() throws BuildException { 147 AntClassLoader cl = new AntClassLoader(getClass().getClassLoader(), 148 getProject(), 149 classpath == null ? createClasspath() : classpath, 150 false); 151 152 ClassUtils.setDefaultClassLoader(cl); 153 if (extraClasses != null) { 155 StringTokenizer tokenizer = new StringTokenizer (extraClasses, " ,"); 156 while (tokenizer.hasMoreTokens()) { 157 String clsName = tokenizer.nextToken(); 158 ClassUtils.setClassLoader(clsName, cl); 159 } 160 } 161 162 CommandlineJava.SysProperties sysProperties = 163 commandline.getSystemProperties(); 164 if (sysProperties != null) { 165 sysProperties.setSystem(); 166 } 167 try { 168 traceParams(Project.MSG_VERBOSE); 169 validate(); 170 171 Emitter emitter = new Emitter(); 173 mappings.execute(this,namespaceMap, true); 175 if (!namespaceMap.isEmpty()) { 176 emitter.setNamespaceMap(namespaceMap); 177 } 178 if (servicePortName != null) { 179 emitter.setServicePortName(servicePortName); 180 } 181 if (portTypeName != null) { 182 emitter.setPortTypeName(portTypeName); 183 } 184 if (bindingName != null) { 185 emitter.setBindingName(bindingName); 186 } 187 log("Java2WSDL " + className, Project.MSG_INFO); 188 emitter.setCls(className); 189 if (implClass != null) { 190 emitter.setImplCls(implClass); 191 } 192 if (exclude != null) { 193 emitter.setDisallowedMethods(exclude); 194 } 195 if (stopClasses != null) { 196 emitter.setStopClasses(stopClasses); 197 } 198 if (extraClasses != null) { 199 emitter.setExtraClasses(extraClasses); 200 } 201 202 TypeMappingRegistryImpl tmr = new TypeMappingRegistryImpl(); 203 tmr.doRegisterFromVersion(typeMappingVersion); 204 emitter.setTypeMappingRegistry(tmr); 205 206 TypeMappingDelegate tmi = (TypeMappingDelegate)tmr.getDefaultTypeMapping(); 208 Iterator i = complexTypes.iterator(); 209 while (i.hasNext()) { 210 ((ComplexType) i.next()).register(tmi); 211 } 212 213 if (style != null) { 214 emitter.setStyle(style); 215 } 216 217 if (use != null) { 218 emitter.setUse(use); 219 } 220 221 if (importSchema != null) { 222 emitter.setInputSchema(importSchema); 223 } 224 if (input != null) { 225 emitter.setInputWSDL(input); 226 } 227 emitter.setIntfNamespace(namespace); 228 emitter.setImplNamespace(namespaceImpl); 229 emitter.setLocationUrl(location); 230 emitter.setImportUrl(locationImport); 231 emitter.setUseInheritedMethods(useInheritedMethods); 232 if(serviceElementName!=null) { 233 emitter.setServiceElementName(serviceElementName); 234 } 235 if(methods!=null) { 236 emitter.setAllowedMethods(methods); 237 } 238 if (soapAction != null) { 239 emitter.setSoapAction(soapAction); 240 } 241 if (outputImpl == null) { 242 emitter.emit(output, Emitter.MODE_ALL); 244 } else { 245 emitter.emit(output, outputImpl); 247 } 248 249 if (isDeploy == true) { 250 generateServerSide(emitter, (outputImpl != null) ? outputImpl : output); 251 } 252 253 } catch(BuildException b) { 254 throw b; 256 } catch (Throwable t) { 257 StringWriter writer = new StringWriter (); 259 t.printStackTrace(new PrintWriter (writer)); 260 log(writer.getBuffer().toString(), Project.MSG_ERR); 261 throw new BuildException("Error while running " + getClass().getName(), t); 262 } finally { 263 if (sysProperties != null) { 264 sysProperties.restoreSystem(); 265 } 266 } 267 } 268 269 275 public void setOutput(File parameter) { 276 this.output = parameter.getPath(); 277 } 278 279 284 public void setImportSchema(File parameter) throws BuildException { 285 try { 286 this.importSchema = parameter.toURL().toString(); 287 } catch (java.io.IOException ioe) { 288 throw new BuildException(ioe); 289 } 290 } 291 292 300 public void setInput(File parameter) { 301 this.input = parameter.getPath(); 302 } 303 304 310 public void setOutputImpl(File parameter) { 311 this.outputImpl = parameter.getPath(); 312 } 313 314 321 public void setLocation(String parameter) { 322 this.location = parameter; 323 } 324 325 330 public void setLocationImport(String parameter) { 331 this.locationImport = parameter; 332 } 333 334 339 public void setClassName(String parameter) { 340 this.className = parameter; 341 } 342 343 348 public void setImplClass(String parameter) { 349 this.implClass = parameter; 350 } 351 352 356 public void setServicePortName(String parameter) { 357 this.servicePortName = parameter; 358 } 359 360 365 public void setPortTypeName(String parameter) { 366 this.portTypeName = parameter; 367 } 368 369 375 public void setBindingName(String parameter) { 376 this.bindingName = parameter; 377 } 378 379 383 public void setNamespace(String parameter) { 384 this.namespace = parameter; 385 } 386 387 391 public void setNamespaceImpl(String parameter) { 392 this.namespaceImpl = parameter; 393 } 394 395 399 public void setUseInheritedMethods(boolean parameter) { 400 this.useInheritedMethods = parameter; 401 } 402 403 407 public void setExclude(String exclude) { 408 this.exclude = exclude; 409 } 410 411 416 public void setStopClasses(String stopClasses) { 417 this.stopClasses = stopClasses; 418 } 419 420 427 public void setStyle(String style) { 428 this.style = style; 429 } 430 431 434 public void addMapping(NamespaceMapping mapping) { 435 mappings.addMapping(mapping); 436 } 437 438 441 public void addMappingSet(MappingSet mappingset) { 442 mappings.addMappingSet(mappingset); 443 } 444 445 446 451 public void setTypeMappingVersion(TypeMappingVersionEnum parameter) { 452 this.typeMappingVersion = parameter.getValue(); 453 } 454 455 462 public void setMethods(String methods) { 463 this.methods = methods; 464 } 465 466 469 public void setUse(String use) { 470 this.use = use; 471 } 472 473 478 public void setServiceElementName(String serviceElementName) { 479 this.serviceElementName = serviceElementName; 480 } 481 482 485 public void setExtraClasses(String extraClasses) { 486 this.extraClasses = extraClasses; 487 } 488 489 492 public void setSoapAction( String soapAction ) { 493 this.soapAction = soapAction; 494 } 495 496 501 public void addComplexType(ComplexType ct) { 502 complexTypes.add(ct); 503 } 504 505 510 public void setClasspath(Path classpath) { 511 createClasspath().append(classpath); 512 } 513 514 519 public Path createClasspath() { 520 if (classpath == null) { 521 classpath = new Path(getProject()); 522 classpath = classpath.concatSystemClasspath(); 523 } 524 return classpath.createPath(); 525 } 526 527 532 public void setClasspathRef(Reference r) { 533 createClasspath().setRefid(r); 534 } 535 536 540 public void addSysproperty(Environment.Variable sysp) { 541 commandline.addSysproperty(sysp); 542 } 543 544 548 public void setDeploy(boolean deploy) { 549 this.isDeploy = deploy; 550 } 551 552 559 protected void generateServerSide(Emitter j2w, String wsdlFileName) throws Exception { 560 org.apache.axis.wsdl.toJava.Emitter w2j = new org.apache.axis.wsdl.toJava.Emitter(); 561 File wsdlFile = new File (wsdlFileName); 562 w2j.setServiceDesc(j2w.getServiceDesc()); 563 w2j.setQName2ClassMap(j2w.getQName2ClassMap()); 564 w2j.setOutputDir(wsdlFile.getParent()); 565 w2j.setServerSide(true); 566 w2j.setDeploy(true); 567 w2j.setHelperWanted(true); 568 569 String ns = j2w.getIntfNamespace(); 571 String clsName = j2w.getCls().getName(); 572 int idx = clsName.lastIndexOf("."); 573 String pkg = null; 574 if (idx > 0) { 575 pkg = clsName.substring(0, idx); 576 w2j.getNamespaceMap().put(ns, pkg); 577 } 578 579 Map nsmap = j2w.getNamespaceMap(); 580 if (nsmap != null) { 581 for (Iterator i = nsmap.keySet().iterator(); i.hasNext(); ) { 582 pkg = (String ) i.next(); 583 ns = (String ) nsmap.get(pkg); 584 w2j.getNamespaceMap().put(ns, pkg); 585 } 586 } 587 588 w2j.setDeploy(true); 590 591 if (j2w.getImplCls() != null) { 592 w2j.setImplementationClassName(j2w.getImplCls().getName()); 593 } else { 594 if (!j2w.getCls().isInterface()) { 595 w2j.setImplementationClassName(j2w.getCls().getName()); 596 } else { 597 throw new Exception ("implementation class is not specified."); 598 } 599 } 600 601 w2j.run(wsdlFileName); 602 } 603 } 604 | Popular Tags |