Class ScriptUtil

java.lang.Object
org.apache.ofbiz.base.util.ScriptUtil

public final class ScriptUtil extends 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 Details

    • WIDGET_CONTEXT_KEY

      public static final String WIDGET_CONTEXT_KEY
      The screen widget context map bindings key.
      See Also:
    • PARAMETERS_KEY

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

      public static final String RESULT_KEY
      The result map bindings key.
      See Also:
    • SCRIPT_HELPER_KEY

      public static final String SCRIPT_HELPER_KEY
      The ScriptHelper key.
      See Also:
    • SCRIPT_NAMES

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

    • compileScriptFile

      public static CompiledScript compileScriptFile(String filePath) throws ScriptException, 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:
      IllegalArgumentException
      ScriptException
      IOException
    • compileScriptString

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

      public static ScriptContext createScriptContext(Map<String,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 ScriptContext createScriptContext(Map<String,Object> context, Set<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:
    • evaluate

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

      public static Object executeScript(CompiledScript script, String functionName, ScriptContext scriptContext, Object[] args) throws ScriptException, 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:
      IllegalArgumentException
      ScriptException
      NoSuchMethodException
    • executeScript

      public static Object executeScript(String filePath, String functionName, Map<String,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:
      IllegalArgumentException
    • executeScript

      public static Object executeScript(String filePath, String functionName, Map<String,Object> context, 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:
      IllegalArgumentException
    • executeScript

      public static Object executeScript(String filePath, String functionName, ScriptContext scriptContext, Object[] args) throws ScriptException, NoSuchMethodException, 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:
      ScriptException
      NoSuchMethodException
      IOException
      IllegalArgumentException
    • parseScript

      public static Class<?> parseScript(String language, String script)