[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.
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.
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, if script isn't.

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.

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.

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) R Gordon Ltd 2005 - Present