FileDocCategorySizeDatePackage
Java2WsdlAntTask.javaAPI DocApache Axis 1.420123Sat Apr 22 18:57:28 BST 2006org.apache.axis.tools.ant.wsdl

Java2WsdlAntTask

public class Java2WsdlAntTask extends org.apache.tools.ant.Task
Generates a WSDL description from a Java class.
author
Rich Scheuerle (scheu@us.ibm.com)
author
Steve Loughran
ant.task
category="axis" name="axis-java2wsdl"

Fields Summary
private String
namespace
private String
namespaceImpl
private HashMap
namespaceMap
private String
location
private String
locationImport
private String
output
private String
importSchema
private String
input
private String
outputImpl
private String
className
private String
servicePortName
private String
portTypeName
private String
bindingName
private String
implClass
private boolean
useInheritedMethods
private String
exclude
private String
stopClasses
private String
typeMappingVersion
private String
style
private String
serviceElementName
private String
methods
private String
use
private MappingSet
mappings
private String
extraClasses
private org.apache.tools.ant.types.Path
classpath
private String
soapAction
private List
complexTypes
private boolean
isDeploy
private org.apache.tools.ant.types.CommandlineJava
commandline
Constructors Summary
Methods Summary
public voidaddComplexType(ComplexType ct)
Nested element for Complex Types. Each Complex Type uses the following fields:

param
ct

        complexTypes.add(ct);
    
public voidaddMapping(NamespaceMapping mapping)
add a mapping of namespaces to packages

        mappings.addMapping(mapping);
    
public voidaddMappingSet(MappingSet mappingset)
add a mapping of namespaces to packages

        mappings.addMappingSet(mappingset);
    
public voidaddSysproperty(Environment.Variable sysp)
Adds a system property that tests can access.

param
sysp environment variable to add

        commandline.addSysproperty(sysp);
    
public org.apache.tools.ant.types.PathcreateClasspath()
Set the optional classpath

return
a path instance to be configured by the Ant core.

        if (classpath == null) {
            classpath = new Path(getProject());
            classpath = classpath.concatSystemClasspath();
        }
        return classpath.createPath();
    
public voidexecute()
execute the task

throws
BuildException

        AntClassLoader cl = new AntClassLoader(getClass().getClassLoader(),
        		getProject(),
                classpath == null ? createClasspath() : classpath,
                false);
        
        ClassUtils.setDefaultClassLoader(cl);
        //add extra classes to the classpath when the classpath attr is not null
        if (extraClasses != null) {
            StringTokenizer tokenizer = new StringTokenizer(extraClasses, " ,");
            while (tokenizer.hasMoreTokens()) {
                String clsName = tokenizer.nextToken();
                ClassUtils.setClassLoader(clsName, cl);
            }
        }

        CommandlineJava.SysProperties sysProperties =
                commandline.getSystemProperties();
        if (sysProperties != null) {
            sysProperties.setSystem();
        }
        try {
            traceParams(Project.MSG_VERBOSE);
            validate();

            // Instantiate the emitter
            Emitter emitter = new Emitter();
            //do the mappings, packages are the key for this map
            mappings.execute(this,namespaceMap, true);
            if (!namespaceMap.isEmpty()) {
                emitter.setNamespaceMap(namespaceMap);
            }
            if (servicePortName != null) {
                emitter.setServicePortName(servicePortName);
            }
            if (portTypeName != null) {
                emitter.setPortTypeName(portTypeName);
            }
            if (bindingName != null) {
                emitter.setBindingName(bindingName);
            }
            log("Java2WSDL " + className, Project.MSG_INFO);
            emitter.setCls(className);
            if (implClass != null) {
                emitter.setImplCls(implClass);
            }
            if (exclude != null) {
                emitter.setDisallowedMethods(exclude);
            }
            if (stopClasses != null) {
                emitter.setStopClasses(stopClasses);
            }
            if (extraClasses != null) {
                emitter.setExtraClasses(extraClasses);
            }

            TypeMappingRegistryImpl tmr = new TypeMappingRegistryImpl();
            tmr.doRegisterFromVersion(typeMappingVersion);
            emitter.setTypeMappingRegistry(tmr);

            // Create TypeMapping and register complex types
            TypeMappingDelegate tmi = (TypeMappingDelegate)tmr.getDefaultTypeMapping();
            Iterator i = complexTypes.iterator();
            while (i.hasNext()) {
                ((ComplexType) i.next()).register(tmi);
            }
            
            if (style != null) {
                emitter.setStyle(style);
            }

            if (use != null) {
                emitter.setUse(use);
            }

            if (importSchema != null) {
                emitter.setInputSchema(importSchema);
            }
            if (input != null) {
                emitter.setInputWSDL(input);
            }
            emitter.setIntfNamespace(namespace);
            emitter.setImplNamespace(namespaceImpl);
            emitter.setLocationUrl(location);
            emitter.setImportUrl(locationImport);
            emitter.setUseInheritedMethods(useInheritedMethods);
            if(serviceElementName!=null) {
                emitter.setServiceElementName(serviceElementName);
            }
            if(methods!=null) {
                emitter.setAllowedMethods(methods);
            }
            if (soapAction != null) {
                emitter.setSoapAction(soapAction);
            }
            if (outputImpl == null) {
                // Normal case
                emitter.emit(output, Emitter.MODE_ALL);
            } else {
                // Emit interface and implementation wsdls
                emitter.emit(output, outputImpl);
            }

            if (isDeploy == true) {
                generateServerSide(emitter, (outputImpl != null) ? outputImpl : output);
            }

        } catch(BuildException b) {
            //pass build exceptions up the wire
           throw b;
        } catch (Throwable t) {
            //other trouble: stack trace the trouble and throw an exception
            StringWriter writer = new StringWriter();
            t.printStackTrace(new PrintWriter(writer));
            log(writer.getBuffer().toString(), Project.MSG_ERR);
            throw new BuildException("Error while running " + getClass().getName(), t);
        } finally {
            if (sysProperties != null) {
                sysProperties.restoreSystem();
            }
        }
    
