The im4java Library, Version 1.4.0

org.im4java.process
Class ProcessStarter

java.lang.Object
  extended by org.im4java.process.ProcessStarter
Direct Known Subclasses:
ImageCommand

public class ProcessStarter
extends java.lang.Object

This class implements the processing of os-commands using a ProcessBuilder.

This is the core class of the im4java-library where all the magic takes place. It does add some overhead compared to a direct call of ProcessBuilder, but you gain additional features like piping and asynchronous execution.

Since:
0.95
Version:
$Revision: 1.38 $
Author:
$Author: bablokb $

Field Summary
static int BUFFER_SIZE
          Buffer size of process input-stream (used for reading the output (sic!)
 
Constructor Summary
protected ProcessStarter()
          Constructor.
 
Method Summary
 void addProcessEventListener(ProcessEventListener pListener)
          Add a ProcessEventListener to this ProcessStarter.
 void addProcessListener(ProcessListener pProcessListener)
          Deprecated. use addProcessEventListener(org.im4java.process.ProcessEventListener) instead
protected  void finished(java.lang.Exception pException)
          Post-processing after the process has terminated with an exception.
protected  void finished(int pReturnCode)
          Post-processing after the process has terminated.
static java.lang.String getGlobalSearchPath()
          Query the global (static) search path.
 int getPID()
          Query the process-id.
protected  ProcessTask getProcessTask(java.util.LinkedList<java.lang.String> pArgs)
          Return a ProcessTask for future execution.
 java.lang.String getSearchPath()
          Query the per object search path.
 boolean isAsyncMode()
          Query the async-execution mode.
 void removeProcessEventListener(ProcessEventListener pListener)
          Remove a ProcessEventListener from this ProcessStarter.
protected  int run(java.util.LinkedList<java.lang.String> pArgs)
          Execute the command.
 java.lang.String searchForCmd(java.lang.String pCmd, java.lang.String pPath)
          Query the per object search path.
 void setAsyncMode(boolean pAsyncMode)
          Set the async-execution mode.
 void setErrorConsumer(ErrorConsumer pErrorConsumer)
          Set the ErrorConsumer for the stderr of the ProcessStarter.
static void setGlobalSearchPath(java.lang.String pGlobalSearchPath)
          Set the global (static) search path.
 void setInputProvider(InputProvider pInputProvider)
          Set the InputProvider for the ProcessStarter (if used as a pipe).
 void setOutputConsumer(OutputConsumer pOutputConsumer)
          Set the OutputConsumer for the ProcessStarter (if used as a pipe).
 void setPID(int pPID)
          Set the process-id of this ProcessStarter.
static void setPIDCounter(int pPID)
          Set the process-id counter of the class.
 void setSearchPath(java.lang.String pSearchPath)
          Set the per object search path.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BUFFER_SIZE

public static final int BUFFER_SIZE
Buffer size of process input-stream (used for reading the output (sic!) of the process). Currently 64KB.

See Also:
Constant Field Values
Constructor Detail

ProcessStarter

protected ProcessStarter()
Constructor.

Method Detail

setInputProvider

public void setInputProvider(InputProvider pInputProvider)
Set the InputProvider for the ProcessStarter (if used as a pipe).


setOutputConsumer

public void setOutputConsumer(OutputConsumer pOutputConsumer)
Set the OutputConsumer for the ProcessStarter (if used as a pipe).


setErrorConsumer

public void setErrorConsumer(ErrorConsumer pErrorConsumer)
Set the ErrorConsumer for the stderr of the ProcessStarter.


addProcessEventListener

public void addProcessEventListener(ProcessEventListener pListener)
Add a ProcessEventListener to this ProcessStarter.

Parameters:
pListener - the ProcessEventListener to add

removeProcessEventListener

public void removeProcessEventListener(ProcessEventListener pListener)
Remove a ProcessEventListener from this ProcessStarter.

Parameters:
pListener - the ProcessEventListener to remove

addProcessListener

public void addProcessListener(ProcessListener pProcessListener)
Deprecated. use addProcessEventListener(org.im4java.process.ProcessEventListener) instead

Add a ProcessListener to this ProcessStarter.

Parameters:
pProcessListener - the ProcessListener to add

run

protected int run(java.util.LinkedList<java.lang.String> pArgs)
           throws java.io.IOException,
                  java.lang.InterruptedException,
                  java.lang.Exception
Execute the command.

Parameters:
pArgs - arguments for ProcessBuilder
Throws:
java.io.IOException
java.lang.InterruptedException
java.lang.Exception

getProcessTask

protected ProcessTask getProcessTask(java.util.LinkedList<java.lang.String> pArgs)
Return a ProcessTask for future execution.

Parameters:
pArgs - arguments for ProcessBuilder

setAsyncMode

public void setAsyncMode(boolean pAsyncMode)
Set the async-execution mode.

Parameters:
pAsyncMode - the iAsyncMode to set

isAsyncMode

public boolean isAsyncMode()
Query the async-execution mode.

Returns:
the iAsyncMode

setGlobalSearchPath

public static void setGlobalSearchPath(java.lang.String pGlobalSearchPath)
Set the global (static) search path. You can override this search path on a per object basis.

Parameters:
pGlobalSearchPath - the global search path

getGlobalSearchPath

public static java.lang.String getGlobalSearchPath()
Query the global (static) search path.


setSearchPath

public void setSearchPath(java.lang.String pSearchPath)
Set the per object search path. This overrides the global search path (if set).

Parameters:
pSearchPath - the search path

getSearchPath

public java.lang.String getSearchPath()
Query the per object search path.


setPIDCounter

public static void setPIDCounter(int pPID)
Set the process-id counter of the class.

Parameters:
pPID - the process-id

setPID

public void setPID(int pPID)
Set the process-id of this ProcessStarter.

Parameters:
pPID - the process-id

getPID

public int getPID()
Query the process-id.


finished

protected void finished(int pReturnCode)
                 throws java.lang.Exception
Post-processing after the process has terminated. Subclasses might override this method to do some specific post-processing.

Parameters:
pReturnCode - the return-code of the process
Throws:
java.lang.Exception

finished

protected void finished(java.lang.Exception pException)
                 throws java.lang.Exception
Post-processing after the process has terminated with an exception. Subclasses might override this method to do some specific post-processing. This method is only called in asynchronous execution mode (in synchronous mode, the exception just propagates as usual to the caller).

Note that if this method throws an exception in asynchronous execution mode, the original exception is lost and not propagated to any ProcessEventListeners. Therefore, you should take care to fill in any exception and stack-trace information.

Parameters:
pException - the exception of the process
Throws:
java.lang.Exception

searchForCmd

public java.lang.String searchForCmd(java.lang.String pCmd,
                                     java.lang.String pPath)
                              throws java.io.IOException,
                                     java.io.FileNotFoundException
Query the per object search path.

Parameters:
pCmd - the command to search for
pPath - the search path
Throws:
java.io.IOException
java.io.FileNotFoundException

The im4java Library, Version 1.4.0

Released under the LGPL, (c) Bernhard Bablok 2008-2010
Homepage: http://im4java.sourceforge.net/