Package com.bioanalyticalcomputing.util
Class ProcessWrapper
- java.lang.Object
-
- com.bioanalyticalcomputing.util.ProcessWrapper
-
public final class ProcessWrapper extends java.lang.Object
ProcessWrapper
encapsulates the steps needed to run a program using theProcessBuilder
class. To prevent side effects,ProcessWrapper
decouples input commands from its internalProcessBuilder
and uses wrapper methods to isolate the state of theProcessBuilder
.ProcessWrapper
removes direct access to allProcessBuilder
methods and allows only indirect access toProcessBuilder.command()
andProcessBuilder.inheritIO()
.
returns a copy of the internalProcessWrapper.getCommand()
ProcessBuilder
command, and
creates a copy of a new command for the internalProcessWrapper.setCommand()
ProcessBuilder
.ProcessWrapper
sets theredirectErrorStream
property for the internalProcessBuilder
totrue
.
Note thatProcessWrapper
methods are not synchronized, andProcessWrapper
has not been tested with external synchronization.
Following are some simpleProcessWrapper
examples (using Python 2.7.16 on macOS Catalina):
// Run a program and print the output: ProcessWrapper processWrapper = new ProcessWrapper("python", "-c", "print('Hello, world!')"); processWrapper.run(); processWrapper.printOutput(); // Expected output: // Hello, world! // Modify the input: List<String> exampleCommand = processWrapper.getCommand(); exampleCommand.remove(exampleCommand.size() - 1); exampleCommand.add("print('Hello')"); processWrapper.setCommand(exampleCommand); processWrapper.run(); // Work with the output: List<String> exampleOutput = processWrapper.getOutput(); if (exampleOutput.size() > 0) System.out.println(exampleOutput.get(0) + " again!"); // Expected output: // Hello again!
-
-
Constructor Summary
Constructors Constructor Description ProcessWrapper(String... command)
Passes the command to the second constructor.ProcessWrapper(List<String> command)
Creates aProcessWrapper
and an internalProcessBuilder
using a copy of the command.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description List<String>
getCommand()
Returns a copy of the currentProcessBuilder
command.List<String>
getOutput()
Returns a copy of the output from the command that theProcessBuilder
has executed.void
inheritIO()
Calls theinheritIO()
method of the internalProcessBuilder
.void
printOutput()
Prints the output from the command that theProcessBuilder
has executed.void
run()
Executes the command specified by either aProcessWrapper
constructor
or
and collects the output from the internalProcessWrapper.setCommand()
ProcessBuilder
.void
setCommand(List<String> command)
Replaces the current command for the internalProcessBuilder
with a copy of the passed command.
-
-
-
Constructor Detail
-
ProcessWrapper
public ProcessWrapper(String... command)
Passes the command to the second constructor.- Parameters:
command
- the command to be passed to the internalProcessBuilder
for execution
-
ProcessWrapper
public ProcessWrapper(List<String> command)
Creates aProcessWrapper
and an internalProcessBuilder
using a copy of the command. The internal command cannot be modified outside of the constructed task builder (since the internal commandList
is a copy andStrings
are immutable).- Parameters:
command
- the command to be passed to the internalProcessBuilder
for execution
-
-
Method Detail
-
inheritIO
public void inheritIO()
Calls theinheritIO()
method of the internalProcessBuilder
.
-
getCommand
public List<String> getCommand()
Returns a copy of the currentProcessBuilder
command. Changes made to the returned command will not be reflected in the state of the internalProcessBuilder
(since the returned commandList
is a copy andStrings
are immutable).- Returns:
- a copy of the current
ProcessBuilder
command
-
setCommand
public void setCommand(List<String> command)
Replaces the current command for the internalProcessBuilder
with a copy of the passed command. Changes made to the original command will not be reflected in the state of the internalProcessBuilder
(since the internal commandList
is a copy andStrings
are immutable).- Parameters:
command
- the new command to be copied into the internalProcessBuilder
-
run
public void run()
Executes the command specified by either aProcessWrapper
constructor
or
and collects the output from the internalProcessWrapper.setCommand()
ProcessBuilder
.
-
getOutput
public List<String> getOutput()
Returns a copy of the output from the command that theProcessBuilder
has executed.- Returns:
- the output from the executed command
-
printOutput
public void printOutput()
Prints the output from the command that theProcessBuilder
has executed.
-
-