Class ExternalProcess
java.lang.Object
flexagon.ff.common.core.externalprocess.ExternalProcess
Represents an external operating system process (e.g. a unix shell script or windows batch file).
This class manages the configuration, execution,
and output of such process, which are often time necessary within FlexDeploy plugin
implementations.
For example,
For example,
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 Summary
ConstructorsConstructorDescriptionCreates an external processExternalProcess(File workdir) Creates an external processExternalProcess(File workdir, String command) Creates an external process -
Method Summary
Modifier and TypeMethodDescriptionvoidaddEnvironment(String key, String value, boolean secure) Adds an environment variable for the external process.voidaddEnvironment(List<EnvironmentKeyValue> environmentList) Adds a list of environment variable for the external process.voidaddToDoNotPrintKeys(String pKey) Sets a specific environment variable not to print to the logs during execution.voidDestroys the external process after use, freeing up its resources.voidexecute()Executes the external processReturns the list of commands to execute for this external process.Returns the list of commands appropriate for printing.Returns the Process command which is encapsulated by this ExternalProcess command.The trapped standard error logs as a List of StringsThe trapped standard out logs as a List of StringsintReturns the return code from the external process execution.Returns the configured timeout in milliseconds.Returns the working directory for the external process execution.booleanbooleanReturns whether to print the command during executionbooleanReturns whether to print environment variables during executionbooleanIf set to capture logs, and setPrintOutput(false), then the output is only stored in memory.static voidvoidsetCaptureLogs(boolean pCaptureLogs) Sets whether standard out and standard error logs from the process should be trapped.voidsetCommands(List<String> commands) Sets the commands to execute for this external process.voidsetPipeErrorStreamToOutput(boolean pPipeErrorStreamToOutput) Standard out stream will contain error stream data as well if this is set to true.voidsetPrintCommand(boolean pPrintCommand) Sets whether the command should be printed in the logs during execution.voidsetPrintEnvVariables(boolean pPrintEnvVariables) Sets whether to print environment variables to the logs during execution.voidsetPrintOutput(boolean pPrintOutput) If set to capture logs, and setPrintOutput(false), then the output is only stored in memory.voidsetSilentMode(boolean pSilentMode) Completely suppress log messages.voidsetThrowExceptionOnProcessFailure(boolean pThrowExceptionOnProcessFailure) Sets whether a non-zero return code returned from an external process execution should result in an exception being thrown.voidsetTimeout(Long pTimeout) Sets a timeout in milliseconds for the external process.voidsetWaitForProcess(boolean pWaitForProcess) Sets whether external process execution to wait for completion before returning.voidsetWorkDir(File workDir) Sets the working directory for the external process execution.
-
Constructor Details
-
ExternalProcess
public ExternalProcess()Creates an external process -
ExternalProcess
Creates an external process- Parameters:
workdir- the working directory
-
ExternalProcess
Creates an external process- Parameters:
workdir- the working directorycommand- the command to execute
-
-
Method Details
-
setThrowExceptionOnProcessFailure
public void setThrowExceptionOnProcessFailure(boolean pThrowExceptionOnProcessFailure) Sets whether a non-zero return code returned from an external process execution should result in an exception being thrown.- Parameters:
pThrowExceptionOnProcessFailure- whether to throw an exception on failure
-
setTimeout
Sets a timeout in milliseconds for the external process. If null or not specified, there is no timeout and execution will run indefinitely.- Parameters:
pTimeout- the timeout in milliseconds
-
getTimeout
Returns the configured timeout in milliseconds. If null, there is no configured timeout.- Returns:
- the timeout in milliseconds
-
getWorkDir
Returns the working directory for the external process execution.- Returns:
- the working directory
-
setWorkDir
Sets the working directory for the external process execution. If null or not specified, the default is Java's system property "user.dir".- Parameters:
workDir- the working directory
-
getCommands
Returns the list of commands to execute for this external process.- Returns:
- the list of commands
-
setCommands
Sets the commands to execute for this external process. The commands are made up of a single command and its arguments. For example, {"grep","-i","foo"}.- Parameters:
commands- the commands to execute
-
setWaitForProcess
public void setWaitForProcess(boolean pWaitForProcess) Sets whether external process execution to wait for completion before returning.- Parameters:
pWaitForProcess- whether to wait for process to complete
-
setCaptureLogs
public void setCaptureLogs(boolean pCaptureLogs) Sets whether standard out and standard error logs from the process should be trapped.- Parameters:
pCaptureLogs- whether to capture standard out and standard error logs
-
getProcess
Returns the Process command which is encapsulated by this ExternalProcess command.- Returns:
- the process object
-
getReturnCode
public int getReturnCode()Returns the return code from the external process execution.- Returns:
- the return code
-
getProcessOutput
The trapped standard out logs as a List of Strings- Returns:
- the standard out logs
-
getProcessError
The trapped standard error logs as a List of Strings- Returns:
- the standard error logs
-
getProcessErrorString
-
addEnvironment
Adds a list of environment variable for the external process.- Parameters:
environmentList- a list of environment variables
-
addEnvironment
Adds an environment variable for the external process.- Parameters:
key- the environment variable keyvalue- the environment variable valuesecure- whether the environment variable is secure
-
getPrintableCommands
Returns the list of commands appropriate for printing. This method will obfuscate secure variables within the commands.- Returns:
- the printable commands
-
execute
public void execute()Executes the external process -
destroyProcess
public void destroyProcess()Destroys the external process after use, freeing up its resources. -
setPrintEnvVariables
public void setPrintEnvVariables(boolean pPrintEnvVariables) Sets whether to print environment variables to the logs during execution. The default value is true.- Parameters:
pPrintEnvVariables- whether to print environment variables
-
isPrintEnvVariables
public boolean isPrintEnvVariables()Returns whether to print environment variables during execution- Returns:
- true if environment variables should be printed; false otherwise
-
setPrintCommand
public void setPrintCommand(boolean pPrintCommand) Sets whether the command should be printed in the logs during execution. The default is true. -
isPrintCommand
public boolean isPrintCommand()Returns whether to print the command during execution- Returns:
- true if the command should be printed; false otherwise
-
main
- Throws:
Exception
-
addToDoNotPrintKeys
Sets a specific environment variable not to print to the logs during execution.- Parameters:
pKey- the key of an environment variable
-
setPrintOutput
public void setPrintOutput(boolean pPrintOutput) If set to capture logs, and setPrintOutput(false), then the output is only stored in memory. If either or both capture logs or print output are true, it is printed the console. Defaults to true if unset, as was the original behavior. Usually, leave it as true. -
isPrintOutput
public boolean isPrintOutput()If set to capture logs, and setPrintOutput(false), then the output is only stored in memory. If either or both capture logs or print output are true, it is printed the console.- Returns:
- true or false.
-
setSilentMode
public void setSilentMode(boolean pSilentMode) Completely suppress log messages. -
setPipeErrorStreamToOutput
public void setPipeErrorStreamToOutput(boolean pPipeErrorStreamToOutput) Standard out stream will contain error stream data as well if this is set to true. -
isPipeErrorStreamToOutput
public boolean isPipeErrorStreamToOutput()
-