Class ProcessRunner


  • public class ProcessRunner
    extends java.lang.Object
    Utility class to run processes based on command line arguments by wrapping a .
    • Constructor Summary

      Constructors 
      Constructor Description
      ProcessRunner​(java.lang.String... args)
      Constructor, allowing some command line arguments
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addArg​(java.lang.String arg)
      Adds a command line argument to the list of existing list of command line arguments
      void addArgs​(java.lang.String... args)
      Adds a varargs list of arguments to the existing list of command line of arguments
      void addArgs​(java.util.Collection<java.lang.String> args)
      Adds a collection of arguments to the existing list of command line of arguments
      void addSecretArg​(java.lang.String arg)
      Adds a command line argument to the list of existing list of command line arguments, marking it as secret argument, in order to avoid logging
      void addToEnv​(java.lang.String key, java.lang.String value)
      Adds a pair (key, value) to the environment map of the process
      static java.lang.Integer executeWithFeedback​(java.lang.String name, java.lang.String... args)
      Executes a process while printing "." every second the process is running.
      java.lang.String getCmd()  
      java.util.List<java.lang.String> getCmdList()  
      java.lang.String getLastResponse()
      Gets the last line of the output process
      java.lang.String getResponse()
      Gets the response of the process as single string
      java.util.List<java.lang.String> getResponses()
      Gets the response of the process as list of lines
      int runProcess​(java.lang.String processName)
      Runs a process with a given set of command line arguments
      int runProcess​(java.lang.String processName, java.io.File workingDirectory)
      Runs a process with a given set of command line arguments, in a given working directory.
      static java.lang.String runProcessForSingleOutput​(java.lang.String name, java.lang.String... args)
      Static method that can be used to process a given command line, returning the output of the process as a single string, or null if it failed.
      boolean runTimedProcess​(java.lang.String processName, long timeout)
      Runs a process with a given set of command line arguments within a given time frame
      boolean runTimedProcess​(java.lang.String processName, java.io.File workingDirectory, long timeout)
      Runs a process with a given set of command line arguments, in a given working directory, within a given time frame
      void setInfo​(boolean info)
      When set to true, it will log with Level.INFO the output during the process.
      void setInteractive​(boolean interactive)
      When set to true, it will enable user interaction during the process.
      void setLogToFile​(boolean logToFile)
      When set to true, it will log to a file the command line arguments, the output and the result of the process.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ProcessRunner

        public ProcessRunner​(java.lang.String... args)
        Constructor, allowing some command line arguments
        Parameters:
        args - A varargs of command line arguments
    • Method Detail

      • setInfo

        public void setInfo​(boolean info)
        When set to true, it will log with Level.INFO the output during the process. By default is false, and uses Level.DEBUG
        Parameters:
        info - a boolean that sets the log level of the process output
      • setInteractive

        public void setInteractive​(boolean interactive)
        When set to true, it will enable user interaction during the process. By default is false
        Parameters:
        interactive - a boolean that sets the interactive mode
      • setLogToFile

        public void setLogToFile​(boolean logToFile)
        When set to true, it will log to a file the command line arguments, the output and the result of the process. By default it is false, but the log will be created regardless this value for any failing process.
        Parameters:
        logToFile - if true will log the process to a file
      • addArg

        public void addArg​(java.lang.String arg)
        Adds a command line argument to the list of existing list of command line arguments
        Parameters:
        arg - a string passed to the command line arguments
      • addSecretArg

        public void addSecretArg​(java.lang.String arg)
        Adds a command line argument to the list of existing list of command line arguments, marking it as secret argument, in order to avoid logging
        Parameters:
        arg - a string passed to the command line arguments
      • addArgs

        public void addArgs​(java.lang.String... args)
        Adds a varargs list of arguments to the existing list of command line of arguments
        Parameters:
        args - varargs list of arguments
      • addArgs

        public void addArgs​(java.util.Collection<java.lang.String> args)
        Adds a collection of arguments to the existing list of command line of arguments
        Parameters:
        args - a collection of arguments
      • getCmd

        public java.lang.String getCmd()
        Returns:
        the command line of arguments as a string
      • getCmdList

        public java.util.List<java.lang.String> getCmdList()
        Returns:
        the current list of command line arguments
      • addToEnv

        public void addToEnv​(java.lang.String key,
                             java.lang.String value)
        Adds a pair (key, value) to the environment map of the process
        Parameters:
        key - a string with the environmental variable name
        value - a string with the environmental variable value
      • runProcess

        public int runProcess​(java.lang.String processName)
                       throws java.io.IOException,
                              java.lang.InterruptedException
        Runs a process with a given set of command line arguments
        Parameters:
        processName - the name of the process
        Returns:
        0 if the process ends successfully, non-zero values indicate a failure
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • runProcess

        public int runProcess​(java.lang.String processName,
                              java.io.File workingDirectory)
                       throws java.io.IOException,
                              java.lang.InterruptedException
        Runs a process with a given set of command line arguments, in a given working directory.
        Parameters:
        processName - the name of the process
        workingDirectory - a file with the working directory of the process
        Returns:
        0 if the process ends successfully, non-zero values indicate a failure
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • runTimedProcess

        public boolean runTimedProcess​(java.lang.String processName,
                                       long timeout)
                                throws java.io.IOException,
                                       java.lang.InterruptedException
        Runs a process with a given set of command line arguments within a given time frame
        Parameters:
        processName - the name of the process
        timeout - the maximum time allowed to run the process
        Returns:
        true if the process ended successfully, false otherwise
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • runTimedProcess

        public boolean runTimedProcess​(java.lang.String processName,
                                       java.io.File workingDirectory,
                                       long timeout)
                                throws java.io.IOException,
                                       java.lang.InterruptedException
        Runs a process with a given set of command line arguments, in a given working directory, within a given time frame
        Parameters:
        processName - the name of the process
        workingDirectory - a file with the working directory of the process
        timeout - the maximum time allowed to run the process
        Returns:
        true if the process ended successfully, false otherwise
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • getResponse

        public java.lang.String getResponse()
        Gets the response of the process as single string
        Returns:
        a single string with the whole output of the process
      • getResponses

        public java.util.List<java.lang.String> getResponses()
        Gets the response of the process as list of lines
        Returns:
        a list with all the lines of the output
      • getLastResponse

        public java.lang.String getLastResponse()
        Gets the last line of the output process
        Returns:
        a string with the last line of the output
      • runProcessForSingleOutput

        public static java.lang.String runProcessForSingleOutput​(java.lang.String name,
                                                                 java.lang.String... args)
                                                          throws java.io.IOException,
                                                                 java.lang.InterruptedException
        Static method that can be used to process a given command line, returning the output of the process as a single string, or null if it failed. It is convenient when the output of the process to be executed returns a single line
        Parameters:
        name - the name of the process
        args - a varargs list of command line arguments
        Returns:
        a string with the response of the process or null if it failed
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • executeWithFeedback

        public static java.lang.Integer executeWithFeedback​(java.lang.String name,
                                                            java.lang.String... args)
                                                     throws java.io.IOException,
                                                            java.lang.InterruptedException
        Executes a process while printing "." every second the process is running. The feedback is helpful for end user. A lack of feedback can lead to an impression that the process is stuck.
        Parameters:
        name - the name of the process
        args - a varargs list of command line arguments
        Returns:
        Integer result of the process
        Throws:
        java.io.IOException
        java.lang.InterruptedException