net.luniks.process
Class WrapperProcessHandler

java.lang.Object
  extended bynet.luniks.process.ProcessHandler
      extended bynet.luniks.process.WrapperProcessHandler

public class WrapperProcessHandler
extends ProcessHandler

Class to handle native processes

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

Nested Class Summary
 
Nested classes inherited from class net.luniks.process.ProcessHandler
ProcessHandler.InputStreamReaderThread
 
Field Summary
private  java.lang.String pid
           
static java.lang.String WRAPPER
           
 
Fields inherited from class net.luniks.process.ProcessHandler
exitListener, lineListener, name, process, running, stderrReaderThread, stdoutReaderThread, TERM_EXIT_CODE, terminated, threaded
 
Constructor Summary
WrapperProcessHandler()
          Constructs a ProcessHandler with name "Noname" and no listeners.
WrapperProcessHandler(java.lang.String name, ExitListener exitListener)
          Constructs a ProcessHandler with a name and an exit listener.
WrapperProcessHandler(java.lang.String name, LineListener lineListener)
          Constructs a ProcessHandler with a name and a line listener.
WrapperProcessHandler(java.lang.String name, LineListener lineListener, ExitListener exitListener)
          Constructs a ProcessHandler with a name, a line listener and an exit listener.
 
Method Summary
 void exec(java.lang.String[] cmdarray)
          Run the process with its arguments given as argument.
private  java.io.File getWrapper()
          Returns the location of the wrapper as File
 void kill(int signal)
          Method to send the signal given as argument to the process using a native kill command.
 
Methods inherited from class net.luniks.process.ProcessHandler
destroy, getName, hasName, isRunning, setThreaded
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

WRAPPER

public static final java.lang.String WRAPPER
See Also:
Constant Field Values

pid

private java.lang.String pid
Constructor Detail

WrapperProcessHandler

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


WrapperProcessHandler

public WrapperProcessHandler(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

WrapperProcessHandler

public WrapperProcessHandler(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

WrapperProcessHandler

public WrapperProcessHandler(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

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.
The process is executed by the wrapper jpwrapper which allows this method to read the PID of the process and store it in a variable. This PID is then used by the method kill() to send a signal to it, for example SIGINT to shut it down gently as opposed to destroy().
NOTE: The wrapper jpwrapper is expected to be found in the same directory as where this package (or JAR file) is located.

Overrides:
exec in class ProcessHandler
Parameters:
cmdarray - the process to run and its arguments

kill

public void kill(int signal)
Method to send the signal given as argument to the process using a native kill command. If no PID could be obtained when the process was started or the kill command failed, destroy() is called.

Overrides:
kill in class ProcessHandler
Parameters:
signal - the signal to send to the process

getWrapper

private java.io.File getWrapper()
Returns the location of the wrapper as File

Returns:
path to the wrapper


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