org.jboss.ws.tools.ant.WSProvideTask Maven / Gradle / Ivy
/*
* JBoss, Home of Professional Open Source.
* Copyright 2012, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.ws.tools.ant;
import org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.ExecuteJava;
import org.apache.tools.ant.taskdefs.LogOutputStream;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.CommandlineJava;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;
import org.apache.tools.ant.types.CommandlineJava.SysProperties;
import org.jboss.ws.api.tools.WSContractProvider;
import java.io.File;
import java.io.PrintStream;
import java.net.URLClassLoader;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.StringTokenizer;
import java.util.List;
import java.util.ArrayList;
/**
* Ant task which invokes provides a Web Service contract and portable JAX-WS wrapper classes.
*
*
* Attribute Description Default
* fork Whether or not to run the generation task in a separate VM. true
* keep Keep/Enable Java source code generation. false
* destdir The output directory for generated artifacts. "output"
* resourcedestdir The output directory for resource artifacts (WSDL/XSD). value of destdir
* sourcedestdir The output directory for Java source. value of destdir
* genwsdl Whether or not to generate WSDL. false
* address The generated port soap:address in wsdl.
* extension Enable SOAP 1.2 binding extension. false
* verbose Enables more informational output about cmd progress. false
* sei Service Endpoint Implementation.
* classpath The classpath that contains the service endpoint implementation. ""
*
* * = required.
*
* Example:
*
*
* <target name="test-wsproivde" depends="init">
* <taskdef name="WSProvideTask" classname="org.jboss.ws.tools.ant.WSProvideTask">
* <classpath refid="core.classpath"/>
* </taskdef>
* <WSProvideTask
* fork="false"
* keep="true"
* destdir="out"
* resourcedestdir="out-resource"
* sourcedestdir="out-source"
* genwsdl="true"
* extension="true"
* verbose="true"
* sei="org.jboss.test.ws.jaxws.jsr181.soapbinding.DocWrappedServiceImpl">
* <classpath>
* <pathelement path="${tests.output.dir}/classes"/>
* </classpath>
* </WSProvideTask>
* </target>
*
*
* @author Jason T. Greene
* @author Richard Opalka
* @author Alessio Soldano
*/
public class WSProvideTask extends Task
{
private Path classpath = new Path(getProject());
private CommandlineJava command = new CommandlineJava();
private String sei;
private File destdir;
private File resourcedestdir;
private File sourcedestdir;
private boolean keep;
private boolean extension;
private boolean genwsdl;
private boolean verbose;
private boolean fork;
private boolean debug;
private String portSoapAddress;
// Not actually used right now
public void setDebug(boolean debug)
{
this.debug = debug;
}
public Commandline.Argument createJvmarg()
{
return command.createVmArgument();
}
public void setClasspath(Path classpath)
{
this.classpath = classpath;
}
public void setClasspathRef(Reference ref)
{
createClasspath().setRefid(ref);
}
public Path createClasspath()
{
return classpath;
}
public void setDestdir(File destdir)
{
this.destdir = destdir;
}
public void setExtension(boolean extension)
{
this.extension = extension;
}
public void setProtocol(String protocol)
{
if (protocol != null)
{
this.extension = protocol.toLowerCase().indexOf("Xsoap1.2") != -1;
}
}
public void setKeep(boolean keep)
{
this.keep = keep;
}
public void setSei(String sei)
{
this.sei = sei;
}
public void setFork(boolean fork)
{
this.fork = fork;
}
public void setResourcedestdir(File resourcedestdir)
{
this.resourcedestdir = resourcedestdir;
}
public void setSourcedestdir(File sourcedestdir)
{
this.sourcedestdir = sourcedestdir;
}
public void setVerbose(boolean verbose)
{
this.verbose = verbose;
}
public void setGenwsdl(boolean genwsdl)
{
this.genwsdl = genwsdl;
}
public void setPortSoapAddress(String portSoapAddress)
{
this.portSoapAddress = portSoapAddress;
}
private ClassLoader getClasspathLoader(ClassLoader parent)
{
AntClassLoader antLoader = new AntClassLoader(parent, getProject(), classpath, false);
// It's necessary to wrap it into an URLLoader in order to extract that information
// within the actual provider impl.
// See SunRIProviderImpl for instance
List urls = new ArrayList();
StringTokenizer tok = new StringTokenizer(antLoader.getClasspath(), File.separator);
while(tok.hasMoreTokens())
{
try
{
String path = tok.nextToken();
if(!path.startsWith("file://"))
path = "file://"+path;
urls.add(new URL(path));
}
catch (MalformedURLException e)
{
throw new IllegalArgumentException("Failed to wrap classloader", e);
}
}
ClassLoader wrapper = new URLClassLoader(urls.toArray(new URL[0]), antLoader);
return wrapper;
}
public void executeNonForked()
{
ClassLoader prevCL = SecurityActions.getContextClassLoader();
ClassLoader antLoader = SecurityActions.getClassLoader(this.getClass());
SecurityActions.setContextClassLoader(antLoader);
PrintStream ps = null;
try
{
WSContractProvider gen = WSContractProvider.newInstance(
getClasspathLoader(antLoader)
);
if (verbose) {
ps = new PrintStream(new LogOutputStream(this, Project.MSG_INFO));
gen.setMessageStream(ps);
}
gen.setGenerateSource(keep);
gen.setGenerateWsdl(genwsdl);
gen.setExtension(extension);
gen.setPortSoapAddress(portSoapAddress);
if (destdir != null)
gen.setOutputDirectory(destdir);
if (resourcedestdir != null)
gen.setResourceDirectory(resourcedestdir);
if (sourcedestdir != null)
gen.setSourceDirectory(sourcedestdir);
log("Generating from endpoint: " + sei, Project.MSG_INFO);
gen.provide(sei);
}
catch(Throwable t)
{
throw new BuildException(t, getLocation());
}
finally
{
if (ps != null) {
ps.close();
}
SecurityActions.setContextClassLoader(prevCL);
}
}
public void execute() throws BuildException
{
if (sei == null)
throw new BuildException("The sei attribute must be specified!", getLocation());
if (fork)
executeForked();
else
executeNonForked();
}
private Path getTaskClassPath()
{
// Why is everything in the Ant API a big hack???
ClassLoader cl = this.getClass().getClassLoader();
if (cl instanceof AntClassLoader)
{
return new Path(getProject(), ((AntClassLoader)cl).getClasspath());
}
return new Path(getProject());
}
private void executeForked() throws BuildException
{
command.setClassname(org.jboss.ws.tools.cmd.WSProvide.class.getName());
Path path = command.createClasspath(getProject());
path.append(getTaskClassPath());
path.append(classpath);
if (keep)
command.createArgument().setValue("-k");
if (genwsdl)
command.createArgument().setValue("-w");
if (portSoapAddress != null) {
command.createArgument().setValue("-a");
command.createArgument().setValue(portSoapAddress);
}
if (extension)
command.createArgument().setValue("-e");
if (destdir != null)
{
command.createArgument().setValue("-o");
command.createArgument().setFile(destdir);
}
if (resourcedestdir != null)
{
command.createArgument().setValue("-r");
command.createArgument().setFile(resourcedestdir);
}
if (sourcedestdir != null)
{
command.createArgument().setValue("-s");
command.createArgument().setFile(sourcedestdir);
}
if (!verbose)
command.createArgument().setValue("-q");
// Always dump traces
command.createArgument().setValue("-t");
command.createArgument().setValue(sei);
if (verbose)
log("Command invoked: " + command.getJavaCommand().toString());
ExecuteJava execute = new ExecuteJava();
execute.setClasspath(path);
execute.setJavaCommand(command.getJavaCommand());
// propagate system properties (useful e.g. for endorsing)
String[] arguments = command.getVmCommand().getArguments();
SysProperties properties = AntTaskHelper.toSystemProperties(arguments);
execute.setSystemProperties(properties);
if (execute.fork(this) != 0)
throw new BuildException("Could not invoke WSProvideTask", getLocation());
}
}