Top Description Fields Constructors Methods
one.jpro.platform.utils

public Class CommandRunner

extends Object
Class Inheritance
Imports
org.jetbrains.annotations.NotNull, .Nullable, org.slf4j.Logger, .LoggerFactory, java.io.*, java.util.*, java.util.concurrent.TimeUnit, java.util.stream.Collectors

Run processes based on command line arguments.
Author
Besmir Beqiri

Field Summary

Modifier and TypeField and Description
private final List<String>
private final Map<String, String>
private boolean
private final Logger
private boolean
private final StringBuffer
private final List<String>

Constructor Summary

AccessConstructor and Description
public
CommandRunner(String...
command line arguments to be executed by the process.
args
)

Initializes CommandRunner with default logger and command line arguments.

public
CommandRunner(Logger
Logger instance for logging process activities.
logger
,
@NotNull String...
Command line arguments to be executed by the process.
args
)

Initializes CommandRunner with specified logger and command line arguments.

Method Summary

Modifier and TypeMethod and Description
public void
addArg(@NotNull String
a string passed to the command line arguments
arg
)

Adds a command line argument to the list of existing list of command line arguments

public void
addArgs(@NotNull String...
varargs list of arguments
args
)

Adds a varargs list of arguments to the existing list of command line of arguments

public void
addArgs(@NotNull Collection<String>
a collection of arguments
args
)

Adds a collection of arguments to the existing list of command line of arguments

