org.mmbase.util.externalprocess
Class CommandLauncher

java.lang.Object
  extended byorg.mmbase.util.externalprocess.CommandLauncher

public class CommandLauncher
extends java.lang.Object

The command launcher provides a way to comunicate with a external process

Since:
MMBase-1.6
Version:
$Id:
Author:
Nico Klasens (Finalist IT Group)

Field Summary
protected  java.lang.String[] commandArgs
          Command and arguments
protected static int counter
          Counts how many comands are launched Also used for () identification
protected static long DELAY
          The number of milliseconds to pause between polling.
protected  java.lang.String name
          The internal name of the external process
protected  java.lang.Process process
          The process object representing the external process
 
Constructor Summary
CommandLauncher(java.lang.String name)
          Creates a new launcher Fills in stderr and stdout output to the given streams.
 
Method Summary
protected  java.lang.String[] constructCommandArray(java.lang.String command, java.lang.String[] commandArgs)
          Constructs a command array that will be passed to the process
 void execute(java.lang.String command)
          Execute a command
 void execute(java.lang.String[] commandArgs)
          Execute a command
 void execute(java.lang.String[] commandArgs, java.lang.String[] env)
          Execute a command
 void execute(java.lang.String[] commandArgs, java.lang.String[] env, java.lang.String changeToDirectory)
          Execute a command
 void execute(java.lang.String commandPath, java.lang.String[] args)
          Execute a command
 void execute(java.lang.String commandPath, java.lang.String[] args, java.lang.String[] env)
          Execute a command
 void execute(java.lang.String commandPath, java.lang.String[] args, java.lang.String[] env, java.lang.String changeToDirectory)
          Execute a command
 java.lang.String[] getCommandArgs()
          get CommandArgs.
 void printCommandLine(java.lang.String[] commandArgs)
          print Command Line.
protected  void processStreams(ProcessClosure closure, java.io.OutputStream output, java.io.PipedInputStream inputPipe, java.io.OutputStream err, java.io.PipedInputStream errInPipe, IProgressMonitor monitor)
          process the Streams.while the external process returns bytes.
 void waitAndRead(java.io.OutputStream out, java.io.OutputStream err)
          Reads output from the external process to the streams.
 void waitAndRead(java.io.OutputStream output, java.io.OutputStream err, IProgressMonitor monitor)
          Reads output from the external process to the streams.
 void waitAndWrite(java.io.InputStream in, java.io.OutputStream out, java.io.OutputStream err)
          Writes input to and reads output from the external process to the streams.
 void waitAndWrite(java.io.InputStream in, java.io.OutputStream output, java.io.OutputStream err, IProgressMonitor monitor)
          Writes input to and reads output from the external process to the streams.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DELAY

protected static final long DELAY
The number of milliseconds to pause between polling.

See Also:
Constant Field Values

counter

protected static int counter
Counts how many comands are launched Also used for () identification


process

protected java.lang.Process process
The process object representing the external process


commandArgs

protected java.lang.String[] commandArgs
Command and arguments


name

protected java.lang.String name
The internal name of the external process

Constructor Detail

CommandLauncher

public CommandLauncher(java.lang.String name)
Creates a new launcher Fills in stderr and stdout output to the given streams. Streams can be set to null, if output not required

Parameters:
name - internal name of the external process
Method Detail

getCommandArgs

public java.lang.String[] getCommandArgs()
get CommandArgs.

Returns:
String[]

constructCommandArray

protected java.lang.String[] constructCommandArray(java.lang.String command,
                                                   java.lang.String[] commandArgs)
Constructs a command array that will be passed to the process

Parameters:
commandArgs - arguments after the command

execute

public void execute(java.lang.String command)
             throws ProcessException
Execute a command

Parameters:
command - command
Throws:
java.io.IOException - if an I/O error occurs
ProcessException

execute

public void execute(java.lang.String[] commandArgs)
             throws ProcessException
Execute a command

Parameters:
commandArgs - command and arguments
Throws:
java.io.IOException - if an I/O error occurs
ProcessException

execute

public void execute(java.lang.String commandPath,
                    java.lang.String[] args)
             throws ProcessException
