org.oddjob.script
Class ScriptJob

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.script.ScriptJob
All Implemented Interfaces:
Serializable, Runnable, ArooaContextAware, ArooaSessionAware, Forceable, PropertyChangeNotifier, Iconic, LogEnabled, Resetable, Stateful

public class ScriptJob
extends SerializableJob

Description

Execute a script. The script can be in any language that supports the Java Scripting Framework.

The named beans property allow values to be passed to and from the script.

Example

Hello World.
<oddjob>
    <job>
        <script id="s" language="JavaScript">
            <input>
                <buffer>print ("hello world\n");</buffer>
            </input>
        </script>
    </job>
</oddjob>

Example

Variables from and to Oddjob.
<oddjob>
 <job> 
 <sequential>
 <jobs>
  <script id="s" language="JavaScript">
   <input>
    <buffer>
var snack = fruit;
    </buffer>
   </input>
   <beans>
    <value key="fruit" value="apple"/>
   </beans>
  </script>
  <echo id="e">${s.variables(fruit)}</echo>
 </jobs>
 </sequential>
 </job> 
</oddjob>

Example

Using a script to set a property on a Job elsewhere in Oddjob.
<oddjob>
 <job>
  <sequential>
   <jobs>
    <script id="s" language="JavaScript">
     <input>
      <buffer>
vars.set('today', new java.util.Date());
      </buffer>
     </input>
     <beans>
      <value key="vars" value="${v}"/>
     </beans>
    </script>
    <variables id="v">
     <formattedToday>
      <format date="${v.today}" format="yyyyMMdd"/>
     </formattedToday>
    </variables>
   </jobs>
  </sequential>
 </job>
</oddjob>

Example

Invoking a script to provide a substring function.
<oddjob>
    <job>
        <sequential>
            <jobs>
                <script id="substr" language="JavaScript">
                    <input>
                        <buffer>function substr(string, from, to) {
    return string.substring(from, to);
}</buffer>
                    </input>
                </script>
                <properties id="properties">
                    <values>
                        <value key="text.before" value="Apples and Oranges"/>
                        <invoke function="substr" key="text.after">
                            <parameters>
                                <value value="${text.before}"/>
                                <value value="0"/>
                                <value value="6"/>
                            </parameters>
                            <source>
                                <value value="${substr.invocable}"/>
                            </source>
                        </invoke>
                    </values>
                </properties>
            </jobs>
        </sequential>
    </job>
</oddjob>

Example

Setting the script job to not complete.
<oddjob>
  <job>
    <script language="JavaScript" resultVariable="result" resultForState="true">
      <input>
        <buffer>
var result = 1;
        </buffer>
      </input>
    </script>
  </job>
</oddjob>
See Also:
Serialized Form
Author:
Rob Gordon - Based on the original from Ant.

Field Summary
 
Fields inherited from class org.oddjob.framework.SimpleJob
stateHandler, stop
 
Fields inherited from class org.oddjob.framework.BaseComponent
iconHelper
 
Constructor Summary
ScriptJob()
           
 
Method Summary
protected  int execute()
          Execute this job.
 Object getBeans(String name)
          Get the named bean.
 ClassLoader getClassLoader()
           
 InputStream getInput()
          Get the input.
 Invocable getInvocable()
           
 String getLanguage()
          Get the language.
 Object getResult()
           
 String getResultVariable()
           
 Object getVariables(String key)
           
 boolean isResultForState()
           
 void setBeans(String name, Object value)
          Add a named bean.
 void setClassLoader(ClassLoader classLoader)
           
 void setInput(InputStream input)
          Set the input.
 void setLanguage(String language)
          Defines the language (required).
 void setResultForState(boolean resultForState)
           
 void setResultVariable(String resultVariable)
           
 
Methods inherited from class org.oddjob.framework.SimpleJob
fireDestroyedState, force, getStateChanger, hardReset, onDestroy, onReset, onStop, 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.Stateful
addStateListener, lastStateEvent, removeStateListener
 

Constructor Detail

ScriptJob

public ScriptJob()
Method Detail

execute

protected int execute()
               throws IOException
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:
IOException

setLanguage

public void setLanguage(String language)
Defines the language (required).

Parameters:
language - the scripting language name for the script.

getLanguage

public String getLanguage()
Get the language.

Returns:
The language.

getBeans

public Object getBeans(String name)
Get the named bean.

Parameters:
name - The name of the bean
Returns:
The bean or null if it doesn't exist.

setBeans

public void setBeans(String name,
                     Object value)
Add a named bean.

Parameters:
name - The name of the bean.
value - The bean.

getInput

public InputStream getInput()
Get the input.

Returns:
The input.

setInput

public void setInput(InputStream input)
Set the input.

Parameters:
input - The input.

getInvocable

public Invocable getInvocable()

getVariables

public Object getVariables(String key)

Property: variables

Description: Provide access to variables declared within the script.


getResultVariable

public String getResultVariable()

setResultVariable

public void setResultVariable(String resultVariable)

isResultForState

public boolean isResultForState()

setResultForState

public void setResultForState(boolean resultForState)

getResult

public Object getResult()

getClassLoader

public ClassLoader getClassLoader()

setClassLoader

@Inject
public void setClassLoader(ClassLoader classLoader)