net.luniks.process
Class ProcessHandler

java.lang.Object
  extended bynet.luniks.process.ProcessHandler
Direct Known Subclasses:
WrapperProcessHandler

public class ProcessHandler
extends java.lang.Object

Class to handle native processes

Author:
Torsten Römer, www.luniks.net

Nested Class Summary
(package private)  class ProcessHandler.InputStreamReaderThread
          Thread to read from stdout/stderr of the process
 
Field Summary
protected  ExitListener exitListener
           
protected  LineListener lineListener
           
protected  java.lang.String name
           
protected  java.lang.Process process
           
protected  boolean running
           
protected  ProcessHandler.InputStreamReaderThread stderrReaderThread
           
protected  ProcessHandler.InputStreamReaderThread stdoutReaderThread
           
static int TERM_EXIT_CODE
          The exit code will be forced to this value when the process has been terminated using the method destroy().
protected  boolean terminated
           
protected  boolean threaded
           
 
Constructor Summary
ProcessHandler()
          Constructs a ProcessHandler with name "Noname" and no listeners.
ProcessHandler(java.lang.String name, ExitListener exitListener)
          Constructs a ProcessHandler with a name and an exit listener.
ProcessHandler(java.lang.String name, LineListener lineListener)
          Constructs a ProcessHandler with a name and a line listener.
ProcessHandler(java.lang.String name, LineListener lineListener, ExitListener exitListener)
          Constructs a ProcessHandler with a name, a line listener and an exit listener.
 
Method Summary
 void destroy()
          Method to terminate the process.
 void exec(java.lang.String[] cmdarray)
          Run the process with its arguments given as argument.
 java.lang.String getName()
          Returns the name of this process handler.
 boolean hasName(java.lang.String name)
          Returns true if the name of this process handler equals the String given as argument, case sensitive.
 boolean isRunning()
          Returns true if the process is currently running, false if it has not been started yet or has exited.
 void kill(int signal)
          Calls destroy(), the signal given as argument is ignored
private  void lineAction(java.lang.String line)
          Method to synchronize the call to LineListener.lineAction
 void setThreaded(boolean threaded)
          Method to set if the process should be run in a separate thread or not.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TERM_EXIT_CODE

public static final int TERM_EXIT_CODE
The exit code will be forced to this value when the process has been terminated using the method destroy().

See Also:
Constant Field Values

process

protected java.lang.Process process

name

protected java.lang.String name

running

protected boolean running

terminated

protected boolean terminated

threaded

protected boolean threaded

stdoutReaderThread

protected ProcessHandler.InputStreamReaderThread stdoutReaderThread

stderrReaderThread

protected ProcessHandler.InputStreamReaderThread stderrReaderThread

lineListener

protected LineListener lineListener

exitListener

protected ExitListener exitListener
Constructor Detail

ProcessHandler

public ProcessHandler()
Constructs a ProcessHandler with name "Noname" and no listeners. Stdout and stderr of the process will be consumed.


ProcessHandler

public ProcessHandler(java.lang.String name,
                      LineListener lineListener)
Constructs a ProcessHandler with a name and a line listener. The method lineAction of the LineListener will be called whenever a new line has been read from either stdout or stderr of the process, with that line as argument.

Parameters:
name - the name of the process handler
lineListener - the line listener

ProcessHandler

public ProcessHandler(java.lang.String name,
                      ExitListener exitListener)
Constructs a ProcessHandler with a name and an exit listener. The method exitAction of the ExitListener will be called when the process exits with the exit code as argument. If an exception occurs at Process.exec the method exceptionAction will be called with the exception as argument. Stdout and stderr of of the process will be consumed.

Parameters:
name - the name of the process handler
exitListener - the exit listener

ProcessHandler

public ProcessHandler(java.lang.String name,
                      LineListener lineListener,
                      ExitListener exitListener)
Constructs a ProcessHandler with a name, a line listener and an exit listener.

Parameters:
name - the name of the process handler
lineListener - the line listener
exitListener - the exit listener
Method Detail

setThreaded

public void setThreaded(boolean threaded)
Method to set if the process should be run in a separate thread or not. If the argument is true, the method exec will return immediately, and, if given to the constructor, the ExitListener's exitAction will be called when the process exits. If set to false, the method exec will not return until the process exits, at the same time ExitListener.exitAction will be called. Default is true.

Parameters:
threaded - true if the process should be run in a separate process, false otherwise

isRunning

public boolean isRunning()
Returns true if the process is currently running, false if it has not been started yet or has exited.

Returns:
true if the process is running, false otherwise

getName

public java.lang.String getName()
Returns the name of this process handler. Idea is to use this as a (unique) identifier.

Returns:
the name of the process handler

hasName

public boolean hasName(java.lang.String name)
Returns true if the name of this process handler equals the String given as argument, case sensitive.

Returns:
true if the name given as argument equals the process handler's name, false otherwise

exec

public void exec(java.lang.String[] cmdarray)
Run the process with its arguments given as argument. Two threads will be started to read the processe's stdout and stderr. If an ExitListener has been given to the constructor, it's exitAction will be called when this process exits with the exit code as argument, and it's exceptionAction will be called in case an exception occurs, with the exception as argument.

Parameters:
cmdarray - the process to run and its arguments

kill

public void kill(int signal)
Calls destroy(), the signal given as argument is ignored

Parameters:
signal - is ignored

destroy

public void destroy()
Method to terminate the process. Does nothing else than Process.destroy(). If an ExitListener has been given to the constructor its exitAction method will be called with the exit code as argument, which will be forced to TERM_EXIT_CODE. There is a bug in Java 1.4.x and 1.5.0-beta1 on Linux which leads to both InputStreams being closed immediately when this method is called so that it's not possible to read until the end of the streams. A NullPointerException is thrown. (Java bug id 4728096, fixed in tiger-beta2/1.5.0-beta2)


lineAction

private void lineAction(java.lang.String line)
Method to synchronize the call to LineListener.lineAction

Parameters:
line - the line that has just been read from either stdin or stdout of the process


Copyright (c) 2004, 2005 Torsten Römer, dode@luniks.net