ptolemy.util
Class StreamExec

java.lang.Object
  extended by ptolemy.util.StreamExec
All Implemented Interfaces:
ExecuteCommands
Direct Known Subclasses:
StringBufferExec

public class StreamExec
extends java.lang.Object
implements ExecuteCommands

Execute commands in a subprocess and send the results to stderr and stdout.

As an alternative to this class, see JTextAreaExec, which uses Swing; and StringBufferExec, which writes to a StringBuffer.

Sample usage:

 List execCommands = new LinkedList();
 execCommands.add("date");
 execCommands.add("sleep 3");
 execCommands.add("date");
 execCommands.add("notACommand");

 final StreamExec exec = new StreamExec();
 exec.setCommands(execCommands);

 exec.start();
 

Loosely based on Example1.java from http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html

See also http://developer.java.sun.com/developer/qow/archive/135/index.jsp and http://jw.itworld.com/javaworld/jw-12-2000/jw-1229-traps.html

Since:
Ptolemy II 5.2
Version:
$Id: StreamExec.java 55293 2009-07-28 02:08:23Z cxh $
Author:
Christopher Hylands
See Also:
JTextAreaExec, StringBufferExec
Accepted Rating:
Red (cxh)
Proposed Rating:
Red (cxh)

Nested Class Summary
private static class StreamExec._StreamReaderThread
          Private class that reads a stream in a thread and updates the the StreamExec.
 
Field Summary
private  java.util.List _commands
          The list of command to be executed.
private  boolean _debug
           
private  java.lang.String[] _envp
          The environment, which is an array of Strings of the form name=value.
private  java.lang.Process _process
          The Process that we are running.
private  int _subprocessReturnCode
          The return code of the last Runtime.exec() command.
private  java.io.File _workingDirectory
          The working directory of the subprocess.
 
Constructor Summary
StreamExec()
          Create a StreamExec.
 
Method Summary
private  java.lang.String _executeCommands()
          Execute the commands in the list.
protected  void _setProgressBarMaximum(int size)
          Set the maximum of the progress bar.
private  void _updateProgressBar(int i)
          Update the progress bar.
 void appendToPath(java.lang.String directoryName)
          Append to the path of the subprocess.
 void cancel()
          Cancel any running commands.
 void clear()
          Clear the text area, status bar and progress bar.
 java.lang.String getenv(java.lang.String key)
          Get the value of the environment of the subprocess.
 int getLastSubprocessReturnCode()
          Return the return code of the last subprocess that was executed.
 void setCommands(java.util.List commands)
          Set the list of commands.
 void setWorkingDirectory(java.io.File workingDirectory)
          Set the working directory of the subprocess.
 void start()
          Start running the commands.
 void stderr(java.lang.String text)
          Append the text message to stderr.
 void stdout(java.lang.String text)
          Append the text message to the output.
static java.lang.String[] updateEnvironment(java.lang.String key, java.lang.String value)
          Update the environment and return the results.
 void updateStatusBar(java.lang.String text)
          Set the text of the status bar.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_commands

private java.util.List _commands
The list of command to be executed. Each entry in the list is a String. It might be better to have each element of the list be an String [] so that the shell can interpret each word in the command.


_debug

private final boolean _debug
See Also:
Constant Field Values

_envp

private java.lang.String[] _envp
The environment, which is an array of Strings of the form name=value. If this variable is null, then the environment of the calling process is used.


_process

private java.lang.Process _process
The Process that we are running.


_subprocessReturnCode

private int _subprocessReturnCode
The return code of the last Runtime.exec() command.


_workingDirectory

private java.io.File _workingDirectory
The working directory of the subprocess. If null, then the subprocess is executed in the working directory of the current process.

Constructor Detail

StreamExec

public StreamExec()
Create a StreamExec.

Method Detail

appendToPath

public void appendToPath(java.lang.String directoryName)
Append to the path of the subprocess. If directoryName is already in the path, then it is not appended.

Specified by:
appendToPath in interface ExecuteCommands
Parameters:
directoryName - The name of the directory to append to the path.

cancel

public void cancel()
Cancel any running commands.

Specified by:
cancel in interface ExecuteCommands

clear

public void clear()
Clear the text area, status bar and progress bar.

Specified by:
clear in interface ExecuteCommands

getenv

public java.lang.String getenv(java.lang.String key)
Get the value of the environment of the subprocess.

Specified by:
getenv in interface ExecuteCommands
Parameters:
key - The key for which to search.
Returns:
The value of the key. If the key is not set, then null is returned. If appendToPath() has been called, and the then the environment for the subprocess is checked, which might be different than the environment for the current process because appendToPath() was called. Note that that key is searche for in a case-insensitive mode.

getLastSubprocessReturnCode

public int getLastSubprocessReturnCode()
Return the return code of the last subprocess that was executed.

Specified by:
getLastSubprocessReturnCode in interface ExecuteCommands
Returns:
the return code of the last subprocess that was executed.

setCommands

public void setCommands(java.util.List commands)
Set the list of commands.

Specified by:
setCommands in interface ExecuteCommands
Parameters:
commands - A list of Strings, where each element is a command.

setWorkingDirectory

public void setWorkingDirectory(java.io.File workingDirectory)
Set the working directory of the subprocess.

Specified by:
setWorkingDirectory in interface ExecuteCommands
Parameters:
workingDirectory - The working directory of the subprocess. If this argument is null, then the subprocess is executed in the working directory of the current process.

start

public void start()
Start running the commands.

Specified by:
start in interface ExecuteCommands

stderr

public void stderr(java.lang.String text)
Append the text message to stderr. A derived class could append to a StringBuffer. @link{JTextAreaExec} appends to a JTextArea. The output automatically gets a trailing newline appended.

Specified by:
stderr in interface ExecuteCommands
Parameters:
text - The text to append to standard error.

stdout

public void stdout(java.lang.String text)
Append the text message to the output. A derived class could append to a StringBuffer. @link{JTextAreaExec} appends to a JTextArea. The output automatically gets a trailing newline appended.

Specified by:
stdout in interface ExecuteCommands
Parameters:
text - The text to append to standard out.

updateEnvironment

public static java.lang.String[] updateEnvironment(java.lang.String key,
                                                   java.lang.String value)
Update the environment and return the results. Read the environment for the current process, append the value of the value parameter to the environment variable named by the key parameter.

Parameters:
key - The environment variable to be updated.
value - The value to append
Returns:
An array of strings that consists of the subprocess environment variable names and values in the form name=value with the environment variable named by the key parameter updated to include the value of the value parameter.

updateStatusBar

public void updateStatusBar(java.lang.String text)
Set the text of the status bar. In this base class, do nothing, derived classes may update a status bar.

Specified by:
updateStatusBar in interface ExecuteCommands
Parameters:
text - The text with which the status bar is updated.

_setProgressBarMaximum

protected void _setProgressBarMaximum(int size)
Set the maximum of the progress bar. In this base class, do nothing, derived classes may update the size of the progress bar.

Parameters:
size - The maximum size of the progress bar.

_executeCommands

private java.lang.String _executeCommands()
Execute the commands in the list. Update the output with the command being run and the output.


_updateProgressBar

private void _updateProgressBar(int i)
Update the progress bar. In this base class, do nothing.

Parameters:
i - The current location of the progress bar.