org.oddjob.jobs
Class ExecJob

java.lang.Object
  extended by org.oddjob.framework.BaseComponent
      extended by org.oddjob.framework.BasePrimary
          extended by org.oddjob.framework.SimpleJob
              extended by org.oddjob.framework.SerializableJob
                  extended by org.oddjob.jobs.ExecJob
All Implemented Interfaces:
Serializable, Runnable, ArooaContextAware, ArooaSessionAware, Forceable, PropertyChangeNotifier, Iconic, ConsoleOwner, LogEnabled, Resetable, Stateful, Stoppable

public class ExecJob
extends SerializableJob
implements Stoppable, ConsoleOwner

Description

Execute an external program. This job will flag complete if the return state of the external program is 0, otherwise it will flag not complete.

Processes may behave differently on different operating systems - for instance stop doesn't always kill the process. Please see http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4109888 for additional information.

Example

A simple example.
<exec name="Batch Example">
cmd /C "${oddjob.dir}\bin\greeting.bat" Hello
</exec>
Oddjob will treat arguments in quotes as single program argument and allows them to be escaped with backslash. If this is too confusing it is sometimes easier to specify the command as individual arguments. The above is equivalent to:
<exec name="Batch Example">
    <args>
        <list>
            <values>
                <value value="cmd"/>
                <value value="/C"/>
                <value value="${oddjob.dir}\bin\greeting.bat"/>
                <value value="Hello"/>
            </values>
        </list>
    </args>
</exec>

Example

Using the existing environment with an additional environment variable.
<oddjob>
    <job>
        <exec name="Example With Environment" id="exec">
            <environment>
                <value key="ODDJOB_FILE" value="myfile.txt"/>
            </environment>
${platform.set.command}
        </exec>
    </job>
</oddjob>

Example

Capturing console output to a file. The output is Oddjob's command line help.
<oddjob id="this">
    <job>
        <exec redirectStderr="true">
            <stdout>
                <file file="${work.dir}/ExecOutput.log"/>
            </stdout>
            java -jar "${oddjob.run.jar}" -h
        </exec>
    </job>
</oddjob>

Example

Capturing console output to the logger. Note how the logger output can be defined with different log levels for stdout and sterr.
<oddjob id="this">
    <job>
        <exec>
            <stdout>
                <logout level="INFO"/>
            </stdout>
            <stderr>
                <logout level="WARN"/>
            </stderr>
            java -jar ${oddjob.run.jar} -f Missing.xml
        </exec>
    </job>
</oddjob>

Example

Using the output of one process as the input of another. Standard input for the first process is provided by a buffer. A second buffer captures the output of that process and passess it to the second process. The output of the second process is captured and sent to the console of the parent process.
<oddjob id="this">
    <job>
        <sequential>
            <jobs>
                <variables id="vars">
                    <ourBuffer>
                        <buffer/>
                    </ourBuffer>
                </variables>
                <exec>
                    <stdin>
                        <buffer>apples
oranges
pears
</buffer>
                    </stdin>
                    <stdout>
                        <value value="${vars.ourBuffer}"/>
                    </stdout>
java -jar "${oddjob.run.jar}" -f "${this.dir}/OddjobCat.xml"
        </exec>
                <exec id="exec">
                    <stdin>
                        <value value="${vars.ourBuffer}"/>
                    </stdin>
                    <stdout>
                        <stdout/>
                    </stdout>
java -jar "${oddjob.run.jar}" -f "${this.dir}/OddjobCat.xml"
        </exec>
            </jobs>
        </sequential>
    </job>
</oddjob>
See Also:
Serialized Form
Author:
Rob Gordon.

Field Summary
 
Fields inherited from class org.oddjob.framework.SimpleJob
stateHandler, stop
 
Fields inherited from class org.oddjob.framework.BaseComponent
iconHelper
 
Constructor Summary
ExecJob()
           
 
Method Summary
 ArooaTokenizer commandTokenizer()
          Provide the ArooaTokenizer to use for parsing commands.
 LogArchive consoleLog()
          Provide the console archive.
