public class Executable extends Object implements RunnableFuture<Executable>, Serializable
Builder for external processes with a fluent API. Not as lame as ProcessBuilder and works really
well with Groovy.
Queue or buffered in a StringBuffer.turned off to save memory or to prevent buggy programs from crashing your VM.blocking and non-blocking modes.ThreadFactory may be used.stopped if unexpected exit values are encountered.Executable has a logger for executed processes – media.processes.Executable – and
loggers for the output and error streams of each process – media.processes.ExecutableOutput and
media.processes.ExecutableError.
unexpected exit value will have the command line and the error message from
Commons Exec logged as warn.trace is not enabled, each
executed command line will be logged as debug, along with the time in milliseconds it took to execute it.trace is enabled, a message will be logged before and after a command is run.Output streams and error streams are logged as debug to the
media.processes.ExecutableOutput and media.processes.ExecutableError loggers.
Executable ex = new Executable([ 'cp foo bar', 'sha1sum bar' ]).ignoreStderr().lowPriority()
ex.run()
println ex.output.join('\n')
Executable ex = new Executable("cp foo bar").bufferStdout();
someExecutor.execute(ex)
while (ex.get(1, TimeUnit.SECONDS) == null) {
System.out.println("Still running");
}
System.out.println("foo has been copied to bar");
System.out.println(ex.output);
More help is available by turning on assertions (-ea).
heavy use at media.io,
Serialized Form| Modifier and Type | Class and Description |
|---|---|
static class |
Executable.StreamPumper<T>
Sort of mimics Commons Exec's StreamPumper.
|
| Constructor and Description |
|---|
Executable(Iterable<?> commands)
Prepares each command in the given
Iterable. |
Executable(Object... commands)
Prepares each command in the Object array.
|
| Modifier and Type | Method and Description |
|---|---|
Executable |
bufferStderr()
The standard error stream of all commands is returned in a
StringBuffer. |
Executable |
bufferStdout()
The standard output stream of all commands is returned in a
StringBuffer. |
boolean |
cancel(boolean mayInterruptIfRunning) |
protected long |
currentTimeMillis()
Hook if you use something special for System#currentTimeMillis()
|
Executable |
daemonThreads()
The threads reading from stdout and stderr are
daemon threads. |
Executable |
encoding(String encoding)
Sets the encoding.
|
Executable |
expect(int value)
Sets the expected exit value.
|
Executable |
expect(Object values)
Sets the expected exit values.
|
Executable |
get() |
Executable |
get(long timeout,
TimeUnit unit) |
Map<String,Object> |
getEnv() |
Object |
getError()
Returns the standard output stream object.
|
List<Object> |
getExitValues()
Returns the collected exit values.
|
Object |
getOutput()
Returns the standard output stream object.
|
Executable |
ignoreStderr()
The standard error stream of the command is ignored.
|
Executable |
ignoreStdout()
The standard output stream of the command is ignored.
|
Executable |
in(Object workingDirectory)
Sets the directory the commands are run in.
|
boolean |
isCancelled() |
boolean |
isDone() |
boolean |
isRunning()
Bonus method:
true if currently running. |
String |
lastCommand()
Returns the last command that was passed on to commons-exec.
|
Executable |
limit(int linesOrCharacters)
Limits the number of stored lines (if using Queues) or characters (if using
StringBuffers). |
Executable |
lowPriority()
Convenience method that sets the
thread priority to Thread.MIN_PRIORITY and configures
daemon threads. |
Executable |
priority(int priority)
Sets the thread priority of the two threads reading from stdout and stderr.
|
void |
run()
Runs all commands.
|
Executable |
stderr(Executable.StreamPumper pumper)
Sets the standard error
Executable.StreamPumper to use. |
Executable |
stdout(Executable.StreamPumper pumper)
Sets the standard output
Executable.StreamPumper to use. |
Executable |
stopOnFailure()
If a command returns an
unexpected exit value, no further commands will be run. |
Executable |
substitute(Map<String,Object> substitutions)
Substitute all
${key} sequences in any command with the corresponding value in
substitutions. |
Executable |
substitute(String key,
Object value)
Substitute a
${key} sequence in any command with value. |
Executable |
with(ThreadFactory factory)
Sets the thread factory to use.
|
public Executable(Object... commands)
commands - individual commandspublic Executable in(Object workingDirectory)
File or an object that
evaluates to a directory. If the directory does not exist, it will be created.File.mkdirs()public Executable priority(int priority)
Can only be used if no ThreadFactory has been set.
public Executable daemonThreads()
daemon threads.
Can only be used if no ThreadFactory has been set.
public Executable lowPriority()
thread priority to Thread.MIN_PRIORITY and configures
daemon threads.public Executable encoding(String encoding)
Charset.defaultCharset().encoding - the encoding, may not be nullpublic Executable stdout(Executable.StreamPumper pumper)
Executable.StreamPumper to use.public Executable stderr(Executable.StreamPumper pumper)
Executable.StreamPumper to use.public Executable ignoreStdout()
The return value of getOutput() will be a null after this call.
public Executable ignoreStderr()
The return value of getError() will be null after this call.
public Executable bufferStdout()
StringBuffer.public Executable bufferStderr()
StringBuffer.public Executable substitute(String key, Object value)
${key} sequence in any command with value. This method may be called
repeatedly.key - the key, may be nullvalue - the value, may be nullCommandLine.parse(String, Map)public Executable substitute(Map<String,Object> substitutions)
${key} sequences in any command with the corresponding value in
substitutions. Does not replace previous substitutions so substitute(String, Object) and
substitute(Map) may be called repeatedly.substitutions - a Map of substitutions, may be nullCommandLine.parse(String, Map)public Executable expect(Object values)
values - the exit valuesExecutor.setExitValues(int[])public Executable expect(int value)
value - the exit valueExecutor.setExitValues(int[])public Object getOutput()
Queue, null, a
StringBuffer or your own custom implementation.public Object getError()
Queue, null, a
StringBuffer or your own custom implementation.public Executable stopOnFailure()
unexpected exit value, no further commands will be run.public Executable limit(int linesOrCharacters)
StringBuffers).
Has no effect if ignoreStderr() or ignoreStdout() are used.
lines - the maximum number of lines or characters storedpublic Executable with(ThreadFactory factory)
public String lastCommand()
nullpublic boolean isRunning()
true if currently running.public void run()
run in interface Runnablerun in interface RunnableFuture<Executable>Runnable.run()public boolean cancel(boolean mayInterruptIfRunning)
cancel in interface Future<Executable>Future.cancel(boolean)public Executable get() throws InterruptedException, ExecutionException
get in interface Future<Executable>InterruptedExceptionExecutionExceptionFuture.get()public Executable get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
get in interface Future<Executable>InterruptedExceptionExecutionExceptionTimeoutExceptionFuture.get(long, java.util.concurrent.TimeUnit)public boolean isCancelled()
isCancelled in interface Future<Executable>Future.isCancelled()public boolean isDone()
isDone in interface Future<Executable>Future.isDone()protected long currentTimeMillis()
Copyright © 2011-2013 Johann Burkard. All Rights Reserved.