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

    • 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

    • 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)