org.mbari.siam.utils
Class SyncProcessRunner

java.lang.Object
  extended by org.mbari.siam.utils.SyncProcessRunner

public class SyncProcessRunner
extends java.lang.Object

SyncProcessRunner provides a wrapper around the Java Runtime.getRuntime().exec() methods.

The problem with Runtime.exec() is that the created Process does not have its own terminal or console. All standard I/O is read/written to the streams created with the Process, which by default have no readers or writers. This is in contrast with how Linux shells (bash etc) deal with stdio -- the child process inherits the controlling terminal.

As a result, if you Runtime.exec() to a shell script, and that shell script does any I/O, it will hang after filling the output buffer.

SyncProcessRunner is similar to ProcessRunner, but is synchronous. That is, it does not introduce a supervisor thread, as does ProcessRunner. Rather, the caller MUST (immediately) call the waitFor() or exitValue() methods, either of which waits (synchronously) for the child process to finish, and then returns its exit value. While waiting, these functions capture the process' stdout and stderr and save them to a string. Thus, this class uses the thread context of the caller for supervising the process.

Note that this class doesn't resolve the input problem (this could be done, but this implementation doesn't do it). As a result, the scripts and/or programs handed to ProcessRunner should never try to read console input.

Also note that this class currently only implements two flavors of Runtime.exec(): String and String[].


Constructor Summary
SyncProcessRunner()
           
 
Method Summary
 void exec(java.lang.String command)
          exec() method emulates the Runtime.exec(String) method
 void exec(java.lang.String[] cmdarray)
          exec method emulates the Runtime.exec(String[]) method
 int exitValue()
          Returns Process.exitValue()
 java.lang.String getOutputString()
          Returns the output that the Process had sent to stdout and stderr, as a String.
 boolean isRunning()
          Similar to Thread.isAlive(), but returns true if wrapped Process is still running
static void main(java.lang.String[] args)
          Main routine, for testing only
protected  void runIt(long timeout)
          Protected method actually runs the command, throws IOException
 int waitFor()
          Returns Process.waitFor()
 int waitFor(long timeout)
          Returns Process.waitFor()
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SyncProcessRunner

public SyncProcessRunner()
Method Detail

exec

public void exec(java.lang.String[] cmdarray)
          throws java.io.IOException,
                 java.lang.IllegalThreadStateException
exec method emulates the Runtime.exec(String[]) method

Throws:
java.io.IOException
java.lang.IllegalThreadStateException

exec

public void exec(java.lang.String command)
          throws java.io.IOException,
                 java.lang.IllegalThreadStateException
exec() method emulates the Runtime.exec(String) method

Throws:
java.io.IOException
java.lang.IllegalThreadStateException

isRunning

public boolean isRunning()
Similar to Thread.isAlive(), but returns true if wrapped Process is still running


runIt

protected void runIt(long timeout)
              throws java.io.IOException
Protected method actually runs the command, throws IOException

Parameters:
timeout - - timeout in milliseconds. <=0 means no timeout
Throws:
java.io.IOException

waitFor

public int waitFor(long timeout)
            throws java.io.IOException,
                   java.lang.InterruptedException
Returns Process.waitFor()

Parameters:
timeout - Timeout to wait in milliseconds
Throws:
java.io.IOException
java.lang.InterruptedException

waitFor

public int waitFor()
            throws java.io.IOException
Returns Process.waitFor()

Throws:
java.io.IOException

exitValue

public int exitValue()
              throws java.io.IOException
Returns Process.exitValue()

Throws:
java.io.IOException

getOutputString

public java.lang.String getOutputString()
Returns the output that the Process had sent to stdout and stderr, as a String. The normal sequence of calls is exec(), waitFor(), and then getOutputString(). That is, it doesn't make much sense to call getOutputString() until after the call has completed; though it's not illegal to call it sooner.


main

public static void main(java.lang.String[] args)
                 throws java.io.IOException
Main routine, for testing only

Throws:
java.io.IOException


Copyright © 2003 MBARI.

The Monterey Bay Aquarium Research Institute (MBARI) provides this documentation and code "as is", with no warranty, express or implied, of its quality or consistency. It is provided without support and without obligation on the part of MBARI to assist in its use, correction, modification, or enhancement.