Package org.oddjob.script
Class ScriptJob
- All Implemented Interfaces:
Serializable,Runnable,ArooaContextAware,ArooaSessionAware,Forceable,PropertyChangeNotifier,Iconic,ConsoleOwner,LogEnabled,Resettable,Stateful
- Author:
- Rob Gordon - Based on the original from Ant.
- See Also:
Description
Execute a script. The script can be in any language that supports the Java Scripting Framework. The Oddjob distribution comes packaged with Nashorn. All examples here use Nashorn.Configuring a Script
The namedbind property allows values to be passed to a script
so that they are available as variables.
Setting the property bindSession binds all Oddjob's beans
so that they are available as variables. This is equivalent to the variables
available #{} expressions.
Script Results
Variables defined within a script may be accessed in several ways. Thevariables mapped property may be used to access the variable
by name. The {@export } property will export a variable to the oddjob
session. The {@exportAll } property will export all variables into the
oddjob session. The result of a function can be accessed with the
result property. Some scripts don't return a result, in which case
the resultVariable property can be used to take the result from
a variable. If the resultForState property is true then the
result will be used to set the Completion State from the variable, 0 for
Success, otherwise Failure.
Input and Output
Script input and output can be configured using the propertiesstdin
stdout, stderr and redirectStderr.
In Oddjob Explorer, a scripts output is captured in the console tab for
that job.Example
Hello World.
<oddjob>
<job>
<script id="script" language="JavaScript">print ("Hello, World!");</script>
</job>
</oddjob>
Variables from and to Oddjob.
<oddjob>
<job>
<sequential>
<jobs>
<variables id="vars">
<fruit>
<value value="Apple"/>
</fruit>
</variables>
<script id="script" language="JavaScript">
<bind>
<value key="fruit" value="${vars.fruit}"/>
</bind>var snack = fruit;
</script>
<echo id="echo">snack=${script.variable(snack)}
fruit=${script.variable(fruit)}</echo>
</jobs>
</sequential>
</job>
</oddjob>
Binding and exporting to Oddjob's session. The Variables
job uses Identify to insert the 'Apple' into Oddjob's session with the
name fruit. bindSession causes this to be available to script,
where it is assigned to the 'snack' variable. The script also defines
an add function. The exportAll property causes both these to
be exported to Oddjob. The two echo jobs show how these variables are
now available in Oddjob's session. When the script job is reset, the
variables are removed from the session.
<oddjob>
<job>
<sequential>
<jobs>
<variables id="vars">
<fruitVar>
<identify id="fruit">
<value>
<value value="#{'Apple'}"/>
</value>
</identify>
</fruitVar>
</variables>
<script bindSession="true" exportAll="true" id="script" language="JavaScript">function add(x, y) {
return x + y
}
snack = fruit;
</script>
<echo id="echoSnack" name="Snack Is">$${snack} is '${snack}' and ##{snack} is '#{typeof snack == 'undefined' ? 'undefined' : snack}'</echo>
<echo id="echoSum" name="Add Numbers">##add(2, 3) is #{add(2, 3)}</echo>
</jobs>
</sequential>
</job>
</oddjob>
Using a script to set a property on a Job elsewhere in Oddjob.
<oddjob>
<job>
<sequential>
<jobs>
<script id="s" language="JavaScript">
<bind>
<value key="vars" value="${v}"/>
</bind>vars.set('today', new java.util.Date());
</script>
<variables id="v">
<formattedToday>
<format date="${v.today}" format="yyyyMMdd"/>
</formattedToday>
</variables>
</jobs>
</sequential>
</job>
</oddjob>
Invoking a script to provide a substring function.
<oddjob>
<job>
<sequential>
<jobs>
<script id="substr" language="JavaScript">function substr(string, from, to) {
return string.substring(from, to);
}</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>
Setting the script job to not complete. The result of the script
is used to set the state. 0 for Success, anything else for Incomplete.
<oddjob>
<job>
<script language="JavaScript" resultForState="true" resultVariable="result">var result = 1;</script>
</job>
</oddjob>
Defining Java Functions in JavaScript.
<oddjob>
<job>
<sequential>
<jobs>
<script id="funcs">function addTwo(x) { return new java.lang.Integer(x + 2)}
function multiplyByTwo(x) { return new java.lang.Integer(x * 2)}
</script>
<echo id="add">#{funcs.getFunction('addTwo').apply(5)}</echo>
<echo id="multiply">#{funcs.getFunction('multiplyByTwo').apply(3)}</echo>
</jobs>
</sequential>
</job>
</oddjob>
-
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected int_execute()Provide the console archive.protected intexecute()Execute this job.Deprecated.Get a binding.Get the name of the export binding.getFunction(String name) getInput()Get the input.Get the language.getStdin()getVariable(String key) getVariables(String key) Deprecated.booleanbooleanbooleanbooleanprotected voidonReset()Allow sub classes to do something on reset.voidDeprecated.voidAdd a binding.voidsetBindSession(boolean bindSession) voidsetClassLoader(ClassLoader classLoader) voidExport a binding of the first name into the session with the second name.voidsetExportAll(boolean exportAll) voidsetInput(InputStream input) Set the input.voidsetLanguage(String language) Defines the language (required).voidsetRedirectStderr(boolean redirectStderr) voidsetResultForState(boolean resultForState) voidsetResultVariable(String resultVariable) voidvoidsetStderr(OutputStream stderr) voidsetStdin(InputStream stdin) voidsetStdout(OutputStream stdout) Methods inherited from class org.oddjob.framework.extend.SimpleJob
fireDestroyedState, force, getStateChanger, hardReset, iconHelper, isStop, onDestroy, onStop, run, sleep, softReset, stateHandler, stopMethods inherited from class org.oddjob.framework.extend.BasePrimary
configure, getName, logger, logger, loggerName, save, setName, toStringMethods inherited from class org.oddjob.framework.extend.BaseComponent
addIconListener, addPropertyChangeListener, addStateListener, configure, destroy, firePropertyChange, getArooaSession, iconForId, initialise, lastStateEvent, onConfigured, onInitialised, removeIconListener, removePropertyChangeListener, removeStateListener, save, setArooaContext, setArooaSessionMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.oddjob.Stateful
addStateListener, lastStateEvent, removeStateListener
-
Constructor Details
-
ScriptJob
public ScriptJob()
-
-
Method Details
-
execute
Description copied from class:SimpleJobExecute this job.- Specified by:
executein classSimpleJob- Returns:
- 0 if the job is complete, anything else otherwise.
- Throws:
IOException
-
_execute
- Throws:
IOException
-
onReset
protected void onReset()Description copied from class:SimpleJobAllow sub classes to do something on reset. -
consoleLog
Description copied from interface:ConsoleOwnerProvide the console archive.- Specified by:
consoleLogin interfaceConsoleOwner- Returns:
- The archive. Must not be null.
-
setLanguage
Defines the language (required).- Parameters:
language- the scripting language name for the script.
-
getLanguage
Get the language.- Returns:
- The language.
-
getBind
Get a binding.- Parameters:
name- The name of the binding- Returns:
- The bean or null if it doesn't exist.
-
setBind
Add a binding.- Parameters:
name- The name of the binding.value- The thing to be bound.
-
getBeans
Deprecated.Get the named bean.- Parameters:
name- The name of the bean- Returns:
- The bean or null if it doesn't exist.
-
setBeans
Deprecated.- Parameters:
name- The name of the bean.value- The bean.- Reference Property:
Description
Deprecated. Usebindinstead. -
isBindSession
public boolean isBindSession() -
setBindSession
public void setBindSession(boolean bindSession) -
getExport
Get the name of the export binding.- Parameters:
name- The name of the export binding- Returns:
- The name or null if it doesn't exist.
-
setExport
Export a binding of the first name into the session with the second name.- Parameters:
name- The name of the binding.value- The name of the export.
-
isExportAll
public boolean isExportAll() -
setExportAll
public void setExportAll(boolean exportAll) -
getInput
Get the input.- Returns:
- The input.
-
setInput
Set the input.- Parameters:
input- The input.
-
getScript
-
setScript
-
getInvocable
-
getFunction
- Parameters:
name- The name of the script function.- Returns:
- A Java Function.
- Reference Property:
- Required:
- Read Only.
Description
Adapts a script function type to a Java Function. This isn't really necessary with Nashorn as there is a built in Conversion to do this. May be useful for other script engines. -
isRedirectStderr
public boolean isRedirectStderr() -
setRedirectStderr
public void setRedirectStderr(boolean redirectStderr) -
getStdin
-
setStdin
-
getStdout
-
setStdout
-
getStderr
-
setStderr
-
getVariables
Deprecated.- Required:
- Read Only.
Description
Deprecated. Usevariableinstead. -
getVariable
- Required:
- Read Only.
Description
Provide access to variables declared within the script. -
getResultVariable
-
setResultVariable
-
isResultForState
public boolean isResultForState() -
setResultForState
public void setResultForState(boolean resultForState) -
getResult
-
getClassLoader
-
setClassLoader
-