[Index]

script


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 named bind 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. The variables 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 properties stdin stdout, stderr and redirectStderr. In Oddjob Explorer, a scripts output is captured in the console tab for that job.

Property Summary

beans Deprecated.
bind A named bean which is made available to the script.
bindSession Make all Oddjob's components available to the script as bindings.
classLoader ClassLoader to load the Script Engine.
export Export bindings from the engine into the session using the given name.
exportAll Export all bindings from the engine into Oddjob's session.
function Adapts a script function type to a Java Function.
input The script provided as input from file or buffer etc.
invocable Allow a scripted function to be evaluated from elsewhere in Oddjob.
language The name of the language the script is in.
name A name, can be any text.
redirectStderr Combine stdin and stderr.
result The result of executing the script or the script variable chosen as the result variable with the resultVariable property.
resultForState If true then use the result to determine the completion state of the job.
resultVariable The variable in the script that will be used to provide the result.
script The script provided as text.
stderr An output to where stderr of the script will be written.
stdin An input stream which will act as stdin for the script.
stdout An output to where stdout for the script will be written.
stop This flag is set by the stop method and should be examined by any Stoppable jobs in their processing loops.
variable Provide access to variables declared within the script.
variables Deprecated.

Example Summary

Example 1 Hello World.
Example 2 Variables from and to Oddjob.
Example 3 Binding and exporting to Oddjob's session.
Example 4 Using a script to set a property on a Job elsewhere in Oddjob.
Example 5 Invoking a script to provide a substring function.
Example 6 Setting the script job to not complete.
Example 7 Defining Java Functions in JavaScript.

Property Detail

beans

Configured ByELEMENT
AccessREAD_WRITE

Deprecated. Use bind instead.

bind

Configured ByELEMENT
AccessREAD_WRITE
RequiredNo.

A named bean which is made available to the script.

bindSession

Configured ByATTRIBUTE
AccessREAD_WRITE
RequiredNo.

Make all Oddjob's components available to the script as bindings.

classLoader

Configured ByELEMENT
AccessREAD_WRITE
RequiredNo. Automatically set to the current Oddjob class loader.

ClassLoader to load the Script Engine.

export

Configured ByELEMENT
AccessREAD_WRITE
RequiredNo.

Export bindings from the engine into the session using the given name. The first entry is the name of the binding, the second is the name. Repeating the name is tedious so the '.' character can be used to specify the same name as the binding.

exportAll

Configured ByATTRIBUTE
AccessREAD_WRITE
RequiredNo.

Export all bindings from the engine into Oddjob's session.

function

AccessREAD_ONLY
RequiredRead Only.

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.

input

Configured ByELEMENT
AccessREAD_WRITE
RequiredYes, if script isn't.

The script provided as input from file or buffer etc.

invocable

AccessREAD_ONLY
RequiredRead only.

Allow a scripted function to be evaluated from elsewhere in Oddjob.

language

Configured ByATTRIBUTE
AccessREAD_WRITE
RequiredNo. Defaults to JavaScript.

The name of the language the script is in.

name

Configured ByATTRIBUTE
AccessREAD_WRITE
RequiredNo.

A name, can be any text.

redirectStderr

Configured ByATTRIBUTE
AccessREAD_WRITE
RequiredNo.

Combine stdin and stderr.

result

AccessREAD_ONLY

The result of executing the script or the script variable chosen as the result variable with the resultVariable property.

resultForState

Configured ByATTRIBUTE
AccessREAD_WRITE
RequiredNo, defaults to false.

If true then use the result to determine the completion state of the job. If the result is not a number this property will have no affect. If the result is a number and 0 the job will COMPLETE, any other value and the job will be INCOMPLETE.

resultVariable

Configured ByATTRIBUTE
AccessREAD_WRITE
RequiredNo.

The variable in the script that will be used to provide the result. The property is designed for use with scripting languages who's execution does not produce a result. If, however the script does produce a result and this property is set, the variable will override the scripts return value.

script

Configured ByTEXT
AccessREAD_WRITE
RequiredYes, if input isn't.

The script provided as text.

stderr

Configured ByELEMENT
AccessREAD_WRITE
RequiredNo, defaults to none.

An output to where stderr of the script will be written.

stdin

Configured ByELEMENT
AccessREAD_WRITE
RequiredNo, defaults to none.

An input stream which will act as stdin for the script.

stdout

Configured ByELEMENT
AccessREAD_WRITE
RequiredNo, defaults to none.

An output to where stdout for the script will be written.

stop

AccessREAD_ONLY
RequiredRead Only.

This flag is set by the stop method and should be examined by any Stoppable jobs in their processing loops.

variable

AccessREAD_ONLY
RequiredRead Only.

Provide access to variables declared within the script.

variables

AccessREAD_ONLY
RequiredRead Only.

Deprecated. Use variable instead.


Examples

Example 1

Hello World.

<oddjob>
    <job>
        <script id="script" language="JavaScript">print ("Hello, World!");</script>
    </job>
</oddjob>

Example 2

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>

Example 3

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>

Example 4

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>

Example 5

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>

Example 6

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>

Example 7

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>


(c) R Gordon Ltd 2005 - Present