Class ScriptUtil


  • public final class ScriptUtil
    extends java.lang.Object
    Scripting utility methods. This is a facade class that is used to connect OFBiz to JSR-223 scripting engines.

    Important: To avoid a lot of Map copying, all methods that accept a context Map argument will pass that Map directly to the scripting engine. Any variables that are declared or modified in the script will affect the original Map. Client code that wishes to preserve the state of the Map argument should pass a copy of the Map.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String module  
      static java.lang.String PARAMETERS_KEY
      The service/servlet/request parameters map bindings key.
      static java.lang.String RESULT_KEY
      The result map bindings key.
      static java.lang.String SCRIPT_HELPER_KEY
      The ScriptHelper key.
      static java.util.Set<java.lang.String> SCRIPT_NAMES
      A set of script names - derived from the JSR-223 scripting engines.
      static java.lang.String WIDGET_CONTEXT_KEY
      The screen widget context map bindings key.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static javax.script.CompiledScript compileScriptFile​(java.lang.String filePath)
      Returns a compiled script.
      static javax.script.CompiledScript compileScriptString​(java.lang.String language, java.lang.String script)
      Returns a compiled script.
      static javax.script.ScriptContext createScriptContext​(java.util.Map<java.lang.String,​java.lang.Object> context)
      Returns a ScriptContext that contains the members of context.
      static javax.script.ScriptContext createScriptContext​(java.util.Map<java.lang.String,​java.lang.Object> context, java.util.Set<java.lang.String> protectedKeys)
      Returns a ScriptContext that contains the members of context.
      static ScriptHelper createScriptHelper​(javax.script.ScriptContext context)  
      static java.lang.Object evaluate​(java.lang.String language, java.lang.String script, java.lang.Class<?> scriptClass, java.util.Map<java.lang.String,​java.lang.Object> context)
      Executes a script String and returns the result.
      static java.lang.Object executeScript​(java.lang.String filePath, java.lang.String functionName, java.util.Map<java.lang.String,​java.lang.Object> context)
      Executes the script at the specified location and returns the result.
      static java.lang.Object executeScript​(java.lang.String filePath, java.lang.String functionName, java.util.Map<java.lang.String,​java.lang.Object> context, java.lang.Object[] args)
      Executes the script at the specified location and returns the result.
      static java.lang.Object executeScript​(java.lang.String filePath, java.lang.String functionName, javax.script.ScriptContext scriptContext, java.lang.Object[] args)
      Executes the script at the specified location and returns the result.
      static java.lang.Object executeScript​(javax.script.CompiledScript script, java.lang.String functionName, javax.script.ScriptContext scriptContext, java.lang.Object[] args)
      Executes a compiled script and returns the result.
      static java.lang.Class<?> parseScript​(java.lang.String language, java.lang.String script)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • module

        public static final java.lang.String module
      • WIDGET_CONTEXT_KEY

        public static final java.lang.String WIDGET_CONTEXT_KEY
        The screen widget context map bindings key.
        See Also:
        Constant Field Values
      • PARAMETERS_KEY

        public static final java.lang.String PARAMETERS_KEY
        The service/servlet/request parameters map bindings key.
        See Also:
        Constant Field Values
      • RESULT_KEY

        public static final java.lang.String RESULT_KEY
        The result map bindings key.
        See Also:
        Constant Field Values
      • SCRIPT_HELPER_KEY

        public static final java.lang.String SCRIPT_HELPER_KEY
        The ScriptHelper key.
        See Also:
        Constant Field Values
      • SCRIPT_NAMES

        public static final java.util.Set<java.lang.String> SCRIPT_NAMES
        A set of script names - derived from the JSR-223 scripting engines.
    • Method Detail

      • compileScriptFile

        public static javax.script.CompiledScript compileScriptFile​(java.lang.String filePath)
                                                             throws javax.script.ScriptException,
                                                                    java.io.IOException
        Returns a compiled script.
        Parameters:
        filePath - Script path and file name.
        Returns:
        The compiled script, or null if the script engine does not support compilation.
        Throws:
        java.lang.IllegalArgumentException
        javax.script.ScriptException
        java.io.IOException
      • compileScriptString

        public static javax.script.CompiledScript compileScriptString​(java.lang.String language,
                                                                      java.lang.String script)
                                                               throws javax.script.ScriptException
        Returns a compiled script.
        Parameters:
        language -
        script -
        Returns:
        The compiled script, or null if the script engine does not support compilation.
        Throws:
        java.lang.IllegalArgumentException
        javax.script.ScriptException
      • createScriptContext

        public static javax.script.ScriptContext createScriptContext​(java.util.Map<java.lang.String,​java.lang.Object> context)
        Returns a ScriptContext that contains the members of context.

        If a CompiledScript instance is to be shared by multiple threads, then each thread must create its own ScriptContext and pass it to the CompiledScript eval method.

        Parameters:
        context -
        Returns:
      • createScriptContext

        public static javax.script.ScriptContext createScriptContext​(java.util.Map<java.lang.String,​java.lang.Object> context,
                                                                     java.util.Set<java.lang.String> protectedKeys)
        Returns a ScriptContext that contains the members of context.

        If a CompiledScript instance is to be shared by multiple threads, then each thread must create its own ScriptContext and pass it to the CompiledScript eval method.

        Parameters:
        context -
        protectedKeys -
        Returns:
      • createScriptHelper

        public static ScriptHelper createScriptHelper​(javax.script.ScriptContext context)
      • evaluate

        public static java.lang.Object evaluate​(java.lang.String language,
                                                java.lang.String script,
                                                java.lang.Class<?> scriptClass,
                                                java.util.Map<java.lang.String,​java.lang.Object> context)
                                         throws java.lang.Exception
        Executes a script String and returns the result.
        Parameters:
        language -
        script -
        scriptClass -
        context -
        Returns:
        The script result.
        Throws:
        java.lang.Exception
      • executeScript

        public static java.lang.Object executeScript​(javax.script.CompiledScript script,
                                                     java.lang.String functionName,
                                                     javax.script.ScriptContext scriptContext,
                                                     java.lang.Object[] args)
                                              throws javax.script.ScriptException,
                                                     java.lang.NoSuchMethodException
        Executes a compiled script and returns the result.
        Parameters:
        script - Compiled script.
        functionName - Optional function or method to invoke.
        scriptContext - Script execution context.
        Returns:
        The script result.
        Throws:
        java.lang.IllegalArgumentException
        javax.script.ScriptException
        java.lang.NoSuchMethodException
      • executeScript

        public static java.lang.Object executeScript​(java.lang.String filePath,
                                                     java.lang.String functionName,
                                                     java.util.Map<java.lang.String,​java.lang.Object> context)
        Executes the script at the specified location and returns the result.
        Parameters:
        filePath - Script path and file name.
        functionName - Optional function or method to invoke.
        context - Script execution context.
        Returns:
        The script result.
        Throws:
        java.lang.IllegalArgumentException
      • executeScript

        public static java.lang.Object executeScript​(java.lang.String filePath,
                                                     java.lang.String functionName,
                                                     java.util.Map<java.lang.String,​java.lang.Object> context,
                                                     java.lang.Object[] args)
        Executes the script at the specified location and returns the result.
        Parameters:
        filePath - Script path and file name.
        functionName - Optional function or method to invoke.
        context - Script execution context.
        args - Function/method arguments.
        Returns:
        The script result.
        Throws:
        java.lang.IllegalArgumentException
      • executeScript

        public static java.lang.Object executeScript​(java.lang.String filePath,
                                                     java.lang.String functionName,
                                                     javax.script.ScriptContext scriptContext,
                                                     java.lang.Object[] args)
                                              throws javax.script.ScriptException,
                                                     java.lang.NoSuchMethodException,
                                                     java.io.IOException
        Executes the script at the specified location and returns the result.
        Parameters:
        filePath - Script path and file name.
        functionName - Optional function or method to invoke.
        scriptContext - Script execution context.
        args - Function/method arguments.
        Returns:
        The script result.
        Throws:
        javax.script.ScriptException
        java.lang.NoSuchMethodException
        java.io.IOException
        java.lang.IllegalArgumentException
      • parseScript

        public static java.lang.Class<?> parseScript​(java.lang.String language,
                                                     java.lang.String script)