Package nl.vpro.util
Interface CommandExecutor
- All Known Implementing Classes:
CommandExecutorImpl
public interface CommandExecutor
Executor for external commands.
Three types of methods:
execute(InputStream, OutputStream, OutputStream, String...)
To synchronously execute and return exit code.submit(java.io.InputStream, java.io.OutputStream, java.io.OutputStream, java.util.function.IntConsumer, java.lang.String...)
For asynchronous execution
CommandExecutor.Parameters
(or CommandExecutor.Parameters.Builder
or Consumer<Parameters.Builder>
) argument, so you can use the builder pattern to fill in parameters.
lines(InputStream, OutputStream, String...)
For synchronous execution and returing the output as a stream of strings.
- Since:
- 1.6
- Author:
- Michiel Meeuwissen
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic class
static class
static class
The parameters ofsubmit(IntConsumer, Parameters)
, in other words, an object representing the one time parameters of a call to aCommandExecutor
. -
Method Summary
Modifier and TypeMethodDescriptiondefault int
execute
(@Nullable InputStream in, OutputStream out, OutputStream error, String... args) Executes the commanddefault int
execute
(OutputStream out, OutputStream errors, String... args) Executes the command, defaulting version ofexecute(java.io.InputStream, java.io.OutputStream, java.io.OutputStream, java.lang.String...)
, where the first argument isnull
default int
execute
(OutputStream out, String... args) Executes the command with given arguments.default int
Defaulting version ofexecute(OutputStream, String...)
, where the output of the command will be interpreted as a UTF-stream, and be written to the suppliedWriter
.int
Executes with the given arguments.int
execute
(CommandExecutor.Parameters parameters) Executes the command .default int
execute
(CommandExecutor.Parameters.Builder parameters) This defaulting version ofexecute(Parameters)
eliminates the need to callCommandExecutor.Parameters.Builder.build()
.The binary associated with the CommandExecutor.nl.vpro.logging.simple.SimpleLogger
static boolean
isBrokenPipe
(Throwable ioe) lines
(InputStream in, OutputStream errors, String... args) Executes the command streamingly.default CompletableFuture<Integer>
submit
(InputStream in, OutputStream out, OutputStream error, String... args) default CompletableFuture<Integer>
submit
(InputStream in, OutputStream out, OutputStream error, IntConsumer callback, String... args) Executes the command in the background.default CompletableFuture<Integer>
submit
(OutputStream out, OutputStream error, String... args) default CompletableFuture<Integer>
submit
(OutputStream out, String... args) default CompletableFuture<Integer>
default CompletableFuture<Integer>
default CompletableFuture<Integer>
submit
(Consumer<CommandExecutor.Parameters.Builder> parameters) default CompletableFuture<Integer>
submit
(IntConsumer callback, CommandExecutor.Parameters parameters) default CompletableFuture<Integer>
submit
(IntConsumer callback, CommandExecutor.Parameters.Builder parameters) default CompletableFuture<Integer>
submit
(CommandExecutor.Parameters parameters) default CompletableFuture<Integer>
submit
(CommandExecutor.Parameters.Builder parameters)
-
Method Details
-
execute
Executes with the given arguments. The command itself is supposed to be a member of the implementation, so you would have a CommandExecutor instance for every external program you'd like to wrap. The version with no outputstream argument logs the output.- Returns:
- the exit code
-
execute
Executes the command, defaulting version ofexecute(java.io.InputStream, java.io.OutputStream, java.io.OutputStream, java.lang.String...)
, where the first argument isnull
- Parameters:
out
- Stdout of the command will be written to this.errors
- Stderr of the command will be written to this. To log errors useLoggerOutputStream.error(org.slf4j.Logger)
args
- The command and its arguments to be executed on the remote server- Returns:
- The exit code
-
execute
Executes the command with given arguments. Stderr will be logged via slf4j. There will be no stdin supplied.- Parameters:
out
- Stdout will be written to this.- Returns:
- the exit code
-
execute
Defaulting version ofexecute(OutputStream, String...)
, where the output of the command will be interpreted as a UTF-stream, and be written to the suppliedWriter
.- Parameters:
out
- Stdout will be written to this.- Returns:
- the exit code
-
execute
Executes the command- Parameters:
in
- Stdin of the command will be taken from this. This may benull
for no stdin at all.out
- Stdout of the command will be written to this.error
- Stder of the command will be written to this.- Returns:
- The exit code
-
execute
Executes the command .- Parameters:
parameters
- The parameters for doing this wrapped in aCommandExecutor.Parameters
object.
-
execute
This defaulting version ofexecute(Parameters)
eliminates the need to callCommandExecutor.Parameters.Builder.build()
. -
submit
default CompletableFuture<Integer> submit(InputStream in, OutputStream out, OutputStream error, IntConsumer callback, String... args) Executes the command in the background.- Parameters:
callback
- will be called when ready.- Returns:
- A future producing the result code.
-
submit
default CompletableFuture<Integer> submit(IntConsumer callback, CommandExecutor.Parameters.Builder parameters) -
submit
default CompletableFuture<Integer> submit(IntConsumer callback, CommandExecutor.Parameters parameters) -
submit
-
submit
-
submit
-
submit
default CompletableFuture<Integer> submit(InputStream in, OutputStream out, OutputStream error, String... args) -
submit
-
submit
-
submit
-
submit
-
lines
Executes the command streamingly. Stdout is converted to a stream of string (one for each line). E.g.* CommandExecutorImpl env = new CommandExecutorImpl("/usr/bin/env"); long running = env.lines("ps", "u").filter(s -> s.contains("amara_poms_publisher")).count();
- Returns:
- The exit code
-
lines
-
isBrokenPipe
-
getLogger
nl.vpro.logging.simple.SimpleLogger getLogger() -
parameters
-
getBinary
The binary associated with the CommandExecutor.
-