Class JkAbstractProcess<T extends JkAbstractProcess>

java.lang.Object
dev.jeka.core.api.system.JkAbstractProcess<T>
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
JkDocker, JkGit, JkJavaProcess, JkMvn, JkProcess

public abstract class JkAbstractProcess<T extends JkAbstractProcess> extends Object implements Runnable
The JkProcess class represents a process that can be executed on the system. It provides various methods for configuring and executing the process.
  • Field Details

    • CURRENT_JAVA_DIR

      protected static final Path CURRENT_JAVA_DIR
  • Constructor Details

    • JkAbstractProcess

      protected JkAbstractProcess()
    • JkAbstractProcess

      protected JkAbstractProcess(JkAbstractProcess<?> other)
  • Method Details

    • copy

      protected T copy()
    • setParamAt

      public T setParamAt(int index, String parameter)
      Specify the command to execute
    • setCollectStdout

      public T setCollectStdout(boolean collectStdout)
      Sets the flag to indicate whether the std out of the process should be collected.

      This is mandatory to collect output if we want to get the get the JkProcResult.getStdout() after process execution.

      Initial value is false.

    • setCollectStderr

      public T setCollectStderr(boolean collectStderr)
      Sets the flag to indicate whether the std err of the process should be collected.

      This is mandatory to collect output if we want to get the get the JkProcResult.getStdout() after process execution.

      Initial value is false.

    • setDestroyAtJvmShutdown

      public T setDestroyAtJvmShutdown(boolean destroy)
      Sets the flag to destroy the process at JVM shutdown.
    • addParamsAsCmdLine

      public T addParamsAsCmdLine(String cmdLine, Object... tokens)
      Adds the specified parameters as a space separated args to the command line. The string will be parsed in an array of parameters.
      Parameters:
      cmdLine - the parameters as a string (e.g '-X -e run').
      tokens - If command line contains tokens as '%s', they are replaced by these tokens.
    • addParams

      public T addParams(String... parameters)
      Adds specified parameters to the command line
    • removeParam

      public T removeParam(String parameter)
      Removes specified parameter to the command line
    • addParamsIf

      public T addParamsIf(boolean condition, String... parameters)
      Adds specified parameters to the command line if the specified condition is true.
    • addParams

      public T addParams(Collection<String> parameters)
      Adds the specified parameters to the command line.
      See Also:
    • addParamsAt

      public T addParamsAt(int index, Collection<String> parameters)
      Adds the specified parameters to the command line at the beginning of the list. Any null values in the parameters collection will be removed before adding.
    • addParamsAt

      public T addParamsAt(int index, String... parameters)
      Adds the specified parameters to the command line at the beginning of the list.
      See Also:
    • setWorkingDir

      public T setWorkingDir(Path workingDir)
      Sets the specified working directory to launch the process.
    • setWorkingDir

      public T setWorkingDir(String workingDir)
      Sets the specified working directory to launch the process.
      See Also:
    • setEnv

      public T setEnv(String name, String value)
      Sets the value of the environment variable with the specified name.
      Parameters:
      name - the name of the environment variable
      value - the value to be assigned to the environment variable
    • setFailOnError

      public T setFailOnError(boolean fail)
      Specifies if the running process should throw an Exception in case process returns with a code different to 0.

      Initial value is 'true'.

    • setLogCommand

      public T setLogCommand(boolean logCommand)
      If true, the command line will be outputed in the console
    • setLogWithJekaDecorator

      public T setLogWithJekaDecorator(boolean logOutputWithLogDecorator)
      Set if the output streams of the process must be logged keeping the JkLog decorator.

      This may be desirable, as the output will honor the indentation and visibility set by JeKa log decorator. This implies a small extra resource costs that is acceptable in most of the situations.

      Be aware that the stdout and stderr of the sub-process will be mixed in the Process.getInputStream() as specified in this method Javadoc.

      Nevertheless, if you want to bypass this mechanism, you can specify to not using it. In this case, you may set up explicitly a mean to get the output streams. One solution consists in invoking setInheritIO(true)

      Initial value is true..

    • setInheritIO

      public T setInheritIO(boolean value)
      Makes the process use the same streams as its parent.

      This can not be used in conjunction of logWithJekaDecorator, so this one is disabled forcibly when invoking this method.
      Initial value is false

      See Also:
    • inheritJkLogOptions

      public T inheritJkLogOptions()
      Adds a param -lv=[DecoratorStyle] matching the current one.
    • redirectErrorStream

      public T redirectErrorStream(boolean value)
      Same as ProcessBuilder.redirectErrorStream(boolean).

      Initial value is false.

    • getWorkingDir

      public Path getWorkingDir()
      Returns the working directory of this process.
    • getParamAt

      public String getParamAt(int index)
      Returns the command launched by this process.
    • getParams

      public List<String> getParams()
    • isFailOnError

      public boolean isFailOnError()
      Returns true if this process must throw an execption if the underlying process returns code different from 0.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • customizeCommand

      protected void customizeCommand()
      Modifies the command and its execution parameters.

      This method provides a way for subclasses to adjust the process before it is run.

    • run

      public void run()
      Specified by:
      run in interface Runnable
    • exec

      public JkProcResult exec()
      Starts this process and wait for the process has finished prior returning. The output of the created process will be redirected on the current output.
    • execCmd

      public T execCmd(String... commandParams)
      Executes a command with the specified parameters and returns the current instance. This method uses a clone of the current instance's configuration to execute a command.
      Parameters:
      commandParams - The parameters to be added to the command being executed.
      Returns:
      The current instance after executing the command.
    • execCmdLine

      public T execCmdLine(String format, String... tokens)
      See Also:
    • execAsync

      public JkProcHandler execAsync()
      Executes the process asynchronously and returns a JkProcHandler object which can be used to interact with the running process.