java.lang.Object
one.jpro.platform.internal.util.CommandRunner

public class CommandRunner extends Object
Run processes based on command line arguments.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Initializes CommandRunner with default logger and command line arguments.
    CommandRunner(org.slf4j.Logger logger, @NotNull String... args)
    Initializes CommandRunner with specified logger and command line arguments.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addArg(@NotNull String arg)
    Adds a command line argument to the list of existing list of command line arguments
    void
    addArgs(@NotNull String... args)
    Adds a varargs list of arguments to the existing list of command line of arguments
    void
    addArgs(@NotNull Collection<String> args)
    Adds a collection of arguments to the existing list of command line of arguments
    void
    addSecretArg(@Nullable 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(@NotNull String key, @NotNull String value)
    Adds a pair (key, value) to the environment map of the process.
     
     
    Gets the last line of the output process.
    @Nullable String
    Gets the response of the process as single string.
    @NotNull List<String>
    Gets the response of the process as list of lines.
    int
    run(@Nullable String processName)
    Runs a process with a given set of command line arguments.
    int
    run(@Nullable String processName, @Nullable File workingDirectory)
    Runs a process with a given set of command line arguments, in a given working directory.
    runAsync(@Nullable String processName)
    Runs a process asynchronously with a given set of command line arguments.
    runAsync(@Nullable String processName, boolean mergeOutput)
    Runs a process asynchronously with a given set of command line arguments.
    runAsync(@Nullable String processName, @Nullable File workingDirectory)
    Runs a process asynchronously with a given set of command line arguments, in a given working directory.
    runAsync(@Nullable String processName, @Nullable File workingDirectory, boolean mergeOutput)
    Runs a process asynchronously with a given set of command line arguments, in a given working directory.
    boolean
    runTimed(@Nullable String processName, long timeout)
    Runs a process with a given set of command line arguments within a given time frame.
    boolean
    runTimed(@Nullable String processName, @Nullable 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
    setInteractive(boolean interactive)
    When set to true, it will enable user interaction during the process.
    void
    setPrintToConsole(boolean printToConsole)
    When set to true, it will enable the process output to be printed to the console.

    Methods inherited from class java.lang.Object

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

    • CommandRunner

      public CommandRunner(String... args)
      Initializes CommandRunner with default logger and command line arguments.
      Parameters:
      args - command line arguments to be executed by the process.
    • CommandRunner

      public CommandRunner(org.slf4j.Logger logger, @NotNull @NotNull String... args)
      Initializes CommandRunner with specified logger and command line arguments.
      Parameters:
      logger - Logger instance for logging process activities.
      args - Command line arguments to be executed by the process.
  • Method Details

    • 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
    • setPrintToConsole

      public void setPrintToConsole(boolean printToConsole)
      When set to true, it will enable the process output to be printed to the console. By default, is false
      Parameters:
      printToConsole - a boolean that sets the print to console mode
    • addArg

      public void addArg(@NotNull @NotNull 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(@Nullable @Nullable 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(@NotNull @NotNull 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(@NotNull @NotNull Collection<String> args)
      Adds a collection of arguments to the existing list of command line of arguments
      Parameters:
      args - a collection of arguments
    • getCmd

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

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

      public void addToEnv(@NotNull @NotNull String key, @NotNull @NotNull 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
    • run

      public int run(@Nullable @Nullable String processName) throws IOException, 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:
      IOException - if an I/O error occurs
      InterruptedException - if the current thread is interrupted by another thread while it is waiting
    • run

      public int run(@Nullable @Nullable String processName, @Nullable @Nullable File workingDirectory) throws IOException, 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:
      IOException - if an I/O error occurs
      InterruptedException - if the current thread is interrupted by another thread while it is waiting
    • runAsync

      public Process runAsync(@Nullable @Nullable String processName) throws IOException
      Runs a process asynchronously with a given set of command line arguments. By default, it merges the output of the process.
      Parameters:
      processName - the name of the process
      Returns:
      the Process object
      Throws:
      IOException - if an I/O error occurs
    • runAsync

      public Process runAsync(@Nullable @Nullable String processName, @Nullable @Nullable File workingDirectory) throws IOException
      Runs a process asynchronously with a given set of command line arguments, in a given working directory. By default, it merges the output of the process.
      Parameters:
      processName - the name of the process
      workingDirectory - a file with the working directory of the process
      Returns:
      the Process object
      Throws:
      IOException - if an I/O error occurs
    • runAsync

      public Process runAsync(@Nullable @Nullable String processName, boolean mergeOutput) throws IOException
      Runs a process asynchronously with a given set of command line arguments.
      Parameters:
      processName - the name of the process
      mergeOutput - a boolean that sets the merge output mode
      Returns:
      the Process object
      Throws:
      IOException - if an I/O error occurs
    • runAsync

      public Process runAsync(@Nullable @Nullable String processName, @Nullable @Nullable File workingDirectory, boolean mergeOutput) throws IOException
      Runs a process asynchronously 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
      mergeOutput - a boolean that sets the merge output mode
      Returns:
      the Process object
      Throws:
      IOException - if an I/O error occurs
    • runTimed

      public boolean runTimed(@Nullable @Nullable String processName, long timeout) throws IOException, 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 in seconds
      Returns:
      true if the process ended successfully, false otherwise
      Throws:
      IOException - if an I/O error occurs
      InterruptedException - if the current thread is interrupted by another thread while it is waiting
    • runTimed

      public boolean runTimed(@Nullable @Nullable String processName, @Nullable @Nullable File workingDirectory, long timeout) throws IOException, 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:
      IOException - if an I/O error occurs
      InterruptedException - if the current thread is interrupted by another thread while it is waiting
    • getResponse

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

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

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