protected voidgenerateServerSide(org.apache.axis.wsdl.fromJava.Emitter j2w, java.lang.String wsdlFileName)
Generate the server side artifacts from the generated WSDL

param
j2w the Java2WSDL emitter
param
wsdlFileName the generated WSDL file
throws
Exception

        org.apache.axis.wsdl.toJava.Emitter w2j = new org.apache.axis.wsdl.toJava.Emitter();
        File wsdlFile = new File(wsdlFileName);
        w2j.setServiceDesc(j2w.getServiceDesc());
        w2j.setQName2ClassMap(j2w.getQName2ClassMap());
        w2j.setOutputDir(wsdlFile.getParent());
        w2j.setServerSide(true);   
        w2j.setDeploy(true);
        w2j.setHelperWanted(true);

        // setup namespace-to-package mapping
        String ns = j2w.getIntfNamespace();
        String clsName = j2w.getCls().getName();
        int idx = clsName.lastIndexOf(".");
        String pkg = null;
        if (idx > 0) {
            pkg = clsName.substring(0, idx);            
            w2j.getNamespaceMap().put(ns, pkg);
        }
        
        Map nsmap = j2w.getNamespaceMap();
        if (nsmap != null) {
            for (Iterator i = nsmap.keySet().iterator(); i.hasNext(); ) {
                pkg = (String) i.next();
                ns = (String) nsmap.get(pkg);
                w2j.getNamespaceMap().put(ns, pkg);
            }
        }
        
        // set 'deploy' mode
        w2j.setDeploy(true);
        
        if (j2w.getImplCls() != null) {
            w2j.setImplementationClassName(j2w.getImplCls().getName());
        } else {
            if (!j2w.getCls().isInterface()) {
                w2j.setImplementationClassName(j2w.getCls().getName());
            } else {
                throw new Exception("implementation class is not specified.");
            }
        }
        
        w2j.run(wsdlFileName);
    
public voidsetBindingName(java.lang.String parameter)
The name to use use for the binding element. If not specified, the value of the servicePortName + "SoapBinding" is used.

param
parameter

        this.bindingName = parameter;
    
public voidsetClassName(java.lang.String parameter)
the class name to import, eg. org.example.Foo. Required. The class must be on the classpath.

param
parameter fully qualified class name

        this.className = parameter;
    
public voidsetClasspath(org.apache.tools.ant.types.Path classpath)
Set the optional classpath

param
classpath the classpath to use when loading class

        createClasspath().append(classpath);
    
public voidsetClasspathRef(org.apache.tools.ant.types.Reference r)
Set the reference to an optional classpath

param
r the id of the Ant path instance to act as the classpath

        createClasspath().setRefid(r);
    
public voidsetDeploy(boolean deploy)
Sets the deploy flag

param
deploy true if deploy mode

        this.isDeploy = deploy;
    
public voidsetExclude(java.lang.String exclude)
Comma separated list of methods to exclude from the wsdl file.

param
exclude

        this.exclude = exclude;
    
public voidsetExtraClasses(java.lang.String extraClasses)
A comma separated list of classes to add to the classpath.

        this.extraClasses = extraClasses;
    
public voidsetImplClass(java.lang.String parameter)
Sometimes extra information is available in the implementation class file. Use this option to specify the implementation class.

param
parameter

        this.implClass = parameter;
    
public voidsetImportSchema(java.io.File parameter)
Option attribute that indicates the name of an XML Schema file that should be physically imported into the generated WSDL.

param
parameter

        try {
            this.importSchema = parameter.toURL().toString();
        } catch (java.io.IOException ioe) {
            throw new BuildException(ioe);
        }
    
public voidsetInput(java.io.File parameter)
Optional attribute that indicates the name of the input wsdl file. The output wsdl file will contain everything from the input wsdl file plus the new constructs. If a new construct is already present in the input wsdl file, it is not added. This option is useful for constructing a wsdl file with multiple ports, bindings, or portTypes.

param
parameter filename

        this.input = parameter.getPath();
    
