JavaGantt 2011.1 API

eu.beesoft.gaia.util
Class SystemProcess

java.lang.Object
  extended by eu.beesoft.gaia.util.SystemProcess

public class SystemProcess
extends java.lang.Object

This class encapsulates functionality of the java.lang.Process, java.lang.ProcessBuilder and some methods from java.lang.Runtime classes. It supports to:

There is also a set of the static exec() methods to simplify starting of the system process.

Note, this implementation solves the problem of the Process.waitFor() method, which hangs when are not read the process streams.


Constructor Summary
SystemProcess()
          Default constructor, creates a new instance of the SystemProcess.
 
Method Summary
 void addCommand(java.util.List<java.lang.String> cmd)
          Adds each parameter from given list to the command list.
 void addCommand(java.lang.String... cmd)
          Adds each parameter from given array to the command list.
 void addCommand(java.lang.String cmd)
          Adds a new command parameter to the command list.
static SystemProcess exec(java.util.List<java.lang.String> command, java.io.File workingDirectory, boolean waitForEnd)
          Executes the specified command and arguments in a separate process.
static SystemProcess exec(java.lang.String... command)
          Executes the specified command and arguments in a separate process.
static SystemProcess exec(java.lang.String[] command, java.io.File workingDirectory, boolean waitForEnd)
          Executes the specified command and arguments in a separate process.
static SystemProcess exec(java.lang.String command, java.io.File workingDirectory, boolean waitForEnd)
          Executes the specified command in a separate process.
 java.util.List<java.lang.String> getCommand()
          Returns the list of commands (arguments).
 java.util.Map<java.lang.String,java.lang.String> getEnvironment()
          Returns a map of the current system environment.
 java.io.InputStream getErrorStream()
          Gets the error stream of the process.
 int getExitValue()
          Returns the exit value for the subprocess.
 java.io.InputStream getInputStream()
          Gets the input stream of the process.
 java.io.OutputStream getOutputStream()
          Gets the output stream of the subprocess.
 java.io.File getWorkingDirectory()
          Returns the working directory set by setWorkingDirectory(File) method or null.
 void kill()
          Kills this process, it is forcibly terminated.
 void mergeErrorStream()
          Merges error and input stream to the one.
 void setWorkingDirectory(java.io.File workingDirectory)
          Sets a new value for working directory.
 void setWorkingDirectory(java.lang.String dir)
          Sets a new value for working directory.
 void start()
          Starts system process.
 int waitFor()
          Causes the current thread to wait, if necessary, until the process represented by this instance has terminated.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SystemProcess

public SystemProcess()
Default constructor, creates a new instance of the SystemProcess.

Method Detail

getWorkingDirectory

public java.io.File getWorkingDirectory()
Returns the working directory set by setWorkingDirectory(File) method or null.

Returns:
the workingDirectory

setWorkingDirectory

public void setWorkingDirectory(java.io.File workingDirectory)
Sets a new value for working directory.

Parameters:
workingDirectory - the workingDirectory to set
Throws:
java.lang.RuntimeException - if workingDirectory does not exist or is a file (not a directory)

setWorkingDirectory

public void setWorkingDirectory(java.lang.String dir)
Sets a new value for working directory. Invokes setWorkingDirectory(File).

Parameters:
dir - the workingDirectory to set
Throws:
java.lang.RuntimeException - if workingDirectory does not exist or is a file (not a directory)

getCommand

public java.util.List<java.lang.String> getCommand()
Returns the list of commands (arguments). This list can be freely modified by programmer.

Returns:
the command argument list

addCommand

public void addCommand(java.lang.String cmd)
Adds a new command parameter to the command list.

Parameters:
cmd - - parameter to add to the command list

addCommand

public void addCommand(java.util.List<java.lang.String> cmd)
Adds each parameter from given list to the command list.

Parameters:
cmd - - list of parameters to add to the command list

addCommand

public void addCommand(java.lang.String... cmd)
Adds each parameter from given array to the command list.

Parameters:
cmd - - array of parameters to add to the command list

getEnvironment

public java.util.Map<java.lang.String,java.lang.String> getEnvironment()
Returns a map of the current system environment. This map can be freely modified by programmer.

Returns:
the map of environment variables (name : value)

mergeErrorStream

public void mergeErrorStream()
Merges error and input stream to the one. Note, there is no way to split ones combined stream after the invocation of this method.


start

public void start()
Starts system process.


waitFor

public int waitFor()
Causes the current thread to wait, if necessary, until the process represented by this instance has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the subprocess exits. If process was not started, it invokes start() method first.

Returns:
exit code of the process

getExitValue

public int getExitValue()
Returns the exit value for the subprocess.

Returns:
the exit value of the subprocess represented by this Process object. By convention, the value 0 indicates normal termination.
Throws:
java.lang.IllegalThreadStateException - - if the subprocess represented by this Process object has not yet terminated.

getOutputStream

public java.io.OutputStream getOutputStream()
Gets the output stream of the subprocess. Output to the stream is piped into the standard input stream of the process represented by this Process object.

Returns:
the output stream connected to the normal input of the subprocess.

getInputStream

public java.io.InputStream getInputStream()
Gets the input stream of the process. The stream obtains data piped from the standard output stream of the process represented by this instance.

Note, there is an InputStreamGobbler used in the implementation, so output from the running process is available after the end of the process.

Returns:
the input stream connected to the normal output of the process.

getErrorStream

public java.io.InputStream getErrorStream()
Gets the error stream of the process. The stream obtains data piped from the error output stream of the process represented by this instance.

Note, there is an InputStreamGobbler used in the implementation, so output from the running process is available after the end of the process.

Returns:
the input stream connected to the error stream of the process.

kill

public void kill()
Kills this process, it is forcibly terminated.


exec

public static SystemProcess exec(java.lang.String... command)
Executes the specified command and arguments in a separate process.

This is a convenience method. It behaves in exactly the same way as the invocation exec(command, null, false).

Parameters:
command - - array of command and its parameters
Returns:
created and started instance of SystemProcess

exec

public static SystemProcess exec(java.lang.String command,
                                 java.io.File workingDirectory,
                                 boolean waitForEnd)
Executes the specified command in a separate process.

This is a convenience method. It parses given command to the individul tokens (parameters) and invokes exec(List, File, boolean) method.

Parameters:
command - - command String to execute
workingDirectory - - working directory of the process or null if the default system directory should be used
waitForEnd - - if true, method blocks while process is not finished
Returns:
started (and terminated, if waitForEnd=true) instance of SystemProcess

exec

public static SystemProcess exec(java.lang.String[] command,
                                 java.io.File workingDirectory,
                                 boolean waitForEnd)
Executes the specified command and arguments in a separate process.

This is a convenience method. It converts given array to the List instance and then invokes exec(List, File, boolean) method.

Parameters:
command - - array of command and parameters to execute
workingDirectory - - working directory of the process or null if the default system directory should be used
waitForEnd - - if true, method blocks while process is not finished
Returns:
started (and terminated, if waitForEnd=true) instance of SystemProcess

exec

public static SystemProcess exec(java.util.List<java.lang.String> command,
                                 java.io.File workingDirectory,
                                 boolean waitForEnd)
Executes the specified command and arguments in a separate process.

Returns created and started instance of SystemProcess. If given argument waitForEnd is true, blocks while process is not finished.

Parameters:
command - - list of command and parameters to execute
workingDirectory - - working directory of the process or null if the default system directory should be used
waitForEnd - - if true, method blocks while process is not finished
Returns:
started (and terminated, if waitForEnd=true) instance of SystemProcess

JavaGantt 2011.1 API