| 8 | import java.io.StringWriter; |
| 9 | |
| 10 | public class CommandLineExecution extends Execution { |
| 11 | private final Supplier<CommandLine> commandLineSupplier; |
| 12 | private ByteArrayOutputStream out; |
| 13 | private ByteArrayOutputStream err; |
| 14 | private StringWriter outWriter; |
| 15 | private StringWriter errWriter; |
| 16 | private boolean customizeOut; |
| 17 | private boolean customizeErr; |
| 18 | private boolean alive; |
| 19 | |
| 20 | CommandLineExecution(Supplier<CommandLine> commandLineSupplier, String[] args) { |
| 21 | if (commandLineSupplier == null) { throw new NullPointerException("commandLineSupplier is null"); } |
| 22 | if (args == null) { throw new NullPointerException("args array is null"); } |
| 23 | this.commandLineSupplier = commandLineSupplier; |
| 24 | this.args = args; |
| 25 | } |
| 26 | |
| 27 | CommandLineExecution setCustomizeOut(boolean customizeOut) { |
| 28 | this.customizeOut = customizeOut; |
| 29 | return this; |
| 30 | } |
| 31 | |
| 32 | CommandLineExecution setCustomizeErr(boolean customizeErr) { |
| 33 | this.customizeErr = customizeErr; |
| 34 | return this; |
| 35 | } |
| 36 | |
| 37 | protected void execute() { |
| 38 | out = new ByteArrayOutputStream(); |
| 39 | err = new ByteArrayOutputStream(); |
| 40 | PrintStream oldOut = System.out; |
| 41 | PrintStream oldErr = System.err; |
| 42 | try { |
| 43 | System.setOut(new PrintStream(out)); |
| 44 | System.setErr(new PrintStream(err)); |
| 45 | |
| 46 | CommandLine commandLine = commandLineSupplier.get(); |
| 47 | if (customizeOut) { |
| 48 | outWriter = new StringWriter(); |
| 49 | commandLine.setOut(new PrintWriter(outWriter)); |
| 50 | } |
| 51 | if (customizeErr) { |
| 52 | errWriter = new StringWriter(); |
| 53 | commandLine.setErr(new PrintWriter(errWriter)); |
| 54 | } |
| 55 | alive = true; |
| 56 | exitCode = commandLine.execute(args); |
| 57 | } finally { |
| 58 | alive = false; |
| 59 | System.setOut(oldOut); |
| 60 | System.setOut(oldErr); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | protected boolean isAlive() { |
| 65 | return alive; |
| 66 | } |
| 67 |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…