Interface ScriptEvaluator

    • Method Detail

      • evaluateScript

        <T> T evaluateScript​(ScriptObject script,
                             Bindings bindings)
                      throws ScriptException
        Evaluates the given script object using an appropriate script engine for the language of the script. Any bindings associated with the script will be passed to the engine when the script is evaluated.

        There are three scope levels at which variables can be set:

        1. Global variables are set using the bindVariableInGlobalScope(String, Object) method, which makes the objects visible to all script engines. This is most appropriate for stateless API objects such as HTTP clients and other basic functionality.
        2. Script variables are set in the bindings attached to a ScriptObject.
        3. Parameter variables are set by the bindings parameter to this method.
        The three scopes above are listed in reverse order of precedence. If a variable of a given name exists in all three scopes, then the evaluator will use the parameter variable, overriding (hiding) the same-named variable in the other scopes. Likewise, script variables will hide any same-named variables in the global scope.
        Type Parameters:
        T - the type of result expected from the script.
        Parameters:
        script - the script to evaluate.
        bindings - any additional variable bindings to set before running the script.
        Returns:
        the result of evaluating the script, or null if no result produced.
        Throws:
        ScriptException - if an error occurs evaluating the script.
        ClassCastException - if the result is not of the expected type.
        IllegalStateException - if no scripting engine can be located for the given scripting language.
      • bindVariableInGlobalScope

        void bindVariableInGlobalScope​(String name,
                                       Object object)
        Binds the given object to the given variable name in the global scope of all scripts evaluated via this script evaluator. This is intended to be used to bind common API components such as HTTP clients etc that should be available to all scripts. For per-script state to initialise, use the bindings element on the script object itself.
        Parameters:
        name - the name of the variable to bind the object to.
        object - the object to bind into the global scope of all scripts.
        Throws:
        NullPointerException - if either name or object is empty.
        IllegalArgumentException - if the name is an empty string.