Class ScriptJob

All Implemented Interfaces:
Serializable, Runnable, ArooaContextAware, ArooaSessionAware, Forceable, PropertyChangeNotifier, Iconic, ConsoleOwner, LogEnabled, Resettable, Stateful

public class ScriptJob extends SerializableJob implements ConsoleOwner
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 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.

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>
  • Constructor Details

    • ScriptJob

      public ScriptJob()
  • Method Details

    • 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
    • _execute

      protected int _execute() throws IOException
      Throws:
      IOException
    • onReset

      protected void onReset()
      Description copied from class: SimpleJob
      Allow sub classes to do something on reset.
      Overrides:
      onReset in class SimpleJob
    • consoleLog

      public LogArchive consoleLog()
      Description copied from interface: ConsoleOwner
      Provide the console archive.
      Specified by:
      consoleLog in interface ConsoleOwner
      Returns:
      The archive. Must not be null.
    • 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.
    • getBind

      public Object getBind(String name)
      Get a binding.
      Parameters:
      name - The name of the binding
      Returns:
      The bean or null if it doesn't exist.
    • setBind

      public void setBind(String name, Object value)
      Add a binding.
      Parameters:
      name - The name of the binding.
      value - The thing to be bound.
    • getBeans

      @Deprecated public Object getBeans(String name)
      Deprecated.
      Get the named bean.
      Parameters:
      name - The name of the bean
      Returns:
      The bean or null if it doesn't exist.
    • setBeans

      @Deprecated public void setBeans(String name, Object value)
      Deprecated.
      Parameters:
      name - The name of the bean.
      value - The bean.
      Reference Property:

      Description

      Deprecated. Use bind instead.
    • isBindSession

      public boolean isBindSession()
    • setBindSession

      public void setBindSession(boolean bindSession)
    • getExport

      public String getExport(String name)
      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

      public void setExport(String name, String value)
      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

      public InputStream getInput()
      Get the input.
      Returns:
      The input.
    • setInput

      public void setInput(InputStream input)
      Set the input.
      Parameters:
      input - The input.
    • getScript

      public String getScript()
    • setScript

      public void setScript(String script)
    • getInvocable

      public Invocable getInvocable()
    • getFunction

      public Function<Object,Object> getFunction(String name)
      Parameters:
      name - The name of the script function.
      Returns:
      A Java Function.
      Reference Property:

      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.
      Required:
      Read Only.
    • isRedirectStderr

      public boolean isRedirectStderr()
    • setRedirectStderr

      public void setRedirectStderr(boolean redirectStderr)
    • getStdin

      public InputStream getStdin()
    • setStdin

      public void setStdin(InputStream stdin)
    • getStdout

      public OutputStream getStdout()
    • setStdout

      public void setStdout(OutputStream stdout)
    • getStderr

      public OutputStream getStderr()
    • setStderr

      public void setStderr(OutputStream stderr)
    • getVariables

      @Deprecated public Object getVariables(String key)
      Deprecated.

      Description

      Deprecated. Use variable instead.
      Required:
      Read Only.
    • getVariable

      public Object getVariable(String key)

      Description

      Provide access to variables declared within the script.
      Required:
      Read Only.
    • 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)