[Index]

script


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.


Property Summary

beans A named bean which is made available to the script.
classLoader ClassLoader to load the Script Engine.
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.
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.
stop This flag is set by the stop method and should be examined by any Stoppable jobs in their processing loops.
variables Provide access to variables declared within the script.

Example Summary

Example 1 Hello World.
Example 2 Variables from and to Oddjob.
Example 3 Using a script to set a property on a Job elsewhere in Oddjob.
Example 4 Invoking a script to provide a substring function.
Example 5 Setting the script job to not complete.

Property Detail

beans

Configured ByELEMENT
AccessREAD_WRITE
RequiredNo.

A named bean which is made available to the script.

classLoader

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

ClassLoader to load the Script Engine.

input

Configured ByELEMENT
AccessREAD_WRITE
RequiredYes.

The script provided as input from file or buffer etc.

invocable

AccessREAD_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.

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.

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.

variables

AccessREAD_ONLY

Provide access to variables declared within the script.


Examples

Example 1

Hello World.

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

Example 2

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 3

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 4

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 5

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>


(c) Rob Gordon 2005 - 2017