public class DefaultExecutor extends java.lang.Object implements Executor
Executor exec = new DefaultExecutor(); CommandLine cl = new CommandLine("ls -l"); int exitvalue = exec.execute(cl);
Modifier and Type | Field and Description |
---|---|
private java.io.IOException |
exceptionCaught
the first exception being caught to be thrown to the caller
|
private java.lang.Thread |
executorThread
worker thread for asynchronous execution
|
private int[] |
exitValues
the exit values considered to be successful
|
private CommandLauncher |
launcher
launches the command in a new process
|
private ProcessDestroyer |
processDestroyer
optional cleanup of started processes
|
private ExecuteStreamHandler |
streamHandler
taking care of output and error stream
|
private ExecuteWatchdog |
watchdog
monitoring of long running processes
|
private java.io.File |
workingDirectory
the working directory of the process
|
INVALID_EXITVALUE
Constructor and Description |
---|
DefaultExecutor()
Default constructor creating a default
PumpStreamHandler
and sets the working directory of the subprocess to the current
working directory. |
Modifier and Type | Method and Description |
---|---|
private void |
closeProcessStreams(java.lang.Process process)
Close the streams belonging to the given Process.
|
protected java.lang.Thread |
createThread(java.lang.Runnable runnable,
java.lang.String name)
Factory method to create a thread waiting for the result of an
asynchronous execution.
|
int |
execute(CommandLine command)
Methods for starting synchronous execution.
|
void |
execute(CommandLine command,
ExecuteResultHandler handler)
Methods for starting asynchronous execution.
|
int |
execute(CommandLine command,
java.util.Map<java.lang.String,java.lang.String> environment)
Methods for starting synchronous execution.
|
void |
execute(CommandLine command,
java.util.Map<java.lang.String,java.lang.String> environment,
ExecuteResultHandler handler)
Methods for starting asynchronous execution.
|
private int |
executeInternal(CommandLine command,
java.util.Map<java.lang.String,java.lang.String> environment,
java.io.File dir,
ExecuteStreamHandler streams)
Execute an internal process.
|
private java.io.IOException |
getExceptionCaught()
Get the first IOException being thrown.
|
protected java.lang.Thread |
getExecutorThread()
Get the worker thread being used for asynchronous execution.
|
ProcessDestroyer |
getProcessDestroyer()
Set the handler for cleanup of started processes if the main process
is going to terminate.
|
ExecuteStreamHandler |
getStreamHandler()
Get the StreamHandler used for providing input and
retrieving the output.
|
ExecuteWatchdog |
getWatchdog()
Get the watchdog used to kill of processes running,
typically, too long time.
|
java.io.File |
getWorkingDirectory()
Get the working directory of the created process.
|
boolean |
isFailure(int exitValue)
Checks whether
exitValue signals a failure. |
protected java.lang.Process |
launch(CommandLine command,
java.util.Map<java.lang.String,java.lang.String> env,
java.io.File dir)
Creates a process that runs a command.
|
private void |
setExceptionCaught(java.io.IOException e)
Keep track of the first IOException being thrown.
|
void |
setExitValue(int value)
Define the
exitValue of the process to be considered
successful. |
void |
setExitValues(int[] values)
Define a list of
exitValue of the process to be considered
successful. |
void |
setProcessDestroyer(ProcessDestroyer processDestroyer)
Get the handler for cleanup of started processes if the main process
is going to terminate.
|
void |
setStreamHandler(ExecuteStreamHandler streamHandler)
Set a custom the StreamHandler used for providing
input and retrieving the output.
|
void |
setWatchdog(ExecuteWatchdog watchDog)
Set the watchdog used to kill of processes running,
typically, too long time.
|
void |
setWorkingDirectory(java.io.File dir)
Set the working directory of the created process.
|
private ExecuteStreamHandler streamHandler
private java.io.File workingDirectory
private ExecuteWatchdog watchdog
private int[] exitValues
private final CommandLauncher launcher
private ProcessDestroyer processDestroyer
private java.lang.Thread executorThread
private java.io.IOException exceptionCaught
public DefaultExecutor()
PumpStreamHandler
and sets the working directory of the subprocess to the current
working directory.
The PumpStreamHandler
pumps the output of the subprocess
into our System.out
and System.err
to avoid
into our System.out
and System.err
to avoid
a blocked or deadlocked subprocess (seeProcess
).public ExecuteStreamHandler getStreamHandler()
Executor
getStreamHandler
in interface Executor
Executor.getStreamHandler()
public void setStreamHandler(ExecuteStreamHandler streamHandler)
Executor
Process
).setStreamHandler
in interface Executor
streamHandler
- the stream handlerExecutor.setStreamHandler(org.apache.commons.exec.ExecuteStreamHandler)
public ExecuteWatchdog getWatchdog()
Executor
getWatchdog
in interface Executor
Executor.getWatchdog()
public void setWatchdog(ExecuteWatchdog watchDog)
Executor
setWatchdog
in interface Executor
watchDog
- the watchdogExecutor.setWatchdog(org.apache.commons.exec.ExecuteWatchdog)
public ProcessDestroyer getProcessDestroyer()
Executor
getProcessDestroyer
in interface Executor
Executor.getProcessDestroyer()
public void setProcessDestroyer(ProcessDestroyer processDestroyer)
Executor
setProcessDestroyer
in interface Executor
processDestroyer
- the ProcessDestroyerExecutor.setProcessDestroyer(ProcessDestroyer)
public java.io.File getWorkingDirectory()
Executor
getWorkingDirectory
in interface Executor
Executor.getWorkingDirectory()
public void setWorkingDirectory(java.io.File dir)
Executor
setWorkingDirectory
in interface Executor
dir
- the working directoryExecutor.setWorkingDirectory(java.io.File)
public int execute(CommandLine command) throws ExecuteException, java.io.IOException
Executor
execute
in interface Executor
command
- the command to executeExecuteException
- execution of subprocess failed or the
subprocess returned a exit value indicating a failure
Executor.setExitValue(int)
.java.io.IOException
Executor.execute(CommandLine)
public int execute(CommandLine command, java.util.Map<java.lang.String,java.lang.String> environment) throws ExecuteException, java.io.IOException
Executor
execute
in interface Executor
command
- the command to executeenvironment
- The environment for the new process. If null, the
environment of the current process is used.ExecuteException
- execution of subprocess failed or the
subprocess returned a exit value indicating a failure
Executor.setExitValue(int)
.java.io.IOException
Executor.execute(CommandLine, java.util.Map)
public void execute(CommandLine command, ExecuteResultHandler handler) throws ExecuteException, java.io.IOException
Executor
execute
in interface Executor
command
- the command to executehandler
- capture process termination and exit codeExecuteException
- execution of subprocess failedjava.io.IOException
Executor.execute(CommandLine,
org.apache.commons.exec.ExecuteResultHandler)
public void execute(CommandLine command, java.util.Map<java.lang.String,java.lang.String> environment, ExecuteResultHandler handler) throws ExecuteException, java.io.IOException
Executor
execute
in interface Executor
command
- the command to executeenvironment
- The environment for the new process. If null, the
environment of the current process is used.handler
- capture process termination and exit codeExecuteException
- execution of subprocess failedjava.io.IOException
Executor.execute(CommandLine,
java.util.Map, org.apache.commons.exec.ExecuteResultHandler)
public void setExitValue(int value)
Executor
exitValue
of the process to be considered
successful. If a different exit value is returned by
the process then Executor.execute(CommandLine)
will throw an ExecuteException
setExitValue
in interface Executor
value
- the exit code representing successful executionExecutor.setExitValue(int)
public void setExitValues(int[] values)
Executor
exitValue
of the process to be considered
successful. The caller can pass one of the following values
Executor.isFailure(int)
Executor.execute(CommandLine)
will
throw an ExecuteException
.setExitValues
in interface Executor
values
- a list of the exit codesExecutor.setExitValues(int[])
public boolean isFailure(int exitValue)
Executor
exitValue
signals a failure. If no
exit values are set than the default conventions of the OS is
used. e.g. most OS regard an exit code of '0' as successful
execution and everything else as failure.isFailure
in interface Executor
exitValue
- the exit value (return code) to be checkedtrue
if exitValue
signals a failureExecutor.isFailure(int)
protected java.lang.Thread createThread(java.lang.Runnable runnable, java.lang.String name)
runnable
- the runnable passed to the threadname
- the name of the threadprotected java.lang.Process launch(CommandLine command, java.util.Map<java.lang.String,java.lang.String> env, java.io.File dir) throws java.io.IOException
command
- the command to runenv
- the environment for the commanddir
- the working directory for the commandjava.io.IOException
- forwarded from the particular launcher usedprotected java.lang.Thread getExecutorThread()
private void closeProcessStreams(java.lang.Process process)
process
- the Process
.private int executeInternal(CommandLine command, java.util.Map<java.lang.String,java.lang.String> environment, java.io.File dir, ExecuteStreamHandler streams) throws java.io.IOException
command
- the command to executeenvironment
- the execution environmentdir
- the working directorystreams
- process the streams (in, out, err) of the processjava.io.IOException
- executing the process failedprivate void setExceptionCaught(java.io.IOException e)
e
- the IOExceptionprivate java.io.IOException getExceptionCaught()