Class ScriptedTask<T>

java.lang.Object
org.jppf.node.protocol.AbstractTask<T>
org.jppf.node.protocol.ScriptedTask<T>
Type Parameters:
T - the type of result returned by this task.
All Implemented Interfaces:
Serializable, Runnable, Interruptibility, PositionalElement<Task<T>>, Task<T>

public class ScriptedTask<T> extends AbstractTask<T>
A task implementation which executes a script in a specified JSR 223 (javax.script APIs) script language.

In addition to the user-specified bindings, this task always provides to the script engine a reference to itself with the name 'jppfTask'.

A reusableId can be specified via the constructors, indicating that, if the script engine has that capability, compiled scripts will be stored and reused, to avoid compiling the same scripts repeatedly.

Author:
Laurent Cohen
See Also:
  • Field Details

    • scriptSpec

      protected ScriptDefinition scriptSpec
      Specfications for the script to execute.
  • Constructor Details

    • ScriptedTask

      protected ScriptedTask()
      Default constructor provided as a convenience for subclassing.
    • ScriptedTask

      public ScriptedTask(String language, String script, String reusableId, Map<String,Object> bindings) throws IllegalArgumentException
      Initialize this task with the specified script language, script provided as a string, and a set of variable bindings to be used in the scripts.
      Parameters:
      language - the JSR 223 script language to use.
      script - the script to execute from this task.
      reusableId - a unique identifier for the script, which allows reusing a compiled version if one has already been produced.
      bindings - a set of variables that will be available to the script.
      Throws:
      IllegalArgumentException - if language or script is null.
    • ScriptedTask

      public ScriptedTask(String language, Reader scriptReader, String reusableId, Map<String,Object> bindings) throws IllegalArgumentException, IOException
      Initialize this task with the specified script language, script provided from a Reader, and a set of variable bindings to be used in the scripts.
      Parameters:
      language - the JSR 223 script language to use.
      scriptReader - a reader form which to read the script source.
      reusableId - a unique identifier for the script, which allows reusing a compiled version if one has already been produced.
      bindings - a set of variables that will be available to the script.
      Throws:
      IllegalArgumentException - if language or script is null.
      IOException - if an I/O error occurs while reading the script source.
    • ScriptedTask

      public ScriptedTask(String language, File scriptFile, String reusableId, Map<String,Object> bindings) throws IllegalArgumentException, IOException
      Initialize this task with the specified script language, script provided as a file, and a set of variable bindings to be used in the scripts.
      Parameters:
      language - the JSR 223 script language to use.
      scriptFile - a file from which to read the script source.
      reusableId - a unique identifier for the script, which allows reusing a compiled version if one has already been produced.
      bindings - a set of variables that will be available to the script.
      Throws:
      IllegalArgumentException - if language or script is null.
      IOException - if an I/O error occurs while reading the script source.
  • Method Details

    • run

      public void run()
      Specified by:
      run in interface Runnable
      Overrides:
      run in class AbstractTask<T>
    • getLanguage

      public String getLanguage()
      Get the JSR 223 script language to use.
      Returns:
      the script language a s a script.
    • getScript

      public String getScript()
      Get the script to execute from this task.
      Returns:
      the script source as a string.
    • getReusableId

      public String getReusableId()
      Get the unique identifier for the script.
      Returns:
      the identifier as a string.
    • getBindings

      public Map<String,Object> getBindings()
      Get the user-defined variable bindings.
      Returns:
      a map of variable names to their value.
    • addBinding

      public ScriptedTask<T> addBinding(String name, Object value)
      Add the specified variable to the user-defined bindings.
      Parameters:
      name - the name of the variable binding to add.
      value - the value of the variable.
      Returns:
      this task, for method chaining.
    • removeBinding

      public Object removeBinding(String name)
      Remove the specified variable from the user-defined bindings.
      Parameters:
      name - the name of the variable binding to remove.
      Returns:
      the value of the variable, or null if the variable was not bound.