public abstract class JkAbstractProcess<T extends JkAbstractProcess>
extends java.lang.Object
implements java.lang.Runnable
Modifier and Type | Field and Description |
---|---|
protected static java.nio.file.Path |
CURRENT_JAVA_DIR |
Modifier | Constructor and Description |
---|---|
protected |
JkAbstractProcess() |
protected |
JkAbstractProcess(JkAbstractProcess<?> other) |
Modifier and Type | Method and Description |
---|---|
T |
addParams(java.util.Collection<java.lang.String> parameters)
Adds the specified parameters to the command line.
|
T |
addParams(java.lang.String... parameters)
Adds specified parameters to the command line
|
T |
addParamsAsCmdLine(java.lang.String cmdLine,
java.lang.Object... tokens)
Adds the specified parameters as a space separated args to the command line.
|
T |
addParamsAt(int index,
java.util.Collection<java.lang.String> parameters)
Adds the specified parameters to the command line at the beginning of the list.
|
T |
addParamsAt(int index,
java.lang.String... parameters)
Adds the specified parameters to the command line at the beginning of the list.
|
T |
addParamsIf(boolean condition,
java.lang.String... parameters)
Adds specified parameters to the command line if the specified condition is true.
|
protected T |
copy() |
protected void |
customizeCommand()
Modifies the command and its execution parameters.
|
JkProcResult |
exec()
Starts this process and wait for the process has finished prior
returning.
|
JkProcHandler |
execAsync()
Executes the process asynchronously and returns a
JkProcHandler object
which can be used to interact with the running process. |
T |
execCmd(java.lang.String... commandParams)
Executes a command with the specified parameters and returns the current instance.
|
T |
execCmdLine(java.lang.String format,
java.lang.String... tokens) |
java.lang.String |
getParamAt(int index)
Returns the command launched by this process.
|
java.util.List<java.lang.String> |
getParams() |
java.nio.file.Path |
getWorkingDir()
Returns the working directory of this process.
|
T |
inheritJkLogOptions()
Adds a param -lv=[DecoratorStyle] matching the current one.
|
boolean |
isFailOnError()
Returns
true if this process must throw an execption if the underlying process returns
code different from 0. |
T |
redirectErrorStream(boolean value)
Same as
ProcessBuilder.redirectErrorStream(boolean) . |
T |
removeParam(java.lang.String parameter)
Removes specified parameter to the command line
|
void |
run() |
T |
setCollectStderr(boolean collectStderr)
Sets the flag to indicate whether the std err of the process should be collected.
|
T |
setCollectStdout(boolean collectStdout)
Sets the flag to indicate whether the std out of the process should be collected.
|
T |
setDestroyAtJvmShutdown(boolean destroy)
Sets the flag to destroy the process at JVM shutdown.
|
T |
setEnv(java.lang.String name,
java.lang.String value)
Sets the value of the environment variable with the specified name.
|
T |
setFailOnError(boolean fail)
Specifies if the running process should throw an Exception in case process returns with a
code different to 0.
|
T |
setInheritIO(boolean value)
Makes the process use the same streams as its parent.
|
T |
setLogCommand(boolean logCommand)
If true, the command line will be outputed in the console
|
T |
setLogWithJekaDecorator(boolean logOutputWithLogDecorator)
Set if the output streams of the process must be logged keeping the JkLog decorator.
|
T |
setParamAt(int index,
java.lang.String parameter)
Specify the command to execute
|
T |
setWorkingDir(java.nio.file.Path workingDir)
Sets the specified working directory to launch the process.
|
T |
setWorkingDir(java.lang.String workingDir)
Sets the specified working directory to launch the process.
|
java.lang.String |
toString() |
protected JkAbstractProcess()
protected JkAbstractProcess(JkAbstractProcess<?> other)
protected T copy()
public T setParamAt(int index, java.lang.String parameter)
public T setCollectStdout(boolean collectStdout)
This is mandatory to collect output if we want to get the get the JkProcResult.getStdout()
after process execution.
Initial value is false
.
public T setCollectStderr(boolean collectStderr)
This is mandatory to collect output if we want to get the get the JkProcResult.getStdout()
after process execution.
Initial value is false
.
public T setDestroyAtJvmShutdown(boolean destroy)
public T addParamsAsCmdLine(java.lang.String cmdLine, java.lang.Object... tokens)
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.public T addParams(java.lang.String... parameters)
public T removeParam(java.lang.String parameter)
public T addParamsIf(boolean condition, java.lang.String... parameters)
public T addParams(java.util.Collection<java.lang.String> parameters)
addParams(String...)
public T addParamsAt(int index, java.util.Collection<java.lang.String> parameters)
public T addParamsAt(int index, java.lang.String... parameters)
addParamsAt(int, Collection)
public T setWorkingDir(java.nio.file.Path workingDir)
public T setWorkingDir(java.lang.String workingDir)
.
public T setEnv(java.lang.String name, java.lang.String value)
name
- the name of the environment variablevalue
- the value to be assigned to the environment variablepublic T setFailOnError(boolean fail)
Initial value is 'true'.
public T setLogCommand(boolean logCommand)
public T setLogWithJekaDecorator(boolean logOutputWithLogDecorator)
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
..
public T setInheritIO(boolean value)
This can not be used in conjunction of logWithJekaDecorator
, so this one is disabled forcibly when invoking this method.
Initial value is false
ProcessBuilder.inheritIO()
public T inheritJkLogOptions()
public T redirectErrorStream(boolean value)
ProcessBuilder.redirectErrorStream(boolean)
.
Initial value is false.
public java.nio.file.Path getWorkingDir()
public java.lang.String getParamAt(int index)
public java.util.List<java.lang.String> getParams()
public boolean isFailOnError()
true
if this process must throw an execption if the underlying process returns
code different from 0.public java.lang.String toString()
toString
in class java.lang.Object
protected void customizeCommand()
This method provides a way for subclasses to adjust the process before it is run.
public void run()
run
in interface java.lang.Runnable
public JkProcResult exec()
public T execCmd(java.lang.String... commandParams)
commandParams
- The parameters to be added to the command being executed.public T execCmdLine(java.lang.String format, java.lang.String... tokens)
execCmd(String...)
public JkProcHandler execAsync()
JkProcHandler
object
which can be used to interact with the running process.