ShellProcessBuilder -> ShellExecutor. Static method to simplify shell

execution added. Added ability to pass in environment variables to new
process. Added better detection for current shell in linux/unix (could
be improved...). Added flag for executing process as a shell command.
Output now defaults to en-US.UTF_8
This commit is contained in:
nathan 2017-07-15 23:53:12 +02:00
parent cf0f04d6a7
commit 38a6899049

View File

@ -22,6 +22,7 @@ import java.io.PrintStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import dorkbox.util.OS; import dorkbox.util.OS;
@ -36,51 +37,85 @@ import dorkbox.util.OS;
* String output = ShellProcessBuilder.getOutput(byteArrayOutputStream); * String output = ShellProcessBuilder.getOutput(byteArrayOutputStream);
* }</pre> * }</pre>
*/ */
@SuppressWarnings({"UnusedReturnValue", "unused", "ManualArrayToCollectionCopy", "UseBulkOperation", "Convert2Diamond", "Convert2Lambda",
"Anonymous2MethodRef", "WeakerAccess"})
public public
class ShellProcessBuilder { class ShellExecutor {
// TODO: Add the ability to get the process PID via java for mac/windows/linux. Linux is avail from jvm, windows needs JNA // TODO: Add the ability to get the process PID via java for mac/windows/linux. Linux is avail from jvm, windows needs JNA
private static String defaultShell = null;
private final PrintStream outputStream; private final PrintStream outputStream;
private final PrintStream outputErrorStream; 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>();
private Map<String, String> environment = null;
private String workingDirectory = null; private String workingDirectory = null;
private String executableName = null; private String executableName = null;
private String executableDirectory = null; private String executableDirectory = null;
private Process process = null; private Process process = null;
private ProcessProxy writeToProcess_input = null; private ProcessProxy writeToProcess_input = null;
private ProcessProxy readFromProcess_output = null; private ProcessProxy readFromProcess_output = null;
private ProcessProxy readFromProcess_error = null; private ProcessProxy readFromProcess_error = null;
private boolean createReadWriterThreads = false; private boolean createReadWriterThreads = false;
private boolean isShell; private boolean executeAsShell;
private String pipeToNullString = ""; private String pipeToNullString = "";
private ByteArrayOutputStream byteArrayOutputStream;
private List<String> fullCommand; private List<String> fullCommand;
/** /**
* This will cause the spawned process to pipe it's output to null. * This is a convenience method to easily create a default ShellExecutor. Will block until the process is finished running
*
* @param executableName the name of the executable to run
* @param args the arguments for the executable
*
* @return true if the process ran successfully (exit value was 0), otherwise false
*/
public static boolean run(String executableName, String... args) {
ShellExecutor shellExecutor = new ShellExecutor();
shellExecutor.setExecutable(executableName);
shellExecutor.addArguments(args);
// blocks until finished
return shellExecutor.start() == 0;
}
/**
* This will cause the spawned process to pipe it's output to a String, so it can be retrieved.
*/ */
public public
ShellProcessBuilder() { ShellExecutor() {
this(null, null, null); byteArrayOutputStream = new ByteArrayOutputStream(8196);
PrintStream outputStream = new PrintStream(byteArrayOutputStream);
this.inputStream = null;
this.outputStream = outputStream;
this.outputErrorStream = outputStream;
} }
public public
ShellProcessBuilder(final PrintStream out) { ShellExecutor(final PrintStream out) {
this(null, out, out); this.inputStream = null;
this.outputStream = out;
this.outputErrorStream = out;
} }
public public
ShellProcessBuilder(final InputStream in, final PrintStream out) { ShellExecutor(final InputStream in, final PrintStream out) {
this(in, out, out); this.inputStream = in;
this.outputStream = out;
this.outputErrorStream = out;
} }
public public
ShellProcessBuilder(final InputStream in, final PrintStream out, final PrintStream err) { ShellExecutor(final InputStream in, final PrintStream out, final PrintStream err) {
this.inputStream = in; this.inputStream = in;
this.outputStream = out; this.outputStream = out;
this.outputErrorStream = err; this.outputErrorStream = err;
@ -97,7 +132,7 @@ class ShellProcessBuilder {
* *
*/ */
public final public final
ShellProcessBuilder createReadWriterThreads() { ShellExecutor createReadWriterThreads() {
createReadWriterThreads = true; createReadWriterThreads = true;
return this; return this;
} }
@ -106,20 +141,30 @@ class ShellProcessBuilder {
* When launched from eclipse, the working directory is USUALLY the root of the project folder * When launched from eclipse, the working directory is USUALLY the root of the project folder
*/ */
public final public final
ShellProcessBuilder setWorkingDirectory(final String workingDirectory) { ShellExecutor setWorkingDirectory(final String workingDirectory) {
// MUST be absolute path!! // MUST be absolute path!!
this.workingDirectory = new File(workingDirectory).getAbsolutePath(); this.workingDirectory = new File(workingDirectory).getAbsolutePath();
return this; return this;
} }
/**
* The Shell's execution environment variables. Set to `null` to only use the default environment variables (From what
* {@link System#getenv} returns)
*/
public final public final
ShellProcessBuilder addArgument(final String argument) { ShellExecutor setEnvironment(final Map<String,String> environment) {
this.environment = environment;
return this;
}
public final
ShellExecutor addArgument(final String argument) {
this.arguments.add(argument); this.arguments.add(argument);
return this; return this;
} }
public final public final
ShellProcessBuilder addArguments(final String... paths) { ShellExecutor addArguments(final String... paths) {
for (String path : paths) { for (String path : paths) {
this.arguments.add(path); this.arguments.add(path);
} }
@ -127,29 +172,41 @@ class ShellProcessBuilder {
} }
public final public final
ShellProcessBuilder addArguments(final List<String> paths) { ShellExecutor addArguments(final List<String> paths) {
this.arguments.addAll(paths); this.arguments.addAll(paths);
return this; return this;
} }
public final public final
ShellProcessBuilder setExecutable(final String executableName) { ShellExecutor setExecutable(final String executableName) {
this.executableName = executableName; this.executableName = executableName;
return this; return this;
} }
public public
ShellProcessBuilder setExecutableDirectory(final String executableDirectory) { ShellExecutor setExecutableDirectory(final String executableDirectory) {
// MUST be absolute path!! // MUST be absolute path!!
this.executableDirectory = new File(executableDirectory).getAbsolutePath(); this.executableDirectory = new File(executableDirectory).getAbsolutePath();
return this; return this;
} }
/**
* This will execute as a shell command (bash/cmd/etc) instead of as a forked process.
*/
public
ShellExecutor executeAsShellCommand() {
this.executeAsShell = true;
return this;
}
/** /**
* Sends all output data for this process to "null" in a cross platform method * Sends all output data for this process to "null" in a cross platform method
*/ */
public public
ShellProcessBuilder pipeOutputToNull() throws IllegalArgumentException { ShellExecutor pipeOutputToNull() throws IllegalArgumentException {
if (outputStream != null || outputErrorStream != null) { if (outputStream != null || outputErrorStream != null) {
throw new IllegalArgumentException("Cannot pipe shell command to 'null' if an output stream is specified"); throw new IllegalArgumentException("Cannot pipe shell command to 'null' if an output stream is specified");
} }
@ -190,34 +247,50 @@ class ShellProcessBuilder {
public public
int start() { int start() {
fullCommand = new ArrayList<String>(); fullCommand = new ArrayList<String>();
if (executeAsShell) {
// if no executable, then use the command shell
if (this.executableName == null) {
isShell = true;
if (OS.isWindows()) { if (OS.isWindows()) {
// windows fullCommand.add("cmd");
this.executableName = "cmd";
fullCommand.add(this.executableName);
fullCommand.add("/c"); fullCommand.add("/c");
} }
else { else {
// *nix if (defaultShell == null) {
this.executableName = "/bin/bash"; String[] shells = new String[] {"/bin/bash", "/usr/bin/bash",
"/bin/pfbash", "/usr/bin/pfbash",
"/bin/csh", "/usr/bin/csh",
"/bin/pfcsh", "/usr/bin/pfcsh",
"/bin/jsh", "/usr/bin/jsh",
"/bin/ksh", "/usr/bin/ksh",
"/bin/pfksh", "/usr/bin/pfksh",
"/bin/ksh93", "/usr/bin/ksh93",
"/bin/pfksh93", "/usr/bin/pfksh93",
"/bin/pfsh", "/usr/bin/pfsh",
"/bin/tcsh", "/usr/bin/tcsh",
"/bin/pftcsh", "/usr/bin/pftcsh",
"/usr/xpg4/bin/sh", "/usr/xp4/bin/pfsh",
"/bin/zsh", "/usr/bin/zsh",
"/bin/pfzsh", "/usr/bin/pfzsh",
"/bin/sh", "/usr/bin/sh",};
File file = new File(this.executableName); for (String shell : shells) {
if (!file.canExecute()) { if (new File(shell).canExecute()) {
this.executableName = "/bin/sh"; defaultShell = shell;
break;
}
}
}
if (defaultShell == null) {
throw new RuntimeException("Unable to determine the default shell for the linux/unix environment.");
}
// *nix
fullCommand.add(defaultShell);
fullCommand.add("-c");
} }
fullCommand.add(this.executableName); fullCommand.add(this.executableName);
fullCommand.add("-c"); } else {
}
}
else {
// shell and working/exe directory are mutually exclusive // shell and working/exe directory are mutually exclusive
if (this.workingDirectory != null) { if (this.workingDirectory != null) {
if (!this.workingDirectory.endsWith(File.separator)) { if (!this.workingDirectory.endsWith(File.separator)) {
this.workingDirectory += File.separator; this.workingDirectory += File.separator;
@ -239,7 +312,7 @@ class ShellProcessBuilder {
// if we don't want output... // if we don't want output...
boolean pipeToNull = !pipeToNullString.isEmpty(); boolean pipeToNull = !pipeToNullString.isEmpty();
if (isShell && !OS.isWindows()) { if (executeAsShell && !OS.isWindows()) {
// when a shell AND on *nix, we have to place ALL the args into a single "arg" that is passed in // 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); final StringBuilder stringBuilder = new StringBuilder(1024);
@ -266,8 +339,11 @@ class ShellProcessBuilder {
// (this is how it works on the command line!) // (this is how it works on the command line!)
String[] split = arg.split(" "); String[] split = arg.split(" ");
for (String s : split) { for (String s : split) {
s = s.trim();
if (!s.isEmpty()) {
fullCommand.add(s); fullCommand.add(s);
} }
}
} else { } else {
fullCommand.add(arg); fullCommand.add(arg);
} }
@ -285,6 +361,25 @@ class ShellProcessBuilder {
processBuilder.directory(new File(this.workingDirectory)); processBuilder.directory(new File(this.workingDirectory));
} }
// These env variables are a copy of System.getenv()
Map<String, String> environment = processBuilder.environment();
// Make sure all shell calls are LANG=en_US.UTF-8 THIS CAN BE OVERRIDDEN
if (OS.isMacOsX()) {
// Enable LANG overrides
environment.put("SOFTWARE", "");
}
// "export LANG=en_US.UTF-8"
environment.put("LANG", "C");
if (this.environment != null) {
for (Map.Entry<String, String> e : this.environment.entrySet()) {
environment.put(e.getKey(), e.getValue());
}
}
// combine these so output is properly piped to null. // combine these so output is properly piped to null.
if (pipeToNull || this.outputErrorStream == null) { if (pipeToNull || this.outputErrorStream == null) {
processBuilder.redirectErrorStream(true); processBuilder.redirectErrorStream(true);
@ -329,7 +424,7 @@ class ShellProcessBuilder {
} }
// we want to pipe our input/output from process to ourselves // we want to pipe our input/output from process to ourselves
else { else {
/** /*
* Proxy the System.out and System.err from the spawned process back * Proxy the System.out and System.err from the spawned process back
* to the user's window. This is important or the spawned process could block. * to the user's window. This is important or the spawned process could block.
*/ */
@ -346,7 +441,7 @@ class ShellProcessBuilder {
} }
if (this.inputStream != null) { if (this.inputStream != null) {
/** /*
* Proxy System.in from the user's window to the spawned process * Proxy System.in from the user's window to the spawned process
*/ */
// writer (read console -> write process) // writer (read console -> write process)
@ -363,7 +458,7 @@ class ShellProcessBuilder {
@Override @Override
public public
void run() { void run() {
ShellProcessBuilder.this.process.destroy(); ShellExecutor.this.process.destroy();
} }
}); });
hook.setName("ShellProcess Shutdown Hook"); hook.setName("ShellProcess Shutdown Hook");
@ -445,10 +540,39 @@ class ShellProcessBuilder {
return 1; return 1;
} }
/**
* There will never be a trailing newline character at the end of this output.
*
* @return A string representing the output of the process, null if the thread for this was interrupted or the output wasn't saved
*/
public
String getOutput() {
if (byteArrayOutputStream != null) {
return getOutput(byteArrayOutputStream);
}
return null;
}
/**
* Converts the baos to a string in a safe way. There will never be a trailing newline character at the end of this output. This will
* block until there is a line of input available.
*
* @return A string representing the output of the process, null if the thread for this was interrupted or the output wasn't saved
*/
public
String getOutputLineBuffered() {
if (byteArrayOutputStream != null) {
return getOutputLineBuffered(byteArrayOutputStream);
}
return null;
}
/** /**
* Converts the baos to a string in a safe way. There will never be a trailing newline character at the end of this output. * Converts the baos to a string in a safe way. There will never be a trailing newline character at the end of this output.
* *
* @param byteArrayOutputStream the baos that is used in the {@link ShellProcessBuilder#ShellProcessBuilder(PrintStream)} (or similar * @param byteArrayOutputStream the baos that is used in the {@link ShellExecutor#ShellExecutor(PrintStream)} (or similar
* calls) * calls)
* *
* @return A string representing the output of the process, null if the thread for this was interrupted * @return A string representing the output of the process, null if the thread for this was interrupted
@ -474,15 +598,14 @@ class ShellProcessBuilder {
* Converts the baos to a string in a safe way. There will never be a trailing newline character at the end of this output. This will * Converts the baos to a string in a safe way. There will never be a trailing newline character at the end of this output. This will
* block until there is a line of input available. * block until there is a line of input available.
* *
* @param byteArrayOutputStream the baos that is used in the {@link ShellProcessBuilder#ShellProcessBuilder(PrintStream)} (or similar * @param byteArrayOutputStream the baos that is used in the {@link ShellExecutor#ShellExecutor(PrintStream)} (or similar
* calls) * calls)
* *
* @return A string representing the output of the process, null if the thread for this was interrupted * @return A string representing the output of the process, null if the thread for this was interrupted
*/ */
public String public static
getOutputLineBuffered(final ByteArrayOutputStream byteArrayOutputStream) { String getOutputLineBuffered(final ByteArrayOutputStream byteArrayOutputStream) {
String s; String s;
synchronized (byteArrayOutputStream) { synchronized (byteArrayOutputStream) {
try { try {
byteArrayOutputStream.wait(); byteArrayOutputStream.wait();