Execute a command

Parameters:
commandPath - path of comand
args - arguments after the command
Throws:
java.io.IOException - if an I/O error occurs
ProcessException

execute

public void execute(java.lang.String[] commandArgs,
                    java.lang.String[] env)
             throws ProcessException
Execute a command

Parameters:
commandArgs - command and arguments
env - environment name value pairs
Throws:
java.io.IOException - if an I/O error occurs
ProcessException

execute

public void execute(java.lang.String commandPath,
                    java.lang.String[] args,
                    java.lang.String[] env)
             throws ProcessException
Execute a command

Parameters:
commandPath - path of comand
args - arguments after the comand
env - environment name value pairs
Throws:
java.io.IOException - if an I/O error occurs
ProcessException

execute

public void execute(java.lang.String[] commandArgs,
                    java.lang.String[] env,
                    java.lang.String changeToDirectory)
             throws ProcessException
Execute a command

Parameters:
commandArgs - command and arguments
env - environment name value pairs
changeToDirectory - working directory
Throws:
java.io.IOException - if an I/O error occurs
ProcessException

execute

public void execute(java.lang.String commandPath,
                    java.lang.String[] args,
                    java.lang.String[] env,
                    java.lang.String changeToDirectory)
             throws ProcessException
Execute a command

Parameters:
commandPath - path of comand
args - arguments after the comand
env - environment name value pairs
changeToDirectory - working directory
Throws:
java.io.IOException - if an I/O error occurs
ProcessException

waitAndRead

public void waitAndRead(java.io.OutputStream out,
                        java.io.OutputStream err)
                 throws ProcessException
Reads output from the external process to the streams.

Parameters:
err - process stderr is written to this stream
Throws:
ProcessException - if process not yet executed

waitAndRead

public void waitAndRead(java.io.OutputStream output,
                        java.io.OutputStream err,
                        IProgressMonitor monitor)
                 throws ProcessException
Reads output from the external process to the streams. A progress monitor is polled to test for cancellation. Destroys the process if the monitor becomes cancelled

Parameters:
output - process stdout is written to this stream
err - process stderr is written to this stream
monitor - monitor monitor to receive progress info and to cancel the external process
Throws:
ProcessException - if process not yet executed or if process cancelled

waitAndWrite

public void waitAndWrite(java.io.InputStream in,
                         java.io.OutputStream out,
                         java.io.OutputStream err)
                  throws ProcessException
Writes input to and reads output from the external process to the streams.

Parameters:
in - process stdin is read from this stream
err - process stderr is written to this stream
Throws:
ProcessException - if process not yet executed

waitAndWrite

public void waitAndWrite(java.io.InputStream in,
                         java.io.OutputStream output,
                         java.io.OutputStream err,
                         IProgressMonitor monitor)
                  throws ProcessException
Writes input to and reads output from the external process to the streams. A progress monitor is polled to test for cancellation. Destroys the process if the monitor becomes cancelled

Parameters:
in - process stdin is read from this stream
output - process stdout is written to this stream
err - process stderr is written to this stream
monitor - monitor monitor to receive progress info and to cancel the external process
Throws:
ProcessException - if process not yet executed or if process cancelled

processStreams

protected void processStreams(ProcessClosure closure,
                              java.io.OutputStream output,
                              java.io.PipedInputStream inputPipe,
                              java.io.OutputStream err,
                              java.io.PipedInputStream errInPipe,
                              IProgressMonitor monitor)
                       throws ProcessException
process the Streams.while the external process returns bytes. Cancellation is possible by the ProgressMonitor

Parameters:
closure - process closure object which handles the interaction with the external process
output - process stdout is written to this stream
inputPipe - piped stream to other thread for the stdout
err - process stderr is written to this stream
errInPipe - piped stream to other thread for the stderr
monitor - monitor to receive progress info and to cancel the external process
Throws:
ProcessException - if process cancelled

printCommandLine

public void printCommandLine(java.lang.String[] commandArgs)
print Command Line.

Parameters:
commandArgs - array of comand and args


MMBase build 1.7.4.20050922