public class ProcessExecutor extends Object
Warning: caution should be exercised when using this class - it allows any process to be started with no security restrictions.
| Modifier and Type | Class and Description |
|---|---|
protected static class |
ProcessExecutor.RedirectThreads
Wrapper for threads used for capturing output.
|
| Constructor and Description |
|---|
ProcessExecutor()
Constructs using a new, cached thread pool.
|
ProcessExecutor(ExecutorService threadPool)
Constructs with a thread pool which executes tasks.
|
| Modifier and Type | Method and Description |
|---|---|
protected File |
createOutputFile(ProcessToStart process)
Creates the output file and returns its
File representation. |
ProcessExecutorResults |
execute(ProcessToStart processToStart)
This executes any operating system process as described in the given start command.
|
protected String[] |
getCommandLine(ProcessToStart process)
Builds the command line containing the full path to the program executable and any arguments that are to be
passed to the program.
|
protected String |
getFullProgramExecutablePath(ProcessToStart process)
Returns the full pathname to the program executable.
|
protected File |
getInputFile(ProcessToStart process)
Gets the input file and returns its
File representation. |
protected File |
getWorkingDirectory(ProcessToStart process)
Returns the full pathname to the working directory.
|
protected ProcessExecutor.RedirectThreads |
redirectAllStreams(ProcessToStart process,
Process childProcess)
This method redirects the stdout/stderr streams of the child process to the output log file and pipes the
contents of the input file (if one was specified) to the stdin stream of the child process.
|
protected void |
redirectStreams(ProcessToStart process,
Process childProcess)
Deprecated.
|
protected Integer |
startProgram(ProcessToStart process)
Starts a child process.
|
public ProcessExecutor(ExecutorService threadPool)
public ProcessExecutor()
public ProcessExecutorResults execute(ProcessToStart processToStart)
ProcessToStart.setWaitForExit(Long) to a positive, non-zero timeout
in milliseconds. On error, the exception will be returned in the returned results.processToStart - the information on what process to startprotected Integer startProgram(ProcessToStart process) throws Exception
ProcessToStart.getWaitForExit() is positive, non-zero. In that case, the returned value will be the exit
code of the process. If this method is told not to wait, the returned value will be null.process - provides the information necessary to start the child processnull if this method was to only
start the process but not wait or was to wait and the wait time expired before the process exitedException - if any error occurs while trying to start the child process@Deprecated protected void redirectStreams(ProcessToStart process, Process childProcess) throws IOException
IOExceptionprotected ProcessExecutor.RedirectThreads redirectAllStreams(ProcessToStart process, Process childProcess) throws IOException
This is done asynchronously so as to avoid deadlocking and to allow the main thread to continue its work.
Once the child process dies, so do the piping threads.
process - used to configure the processchildProcess - the newly spawned child processIOException - if failed to pipe data to/from stdin/stdoutprotected File createOutputFile(ProcessToStart process) throws IOException
File representation. This is the location where the child
process' stdout/stderr output streams get redirected to. Note that if the file does not exist, it will be
created; if it does exist, the original will be renamed with a timestamp before a new file is created only if the
backup output file flag is Boolean.TRUE; otherwise it
will be overwritten.
If the output directory was not specified, a temporary location
will be used (i.e. the System property java.io.tmpdir). If the
output file name was not specified, one will be generated automatically -
using the title if one was specified.
If we are not to capture the output, this method returns
null.
process - the process to startnull)IOException - if the output file could not be createdFileNotFoundException - the output directory does not exist or is not a valid directoryprotected File getInputFile(ProcessToStart process) throws IOException
File representation. This is the file whose data will be sent to
the child process' stdin input stream. Note that if the input file that was specified in the command does not
exist, an exception is thrown. If the command does not specify an input file, null is returned.process - the start commandnull)IOException - if the input file could not be foundFileNotFoundException - the input directory does not exist or is not a valid directoryprotected String getFullProgramExecutablePath(ProcessToStart process) throws FileNotFoundException
process - the process to startFileNotFoundException - if the program executable does not existprotected File getWorkingDirectory(ProcessToStart process) throws FileNotFoundException
null, child process inherits the parent process's current working
directory.process - the process to startFileNotFoundException - if the working directory does not existprotected String[] getCommandLine(ProcessToStart process) throws FileNotFoundException
process - the process to startFileNotFoundException - if the program executable file does not existCopyright © 2008-2014 Red Hat, Inc.. All Rights Reserved.