Class ExternalProcess

java.lang.Object
flexagon.ff.common.core.externalprocess.ExternalProcess

public class ExternalProcess extends Object
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,

      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 Details

    • ExternalProcess

      public ExternalProcess()
      Creates an external process
    • ExternalProcess

      public ExternalProcess(File workdir)
      Creates an external process
      Parameters:
      workdir - the working directory
    • ExternalProcess

      public ExternalProcess(File workdir, String command)
      Creates an external process
      Parameters:
      workdir - the working directory
      command - 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

      public void setTimeout(Long pTimeout)
      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

      public Long getTimeout()
      Returns the configured timeout in milliseconds. If null, there is no configured timeout.
      Returns:
      the timeout in milliseconds
    • getWorkDir

      public File getWorkDir()
      Returns the working directory for the external process execution.
      Returns:
      the working directory
    • setWorkDir

      public void setWorkDir(File workDir)
      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

      public List<String> getCommands()
      Returns the list of commands to execute for this external process.
      Returns:
      the list of commands
    • setCommands

      public void setCommands(List<String> commands)
      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

      public Process 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

      public List<String> getProcessOutput()
      The trapped standard out logs as a List of Strings
      Returns:
      the standard out logs
    • getProcessError

      public List<String> getProcessError()
      The trapped standard error logs as a List of Strings
      Returns:
      the standard error logs
    • getProcessErrorString

      public String getProcessErrorString()
    • addEnvironment

      public void addEnvironment(List<EnvironmentKeyValue> environmentList)
      Adds a list of environment variable for the external process.
      Parameters:
      environmentList - a list of environment variables
    • addEnvironment

      public void addEnvironment(String key, String value, boolean secure)
      Adds an environment variable for the external process.
      Parameters:
      key - the environment variable key
      value - the environment variable value
      secure - whether the environment variable is secure
    • getPrintableCommands

      public List<String> 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

      public static void main(String[] args) throws Exception
      Throws:
      Exception
    • addToDoNotPrintKeys

      public void addToDoNotPrintKeys(String pKey)
      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()