Package org.apache.ofbiz.base.util
Class ScriptUtil
- java.lang.Object
-
- org.apache.ofbiz.base.util.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 contextMap
argument will pass thatMap
directly to the scripting engine. Any variables that are declared or modified in the script will affect the originalMap
. Client code that wishes to preserve the state of theMap
argument should pass a copy of theMap
.
-
-
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
TheScriptHelper
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 aScriptContext
that contains the members ofcontext
.static javax.script.ScriptContext
createScriptContext(java.util.Map<java.lang.String,java.lang.Object> context, java.util.Set<java.lang.String> protectedKeys)
Returns aScriptContext
that contains the members ofcontext
.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 scriptString
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)
-
-
-
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
TheScriptHelper
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 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 javax.script.ScriptContext createScriptContext(java.util.Map<java.lang.String,java.lang.Object> context, java.util.Set<java.lang.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:
-
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 scriptString
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)
-
-