Class ScriptHandler

java.lang.Object
org.jppf.utils.configuration.ScriptHandler

public class ScriptHandler extends Object
Handles the properties whose value contains one or more expressions of the form:
my.property = $script:language:source_type{script_source}$
Where:
  • language is the script language to use, such as provided by the javax.script APIs. It defaults to "javascript"
  • source_type determines how to find the script, with possible values "inline", "file" or "url". It defaults to "inline"
  • script_source is either the script expression, or its location, depending on the value of source_type:
    • if source_type = inline, then script_source is the script itself.
      for example: my.prop = $script:javascript:inline{"hello" + "world"}$
    • if source_type = file, then script_source is a script file, looked up first in the file system, then in the classpath.
      For example: my.prop = $script:javascript:file{/home/me/myscript.js}$
    • if source_type = url, then script_source is a script loaded from a URL,
      for example: my.prop = $script:javascript:url{file:///home/me/myscript.js}$

If the language or source type are left unspecified, they will be assigned their default value. For instance the following patterns will all resolve in language = 'javascript' and source_type = 'inline' :

  • $script{ 2 + 3 }$
  • $script:{ 2 + 3 }$
  • $script::{ 2 + 3 }$
  • $script::inline{ 2 + 3 }$
  • $script:javascript{ 2 + 3 }$
  • $script:javascript:{ 2 + 3 }$

The default script language can be specified with the property 'jppf.script.default.language'. This property is always evaluated first, in case it is also expressed with script expressions (which can only be in javascript). If it is not specified explicitely, it will default to 'javascript'. For example, in the following:

 # using javascript expression
 jppf.script.default.language = $script{ 'groo' + 'vy' }
 # inline expression using default language 'groovy'
 my.property = $script{ return 2 + 3 }
The value of 'my.property' will evaluate to 5, resulting from the inline groovy expression 'return 2 + 3'.

The scripts are evaluated after all includes and variable substitutions have been resolved. This will allow the scripts to use a variable binding for the Properties (or TypedProperties object) being loaded.

Author:
Laurent Cohen
  • Field Details

    • SCRIPT_PATTERN

      public static final Pattern SCRIPT_PATTERN
      The regex pattern for identifying scripted property values.
  • Constructor Details

    • ScriptHandler

      public ScriptHandler()
      Default constructor.
  • Method Details

    • process

      public void process(Properties props)
      Evaluate all scripted expressions in all values of the specified properties object and replace the values with the results of the evaluations.
      Parameters:
      props - the properties object to evaluate.
    • evaluate

      public String evaluate(String name, String value, Map<String,Object> bindings)
      Evaluate the value of the specified property by replacing each script expression with its result.
      Parameters:
      name - the name of the property to evaluate.
      value - the value of the property to evaluate.
      bindings - variable bindings that can be used in the scripts.
      Returns:
      a string where all script expressions are replaced with their value.