Class JexlScriptEngine
- java.lang.Object
-
- javax.script.AbstractScriptEngine
-
- org.apache.commons.jexl3.scripting.JexlScriptEngine
-
- All Implemented Interfaces:
javax.script.Compilable
,javax.script.ScriptEngine
public class JexlScriptEngine extends javax.script.AbstractScriptEngine implements javax.script.Compilable
Implements the JEXL ScriptEngine for JSF-223.This implementation gives access to both ENGINE_SCOPE and GLOBAL_SCOPE bindings. When a JEXL script accesses a variable for read or write, this implementation checks first ENGINE and then GLOBAL scope. The first one found is used. If no variable is found, and the JEXL script is writing to a variable, it will be stored in the ENGINE scope.
The implementation also creates the "JEXL" script object as an instance of the class
See Java Scripting API Javadoc.JexlScriptEngine.JexlScriptObject
for access to utility methods and variables.- Since:
- 2.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
JexlScriptEngine.FactorySingletonHolder
Holds singleton JexlScriptEngineFactory (IODH).private class
JexlScriptEngine.JexlCompiledScript
Wrapper to help convert a JEXL JexlScript into a JSR-223 CompiledScript.private class
JexlScriptEngine.JexlContextWrapper
Wrapper to help convert a JSR-223 ScriptContext into a JexlContext.class
JexlScriptEngine.JexlScriptObject
Implements engine and engine context properties for use by JEXL scripts.
-
Field Summary
Fields Modifier and Type Field Description (package private) static int
CACHE_SIZE
The shared expression cache size.static java.lang.String
CONTEXT_KEY
Reserved key for context (mandated by JSR-223).private static java.lang.ref.Reference<JexlEngine>
ENGINE
The shared engine instance.static java.lang.String
JEXL_OBJECT_KEY
Reserved key for JexlScriptObject.(package private) JexlEngine
jexlEngine
The JEXL EL engine.(package private) JexlScriptEngine.JexlScriptObject
jexlObject
The JexlScriptObject instance.(package private) static org.apache.commons.logging.Log
LOG
The logger.(package private) javax.script.ScriptEngineFactory
parentFactory
The factory which created this instance.private static JexlPermissions
PERMISSIONS
The permissions used to create the script engine.
-
Constructor Summary
Constructors Constructor Description JexlScriptEngine()
Default constructor.JexlScriptEngine(javax.script.ScriptEngineFactory factory)
Create a scripting engine using the supplied factory.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description javax.script.CompiledScript
compile(java.io.Reader script)
javax.script.CompiledScript
compile(java.lang.String script)
javax.script.Bindings
createBindings()
java.lang.Object
eval(java.io.Reader reader, javax.script.ScriptContext context)
java.lang.Object
eval(java.lang.String script, javax.script.ScriptContext context)
private static JexlEngine
getEngine()
javax.script.ScriptEngineFactory
getFactory()
private static java.lang.String
readerToString(java.io.Reader scriptReader)
Read from a reader into a local buffer and return a String with the contents of the reader.(package private) static javax.script.ScriptException
scriptException(java.lang.Exception e)
static void
setInstance(JexlEngine engine)
Sets the shared instance used for the script engine.static void
setPermissions(JexlPermissions permissions)
Sets the permissions instance used to create the script engine.
-
-
-
Field Detail
-
ENGINE
private static java.lang.ref.Reference<JexlEngine> ENGINE
The shared engine instance.A single soft-reference JEXL engine and JexlUberspect is shared by all instances of JexlScriptEngine.
-
PERMISSIONS
private static JexlPermissions PERMISSIONS
The permissions used to create the script engine.
-
LOG
static final org.apache.commons.logging.Log LOG
The logger.
-
CACHE_SIZE
static final int CACHE_SIZE
The shared expression cache size.- See Also:
- Constant Field Values
-
CONTEXT_KEY
public static final java.lang.String CONTEXT_KEY
Reserved key for context (mandated by JSR-223).- See Also:
- Constant Field Values
-
JEXL_OBJECT_KEY
public static final java.lang.String JEXL_OBJECT_KEY
Reserved key for JexlScriptObject.- See Also:
- Constant Field Values
-
jexlObject
final JexlScriptEngine.JexlScriptObject jexlObject
The JexlScriptObject instance.
-
parentFactory
final javax.script.ScriptEngineFactory parentFactory
The factory which created this instance.
-
jexlEngine
final JexlEngine jexlEngine
The JEXL EL engine.
-
-
Constructor Detail
-
JexlScriptEngine
public JexlScriptEngine()
Default constructor.Only intended for use when not using a factory. Sets the factory to
JexlScriptEngineFactory
.
-
JexlScriptEngine
public JexlScriptEngine(javax.script.ScriptEngineFactory factory)
Create a scripting engine using the supplied factory.- Parameters:
factory
- the factory which created this instance.- Throws:
java.lang.NullPointerException
- if factory is null
-
-
Method Detail
-
getEngine
private static JexlEngine getEngine()
- Returns:
- the shared JexlEngine instance, create it if necessary
-
readerToString
private static java.lang.String readerToString(java.io.Reader scriptReader) throws javax.script.ScriptException
Read from a reader into a local buffer and return a String with the contents of the reader.- Parameters:
scriptReader
- to be read.- Returns:
- the contents of the reader as a String.
- Throws:
javax.script.ScriptException
- on any error reading the reader.
-
scriptException
static javax.script.ScriptException scriptException(java.lang.Exception e)
-
setInstance
public static void setInstance(JexlEngine engine)
Sets the shared instance used for the script engine.This should be called early enough to have an effect, ie before any
ScriptEngineManager
features.To restore 3.2 script behavior:
JexlScriptEngine.setInstance(new JexlBuilder() .cache(512) .logger(LogFactory.getLog(JexlScriptEngine.class)) .permissions(JexlPermissions.UNRESTRICTED) .create());
- Parameters:
engine
- the JexlEngine instance to use- Since:
- 3.3
-
setPermissions
public static void setPermissions(JexlPermissions permissions)
Sets the permissions instance used to create the script engine.Calling this method will force engine instance re-creation.
To restore 3.2 script behavior:
JexlScriptEngine.setPermissions(JexlPermissions.UNRESTRICTED);
- Parameters:
permissions
- the permissions instance to use or null to use theJexlBuilder
default- Since:
- 3.3
-
compile
public javax.script.CompiledScript compile(java.io.Reader script) throws javax.script.ScriptException
- Specified by:
compile
in interfacejavax.script.Compilable
- Throws:
javax.script.ScriptException
-
compile
public javax.script.CompiledScript compile(java.lang.String script) throws javax.script.ScriptException
- Specified by:
compile
in interfacejavax.script.Compilable
- Throws:
javax.script.ScriptException
-
createBindings
public javax.script.Bindings createBindings()
- Specified by:
createBindings
in interfacejavax.script.ScriptEngine
-
eval
public java.lang.Object eval(java.io.Reader reader, javax.script.ScriptContext context) throws javax.script.ScriptException
- Specified by:
eval
in interfacejavax.script.ScriptEngine
- Throws:
javax.script.ScriptException
-
eval
public java.lang.Object eval(java.lang.String script, javax.script.ScriptContext context) throws javax.script.ScriptException
- Specified by:
eval
in interfacejavax.script.ScriptEngine
- Throws:
javax.script.ScriptException
-
getFactory
public javax.script.ScriptEngineFactory getFactory()
- Specified by:
getFactory
in interfacejavax.script.ScriptEngine
-
-