public voidsetLocation(java.lang.String parameter)
The url of the location of the service. The name after the last slash or backslash is the name of the service port (unless overridden by the -s option). The service port address location attribute is assigned the specified value.

param
parameter a URL

        this.location = parameter;
    
public voidsetLocationImport(java.lang.String parameter)
the location of the interface WSDL when generating an implementation WSDL Required when outputImpl is set

param
parameter URL?

        this.locationImport = parameter;
    
public voidsetMethods(java.lang.String methods)
If this option is specified, only the indicated methods in your interface class will be exported into the WSDL file. The methods list must be comma separated. If not specified, all methods declared in the interface class will be exported into the WSDL file

param
methods list of methods

        this.methods = methods;
    
public voidsetNamespace(java.lang.String parameter)
the target namespace. Required.

param
parameter

        this.namespace = parameter;
    
public voidsetNamespaceImpl(java.lang.String parameter)
Namespace of the implementation WSDL.

param
parameter

        this.namespaceImpl = parameter;
    
public voidsetOutput(java.io.File parameter)
The name of the output WSDL file. If not specified, a suitable default WSDL file is written into the current directory.

param
parameter

        this.output = parameter.getPath();
    
public voidsetOutputImpl(java.io.File parameter)
Use this option to indicate the name of the output implementation WSDL file. If specified, Java2WSDL will produce separate interface and implementation WSDL files. If not, a single WSDL file is generated

param
parameter

        this.outputImpl = parameter.getPath();
    
public voidsetPortTypeName(java.lang.String parameter)
Indicates the name to use use for the portType element. If not specified, the class-of-portType name is used.

param
parameter

        this.portTypeName = parameter;
    
public voidsetServiceElementName(java.lang.String serviceElementName)
the name of the service element. If not specified, the service element is the portTypeNameService.

param
serviceElementName

        this.serviceElementName = serviceElementName;
    
public voidsetServicePortName(java.lang.String parameter)
service port name (obtained from location if not specified)

param
parameter portname

        this.servicePortName = parameter;
    
public voidsetSoapAction(java.lang.String soapAction)
The setter for the "soapAction" attribute

		this.soapAction = soapAction;
    
public voidsetStopClasses(java.lang.String stopClasses)
Comma separated list of classes which stop the Java2WSDL inheritance search.

param
stopClasses

        this.stopClasses = stopClasses;
    
public voidsetStyle(java.lang.String style)
The style of the WSDL document: RPC, DOCUMENT or WRAPPED. If RPC, a rpc/encoded wsdl is generated. If DOCUMENT, a document/literal wsdl is generated. If WRAPPED, a document/literal wsdl is generated using the wrapped approach.

param
style

        this.style = style;
    
public voidsetTypeMappingVersion(TypeMappingVersionEnum parameter)
the default type mapping registry to use. Either 1.1 or 1.2. Default is 1.1

param
parameter new version

        this.typeMappingVersion = parameter.getValue();
    
public voidsetUse(java.lang.String use)
Set the use option

        this.use = use;
    
public voidsetUseInheritedMethods(boolean parameter)
should inherited methods be exported too? Default=false

param
parameter

        this.useInheritedMethods = parameter;
    
public voidtraceParams(int logLevel)
trace out parameters

param
logLevel to log at
see
org.apache.tools.ant.Project#log


                   
        
        log("Running Java2WsdlAntTask with parameters:", logLevel);
        log("\tnamespace:" + namespace, logLevel);
        log("\tPkgtoNS:" + namespaceMap, logLevel);
        log("\tlocation:" + location, logLevel);
        log("\toutput:" + output, logLevel);
        log("\timportSchema:" + importSchema, logLevel);
        log("\tinput:" + input, logLevel);
        log("\tclassName:" + className, logLevel);
        log("\tservicePortName:" + servicePortName, logLevel);
        log("\tportTypeName:" + portTypeName, logLevel);
        log("\tbindingName:" + bindingName, logLevel);
        log("\timplClass:" + implClass, logLevel);
        log("\tinheritance:" + useInheritedMethods, logLevel);
        log("\texcluded:" + exclude, logLevel);
        log("\tstopClasses:" + stopClasses, logLevel);
        log("\ttypeMappingVersion:" + typeMappingVersion, logLevel);
        log("\tstyle:" + style, logLevel);
        log("\toutputImpl:" + outputImpl, logLevel);
        log("\tuse:" + use, logLevel);
        log("\tnamespaceImpl:" + namespaceImpl, logLevel);
        log("\tlocationImport:" + locationImport, logLevel);
        log("\tserviceElementName:" + serviceElementName, logLevel);
        log("\tmethods:" + methods, logLevel);
        log("\textraClasses:" + extraClasses, logLevel);
        log("\tsoapAction:" + soapAction, logLevel);
        log("\tclasspath:" + classpath, logLevel);
      
protected voidvalidate()
validation code

throws
BuildException if validation failed

        if(className==null || className.length() ==0) {
            throw new BuildException("No classname was specified");
        }
        if(location==null || location.length() == 0) {
            throw new BuildException("No location was specified");
        }