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
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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
-
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
-
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
-