Methods Summary |
---|
public void | addAssertions(org.apache.tools.ant.types.Assertions asserts)Add assertions to enable in this program (if fork=true).
if (getCommandLine().getAssertions() != null) {
throw new BuildException("Only one assertion declaration is allowed");
}
getCommandLine().setAssertions(asserts);
|
public void | addConfiguredRedirector(org.apache.tools.ant.types.RedirectorElement redirectorElement)Add a RedirectorElement to this task.
if (this.redirectorElement != null) {
throw new BuildException("cannot have > 1 nested redirectors");
}
this.redirectorElement = redirectorElement;
incompatibleWithSpawn = true;
|
public void | addEnv(Environment.Variable var)Add an environment variable.
Will be ignored if we are not forking a new VM.
env.addVariable(var);
|
public void | addSysproperty(Environment.Variable sysp)Add a system property.
getCommandLine().addSysproperty(sysp);
|
public void | addSyspropertyset(org.apache.tools.ant.types.PropertySet sysp)Add a set of properties as system properties.
getCommandLine().addSyspropertyset(sysp);
|
public void | clearArgs()Clear out the arguments to this java task.
getCommandLine().clearJavaArgs();
|
public Commandline.Argument | createArg()Add a command-line argument.
return getCommandLine().createArgument();
|
public org.apache.tools.ant.types.Path | createBootclasspath()Add a path to the bootclasspath.
return getCommandLine().createBootclasspath(getProject()).createPath();
|
public org.apache.tools.ant.types.Path | createClasspath()Add a path to the classpath.
return getCommandLine().createClasspath(getProject()).createPath();
|
public Commandline.Argument | createJvmarg()Adds a JVM argument.
return getCommandLine().createVmArgument();
|
public org.apache.tools.ant.types.Permissions | createPermissions()Set the permissions for the application run inside the same JVM.
perm = (perm == null) ? new Permissions() : perm;
return perm;
|
protected ExecuteWatchdog | createWatchdog()Create the Watchdog to kill a runaway process.
if (timeout == null) {
return null;
}
return new ExecuteWatchdog(timeout.longValue());
|
public void | execute()Do the execution.
File savedDir = dir;
Permissions savedPermissions = perm;
int err = -1;
try {
err = executeJava();
if (err != 0) {
if (failOnError) {
throw new ExitStatusException("Java returned: " + err,
err,
getLocation());
} else {
log("Java Result: " + err, Project.MSG_ERR);
}
}
maybeSetResultPropertyValue(err);
} finally {
dir = savedDir;
perm = savedPermissions;
}
|
public int | executeJava()Do the execution and return a return code.
String classname = getCommandLine().getClassname();
if (classname == null && getCommandLine().getJar() == null) {
throw new BuildException("Classname must not be null.");
}
if (!fork && getCommandLine().getJar() != null) {
throw new BuildException("Cannot execute a jar in non-forked mode."
+ " Please set fork='true'. ");
}
if (spawn && !fork) {
throw new BuildException("Cannot spawn a java process in non-forked mode."
+ " Please set fork='true'. ");
}
if (getCommandLine().getClasspath() != null
&& getCommandLine().getJar() != null) {
log("When using 'jar' attribute classpath-settings are ignored. "
+ "See the manual for more information.", Project.MSG_VERBOSE);
}
if (spawn && incompatibleWithSpawn) {
getProject().log("spawn does not allow attributes related to input, "
+ "output, error, result", Project.MSG_ERR);
getProject().log("spawn also does not allow timeout", Project.MSG_ERR);
getProject().log("finally, spawn is not compatible "
+ "with a nested I/O <redirector>", Project.MSG_ERR);
throw new BuildException("You have used an attribute "
+ "or nested element which is not compatible with spawn");
}
if (getCommandLine().getAssertions() != null && !fork) {
log("Assertion statements are currently ignored in non-forked mode");
}
if (fork) {
if (perm != null) {
log("Permissions can not be set this way in forked mode.", Project.MSG_WARN);
}
log(getCommandLine().describeCommand(), Project.MSG_VERBOSE);
} else {
if (getCommandLine().getVmCommand().size() > 1) {
log("JVM args ignored when same JVM is used.",
Project.MSG_WARN);
}
if (dir != null) {
log("Working directory ignored when same JVM is used.",
Project.MSG_WARN);
}
if (newEnvironment || null != env.getVariables()) {
log("Changes to environment variables are ignored when same "
+ "JVM is used.", Project.MSG_WARN);
}
if (getCommandLine().getBootclasspath() != null) {
log("bootclasspath ignored when same JVM is used.",
Project.MSG_WARN);
}
if (perm == null) {
perm = new Permissions(true);
log("running " + this.getCommandLine().getClassname()
+ " with default permissions (exit forbidden)", Project.MSG_VERBOSE);
}
log("Running in same VM " + getCommandLine().describeJavaCommand(),
Project.MSG_VERBOSE);
}
setupRedirector();
try {
if (fork) {
if (!spawn) {
return fork(getCommandLine().getCommandline());
} else {
spawn(getCommandLine().getCommandline());
return 0;
}
} else {
try {
run(getCommandLine());
return 0;
} catch (ExitException ex) {
return ex.getStatus();
}
}
} catch (BuildException e) {
if (e.getLocation() == null && getLocation() != null) {
e.setLocation(getLocation());
}
if (failOnError) {
throw e;
} else {
log(e);
return 0;
}
} catch (ThreadDeath t) {
throw t; // cf. NB #47191
} catch (Throwable t) {
if (failOnError) {
throw new BuildException(t, getLocation());
} else {
log(t);
return 0;
}
}
|
private int | fork(java.lang.String[] command)Executes the given classname with the given arguments in a separate VM.
Execute exe
= new Execute(redirector.createHandler(), createWatchdog());
setupExecutable(exe, command);
try {
int rc = exe.execute();
redirector.complete();
if (exe.killedProcess()) {
throw new BuildException("Timeout: killed the sub-process");
}
return rc;
} catch (IOException e) {
throw new BuildException(e, getLocation());
}
|
public org.apache.tools.ant.types.CommandlineJava | getCommandLine()Accessor to the command line.
return cmdl;
|
public CommandlineJava.SysProperties | getSysProperties()Get the system properties of the command line.
return getCommandLine().getSystemProperties();
|
protected void | handleErrorFlush(java.lang.String output)Handle output sent to System.err and flush the stream.
if (redirector.getErrorStream() != null) {
redirector.handleErrorFlush(output);
} else {
super.handleErrorOutput(output);
}
|
protected void | handleErrorOutput(java.lang.String output)Handle output sent to System.err.
if (redirector.getErrorStream() != null) {
redirector.handleErrorOutput(output);
} else {
super.handleErrorOutput(output);
}
|
protected void | handleFlush(java.lang.String output)Pass output sent to System.out to specified output file.
if (redirector.getOutputStream() != null) {
redirector.handleFlush(output);
} else {
super.handleFlush(output);
}
|
public int | handleInput(byte[] buffer, int offset, int length)Handle an input request by this task.
// Should work whether or not redirector.inputStream == null:
return redirector.handleInput(buffer, offset, length);
|
protected void | handleOutput(java.lang.String output)Pass output sent to System.out to specified output file.
if (redirector.getOutputStream() != null) {
redirector.handleOutput(output);
} else {
super.handleOutput(output);
}
|
private void | log(java.lang.Throwable t)Log the specified Throwable.
StringWriter sw = new StringWriter();
PrintWriter w = new PrintWriter(sw);
t.printStackTrace(w);
w.close();
log(sw.toString(), Project.MSG_ERR);
|
protected void | maybeSetResultPropertyValue(int result)Helper method to set result property to the
passed in value if appropriate.
String res = Integer.toString(result);
if (resultProperty != null) {
getProject().setNewProperty(resultProperty, res);
}
|
private void | run(org.apache.tools.ant.types.CommandlineJava command)Executes the given classname with the given arguments as it
were a command line application.
try {
ExecuteJava exe = new ExecuteJava();
exe.setJavaCommand(command.getJavaCommand());
exe.setClasspath(command.getClasspath());
exe.setSystemProperties(command.getSystemProperties());
exe.setPermissions(perm);
exe.setTimeout(timeout);
redirector.createStreams();
exe.execute(getProject());
redirector.complete();
if (exe.killedProcess()) {
throw new BuildException("Timeout: killed the sub-process");
}
} catch (IOException e) {
throw new BuildException(e);
}
|
protected void | run(java.lang.String classname, java.util.Vector args)Executes the given classname with the given arguments as if it
were a command line application.
CommandlineJava cmdj = new CommandlineJava();
cmdj.setClassname(classname);
for (int i = 0; i < args.size(); i++) {
cmdj.createArgument().setValue((String) args.elementAt(i));
}
run(cmdj);
|
public void | setAppend(boolean append)If true, append output to existing file.
redirector.setAppend(append);
incompatibleWithSpawn = true;
|
public void | setArgs(java.lang.String s)Deprecated: use nested arg instead.
Set the command line arguments for the class.
log("The args attribute is deprecated. "
+ "Please use nested arg elements.", Project.MSG_WARN);
getCommandLine().createArgument().setLine(s);
|
public void | setClassname(java.lang.String s)Set the Java class to execute.
if (getCommandLine().getJar() != null) {
throw new BuildException("Cannot use 'jar' and 'classname' "
+ "attributes in same command");
}
getCommandLine().setClassname(s);
|
public void | setClasspath(org.apache.tools.ant.types.Path s)Set the classpath to be used when running the Java class.
createClasspath().append(s);
|
public void | setClasspathRef(org.apache.tools.ant.types.Reference r)Set the classpath to use by reference.
createClasspath().setRefid(r);
|
public void | setCloneVm(boolean cloneVm)If set, system properties will be copied to the cloned VM--as
well as the bootclasspath unless you have explicitly specified
a bootclaspath.
Doesn't have any effect unless fork is true.
getCommandLine().setCloneVm(cloneVm);
|
public void | setDir(java.io.File d)Set the working directory of the process.
this.dir = d;
|
public void | setError(java.io.File error)Set the File to which the error stream of the process is redirected.
this.error = error;
incompatibleWithSpawn = true;
|
public void | setErrorProperty(java.lang.String errorProperty)Set the property name whose value should be set to the error of
the process.
redirector.setErrorProperty(errorProperty);
incompatibleWithSpawn = true;
|
public void | setFailonerror(boolean fail)If true, then fail if the command exits with a
returncode other than zero.
failOnError = fail;
incompatibleWithSpawn |= fail;
|
public void | setFork(boolean s)If true, execute in a new VM.
this.fork = s;
|
public void | setInput(java.io.File input)Set the input to use for the task.
if (inputString != null) {
throw new BuildException("The \"input\" and \"inputstring\" "
+ "attributes cannot both be specified");
}
this.input = input;
incompatibleWithSpawn = true;
|
public void | setInputString(java.lang.String inputString)Set the string to use as input.
if (input != null) {
throw new BuildException("The \"input\" and \"inputstring\" "
+ "attributes cannot both be specified");
}
this.inputString = inputString;
incompatibleWithSpawn = true;
|
public void | setJVMVersion(java.lang.String value)Set the JVM version.
getCommandLine().setVmversion(value);
|
public void | setJar(java.io.File jarfile)Set the location of the JAR file to execute.
if (getCommandLine().getClassname() != null) {
throw new BuildException("Cannot use 'jar' and 'classname' "
+ "attributes in same command.");
}
getCommandLine().setJar(jarfile.getAbsolutePath());
|
public void | setJvm(java.lang.String s)Set the command used to start the VM (only if forking).
getCommandLine().setVm(s);
|
public void | setJvmargs(java.lang.String s)Set the command line arguments for the JVM.
log("The jvmargs attribute is deprecated. "
+ "Please use nested jvmarg elements.", Project.MSG_WARN);
getCommandLine().createVmArgument().setLine(s);
|
public void | setLogError(boolean logError)Set whether error output of exec is logged. This is only useful
when output is being redirected and error output is desired in the
Ant log.
redirector.setLogError(logError);
incompatibleWithSpawn |= logError;
|
public void | setMaxmemory(java.lang.String max)Corresponds to -mx or -Xmx depending on VM version.
getCommandLine().setMaxmemory(max);
|
public void | setNewenvironment(boolean newenv)If true, use a completely new environment.
Will be ignored if we are not forking a new VM.
newEnvironment = newenv;
|
public void | setOutput(java.io.File out)Set the File to which the output of the process is redirected.
this.output = out;
incompatibleWithSpawn = true;
|
public void | setOutputproperty(java.lang.String outputProp)Set the property name whose value should be set to the output of
the process.
redirector.setOutputProperty(outputProp);
incompatibleWithSpawn = true;
|
public void | setResultProperty(java.lang.String resultProperty)Set the name of the property in which the return code of the
command should be stored. Only of interest if failonerror=false.
this.resultProperty = resultProperty;
incompatibleWithSpawn = true;
|
public void | setSpawn(boolean spawn)Set whether or not you want the process to be spawned;
default is not spawned.
this.spawn = spawn;
|
public void | setTimeout(java.lang.Long value)Set the timeout in milliseconds after which the process will be killed.
timeout = value;
incompatibleWithSpawn |= timeout != null;
|
private void | setupCommandLine(Execute exe, java.lang.String[] command)Set the command line for the exe.
On VMS, hands off to {@link #setupCommandLineForVMS(Execute, String[])}.
//On VMS platform, we need to create a special java options file
//containing the arguments and classpath for the java command.
//The special file is supported by the "-V" switch on the VMS JVM.
if (Os.isFamily("openvms")) {
setupCommandLineForVMS(exe, command);
} else {
exe.setCommandline(command);
}
|
private void | setupCommandLineForVMS(Execute exe, java.lang.String[] command)On VMS platform, we need to create a special java options file
containing the arguments and classpath for the java command.
The special file is supported by the "-V" switch on the VMS JVM.
ExecuteJava.setupCommandLineForVMS(exe, command);
|
private void | setupEnvironment(Execute exe)Set up our environment variables.
String[] environment = env.getVariables();
if (environment != null) {
for (int i = 0; i < environment.length; i++) {
log("Setting environment variable: " + environment[i],
Project.MSG_VERBOSE);
}
}
exe.setNewenvironment(newEnvironment);
exe.setEnvironment(environment);
|
private void | setupExecutable(Execute exe, java.lang.String[] command)Do all configuration for an executable that
is common across the {@link #fork(String[])} and
{@link #spawn(String[])} methods.
exe.setAntRun(getProject());
setupWorkingDir(exe);
setupEnvironment(exe);
setupCommandLine(exe, command);
|
protected void | setupRedirector()Set up properties on the redirector that we needed to store locally.
redirector.setInput(input);
redirector.setInputString(inputString);
redirector.setOutput(output);
redirector.setError(error);
if (redirectorElement != null) {
redirectorElement.configure(redirector);
}
if (!spawn && input == null && inputString == null) {
// #24918: send standard input to the process by default.
redirector.setInputStream(
new KeepAliveInputStream(getProject().getDefaultInputStream()));
}
|
private void | setupWorkingDir(Execute exe)Set the working dir of the new process.
if (dir == null) {
dir = getProject().getBaseDir();
} else if (!dir.exists() || !dir.isDirectory()) {
throw new BuildException(dir.getAbsolutePath()
+ " is not a valid directory",
getLocation());
}
exe.setWorkingDirectory(dir);
|
private void | spawn(java.lang.String[] command)Executes the given classname with the given arguments in a separate VM.
Execute exe = new Execute();
setupExecutable(exe, command);
try {
exe.spawn();
} catch (IOException e) {
throw new BuildException(e, getLocation());
}
|