Package org.apache.ofbiz.base.util
Class ScriptUtil
java.lang.Object
org.apache.ofbiz.base.util.ScriptUtil
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
Modifier and TypeFieldDescriptionstatic final String
The service/servlet/request parameters map bindings key.static final String
The result map bindings key.static final String
TheScriptHelper
key.A set of script names - derived from the JSR-223 scripting engines.static final String
The screen widget context map bindings key. -
Method Summary
Modifier and TypeMethodDescriptionstatic CompiledScript
compileScriptFile
(String filePath) Returns a compiled script.static CompiledScript
compileScriptString
(String language, String script) Returns a compiled script.static ScriptContext
createScriptContext
(Map<String, Object> context) Returns aScriptContext
that contains the members ofcontext
.static ScriptContext
Returns aScriptContext
that contains the members ofcontext
.static Object
Executes a scriptString
and returns the result.static Object
Executes the script at the specified location and returns the result.static Object
Executes the script at the specified location and returns the result.static Object
executeScript
(String filePath, String functionName, ScriptContext scriptContext, Object[] args) Executes the script at the specified location and returns the result.static Object
executeScript
(CompiledScript script, String functionName, ScriptContext scriptContext, Object[] args) Executes a compiled script and returns the result.static Class<?>
parseScript
(String language, String script)
-
Field Details
-
WIDGET_CONTEXT_KEY
The screen widget context map bindings key.- See Also:
-
PARAMETERS_KEY
The service/servlet/request parameters map bindings key.- See Also:
-
RESULT_KEY
The result map bindings key.- See Also:
-
SCRIPT_HELPER_KEY
TheScriptHelper
key.- See Also:
-
SCRIPT_NAMES
A set of script names - derived from the JSR-223 scripting engines.
-
-
Method Details
-
compileScriptFile
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
Returns aScriptContext
that contains the members ofcontext
.If a
CompiledScript
instance is to be shared by multiple threads, then each thread must create its ownScriptContext
and pass it to theCompiledScript
eval method.- Parameters:
context
-- Returns:
-
createScriptContext
public static ScriptContext createScriptContext(Map<String, Object> context, Set<String> protectedKeys) Returns aScriptContext
that contains the members ofcontext
.If a
CompiledScript
instance is to be shared by multiple threads, then each thread must create its ownScriptContext
and pass it to theCompiledScript
eval method.- Parameters:
context
-protectedKeys
-- Returns:
-
evaluate
public static Object evaluate(String language, String script, Class<?> scriptClass, Map<String, Object> context) throws ExceptionExecutes a scriptString
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
-