cz.cuni.amis.utils.process
Class ProcessExecution

Package class diagram package ProcessExecution
java.lang.Object
  extended by cz.cuni.amis.utils.process.ProcessExecution

public class ProcessExecution
extends Object

Class that wrapps the execution of the arbitrary process that is configured by ProcessExecutionConfig.

The class is not suitable for inheritance ... better copy-paste and adjust for yourself.

Author:
Jimmy

Field Summary
protected  ProcessExecutionConfig config
          Configuration of the process to run.
static Pattern FORMAL_PARAM
           
protected  Logger log
          Log used by the object, specified during the construction.
protected  Process process
          When start()ed it contains the process.
protected  cz.cuni.amis.utils.flag.Flag<Boolean> running
          Flag whether the process is running, we're synchronizing access to this class on this flag.
protected  Runnable shutDownHook
          Task that will kill the process when user forgets to do so before the JVM is killed.
protected  Thread shutDownHookThread
           
static Pattern SIMPLE_PARAM
           
protected  cz.cuni.amis.utils.StreamSink streamSinkError
          Sink for the STDERR of the process, it may be redirected to log.
protected  cz.cuni.amis.utils.StreamSink streamSinkOutput
          Sink for the STDOUT of the process, it may be redirected to log.
protected  boolean timedout
          Whether the process timed out or not.
protected  Runnable waitForEnd
          Task that is waiting for the end of the process to switch running back to false and shutdown all other utility threads (like sink).
protected  Thread waitForEndThread
           
 
Constructor Summary
ProcessExecution(ProcessExecutionConfig config, Logger log)
           
 
Method Summary
 Process getBotProcess()
          Process of the process (non-null if started).
 Integer getExitValue()
          Returns exit value of the process.
 cz.cuni.amis.utils.flag.ImmutableFlag<Boolean> getRunning()
          Flag of the state of the process.
 boolean isFailed()
          Tells whether the process has failed == timed out / exit value is not 0.
 boolean isFailed(boolean report)
          Tells whether the process has failed == timed out / exit value is not 0.
 boolean isRunning()
          Flag of the state of the process.
 boolean isTimeout()
          If ProcessExecutionConfig.getTimeout() is specified, this will indicate whether the started process has timed out or not.
protected  void shutdown(boolean waitForEndThread)
          Shutdowns the process and cleans-up everything, preparing the object to be start()ed again.
 void start()
          Start the process.
 void stop()
          Shuts down the process (if it is running, otherwise does nothing).
static String substituteParams(String command, Logger log)
          Searches for $XXX or ${XXX} and substitute them with System.getenv() (prioritized) or System.getProperty(String).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

config

protected ProcessExecutionConfig config
Configuration of the process to run.


running

protected cz.cuni.amis.utils.flag.Flag<Boolean> running
Flag whether the process is running, we're synchronizing access to this class on this flag.

Set to TRUE ONLY IN start() and back to FALSE ONLY IN shutdown(boolean). Do not alter the flag from anywhere else!


process

protected Process process
When start()ed it contains the process.


streamSinkOutput

protected cz.cuni.amis.utils.StreamSink streamSinkOutput
Sink for the STDOUT of the process, it may be redirected to log.


streamSinkError

protected cz.cuni.amis.utils.StreamSink streamSinkError
Sink for the STDERR of the process, it may be redirected to log.


log

protected Logger log
Log used by the object, specified during the construction.


shutDownHook

protected Runnable shutDownHook
Task that will kill the process when user forgets to do so before the JVM is killed.


shutDownHookThread

protected Thread shutDownHookThread

timedout

protected boolean timedout
Whether the process timed out or not.


waitForEnd

protected Runnable waitForEnd
Task that is waiting for the end of the process to switch running back to false and shutdown all other utility threads (like sink).


waitForEndThread

protected Thread waitForEndThread

SIMPLE_PARAM

public static final Pattern SIMPLE_PARAM

FORMAL_PARAM

public static final Pattern FORMAL_PARAM
Constructor Detail

ProcessExecution

public ProcessExecution(ProcessExecutionConfig config,
                        Logger log)
Parameters:
config - process execution configuration (contains path to file to run)
log - log to be used (stdout/stderr of the process will be redirected there as well with Level.INFO)
Method Detail

substituteParams

public static String substituteParams(String command,
                                      Logger log)
Searches for $XXX or ${XXX} and substitute them with System.getenv() (prioritized) or System.getProperty(String).

Parameters:
command -
Returns:

start

public void start()
           throws cz.cuni.amis.utils.exception.PogamutIOException
Start the process. Throws PogamutIOException if it fails to start the JVM.

It is wise to observe the state of the getRunning() flag to obtain the state of the process (whether the process is running or is dead). Usage of FlagListener is advised, see ImmutableFlag.addListener(FlagListener).

Throws:
cz.cuni.amis.utils.exception.PogamutIOException

shutdown

protected void shutdown(boolean waitForEndThread)
Shutdowns the process and cleans-up everything, preparing the object to be start()ed again.

Parameters:
waitForEndThread - whether we're being called from the waitForEnd thread.

stop

public void stop()
Shuts down the process (if it is running, otherwise does nothing).


getBotProcess

public Process getBotProcess()
Process of the process (non-null if started).

Returns:

getRunning

public cz.cuni.amis.utils.flag.ImmutableFlag<Boolean> getRunning()
Flag of the state of the process. True == process is running (has been started using start()), False == process was not started or has been already terminated, either by itself or via stop().

Returns:

isRunning

public boolean isRunning()
Flag of the state of the process. True == process is running (has been started using start()), False == process was not started or has been already terminated, either by itself or via stop().

Returns:

getExitValue

public Integer getExitValue()
Returns exit value of the process. If null is returned == process is still running / has never finished before.

Returns:

isTimeout

public boolean isTimeout()
If ProcessExecutionConfig.getTimeout() is specified, this will indicate whether the started process has timed out or not.

Returns:

isFailed

public boolean isFailed(boolean report)
Tells whether the process has failed == timed out / exit value is not 0.

True == process has failed.

False == process is running or process has finished OK.

Parameters:
report - whether the reason of failure / success should be reported
Returns:

isFailed

public boolean isFailed()
Tells whether the process has failed == timed out / exit value is not 0.

True == process has failed.

False == process is running or process has finished OK.

Returns:


Copyright © 2015 AMIS research group, Faculty of Mathematics and Physics, Charles University in Prague, Czech Republic. All Rights Reserved.