| Method from org.apache.tools.ant.taskdefs.Execute Detail: |
public static void closeStreams(Process process) {
FileUtils.close(process.getInputStream());
FileUtils.close(process.getOutputStream());
FileUtils.close(process.getErrorStream());
}
Close the streams belonging to the given Process. |
public int execute() throws IOException {
if (workingDirectory != null && !workingDirectory.exists()) {
throw new BuildException(workingDirectory + " doesn't exist.");
}
final Process process = launch(project, getCommandline(),
getEnvironment(), workingDirectory,
useVMLauncher);
try {
streamHandler.setProcessInputStream(process.getOutputStream());
streamHandler.setProcessOutputStream(process.getInputStream());
streamHandler.setProcessErrorStream(process.getErrorStream());
} catch (IOException e) {
process.destroy();
throw e;
}
streamHandler.start();
try {
// add the process to the list of those to destroy if the VM exits
//
processDestroyer.add(process);
if (watchdog != null) {
watchdog.start(process);
}
waitFor(process);
if (watchdog != null) {
watchdog.stop();
}
streamHandler.stop();
closeStreams(process);
if (watchdog != null) {
watchdog.checkException();
}
return getExitValue();
} catch (ThreadDeath t) {
// #31928: forcibly kill it before continuing.
process.destroy();
throw t;
} finally {
// remove the process to the list of those to destroy if
// the VM exits
//
processDestroyer.remove(process);
}
}
Runs a process defined by the command line and returns its exit status. |
public String[] getCommandline() {
return cmdl;
}
Returns the commandline used to create a subprocess. |
public String[] getEnvironment() {
return (env == null || newEnvironment)
? env : patchEnvironment();
}
Returns the environment used to create a subprocess. |
public int getExitValue() {
return exitValue;
}
Query the exit value of the process. |
public static synchronized Vector getProcEnvironment() {
if (procEnvironment != null) {
return procEnvironment;
}
procEnvironment = new Vector();
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Execute exe = new Execute(new PumpStreamHandler(out));
exe.setCommandline(getProcEnvCommand());
// Make sure we do not recurse forever
exe.setNewenvironment(true);
int retval = exe.execute();
if (retval != 0) {
// Just try to use what we got
}
BufferedReader in =
new BufferedReader(new StringReader(toString(out)));
if (Os.isFamily("openvms")) {
procEnvironment = addVMSLogicals(procEnvironment, in);
return procEnvironment;
}
String var = null;
String line, lineSep = StringUtils.LINE_SEP;
while ((line = in.readLine()) != null) {
if (line.indexOf('=") == -1) {
// Chunk part of previous env var (UNIX env vars can
// contain embedded new lines).
if (var == null) {
var = lineSep + line;
} else {
var += lineSep + line;
}
} else {
// New env var...append the previous one if we have it.
if (var != null) {
procEnvironment.addElement(var);
}
var = line;
}
}
// Since we "look ahead" before adding, there's one last env var.
if (var != null) {
procEnvironment.addElement(var);
}
} catch (java.io.IOException exc) {
exc.printStackTrace();
// Just try to see how much we got
}
return procEnvironment;
}
Find the list of environment variables for this process. |
public File getWorkingDirectory() {
return workingDirectory == null ? new File(antWorkingDirectory)
: workingDirectory;
}
Return the working directory. |
public boolean isFailure() {
return isFailure(getExitValue());
}
Did this execute return in a failure. |
public static boolean isFailure(int exitValue) {
//on openvms even exit value signals failure;
// for other platforms nonzero exit value signals failure
return Os.isFamily("openvms")
? (exitValue % 2 == 0) : (exitValue != 0);
}
Checks whether exitValue signals a failure on the current
system (OS specific).
Note that this method relies on the conventions of
the OS, it will return false results if the application you are
running doesn't follow these conventions. One notable
exception is the Java VM provided by HP for OpenVMS - it will
return 0 if successful (like on any other platform), but this
signals a failure on OpenVMS. So if you execute a new Java VM
on OpenVMS, you cannot trust this method. |
public boolean killedProcess() {
return watchdog != null && watchdog.killedProcess();
}
Test for an untimely death of the process. |
public static Process launch(Project project,
String[] command,
String[] env,
File dir,
boolean useVM) throws IOException {
if (dir != null && !dir.exists()) {
throw new BuildException(dir + " doesn't exist.");
}
CommandLauncher launcher
= ((useVM && vmLauncher != null) ? vmLauncher : shellLauncher);
return launcher.exec(project, command, env, dir);
}
Creates a process that runs a command. |
public static void runCommand(Task task,
String[] cmdline) throws BuildException {
try {
task.log(Commandline.describeCommand(cmdline),
Project.MSG_VERBOSE);
Execute exe = new Execute(
new LogStreamHandler(task, Project.MSG_INFO, Project.MSG_ERR));
exe.setAntRun(task.getProject());
exe.setCommandline(cmdline);
int retval = exe.execute();
if (isFailure(retval)) {
throw new BuildException(cmdline[0]
+ " failed with return code " + retval, task.getLocation());
}
} catch (java.io.IOException exc) {
throw new BuildException("Could not launch " + cmdline[0] + ": "
+ exc, task.getLocation());
}
}
A utility method that runs an external command. Writes the output and
error streams of the command to the project log. |
public void setAntRun(Project project) throws BuildException {
this.project = project;
}
Set the name of the antRun script using the project's value. |
public void setCommandline(String[] commandline) {
cmdl = commandline;
}
Sets the commandline of the subprocess to launch. |
public void setEnvironment(String[] env) {
this.env = env;
}
Sets the environment variables for the subprocess to launch. |
protected void setExitValue(int value) {
exitValue = value;
}
|
public void setNewenvironment(boolean newenv) {
newEnvironment = newenv;
}
Set whether to propagate the default environment or not. |
public void setSpawn(boolean spawn) {
/*
* Builds a command launcher for the OS and JVM we are running under.
*/
// Try using a JDK 1.3 launcher
try {
if (!Os.isFamily("os/2")) {
vmLauncher = new Java13CommandLauncher();
}
} catch (NoSuchMethodException exc) {
// Ignore and keep trying
}
if (Os.isFamily("mac") && !Os.isFamily("unix")) {
// Mac
shellLauncher = new MacCommandLauncher(new CommandLauncher());
} else if (Os.isFamily("os/2")) {
// OS/2
shellLauncher = new OS2CommandLauncher(new CommandLauncher());
} else if (Os.isFamily("windows")) {
environmentCaseInSensitive = true;
CommandLauncher baseLauncher = new CommandLauncher();
if (!Os.isFamily("win9x")) {
// Windows XP/2000/NT
shellLauncher = new WinNTCommandLauncher(baseLauncher);
} else {
// Windows 98/95 - need to use an auxiliary script
shellLauncher
= new ScriptCommandLauncher("bin/antRun.bat", baseLauncher);
}
} else if (Os.isFamily("netware")) {
CommandLauncher baseLauncher = new CommandLauncher();
shellLauncher
= new PerlScriptCommandLauncher("bin/antRun.pl", baseLauncher);
} else if (Os.isFamily("openvms")) {
// OpenVMS
try {
shellLauncher = new VmsCommandLauncher();
} catch (NoSuchMethodException exc) {
// Ignore and keep trying
}
} else {
// Generic
shellLauncher = new ScriptCommandLauncher("bin/antRun",
new CommandLauncher());
}
this.spawn = spawn;
}
Set whether or not you want the process to be spawned.
Default is not spawned. |
public void setStreamHandler(ExecuteStreamHandler streamHandler) {
this.streamHandler = streamHandler;
}
Set the stream handler to use. |
public void setVMLauncher(boolean useVMLauncher) {
this.useVMLauncher = useVMLauncher;
}
Launch this execution through the VM, where possible, rather than through
the OS's shell. In some cases and operating systems using the shell will
allow the shell to perform additional processing such as associating an
executable with a script, etc. |
public void setWorkingDirectory(File wd) {
workingDirectory =
(wd == null || wd.getAbsolutePath().equals(antWorkingDirectory))
? null : wd;
}
Sets the working directory of the process to execute.
This is emulated using the antRun scripts unless the OS is
Windows NT in which case a cmd.exe is spawned,
or MRJ and setting user.dir works, or JDK 1.3 and there is
official support in java.lang.Runtime. |
public void spawn() throws IOException {
if (workingDirectory != null && !workingDirectory.exists()) {
throw new BuildException(workingDirectory + " doesn't exist.");
}
final Process process = launch(project, getCommandline(),
getEnvironment(), workingDirectory,
useVMLauncher);
if (Os.isFamily("windows")) {
try {
Thread.sleep(ONE_SECOND);
} catch (InterruptedException e) {
project.log("interruption in the sleep after having spawned a"
+ " process", Project.MSG_VERBOSE);
}
}
OutputStream dummyOut = new OutputStream() {
public void write(int b) throws IOException {
}
};
ExecuteStreamHandler handler = new PumpStreamHandler(dummyOut);
handler.setProcessErrorStream(process.getErrorStream());
handler.setProcessOutputStream(process.getInputStream());
handler.start();
process.getOutputStream().close();
project.log("spawned process " + process.toString(),
Project.MSG_VERBOSE);
}
Starts a process defined by the command line.
Ant will not wait for this process, nor log its output. |
public static String toString(ByteArrayOutputStream bos) {
if (Os.isFamily("z/os")) {
try {
return bos.toString("Cp1047");
} catch (java.io.UnsupportedEncodingException e) {
//noop default encoding used
}
} else if (Os.isFamily("os/400")) {
try {
return bos.toString("Cp500");
} catch (java.io.UnsupportedEncodingException e) {
//noop default encoding used
}
}
return bos.toString();
}
ByteArrayOutputStream#toString doesn't seem to work reliably on
OS/390, at least not the way we use it in the execution
context. |
protected void waitFor(Process process) {
try {
process.waitFor();
setExitValue(process.exitValue());
} catch (InterruptedException e) {
process.destroy();
}
}
Wait for a given process. |