com.cafesoft.core.util
Class MonitoredProcess

java.lang.Object
  |
  +--com.cafesoft.core.util.MonitoredProcess

public class MonitoredProcess
extends Object

MonitoredProcess encapsulates a native java.lang.Process created using one of the java.lang.Runtime.exec() methods. The Process is monitored in a new Thread, with additional Threads reading the System.out and System.err streams. MonitoredProcess enables listeners to asynchronously receive notification of the Process exit status and other runtime exceptions. In addition, the MonitoredProcess can be destroyed at any time. This is useful if the Process does not start within a given period or does not shutdown gracefully.

Since:
6/6/03

Constructor Summary
MonitoredProcess(String processName, Process process, Logger logger)
          Create a new MonitoredProcess.
 
Method Summary
 void addMonitoredProcessEventListener(MonitoredProcessEventListener listener)
          Add a MonitoredProcessEvent listener to this component.
 void destroy()
          Destroy the MonitoredProcess.
 int getExitValue()
          Get the process exit value (valid only if process is not running).
 boolean isRunning()
          Get the process status.
static void main(String[] arg)
          Test a MonitoredProcess.
 void removeMonitoredProcessEventListener(MonitoredProcessEventListener listener)
          Remove a MonitoredProcessEvent listener from this component.
 boolean setDebug(boolean enable)
          Enable or disable DEBUG-level messages.
 void start()
          Start monitoring the Process.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MonitoredProcess

public MonitoredProcess(String processName,
                        Process process,
                        Logger logger)
Create a new MonitoredProcess.

Parameters:
processName - a textual name assigned to the process for identification within MonitoredProcess events and logged output.
process - the java.lang.Process to be monitored.
logger - a Logger to which the Process's System.out and System.err are logged.
Method Detail

setDebug

public boolean setDebug(boolean enable)
Enable or disable DEBUG-level messages.

Parameters:
enable - enable or disable DEBUG-level messages.
Returns:
the previously set debug flag.

start

public void start()
           throws MonitoredProcessException
Start monitoring the Process.

Throws:
MonitoredProcessException - if the MonitoredProcess has already been started.

isRunning

public boolean isRunning()
Get the process status.

Returns:
true if the process is currently running, else false.

getExitValue

public int getExitValue()
Get the process exit value (valid only if process is not running).

Returns:
the process exit value.

destroy

public void destroy()
Destroy the MonitoredProcess. If the enclosed Process is still running, it is destroyed (interrupted) else just dereference internal resources.


addMonitoredProcessEventListener

public void addMonitoredProcessEventListener(MonitoredProcessEventListener listener)
Add a MonitoredProcessEvent listener to this component.

Parameters:
listener - The listener to add

removeMonitoredProcessEventListener

public void removeMonitoredProcessEventListener(MonitoredProcessEventListener listener)
Remove a MonitoredProcessEvent listener from this component.

Parameters:
listener - The listener to remove

main

public static void main(String[] arg)
Test a MonitoredProcess.



Generated on 8:41:50 AM June 06, 2005, © 1996-2005 Cafésoft LLC. All rights reserved.