public void
addSecretArg(@Nullable String
a string passed to the command line arguments
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

public void
addToEnv(@NotNull String
a string with the environmental variable name
key
,
@NotNull String
a string with the environmental variable value
value
)

Adds a pair (key, value) to the environment map of the process.

public String

Returns:

the command line of arguments as a string
getCmd
()

public List<String>

Returns:

the current list of command line arguments
getCmdList
()

public String

Returns:

a string with the last line of the output
getLastResponse
()

Gets the last line of the output process.

public @Nullable String

Returns:

a single string with the whole output of the process
getResponse
()

Gets the response of the process as single string.

public @NotNull List<String>

Returns:

a list with all the lines of the output
getResponses
()

Gets the response of the process as list of lines.

private Thread

Returns:

a thread that captures the process output
mergeProcessOutput
(final InputStream
the input stream of the process
inputStream
)

Captures the process output, logging it and appending to an internal buffer.

public int

Returns:

0 if the process ends successfully, non-zero values indicate a failure
run
(@Nullable String
the name of the process
processName
)

Runs a process with a given set of command line arguments.

public int

Returns:

0 if the process ends successfully, non-zero values indicate a failure
run
(@Nullable String
the name of the process
processName
,
@Nullable File
a file with the working directory of the process
workingDirectory
)

Runs a process with a given set of command line arguments, in a given working directory.

public Process

Returns:

the Process object
runAsync
(@Nullable String
the name of the process
processName
)

Runs a process asynchronously with a given set of command line arguments.

public Process

Returns:

the Process object
runAsync
(@Nullable String
the name of the process
processName
,
@Nullable File
a file with the working directory of the process
workingDirectory
)

Runs a process asynchronously with a given set of command line arguments, in a given working directory.

public Process

Returns:

the Process object
runAsync
(@Nullable String
the name of the process
processName
,
boolean
a boolean that sets the merge output mode
mergeOutput
)

Runs a process asynchronously with a given set of command line arguments.

public Process

Returns:

the Process object
runAsync
(@Nullable String
the name of the process
processName
,
@Nullable File
a file with the working directory of the process
workingDirectory
,
boolean
a boolean that sets the merge output mode
mergeOutput
)

Runs a process asynchronously with a given set of command line arguments, in a given working directory.

public boolean

Returns:

true if the process ended successfully, false otherwise
runTimed
(@Nullable String
the name of the process
processName
,
long
the maximum time allowed to run the process in seconds
timeout
)

Runs a process with a given set of command line arguments within a given time frame.

public boolean

Returns:

true if the process ended successfully, false otherwise
runTimed
(@Nullable String
the name of the process
processName
,
@Nullable File
a file with the working directory of the process
workingDirectory
,
long
the maximum time allowed to run the process
timeout
)

Runs a process with a given set of command line arguments, in a given working directory, within a given time frame

public void
setInteractive(boolean
a boolean that sets the interactive mode
interactive
)

When set to true, it will enable user interaction during the process.

public void
setPrintToConsole(boolean
a boolean that sets the print to console mode
printToConsole
)

When set to true, it will enable the process output to be printed to the console.

private Process

Returns:

the process object
setupProcess
(final String
the name of the process
processName
,
final File
the working directory of the process
directory
)

Prepares the process for execution.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

argsback to summary
private final List<String> args
envVarsback to summary
private final Map<String, String> envVars
interactiveback to summary
private boolean interactive
loggerback to summary
private final Logger logger
printToConsoleback to summary
private boolean printToConsole
processOutputback to summary
private final StringBuffer processOutput
secretArgsback to summary
private final List<String> secretArgs

Constructor Detail

CommandRunnerback to summary
public CommandRunner(String... args)

Initializes CommandRunner with default logger and command line arguments.

Parameters
args:String[]

command line arguments to be executed by the process.

CommandRunnerback to summary
public CommandRunner(Logger logger, @NotNull String... args)

Initializes CommandRunner with specified logger and command line arguments.

Parameters
logger:Logger

Logger instance for logging process activities.

args:@NotNull String[]

Command line arguments to be executed by the process.

Method Detail

addArgback to summary
public void addArg(@NotNull String arg)

Adds a command line argument to the list of existing list of command line arguments

Parameters
arg:@NotNull String

a string passed to the command line arguments

addArgsback to summary
public void addArgs(@NotNull String... args)

Adds a varargs list of arguments to the existing list of command line of arguments

Parameters
args:@NotNull String[]

varargs list of arguments

addArgsback to summary
public void addArgs(@NotNull Collection<String> args)

Adds a collection of arguments to the existing list of command line of arguments

Parameters
args:@NotNull Collection<String>

a collection of arguments

addSecretArgback to summary
public 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

Parameters
arg:@Nullable String

a string passed to the command line arguments

addToEnvback to summary
public void addToEnv(@NotNull String key, @NotNull String value)

Adds a pair (key, value) to the environment map of the process.

Parameters
key:@NotNull String

a string with the environmental variable name

value:@NotNull String

a string with the environmental variable value

getCmdback to summary
public String getCmd()
Returns:String

the command line of arguments as a string

getCmdListback to summary
public List<String> getCmdList()
Returns:List<String>

the current list of command line arguments

getLastResponseback to summary
public String getLastResponse()

Gets the last line of the output process.

Returns:String

a string with the last line of the output

getResponseback to summary
public @Nullable String getResponse()

Gets the response of the process as single string.

Returns:@Nullable String

a single string with the whole output of the process

Annotations
@Nullable
getResponsesback to summary
public @NotNull List<String> getResponses()

Gets the response of the process as list of lines.

Returns:@NotNull List<String>

a list with all the lines of the output

Annotations
@NotNull
mergeProcessOutputback to summary
private Thread mergeProcessOutput(final InputStream inputStream)

Captures the process output, logging it and appending to an internal buffer.

Parameters
inputStream:InputStream

the input stream of the process

Returns:Thread

a thread that captures the process output

runback to summary
public int run(@Nullable String processName) throws IOException, InterruptedException

Runs a process with a given set of command line arguments.

Parameters
processName:@Nullable String

the name of the process

Returns:int

0 if the process ends successfully, non-zero values indicate a failure

Exceptions
IOException:
if an I/O error occurs
InterruptedException:
if the current thread is interrupted by another thread while it is waiting
runback to summary
public int run(@Nullable String processName, @Nullable File workingDirectory) throws IOException, InterruptedException

Runs a process with a given set of command line arguments, in a given working directory.

Parameters
processName:@Nullable String

the name of the process

workingDirectory:@Nullable File

a file with the working directory of the process

Returns:int

0 if the process ends successfully, non-zero values indicate a failure

Exceptions
IOException:
if an I/O error occurs
InterruptedException:
if the current thread is interrupted by another thread while it is waiting
runAsyncback to summary
public Process runAsync(@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:@Nullable String

the name of the process

Returns:Process

the Process object

Exceptions
IOException:
if an I/O error occurs
runAsyncback to summary
public Process runAsync(@Nullable String processName, @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:@Nullable String

the name of the process

workingDirectory:@Nullable File

a file with the working directory of the process

Returns:Process

the Process object

Exceptions
IOException:
if an I/O error occurs
runAsyncback to summary
public Process runAsync(@Nullable String processName, boolean mergeOutput) throws IOException

Runs a process asynchronously with a given set of command line arguments.

Parameters
processName:@Nullable String

the name of the process

mergeOutput:boolean

a boolean that sets the merge output mode

Returns:Process

the Process object

Exceptions
IOException:
if an I/O error occurs
runAsyncback to summary
public Process runAsync(@Nullable String processName, @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:@Nullable String

the name of the process

workingDirectory:@Nullable File

a file with the working directory of the process

mergeOutput:boolean

a boolean that sets the merge output mode

Returns:Process

the Process object

Exceptions
IOException:
if an I/O error occurs
runTimedback to summary
public boolean runTimed(@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:@Nullable String

the name of the process

timeout:long

the maximum time allowed to run the process in seconds

Returns:boolean

true if the process ended successfully, false otherwise

Exceptions
IOException:
if an I/O error occurs
InterruptedException:
if the current thread is interrupted by another thread while it is waiting
runTimedback to summary
public boolean runTimed(@Nullable String processName, @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:@Nullable String

the name of the process

workingDirectory:@Nullable File

a file with the working directory of the process

timeout:long

the maximum time allowed to run the process

Returns:boolean

true if the process ended successfully, false otherwise

Exceptions
IOException:
if an I/O error occurs
InterruptedException:
if the current thread is interrupted by another thread while it is waiting
setInteractiveback to summary
public void setInteractive(boolean interactive)

When set to true, it will enable user interaction during the process. By default, is false

Parameters
interactive:boolean

a boolean that sets the interactive mode

setPrintToConsoleback to summary
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:boolean

a boolean that sets the print to console mode

setupProcessback to summary
private Process setupProcess(final String processName, final File directory) throws IOException

Prepares the process for execution.

Parameters
processName:String

the name of the process

directory:File

the working directory of the process

Returns:Process

the process object

Exceptions
IOException:
if an I/O error occurs