protected  int execute()
          Execute this job.
 String getCommand()
          Get the command.
 File getDir()
           
 String getEnvironment(String name)
           
 int getExitValue()
           
 OutputStream getStderr()
          Get the output stream for stderr.
 InputStream getStdin()
          Get the input stream for stdin.
 OutputStream getStdout()
          Get the output stream for stdout.
 boolean isNewEnvironment()
           
 boolean isRedirectStderr()
           
 void onStop()
          Allow sub classes to do something on stop.
 void setArgs(String[] args)
          Add an argument.
 void setCommand(String command)
          Set the command to run.
 void setDir(File dir)
          Set the working directory.
 void setEnvironment(String name, String value)
          Add an environment variable.
 void setNewEnvironment(boolean explicitEnvironment)
           
 void setRedirectStderr(boolean redirectErrorStream)
           
 void setStderr(OutputStream stderr)
          Set the output stream stderr from the process will be directed to.
 void setStdin(InputStream stdin)
          Set the input stream stdin for the process will be read from.
 void setStdout(OutputStream stdout)
          Set the output stream stdout from the process will be directed to.
 
Methods inherited from class org.oddjob.framework.SimpleJob
fireDestroyedState, force, getStateChanger, hardReset, onDestroy, onReset, run, sleep, softReset, stateHandler, stop
 
Methods inherited from class org.oddjob.framework.BasePrimary
configure, getName, logger, logger, loggerName, save, setName, toString
 
Methods inherited from class org.oddjob.framework.BaseComponent
addIconListener, addPropertyChangeListener, addStateListener, configure, destroy, firePropertyChange, getArooaSession, iconForId, initialise, lastStateEvent, onConfigured, onInitialised, removeIconListener, removePropertyChangeListener, removeStateListener, save, setArooaContext, setArooaSession
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.oddjob.Stoppable
stop
 
Methods inherited from interface org.oddjob.Stateful
addStateListener, lastStateEvent, removeStateListener
 

Constructor Detail

ExecJob

public ExecJob()
Method Detail

setArgs

public void setArgs(String[] args)
Add an argument.

Parameters:
arg - The argument.

setCommand

public void setCommand(String command)
Set the command to run.

Parameters:
command - The command.

getCommand

public String getCommand()
Get the command.

Returns:
The command.

setDir

public void setDir(File dir)
Set the working directory.

Parameters:
dir - The working directory.

setNewEnvironment

public void setNewEnvironment(boolean explicitEnvironment)

Property: newEnvironment

Description: Create a fresh/clean environment.

Required: No.


isNewEnvironment

public boolean isNewEnvironment()

setEnvironment

public void setEnvironment(String name,
                           String value)
Add an environment variable.

Parameters:
nvp - The name/value pair variable.

getEnvironment

public String getEnvironment(String name)

setRedirectStderr

public void setRedirectStderr(boolean redirectErrorStream)

Property: redirectStderr

Description: Redirect the standard error stream in standard output.

Required: No.


isRedirectStderr

public boolean isRedirectStderr()

setStdin

public void setStdin(InputStream stdin)
Set the input stream stdin for the process will be read from.

Parameters:
stdin - An InputStream.

getStdin

public InputStream getStdin()
Get the input stream for stdin. This will be null unless one has been provided.

Returns:
An InputStream or null.

setStdout

public void setStdout(OutputStream stdout)
Set the output stream stdout from the process will be directed to.

Parameters:
stdout - The output stream.

getStdout

public OutputStream getStdout()
Get the output stream for stdout. This will be null unless one has been provided.

Returns:
An OutputStream or null.

setStderr

public void setStderr(OutputStream stderr)
Set the output stream stderr from the process will be directed to.

Parameters:
stderr - The error stream.

getStderr

public OutputStream getStderr()
Get the output stream for stderr. This will be null unless one has been provided.

Returns:
An OutputStream or null.

commandTokenizer

public ArooaTokenizer commandTokenizer()
Provide the ArooaTokenizer to use for parsing commands.

Returns:
A tokenizer, never null.

execute

protected int execute()
               throws Exception
Description copied from class: SimpleJob
Execute this job.

Specified by:
execute in class SimpleJob
Returns:
0 if the job is complete, anything else otherwise.
Throws:
Exception - If the unexpected occurs.

onStop

public void onStop()
Description copied from class: SimpleJob
Allow sub classes to do something on stop.

Overrides:
onStop in class SimpleJob

getDir

public File getDir()
Returns:
Returns the dir.

getExitValue

public int getExitValue()

consoleLog

public LogArchive consoleLog()
Description copied from interface: ConsoleOwner
Provide the console archive.

Specified by:
consoleLog in interface ConsoleOwner
Returns:
The archive. Must not be null.