Fixed issues with race conditions and piping content to null. Also fixed linux shell execution.
This commit is contained in:
parent
7392f1ef71
commit
6ba920355e
|
@ -92,6 +92,7 @@ class LauncherProcessBuilder extends ShellProcessBuilder {
|
||||||
setExecutable("dorkbox");
|
setExecutable("dorkbox");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
createReadWriterThreads();
|
||||||
|
|
||||||
// save off the original arguments
|
// save off the original arguments
|
||||||
List<String> origArguments = new ArrayList<String>(this.arguments.size());
|
List<String> origArguments = new ArrayList<String>(this.arguments.size());
|
||||||
|
|
|
@ -18,12 +18,14 @@ package dorkbox.util.process;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
|
||||||
public
|
public
|
||||||
class ProcessProxy extends Thread {
|
class ProcessProxy extends Thread {
|
||||||
|
|
||||||
private final InputStream is;
|
private final InputStream is;
|
||||||
private final OutputStream os;
|
private final OutputStream os;
|
||||||
|
private final CountDownLatch countDownLatch = new CountDownLatch(1);
|
||||||
|
|
||||||
// when reading from the stdin and outputting to the process
|
// when reading from the stdin and outputting to the process
|
||||||
public
|
public
|
||||||
|
@ -37,6 +39,7 @@ class ProcessProxy extends Thread {
|
||||||
|
|
||||||
public
|
public
|
||||||
void close() {
|
void close() {
|
||||||
|
this.interrupt();
|
||||||
try {
|
try {
|
||||||
if (os != null) {
|
if (os != null) {
|
||||||
os.flush(); // this goes to the console, so we don't want to close it!
|
os.flush(); // this goes to the console, so we don't want to close it!
|
||||||
|
@ -46,10 +49,27 @@ class ProcessProxy extends Thread {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized
|
||||||
|
void start() {
|
||||||
|
super.start();
|
||||||
|
|
||||||
|
// now we have to for it to actually start up. The process can run & complete before this starts, resulting in no input/output
|
||||||
|
// captured
|
||||||
|
try {
|
||||||
|
countDownLatch.await();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public
|
public
|
||||||
void run() {
|
void run() {
|
||||||
final OutputStream os = this.os;
|
final OutputStream os = this.os;
|
||||||
|
final InputStream is = this.is;
|
||||||
|
|
||||||
|
countDownLatch.countDown();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// this thread will read until there is no more data to read. (this is generally what you want)
|
// this thread will read until there is no more data to read. (this is generally what you want)
|
||||||
|
@ -58,17 +78,15 @@ class ProcessProxy extends Thread {
|
||||||
|
|
||||||
if (os == null) {
|
if (os == null) {
|
||||||
// just read so it won't block.
|
// just read so it won't block.
|
||||||
while ((readInt = this.is.read()) != -1) {
|
while (is.read() != -1) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
while ((readInt = this.is.read()) != -1) {
|
while ((readInt = is.read()) != -1) {
|
||||||
os.write(readInt);
|
os.write(readInt);
|
||||||
|
|
||||||
// flush the output on new line.
|
// always flush after a write
|
||||||
if (readInt == '\n') {
|
os.flush();
|
||||||
os.flush();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception ignore) {
|
} catch (Exception ignore) {
|
||||||
|
|
|
@ -40,7 +40,7 @@ public
|
||||||
class ShellProcessBuilder {
|
class ShellProcessBuilder {
|
||||||
|
|
||||||
private final PrintStream outputStream;
|
private final PrintStream outputStream;
|
||||||
private final PrintStream errorStream;
|
private final PrintStream outputErrorStream;
|
||||||
private final InputStream inputStream;
|
private final InputStream inputStream;
|
||||||
|
|
||||||
protected List<String> arguments = new ArrayList<String>();
|
protected List<String> arguments = new ArrayList<String>();
|
||||||
|
@ -51,6 +51,10 @@ class ShellProcessBuilder {
|
||||||
|
|
||||||
// true if we want to save off (usually for debugging) the initial output from this
|
// true if we want to save off (usually for debugging) the initial output from this
|
||||||
private boolean debugInfo = false;
|
private boolean debugInfo = false;
|
||||||
|
private boolean createReadWriterThreads = false;
|
||||||
|
|
||||||
|
private boolean isShell;
|
||||||
|
private String pipeToNullString = "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This will cause the spawned process to pipe it's output to null.
|
* This will cause the spawned process to pipe it's output to null.
|
||||||
|
@ -74,7 +78,23 @@ class ShellProcessBuilder {
|
||||||
ShellProcessBuilder(InputStream in, PrintStream out, PrintStream err) {
|
ShellProcessBuilder(InputStream in, PrintStream out, PrintStream err) {
|
||||||
this.inputStream = in;
|
this.inputStream = in;
|
||||||
this.outputStream = out;
|
this.outputStream = out;
|
||||||
this.errorStream = err;
|
this.outputErrorStream = err;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates extra reader/writer threads for the sub-process. This is useful depending on how the sub-process is designed to run.
|
||||||
|
* </p>
|
||||||
|
* For a process you want interactive IO with, this is required.
|
||||||
|
* </p>
|
||||||
|
* For a long-running sub-process, with no interactive IO, this is what you'd want.
|
||||||
|
* </p>
|
||||||
|
* For a run-and-get-the-results process, this isn't recommended.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public final
|
||||||
|
ShellProcessBuilder createReadWriterThreads() {
|
||||||
|
createReadWriterThreads = true;
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -126,155 +146,189 @@ class ShellProcessBuilder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
ShellProcessBuilder pipeOutputToNull() {
|
||||||
|
if (OS.isWindows()) {
|
||||||
|
// >NUL on windows
|
||||||
|
pipeToNullString = ">NUL";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// we will "pipe" it to /dev/null on *nix
|
||||||
|
pipeToNullString = ">/dev/null 2>&1";
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public
|
public
|
||||||
void start() {
|
void start() {
|
||||||
|
List<String> argumentsList = new ArrayList<String>();
|
||||||
|
|
||||||
// if no executable, then use the command shell
|
// if no executable, then use the command shell
|
||||||
if (this.executableName == null) {
|
if (this.executableName == null) {
|
||||||
|
isShell = true;
|
||||||
|
|
||||||
if (OS.isWindows()) {
|
if (OS.isWindows()) {
|
||||||
// windows
|
// windows
|
||||||
this.executableName = "cmd";
|
this.executableName = "cmd";
|
||||||
this.arguments.add(0, "/c");
|
|
||||||
|
|
||||||
|
argumentsList.add(this.executableName);
|
||||||
|
argumentsList.add("/c");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// *nix
|
// *nix
|
||||||
this.executableName = "/bin/bash";
|
this.executableName = "/bin/bash";
|
||||||
|
|
||||||
File file = new File(this.executableName);
|
File file = new File(this.executableName);
|
||||||
if (!file.canExecute()) {
|
if (!file.canExecute()) {
|
||||||
this.executableName = "/bin/sh";
|
this.executableName = "/bin/sh";
|
||||||
}
|
}
|
||||||
this.arguments.add(0, "-c");
|
|
||||||
}
|
argumentsList.add(this.executableName);
|
||||||
}
|
argumentsList.add("-c");
|
||||||
else if (this.workingDirectory != null) {
|
|
||||||
if (!this.workingDirectory.endsWith("/") && !this.workingDirectory.endsWith("\\")) {
|
|
||||||
this.workingDirectory += File.separator;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
// shell and working/exe directory are mutually exclusive
|
||||||
|
|
||||||
if (this.executableDirectory != null) {
|
if (this.workingDirectory != null) {
|
||||||
if (!this.executableDirectory.endsWith("/") && !this.executableDirectory.endsWith("\\")) {
|
if (!this.workingDirectory.endsWith(File.separator)) {
|
||||||
this.executableDirectory += File.separator;
|
this.workingDirectory += File.separator;
|
||||||
}
|
|
||||||
|
|
||||||
this.executableName = this.executableDirectory + this.executableName;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<String> argumentsList = new ArrayList<String>();
|
|
||||||
argumentsList.add(this.executableName);
|
|
||||||
|
|
||||||
for (String arg : this.arguments) {
|
|
||||||
if (arg.contains(" ")) {
|
|
||||||
// individual arguments MUST be in their own element in order to
|
|
||||||
// be processed properly (this is how it works on the command line!)
|
|
||||||
String[] split = arg.split(" ");
|
|
||||||
for (String s : split) {
|
|
||||||
argumentsList.add(s);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
|
if (this.executableDirectory != null) {
|
||||||
|
if (!this.executableDirectory.endsWith(File.separator)) {
|
||||||
|
this.executableDirectory += File.separator;
|
||||||
|
}
|
||||||
|
|
||||||
|
argumentsList.add(0, this.executableDirectory + this.executableName);
|
||||||
|
} else {
|
||||||
|
argumentsList.add(this.executableName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// if we don't want output...
|
||||||
|
boolean pipeToNull = !pipeToNullString.isEmpty();
|
||||||
|
|
||||||
|
if (isShell && !OS.isWindows()) {
|
||||||
|
// when a shell AND on *nix, we have to place ALL the args into a single "arg" that is passed in
|
||||||
|
final StringBuilder stringBuilder = new StringBuilder(1024);
|
||||||
|
|
||||||
|
for (String arg : this.arguments) {
|
||||||
|
stringBuilder.append(arg).append(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!arguments.isEmpty()) {
|
||||||
|
if (pipeToNull) {
|
||||||
|
stringBuilder.append(pipeToNullString);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// delete last " "
|
||||||
|
stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
argumentsList.add(stringBuilder.toString());
|
||||||
|
|
||||||
|
} else {
|
||||||
|
for (String arg : this.arguments) {
|
||||||
argumentsList.add(arg);
|
argumentsList.add(arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (pipeToNull) {
|
||||||
|
argumentsList.add(pipeToNullString);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// if we don't want output... TODO: i think we want to "exec" (this calls exec -c, which calls our program)
|
|
||||||
// this code as well, since calling it directly won't work
|
|
||||||
boolean pipeToNull = this.errorStream == null || this.outputStream == null;
|
|
||||||
if (pipeToNull) {
|
|
||||||
if (OS.isWindows()) {
|
|
||||||
// >NUL on windows
|
|
||||||
argumentsList.add(">NUL");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// we will "pipe" it to /dev/null on *nix
|
|
||||||
argumentsList.add(">/dev/null 2>&1");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.debugInfo) {
|
if (this.debugInfo) {
|
||||||
if (errorStream != null) {
|
if (outputErrorStream != null) {
|
||||||
this.errorStream.print("Executing: ");
|
this.outputErrorStream.print("Executing: ");
|
||||||
} else {
|
} else {
|
||||||
System.err.println("Executing: ");
|
System.err.print("Executing: ");
|
||||||
}
|
}
|
||||||
|
|
||||||
Iterator<String> iterator = argumentsList.iterator();
|
Iterator<String> iterator = argumentsList.iterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
String s = iterator.next();
|
String s = iterator.next();
|
||||||
if (errorStream != null) {
|
if (outputErrorStream != null) {
|
||||||
this.errorStream.print(s);
|
this.outputErrorStream.print(s);
|
||||||
} else {
|
} else {
|
||||||
System.err.print(s);
|
System.err.print(s);
|
||||||
}
|
}
|
||||||
if (iterator.hasNext()) {
|
if (iterator.hasNext()) {
|
||||||
if (errorStream != null) {
|
if (outputErrorStream != null) {
|
||||||
this.errorStream.print(" ");
|
this.outputErrorStream.print(" ");
|
||||||
} else {
|
} else {
|
||||||
System.err.print(" ");
|
System.err.print(" ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (errorStream != null) {
|
if (outputErrorStream != null) {
|
||||||
this.errorStream.print(OS.LINE_SEPARATOR);
|
this.outputErrorStream.print(OS.LINE_SEPARATOR);
|
||||||
} else {
|
} else {
|
||||||
System.err.print(OS.LINE_SEPARATOR);
|
System.err.print(OS.LINE_SEPARATOR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
ProcessBuilder processBuilder = new ProcessBuilder(argumentsList);
|
ProcessBuilder processBuilder = new ProcessBuilder(argumentsList);
|
||||||
if (this.workingDirectory != null) {
|
if (this.workingDirectory != null) {
|
||||||
processBuilder.directory(new File(this.workingDirectory));
|
processBuilder.directory(new File(this.workingDirectory));
|
||||||
}
|
}
|
||||||
|
|
||||||
// combine these so output is properly piped to null.
|
// combine these so output is properly piped to null.
|
||||||
if (pipeToNull) {
|
if (pipeToNull || this.outputErrorStream == null) {
|
||||||
processBuilder.redirectErrorStream(true);
|
processBuilder.redirectErrorStream(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.process = processBuilder.start();
|
this.process = processBuilder.start();
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
if (errorStream != null) {
|
if (outputErrorStream != null) {
|
||||||
this.errorStream.println("There was a problem executing the program. Details:");
|
this.outputErrorStream.println("There was a problem executing the program. Details:");
|
||||||
} else {
|
} else {
|
||||||
System.err.println("There was a problem executing the program. Details:");
|
System.err.println("There was a problem executing the program. Details:");
|
||||||
}
|
}
|
||||||
ex.printStackTrace(this.errorStream);
|
ex.printStackTrace(this.outputErrorStream);
|
||||||
|
|
||||||
if (this.process != null) {
|
if (this.process != null) {
|
||||||
try {
|
try {
|
||||||
this.process.destroy();
|
this.process.destroy();
|
||||||
this.process = null;
|
this.process = null;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (errorStream != null) {
|
if (outputErrorStream != null) {
|
||||||
this.errorStream.println("Error destroying process:");
|
this.outputErrorStream.println("Error destroying process:");
|
||||||
} else {
|
} else {
|
||||||
System.err.println("Error destroying process:");
|
System.err.println("Error destroying process:");
|
||||||
}
|
}
|
||||||
e.printStackTrace(this.errorStream);
|
e.printStackTrace(this.outputErrorStream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.process != null) {
|
if (this.process != null) {
|
||||||
ProcessProxy writeToProcess_input;
|
ProcessProxy writeToProcess_input = null;
|
||||||
ProcessProxy readFromProcess_output;
|
ProcessProxy readFromProcess_output = null;
|
||||||
ProcessProxy readFromProcess_error;
|
ProcessProxy readFromProcess_error = null;
|
||||||
|
|
||||||
|
if (this.outputErrorStream == null && this.outputStream == null) {
|
||||||
|
if (!pipeToNull) {
|
||||||
|
NullOutputStream nullOutputStream = new NullOutputStream();
|
||||||
|
|
||||||
if (pipeToNull) {
|
// readers (read process -> write console)
|
||||||
NullOutputStream nullOutputStream = new NullOutputStream();
|
// have to keep the output buffers from filling in the target process.
|
||||||
|
readFromProcess_output = new ProcessProxy("Process Reader: " + this.executableName,
|
||||||
processBuilder.redirectErrorStream(true);
|
this.process.getInputStream(),
|
||||||
|
nullOutputStream);
|
||||||
// readers (read process -> write console)
|
}
|
||||||
// have to keep the output buffers from filling in the target process.
|
|
||||||
readFromProcess_output = new ProcessProxy("Process Reader: " + this.executableName,
|
|
||||||
this.process.getInputStream(),
|
|
||||||
nullOutputStream);
|
|
||||||
readFromProcess_error = null;
|
|
||||||
}
|
}
|
||||||
// we want to pipe our input/output from process to ourselves
|
// we want to pipe our input/output from process to ourselves
|
||||||
else {
|
else {
|
||||||
|
@ -287,14 +341,10 @@ class ShellProcessBuilder {
|
||||||
this.process.getInputStream(),
|
this.process.getInputStream(),
|
||||||
this.outputStream);
|
this.outputStream);
|
||||||
|
|
||||||
if (this.errorStream != this.outputStream) {
|
if (this.outputErrorStream != this.outputStream) {
|
||||||
readFromProcess_error = new ProcessProxy("Process Reader: " + this.executableName,
|
readFromProcess_error = new ProcessProxy("Process Reader: " + this.executableName,
|
||||||
this.process.getErrorStream(),
|
this.process.getErrorStream(),
|
||||||
this.errorStream);
|
this.outputErrorStream);
|
||||||
}
|
|
||||||
else {
|
|
||||||
processBuilder.redirectErrorStream(true);
|
|
||||||
readFromProcess_error = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -307,9 +357,6 @@ class ShellProcessBuilder {
|
||||||
this.inputStream,
|
this.inputStream,
|
||||||
this.process.getOutputStream());
|
this.process.getOutputStream());
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
writeToProcess_input = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// the process can be killed in two ways
|
// the process can be killed in two ways
|
||||||
|
@ -320,7 +367,10 @@ class ShellProcessBuilder {
|
||||||
public
|
public
|
||||||
void run() {
|
void run() {
|
||||||
if (ShellProcessBuilder.this.debugInfo) {
|
if (ShellProcessBuilder.this.debugInfo) {
|
||||||
ShellProcessBuilder.this.errorStream.println("Terminating process: " + ShellProcessBuilder.this.executableName);
|
final PrintStream errorStream = ShellProcessBuilder.this.outputErrorStream;
|
||||||
|
if (errorStream != null) {
|
||||||
|
errorStream.println("Terminating process: " + ShellProcessBuilder.this.executableName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ShellProcessBuilder.this.process.destroy();
|
ShellProcessBuilder.this.process.destroy();
|
||||||
}
|
}
|
||||||
|
@ -330,11 +380,27 @@ class ShellProcessBuilder {
|
||||||
.addShutdownHook(hook);
|
.addShutdownHook(hook);
|
||||||
|
|
||||||
if (writeToProcess_input != null) {
|
if (writeToProcess_input != null) {
|
||||||
writeToProcess_input.start();
|
if (createReadWriterThreads) {
|
||||||
|
writeToProcess_input.start();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
writeToProcess_input.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (createReadWriterThreads) {
|
||||||
|
readFromProcess_output.start();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
readFromProcess_output.run();
|
||||||
}
|
}
|
||||||
readFromProcess_output.start();
|
|
||||||
if (readFromProcess_error != null) {
|
if (readFromProcess_error != null) {
|
||||||
readFromProcess_error.start();
|
if (createReadWriterThreads) {
|
||||||
|
readFromProcess_error.start();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
readFromProcess_error.run();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user