public class ExternalProcess
extends java.lang.Object
ExternalProcess p = new ExternalProcess(workDir, "/var/tmp/dowork.sh"); p.setCaptureLogs(false); p.setWaitForProcess(true); p.setTimeout(5000L); // 5 secs p.addEnvironment("foo", "bar", false); p.execute(); System.out.println(p.getReturnCode());
Constructor and Description |
---|
ExternalProcess()
Creates an external process
|
ExternalProcess(java.io.File workdir)
Creates an external process
|
ExternalProcess(java.io.File workdir,
java.lang.String command)
Creates an external process
|
Modifier and Type | Method and Description |
---|---|
void |
addEnvironment(java.util.List<EnvironmentKeyValue> environmentList)
Adds a list of environment variable for the external process.
|
void |
addEnvironment(java.lang.String key,
java.lang.String value,
boolean secure)
Adds an environment variable for the external process.
|
void |
addToDoNotPrintKeys(java.lang.String pKey)
Sets a specific environment variable not to print to the logs during execution.
|
void |
destroyProcess()
Destroys the external process after use, freeing up its resources.
|
void |
execute()
Executes the external process
|
java.util.List<java.lang.String> |
getCommands()
Returns the list of commands to execute for this external process.
|
java.util.List<java.lang.String> |
getPrintableCommands()
Returns the list of commands appropriate for printing.
|
java.lang.Process |
getProcess()
Returns the Process command which is encapsulated by this ExternalProcess command.
|
java.util.List<java.lang.String> |
getProcessError()
The trapped standard error logs as a List of Strings
|
java.util.List<java.lang.String> |
getProcessOutput()
The trapped standard out logs as a List of Strings
|
int |
getReturnCode()
Returns the return code from the external process execution.
|
java.lang.Long |
getTimeout()
Returns the configured timeout in milliseconds.
|
java.io.File |
getWorkDir()
Returns the working directory for the external process execution.
|
boolean |
isPipeErrorStreamToOutput() |
boolean |
isPrintCommand()
Returns whether to print the command during execution
|
boolean |
isPrintEnvVariables()
Returns whether to print environment variables during execution
|
boolean |
isPrintOutput()
If set to capture logs, and setPrintOutput(false), then the output is only stored in memory.
|
static void |
main(java.lang.String[] args) |
void |
setCaptureLogs(boolean pCaptureLogs)
Sets whether standard out and standard error logs from the process should be trapped.
|
void |
setCommands(java.util.List<java.lang.String> commands)
Sets the commands to execute for this external process.
|
void |
setPipeErrorStreamToOutput(boolean pPipeErrorStreamToOutput)
Standard out stream will contain error stream data as well if this is set to true.
|
void |
setPrintCommand(boolean pPrintCommand)
Sets whether the command should be printed in the logs during execution.
|
void |
setPrintEnvVariables(boolean pPrintEnvVariables)
Sets whether to print environment variables to the logs during execution.
|
void |
setPrintOutput(boolean pPrintOutput)
If set to capture logs, and setPrintOutput(false), then the output is only stored in memory.
|
void |
setSilentMode(boolean pSilentMode)
Completely suppress log messages.
|
void |
setThrowExceptionOnProcessFailure(boolean pThrowExceptionOnProcessFailure)
Sets whether a non-zero return code returned from an external process execution should result in an exception being thrown.
|
void |
setTimeout(java.lang.Long pTimeout)
Sets a timeout in milliseconds for the external process.
|
void |
setWaitForProcess(boolean pWaitForProcess)
Sets whether external process execution to wait for completion before returning.
|
void |
setWorkDir(java.io.File workDir)
Sets the working directory for the external process execution.
|
public ExternalProcess()
public ExternalProcess(java.io.File workdir)
workdir
- the working directorypublic ExternalProcess(java.io.File workdir, java.lang.String command)
workdir
- the working directorycommand
- the command to executepublic void setThrowExceptionOnProcessFailure(boolean pThrowExceptionOnProcessFailure)
pThrowExceptionOnProcessFailure
- whether to throw an exception on failurepublic void setTimeout(java.lang.Long pTimeout)
pTimeout
- the timeout in millisecondspublic java.lang.Long getTimeout()
public java.io.File getWorkDir()
public void setWorkDir(java.io.File workDir)
workDir
- the working directorypublic java.util.List<java.lang.String> getCommands()
public void setCommands(java.util.List<java.lang.String> commands)
commands
- the commands to executepublic void setWaitForProcess(boolean pWaitForProcess)
pWaitForProcess
- whether to wait for process to completepublic void setCaptureLogs(boolean pCaptureLogs)
pCaptureLogs
- whether to capture standard out and standard error logspublic java.lang.Process getProcess()
public int getReturnCode()
public java.util.List<java.lang.String> getProcessOutput()
public java.util.List<java.lang.String> getProcessError()
public void addEnvironment(java.util.List<EnvironmentKeyValue> environmentList)
environmentList
- a list of environment variablespublic void addEnvironment(java.lang.String key, java.lang.String value, boolean secure)
key
- the environment variable keyvalue
- the environment variable valuesecure
- whether the environment variable is securepublic java.util.List<java.lang.String> getPrintableCommands()
public void execute()
public void destroyProcess()
public void setPrintEnvVariables(boolean pPrintEnvVariables)
pPrintEnvVariables
- whether to print environment variablespublic boolean isPrintEnvVariables()
public void setPrintCommand(boolean pPrintCommand)
pPrintCommand
- public boolean isPrintCommand()
public static void main(java.lang.String[] args) throws java.lang.Exception
java.lang.Exception
public void addToDoNotPrintKeys(java.lang.String pKey)
pKey
- the key of an environment variablepublic void setPrintOutput(boolean pPrintOutput)
pPrintOutput
- public boolean isPrintOutput()
public void setSilentMode(boolean pSilentMode)
pSilentMode
- public void setPipeErrorStreamToOutput(boolean pPipeErrorStreamToOutput)
pPipeErrorStreamToOutput
- public boolean isPipeErrorStreamToOutput()