org.knime.base.node.util.exttool
Class CommandExecution

java.lang.Object
  extended by java.util.Observable
      extended by org.knime.base.node.util.exttool.CommandExecution

public class CommandExecution
extends Observable

Wraps a Java runtime process. Supports cancellations and progress messages (through the ExecutionMonitor). Picks up the output of the launched process and forwards it into the log file. It keeps a buffer of 500 lines for views to display and implements the Observable interface. All listeners will be provided with a ViewUpdateNotice that contains a new line picked up from the external process.
NOTE: Canceling the execution of the external process will only terminate the command launched by this class. If the command created subprocesses (like a shell script will do), these subprocesses will continue living. Even worse, their output will not be picked up anymore, causing their output buffers to fill up and potentially block the process. Thus, these processes will exist in the system until they are killed manually.

Author:
Kilian Thiel, University of Konstanz

Nested Class Summary
(package private)  class CommandExecution.CheckCanceledRunnable
           
(package private)  class CommandExecution.StdErrCatchRunnable
           
(package private)  class CommandExecution.StdOutCatchRunnable
           
 
Field Summary
(package private) static int MAX_OUTLINES_STORED
          the maximum number of lines stored for stdout and stderr output of the external tool.
 
Constructor Summary
CommandExecution(String... command)
          Creates new instance with the given command to execute.
CommandExecution(String cmdWithArgs)
          Creates new instance with the given command to execute.
 
Method Summary
 void clearBuffers()
          Clears the std err and out buffers.
 int execute(ExecutionContext exec)
          Executes the commands via the Runtime and streams the stdout as well as the stderr to the logger and notifies the registered observer if a msg occurs.
 int execute(ExecutionMonitor exec)
          Executes the commands via the Runtime and streams the stdout as well as the stderr to the logger and notifies the registered observer if a msg occurs.
 String[] getEnvps()
           
 File getExecutionDir()
           
 LinkedList<String> getStdErr()
           
 LinkedList<String> getStdOutput()
           
 void setEnvps(String... envps)
           
 void setExecutionDir(File dir)
           
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_OUTLINES_STORED

static final int MAX_OUTLINES_STORED
the maximum number of lines stored for stdout and stderr output of the external tool. (Keeping default scope so views and buffers here have the same length.)

See Also:
Constant Field Values
Constructor Detail

CommandExecution

public CommandExecution(String... command)
Creates new instance with the given command to execute. Each command line argument to the command must be specified in a separate string.

Parameters:
command - The command to execute and its arguments, each specified separately.

CommandExecution

public CommandExecution(String cmdWithArgs)
Creates new instance with the given command to execute. The command line arguments for the command must be separated by a space (and quoted appropriately).

Parameters:
cmdWithArgs - The command to execute and its arguments.
Method Detail

setExecutionDir

public void setExecutionDir(File dir)
Parameters:
dir - the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process.
See Also:
Runtime.exec(String, String[], File)

getExecutionDir

public File getExecutionDir()
Returns:
the working directory of the subprocess, or null if the subprocess inherits the working directory of the current process.
See Also:
Runtime.exec(String, String[], File)

setEnvps

public void setEnvps(String... envps)
Parameters:
envps - array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process.
See Also:
Runtime.exec(String, String[], File)

getEnvps

public String[] getEnvps()
Returns:
an array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess inherits the environment of the current process.
See Also:
Runtime.exec(String, String[], File)

execute

public int execute(ExecutionContext exec)
            throws Exception
Executes the commands via the Runtime and streams the stdout as well as the stderr to the logger and notifies the registered observer if a msg occurs.

Note: This method exists for historical reasons. It casts the argument to an ExecutionMonitor and calls the corresponding execute method.

Parameters:
exec - The ExecutionContext to monitor the status of execution and to check for user cancellations.
Returns:
The exit value of the subprocess.
Throws:
Exception - If the execution terminates abnormally.
See Also:
Runtime.exec(String[], String[], File)

execute

public int execute(ExecutionMonitor exec)
            throws Exception
Executes the commands via the Runtime and streams the stdout as well as the stderr to the logger and notifies the registered observer if a msg occurs.

Parameters:
exec - The ExecutionMonitor to monitor the status of execution and to check for user cancellations.
Returns:
The exit value of the subprocess.
Throws:
Exception - If the execution terminates abnormally.
See Also:
Runtime.exec(String[], String[], File)

clearBuffers

public void clearBuffers()
Clears the std err and out buffers.


getStdOutput

public LinkedList<String> getStdOutput()
Returns:
a (copy of a) buffer containing the last 500 lines to standard output

getStdErr

public LinkedList<String> getStdErr()
Returns:
a (copy of a) buffer containing the last 500 lines to standard error output


Copyright, 2003 - 2010. All rights reserved.
University of Konstanz, Germany.
Chair for Bioinformatics and Information Mining, Prof. Dr. Michael R. Berthold.
You may not modify, publish, transmit, transfer or sell, reproduce, create derivative works from, distribute, perform, display, or in any way exploit any of the content, in whole or in part, except as otherwise expressly permitted in writing by the copyright owner or as specified in the license file distributed with this product.