|
OpenNMS API 1.2.3 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.opennms.netmgt.utils.ExecRunner
Runs external executables, optionally under a watched thread. In addition, probably the most useful feature of ExecRunner is using it to run a command-line program and obtain its stdout and stderr results in two strings. This is done with exec(String) - see that method for an example. With acknowledgements to Michael C. Daconta, author of "Java Pitfalls, Time Saving Solutions, and Workarounds to Improve Programs." and his article in JavaWorld "When Runtime.exec() Won't".
Field Summary | |
private static java.lang.String |
CLASS_NAME
The name of this class for logging * |
private java.lang.String |
err
String to capture STDERR * |
private static java.lang.String |
MAX_RUN_TIME_EXCEEDED_STRING
String to send to STDERR if program exceeds max run time * |
private boolean |
maxRunTimeExceeded
Flag to indicate if we've exceeded max run time * |
private int |
maxRunTimeSecs
Default max run time (in seconds) * |
private java.lang.String |
out
String to capture STDOUT * |
private static int |
POLL_DELAY_MS
Number of miliseconds to wait between polling watched thread * |
private static java.lang.String |
VERSION
The version of this class (filled in by CVS) * |
private static java.lang.String |
WINDOWS_9X_ME_COMMAND_1
Win 9X/MEHome require cmd.exe to run programs * |
private static java.lang.String |
WINDOWS_9X_ME_COMMAND_2
Win 9X/MEHome require the /C to specify what to run * |
private static java.lang.String |
WINDOWS_NT_2000_COMMAND_1
Win NT/2K/MEPro require cmd.exe to run programs * |
private static java.lang.String |
WINDOWS_NT_2000_COMMAND_2
Win NT/2K/MEPro require the /C to specify what to run * |
Constructor Summary | |
ExecRunner()
Basic ExecRunner constructor. |
|
ExecRunner(java.lang.String command)
ExecRunner constructor which also conveniently runs exec(String). |
Method Summary | |
java.lang.Object |
clone()
We override the clone method here to prevent cloning of
our class. |
int |
exec(java.lang.String command)
The exec(String) method runs a process inside of a watched thread. |
int |
exec(java.lang.String command,
java.io.OutputStream stdoutStream,
java.io.OutputStream stderrStream)
Convenience method for calling exec with OutputStreams. |
int |
exec(java.lang.String command,
java.io.PrintWriter stdoutWriter,
java.io.PrintWriter stderrWriter)
The exec(String, PrintWriter, PrintWriter) method runs a
process inside of a watched thread. |
java.lang.String |
getErrString()
Returns the error string if exec(String) was invoked. |
int |
getMaxRunTimeSecs()
Returns the maximum run time in seconds for this object. |
java.lang.String |
getOutString()
Returns the output string if exec(String) was invoked. |
boolean |
isMaxRunTimeExceeded()
Returns whether the maximum runtime was exceeded or not. |
static void |
main(java.lang.String[] args)
This is for unit testing of the class. |
private void |
readObject(java.io.ObjectInputStream in)
We override the readObject method here to prevent
deserialization of our class for security reasons. |
void |
setMaxRunTimeSecs(int max)
Sets the maximum run time in seconds. |
private void |
writeObject(java.io.ObjectOutputStream out)
We override the writeObject method here to prevent
serialization of our class for security reasons. |
Methods inherited from class java.lang.Object |
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
private static final java.lang.String WINDOWS_NT_2000_COMMAND_1
private static final java.lang.String WINDOWS_NT_2000_COMMAND_2
private static final java.lang.String WINDOWS_9X_ME_COMMAND_1
private static final java.lang.String WINDOWS_9X_ME_COMMAND_2
private static final java.lang.String MAX_RUN_TIME_EXCEEDED_STRING
private java.lang.String out
private java.lang.String err
private int maxRunTimeSecs
private boolean maxRunTimeExceeded
private static final java.lang.String CLASS_NAME
private static final java.lang.String VERSION
private static final int POLL_DELAY_MS
Constructor Detail |
public ExecRunner()
public ExecRunner(java.lang.String command) throws java.lang.ExceptionInInitializerError
command
- The program or command to run
java.lang.ExceptionInInitializerError
- thrown if a problem occursMethod Detail |
public final java.lang.Object clone() throws java.lang.CloneNotSupportedException
clone
method here to prevent cloning of
our class.
java.lang.CloneNotSupportedException
- To indicate cloning is not allowedpublic int exec(java.lang.String command) throws java.io.IOException, java.lang.InterruptedException
// Execute the program and grab the results try { ExecRunner er = new ExecRunner(); er.setMaxRunTimeSecs(5); er.exec("ls -l"); if (!er.getMaxRunTimeExceeded()) { out = er.getOutString(); err = er.getErrString(); } else { System.out.println("Maximum run time exceeded!"); } } catch (Exception e) { System.out.println("Error executing " + program + ": " + e.getMessage()); continue; }
command
- The program or command to run
java.io.IOException
- thrown if a problem occurs
java.lang.InterruptedException
- thrown if a problem occurspublic int exec(java.lang.String command, java.io.OutputStream stdoutStream, java.io.OutputStream stderrStream) throws java.io.IOException, java.lang.InterruptedException
command
- The program or command to runstdoutStream
- java.io.OutputStreamstderrStream
- java.io.OutputStream
java.io.IOException
- thrown if a problem occurs
java.lang.InterruptedException
- thrown if a problem occurspublic int exec(java.lang.String command, java.io.PrintWriter stdoutWriter, java.io.PrintWriter stderrWriter) throws java.io.IOException, java.lang.InterruptedException
exec(String, PrintWriter, PrintWriter)
method runs a
process inside of a watched thread. It returns the client's exit code and
feeds its STDOUT and STDERR to the passed-in streams.
command
- The program or command to runstdoutWriter
- java.io.PrintWriterstderrWriter
- java.io.PrintWriter
java.io.IOException
- thrown if a problem occurs
java.lang.InterruptedException
- thrown if a problem occurspublic java.lang.String getErrString()
public boolean isMaxRunTimeExceeded()
public int getMaxRunTimeSecs()
public java.lang.String getOutString()
public static void main(java.lang.String[] args) throws java.io.IOException
args
- an array of command-line arguments
java.io.IOException
- thrown if a problem occursprivate final void readObject(java.io.ObjectInputStream in) throws java.io.IOException
readObject
method here to prevent
deserialization of our class for security reasons.
in
- java.io.ObjectInputStream
java.io.IOException
- thrown if a problem occurspublic void setMaxRunTimeSecs(int max)
max
- Maximim number of seconds to let program runprivate final void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException
writeObject
method here to prevent
serialization of our class for security reasons.
out
- java.io.ObjectOutputStream
java.io.IOException
- thrown if a problem occurs
|
OpenNMS API 1.2.3 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |