ch.javasoft.job
Class ExecJobMonitor

java.lang.Object
  extended by ch.javasoft.job.ExecJobMonitor
All Implemented Interfaces:
JobMonitor<Void>

public class ExecJobMonitor
extends Object
implements JobMonitor<Void>

The ExecJobMonitor adds some functions specific for ExecJobs to the normal JobMonitor functions. For instance, input and output stream handling methods are available, such as piping the output of the job to some output stream, or piping the output of one job to the input of another job.


Constructor Summary
protected ExecJobMonitor(Process process)
           
 
Method Summary
protected  void closeStreams()
          Close the registered streams on termination
protected static ExecJobMonitor createForException(Throwable th)
           
 InputStream getErrorStream()
           
 InputStream getInputStream()
           
 JobResult<Void> getJobResult()
           
 OutputStream getOutputStream()
           
protected  Process getProcess()
           
 void interrupt()
          Interrupts the process or thread, or does nothing if JobMonitor.isRunning() returns false.
 boolean isRunning()
           
 void pipeErr(OutputStream err, boolean closeOnTerminate)
          Pipe the error stream of the current process to the given stream
 void pipeFrom(ExecJobMonitor src, boolean closeOnTerminate)
          Pipe the output and error stream of src to the standard input of the current process.
 void pipeFrom(InputStream in, boolean closeOnTerminate)
          Read from the given input stream and pipe it to the standard input of the current process.
 void pipeIn(InputStream in, boolean closeOnTerminate)
          Read from the given input stream and pipe it to the standard input of the current process.
 void pipeOut(OutputStream out, boolean closeOnTerminate)
          Pipe the output stream of the current process to the given stream
 void pipeTo(ExecJobMonitor dst, boolean closeOnTerminate)
          Pipe the output and error stream of the current process to the standard input of dst.
 void pipeTo(OutputStream out, OutputStream err, boolean closeOnTerminate)
          Pipe the output and error stream of the current process to the given streams.
 void pushInput(String... inputLines)
           Pushes the given lines to the process' output stream, i.e.
protected  void setResult(JobResult<Void> result)
           
 JobResult<Void> waitForResult()
          Blocks the current thread until the result is available.
<E extends Throwable>
void
waitForResultThrowException(Class<E> exClass)
          Like waitForResult(), but if the result was exceptional, an Exception is thrown.
 String waitForStdOutString()
          Like waitForResult(), but returning a string containing the stuff which was written to the standard output of the process.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExecJobMonitor

protected ExecJobMonitor(Process process)
Method Detail

createForException

protected static ExecJobMonitor createForException(Throwable th)

getProcess

protected Process getProcess()

setResult

protected void setResult(JobResult<Void> result)

interrupt

public void interrupt()
Description copied from interface: JobMonitor
Interrupts the process or thread, or does nothing if JobMonitor.isRunning() returns false. The JobMonitor.waitForResult() method receives an InterruptedException if the job status is affected by this call.

Specified by:
interrupt in interface JobMonitor<Void>

isRunning

public boolean isRunning()
Specified by:
isRunning in interface JobMonitor<Void>
Returns:
true if still processing, false if terminated (with or without exception)

getJobResult

public JobResult<Void> getJobResult()
Specified by:
getJobResult in interface JobMonitor<Void>
Returns:
the result, or null if JobMonitor.isRunning() returns true

waitForResult

public JobResult<Void> waitForResult()
                              throws InterruptedException
Description copied from interface: JobMonitor
Blocks the current thread until the result is available.

Specified by:
waitForResult in interface JobMonitor<Void>
Returns:
the result
Throws:
InterruptedException - if execution has been interrupted, e.g. by calling JobMonitor.interrupt()

waitForResultThrowException

public <E extends Throwable> void waitForResultThrowException(Class<E> exClass)
                                 throws E extends Throwable
Like waitForResult(), but if the result was exceptional, an Exception is thrown. The exception type to be thrown is defined with the exClass parameter, every exception not being of that type will be transformed into a RuntimeException if necessary.

Parameters:
exClass - The desired exception type, e.g. use RuntimeException.class that only runtime exceptions are thrown, or Throwable.class to rethrow every occuring exception unchanged.
Throws:
E - The exception of the specified type
RuntimeException - If an exception was thrown not being of type E and not being an Error. If it was already a runtime exception, it is rethrown, if not, it is nested in a runtime exception which is then thrown.
Error - If an error was thrown not being of type E
E extends Throwable

waitForStdOutString

public String waitForStdOutString()
                           throws ExecException,
                                  ExitValueException,
                                  StdErrNonEmptyException,
                                  InterruptedException
Like waitForResult(), but returning a string containing the stuff which was written to the standard output of the process. If the process also wrote to the standard error, a StdErrNonEmptyException is thrown. If the process exits with an exit code unequal to 0, an ExitValueException is thrown.

Returns:
the string containing what was written to the standard output of the process
Throws:
ExitValueException - if the process terminated with an exit value unequal to 0
StdErrNonEmptyException - if the process wrote to the standard error
InterruptedException - if waiting for termination of the process was interrupted, e.g. by calling interrupt()
ExecException - if any other exception occurred during the process execution

getErrorStream

public InputStream getErrorStream()
Returns:
the standard error of the process, as an input stream

getInputStream

public InputStream getInputStream()
Returns:
the standard output of the process, as an input stream

getOutputStream

public OutputStream getOutputStream()
Returns:
the standard input of the process, as an output stream

pushInput

public void pushInput(String... inputLines)

Pushes the given lines to the process' output stream, i.e. to the standard input. Each line is printed separately, followed by a system dependant new line separator. After pushing all lines, the standard input is closed to indicate end of input.

Pushing the lines is performed in a separate thread.

Parameters:
inputLines - The lines to push to the standard input

pipeFrom

public void pipeFrom(ExecJobMonitor src,
                     boolean closeOnTerminate)
Pipe the output and error stream of src to the standard input of the current process.

This is a convenience method, a call behaves exactly like calling pipeIn(InputStream, boolean) with ouptut and error stream of src.

Piping is performed in separate threads.

Parameters:
src - the source process to get the input from
closeOnTerminate - if true, standard output and error of src will be closed when the current process terminates

pipeTo

public void pipeTo(ExecJobMonitor dst,
                   boolean closeOnTerminate)
Pipe the output and error stream of the current process to the standard input of dst.

This is a convenience method, a call behaves exactly like calling pipeOut(OutputStream, boolean) and pipeErr(OutputStream, boolean) with the standard input of dst.

Piping is performed in separate threads.

Parameters:
dst - the target process to write the output to
closeOnTerminate - if true, standard input of dst will be closed when the current process terminates

pipeTo

public void pipeTo(OutputStream out,
                   OutputStream err,
                   boolean closeOnTerminate)
Pipe the output and error stream of the current process to the given streams.

This is a convenience method, a call behaves exactly like calling pipeOut(OutputStream, boolean) and pipeErr(OutputStream, boolean) with the respective streams.

Piping is performed in separate threads.

Parameters:
out - the target stream for standard output
err - the target stream for standard error
closeOnTerminate - if true, both target streams will be closed when the current process terminates

pipeFrom

public void pipeFrom(InputStream in,
                     boolean closeOnTerminate)
Read from the given input stream and pipe it to the standard input of the current process.

This is a convenience method, a call behaves exactly like calling pipeIn(InputStream, boolean).

Piping is performed in a separate thread.

Parameters:
in - the source stream to get the input from
closeOnTerminate - if true, the input stream in will be closed when the current process terminates

pipeOut

public void pipeOut(OutputStream out,
                    boolean closeOnTerminate)
Pipe the output stream of the current process to the given stream

Piping is performed in a separate thread.

Parameters:
out - the target stream for standard output
closeOnTerminate - if true, the output stream out will be closed when the current process terminates

pipeErr

public void pipeErr(OutputStream err,
                    boolean closeOnTerminate)
Pipe the error stream of the current process to the given stream

Piping is performed in a separate thread.

Parameters:
err - the target stream for standard error
closeOnTerminate - if true, the output stream err will be closed when the current process terminates

pipeIn

public void pipeIn(InputStream in,
                   boolean closeOnTerminate)
Read from the given input stream and pipe it to the standard input of the current process.

Piping is performed in a separate thread.

Parameters:
in - the source stream to get the input from
closeOnTerminate - if true, the input stream in will be closed when the current process terminates

closeStreams

protected void closeStreams()
Close the registered streams on termination