|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.continuent.tungsten.common.exec.ProcessExecutor
public class ProcessExecutor
Wrapper class to encapsulate all aspects of executing native operating system commands which are typically characterized . The wrapper handles up-front collection of inputs such as setting the working directory, environment, and stdin. It also manages the collection output from stdout and stderr.
Here is an example of typical usage:
After the run() method completes it is safe for clients to
examine output values. The class is implemented as runnable to permit callers
to run it easily in another thread.
ProcessExecutor pe = new ProcessExecutor();
pe.setCommands(new String[] {"echo", "hi!"});
pe.setTimeout(1000);
pe.setEnv("myenvvar", "myvalue");
pe.run();
| Field Summary | |
|---|---|
protected java.lang.String[] |
commands
|
protected java.util.HashMap<java.lang.String,java.lang.String> |
env
|
protected java.lang.Throwable |
error
|
protected int |
exitValue
|
protected java.lang.Process |
process
|
protected boolean |
redirectStdErr
|
protected java.lang.String |
stderr
|
protected boolean |
stdErrAppend
|
protected java.io.File |
stdErrFile
|
protected org.apache.log4j.Logger |
stdErrLogger
|
protected java.io.InputStream |
stdin
|
protected java.lang.String |
stdout
|
protected boolean |
stdOutAppend
|
protected java.io.File |
stdOutFile
|
protected org.apache.log4j.Logger |
stdOutLogger
|
protected boolean |
succeeded
|
protected static int |
THREAD_WAIT_MILLIS
Maximum milliseconds to wait for output threads to complete after process completion. |
protected boolean |
timedout
|
protected int |
timeout
|
protected java.io.File |
workDirectory
|
| Constructor Summary | |
|---|---|
ProcessExecutor()
Creates a new instance. |
|
ProcessExecutor(boolean redirectStdErr)
Creates a new instance. |
|
| Method Summary | |
|---|---|
java.lang.String[] |
getCommands()
Returns the program and command line argumetns. |
java.util.HashMap<java.lang.String,java.lang.String> |
getEnv()
Returns the table of environmental variables. |
java.lang.Throwable |
getError()
Returns the exception, if any, generated while executing the command. |
int |
getExitValue()
Returns the exit value of the command or -1 if the command failed to execute. |
java.lang.Process |
getProcess()
Returns the process value. |
java.lang.String |
getStderr()
Returns a String containing stderr output. |
java.util.List<java.lang.String> |
getStderrByLine()
Returns stderr in the form of a String array where each string contains one line of output. |
java.io.InputStream |
getStdin()
Returns the inputstream fed to the process. |
java.lang.String |
getStdout()
Returns a String containing stdout. |
java.util.List<java.lang.String> |
getStdoutByLine()
Returns the stdout in the form of a String array where each string contains one line of output. |
int |
getTimeout()
Returns the process timeout in milliseconds. |
java.io.File |
getWorkDirectory()
Returns the process working directory. |
protected int |
handleProcessIO(java.lang.Process process)
Manage process input and output. |
boolean |
isStdErrAppend()
Returns true if stderr should append to existing file. |
boolean |
isStdOutAppend()
Returns true if stdout should append to existing file. |
boolean |
isSuccessful()
Returns true if we think the process succeeded based on the return code and lack of exceptions or timeout during execution. |
boolean |
isTimedout()
Returns true if this processed exceeded its timeout and was killed. |
void |
run()
Execute the command. |
void |
setCommands(java.lang.String[] commands)
Sets the program and command line arguments. |
void |
setEnv(java.util.HashMap<java.lang.String,java.lang.String> env)
Sets environmental variables to be used by this command. |
void |
setEnv(java.lang.String name,
java.lang.String value)
Sets an environmental variable. |
void |
setStdErr(java.io.File stdErrFile)
Send stderr to a file |
void |
setStdErr(org.apache.log4j.Logger stdErrLogger)
Send stderr to a logger. |
void |
setStdErrAppend(boolean appendStderr)
If true, append stderr to existing file. |
void |
setStdin(java.io.InputStream stdin)
Sets the input stream fed to the process. |
void |
setStdOut(java.io.File stdOutFile)
Send stdout to a file |
void |
setStdOut(org.apache.log4j.Logger stdOutLogger)
Send stdout to a logger. |
void |
setStdOutAppend(boolean appendStdout)
If true, append stdout to existing file. |
void |
setTimeout(int timeout)
Sets the process timeout in milliseconds. |
void |
setWorkDirectory(java.io.File workDirectory)
Sets the process working direcgtory. |
static java.util.List<java.lang.String> |
toStringList(java.lang.String output)
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected static final int THREAD_WAIT_MILLIS
protected java.util.HashMap<java.lang.String,java.lang.String> env
protected java.io.InputStream stdin
protected java.io.File workDirectory
protected java.lang.String[] commands
protected int timeout
protected boolean stdOutAppend
protected boolean stdErrAppend
protected java.io.File stdOutFile
protected java.io.File stdErrFile
protected java.lang.String stdout
protected java.lang.String stderr
protected org.apache.log4j.Logger stdOutLogger
protected org.apache.log4j.Logger stdErrLogger
protected java.lang.Throwable error
protected int exitValue
protected boolean timedout
protected boolean succeeded
protected java.lang.Process process
protected boolean redirectStdErr
| Constructor Detail |
|---|
public ProcessExecutor()
public ProcessExecutor(boolean redirectStdErr)
| Method Detail |
|---|
public java.lang.String[] getCommands()
public void setCommands(java.lang.String[] commands)
public void setEnv(java.lang.String name,
java.lang.String value)
public java.util.HashMap<java.lang.String,java.lang.String> getEnv()
public void setEnv(java.util.HashMap<java.lang.String,java.lang.String> env)
public java.io.InputStream getStdin()
public void setStdin(java.io.InputStream stdin)
public void setStdOut(java.io.File stdOutFile)
stdOutFile - a file for stdout to be stored topublic void setStdOut(org.apache.log4j.Logger stdOutLogger)
stdOutLogger - an initialized Logger for stdout to be appended to.public void setStdErr(java.io.File stdErrFile)
stdErrFile - a file for stderr to be stored topublic void setStdErr(org.apache.log4j.Logger stdErrLogger)
stdErrLogger - an initialized Logger for stderr to be appended to.public boolean isStdOutAppend()
public void setStdOutAppend(boolean appendStdout)
public boolean isStdErrAppend()
public void setStdErrAppend(boolean appendStderr)
public int getTimeout()
public void setTimeout(int timeout)
public java.io.File getWorkDirectory()
public void setWorkDirectory(java.io.File workDirectory)
public java.lang.Throwable getError()
public int getExitValue()
public java.lang.String getStderr()
public java.util.List<java.lang.String> getStderrByLine()
public java.lang.String getStdout()
public java.util.List<java.lang.String> getStdoutByLine()
public boolean isTimedout()
public boolean isSuccessful()
public void run()
run in interface java.lang.Runnable
protected int handleProcessIO(java.lang.Process process)
throws java.lang.InterruptedException
java.lang.InterruptedExceptionpublic static java.util.List<java.lang.String> toStringList(java.lang.String output)
public java.lang.Process getProcess()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||