public class RhinoEngine extends Object
RhinoEngine class acts as a global engine for executing JavaScript codes using Mozilla Rhino.
Each engine instance associates a scope and a caching manager.
During the class initialization time, it creates a static global scope, which will be cloned upon request. This also
has a constructor which accepts class object itself as a parameter. So, it allows you to keep customised versions of
RhinoEngine instances.
It also has several util methods to register hostobjects, methods, properties with the engine's scope.| Constructor and Description |
|---|
RhinoEngine(CacheManager cacheManager,
RhinoContextFactory contextFactory)
This constructor gets an existing
CacheManager instance and returns a new engine instance with the
new cache manager. |
| Modifier and Type | Method and Description |
|---|---|
Object |
call(Reader scriptReader,
String funcName,
Object[] args,
org.mozilla.javascript.ScriptableObject thiz,
org.mozilla.javascript.ScriptableObject scope,
ScriptCachingContext sctx)
Executes a particular JavaScript function from a script on the specified scope and returns the result.
|
Object |
call(Reader scriptReader,
String funcName,
Object[] args,
org.mozilla.javascript.ScriptableObject thiz,
ScriptCachingContext sctx)
Executes a particular JavaScript function from a script on a clone of engine's scope and returns the result.
|
Object |
call(Reader scriptReader,
String funcName,
Object[] args,
ScriptCachingContext sctx)
Executes a particular JavaScript function from a script on a clone of engine's scope and returns the result.
|
void |
defineHostObject(JavaScriptHostObject hostObject)
This method registers a hostobject in the engine scope.
|
static void |
defineHostObject(org.mozilla.javascript.ScriptableObject scope,
JavaScriptHostObject hostObject)
This method registers a hostobject in the given scope.
|
void |
defineMethod(JavaScriptMethod method)
This method registers the given method in the engine scope.
|
static void |
defineMethod(org.mozilla.javascript.ScriptableObject scope,
JavaScriptMethod method)
This method registers the given method in the specified scope.
|
void |
defineModule(JavaScriptModule module)
This method registers the given module in the specified scope.
|
void |
defineProperty(JavaScriptProperty property)
This method registers the specified property in the engine scope.
|
static void |
defineProperty(org.mozilla.javascript.ScriptableObject scope,
JavaScriptProperty property)
This method registers the specified property in the specified scope.
|
void |
defineScript(JavaScriptScript script)
This method executes the given script in the engine scope.
|
static void |
defineScript(org.mozilla.javascript.ScriptableObject scope,
JavaScriptScript script)
This method executes the given script in the specified scope.
|
org.mozilla.javascript.Context |
enterContext()
Creates a Rhino Context in the current thread or returned the already created one
|
static org.mozilla.javascript.Context |
enterContext(org.mozilla.javascript.ContextFactory factory)
Creates a Rhino Context in the current thread using the specified ContextFactory or returned the already
created one
|
static org.mozilla.javascript.Context |
enterGlobalContext()
Creates a Rhino Context in the current thread using the global ContextFactory or returned the already
created one
|
Object |
eval(Reader scriptReader,
org.mozilla.javascript.ScriptableObject scope,
ScriptCachingContext sctx)
Evaluates the specified script and the result is returned.
|
Object |
eval(Reader scriptReader,
ScriptCachingContext sctx)
Evaluates the specified script and the result is returned.
|
void |
exec(Reader scriptReader,
org.mozilla.javascript.ScriptableObject scope,
ScriptCachingContext sctx)
Executes the script on the specified scope.
|
org.mozilla.javascript.ScriptableObject |
exec(Reader scriptReader,
ScriptCachingContext sctx)
Executes the script on a clone of the engine scope and the scope is returned.
|
static void |
exitContext()
Exists from the context associated in the current thread
|
static Object |
getContextProperty(Object key)
Gets a property from the Context of the current thread
|
org.mozilla.javascript.ScriptableObject |
getRuntimeScope()
This clones the engine scope and returns.
|
static org.mozilla.javascript.Scriptable |
newObject(org.mozilla.javascript.ScriptableObject scope)
Creates a new JavaScript object in the given scope
|
static void |
putContextProperty(Object key,
Object value)
Puts a property in the Context of the current thread
|
void |
unloadTenant(String tenantId)
Unloads all the resources associated with a particular tenant
|
public RhinoEngine(CacheManager cacheManager, RhinoContextFactory contextFactory)
CacheManager instance and returns a new engine instance with the
new cache manager.
Scope of the engine will be a clone of the static global scope associates with the RhinoEngine
class.cacheManager - A CacheManager instance to be used as the cache manager of the enginepublic static void defineHostObject(org.mozilla.javascript.ScriptableObject scope,
JavaScriptHostObject hostObject)
throws ScriptException
scope - The scope where hostobject will be definedhostObject - HostObject to be definedScriptExceptionpublic void defineHostObject(JavaScriptHostObject hostObject)
hostObject - HostObject to be definedpublic static void defineProperty(org.mozilla.javascript.ScriptableObject scope,
JavaScriptProperty property)
scope - The scope to register the bean objectproperty - Property to be definedpublic void defineProperty(JavaScriptProperty property)
property - Property to be definedpublic static void defineScript(org.mozilla.javascript.ScriptableObject scope,
JavaScriptScript script)
scope - The scope to register the bean objectscript - Script to be definedpublic void defineScript(JavaScriptScript script)
script - Script to be definedpublic static void defineMethod(org.mozilla.javascript.ScriptableObject scope,
JavaScriptMethod method)
throws ScriptException
scope - The scope to register the bean objectmethod - Method to be definedScriptExceptionpublic void defineMethod(JavaScriptMethod method)
method - Method to be definedpublic void defineModule(JavaScriptModule module)
module - Module to be definedpublic Object eval(Reader scriptReader, ScriptCachingContext sctx) throws ScriptException
sctx is provided and cache is
upto date, cached script will be evaluated instead of the original one. Otherwise, a either cache will be
updated or evaluated the script directly without caching.
A clone of the engine scope will be used as the scope during the evaluation.scriptReader - Reader object to read the script when ever neededsctx - Script caching context which contains caching data. When null is passed for this, caching
will be disabled.ScriptException - If error occurred while evaluatingpublic Object eval(Reader scriptReader, org.mozilla.javascript.ScriptableObject scope, ScriptCachingContext sctx) throws ScriptException
sctx is provided and cache is
upto date, cached script will be evaluated instead of the original one. Otherwise, either the cache will be
updated or evaluated the script directly without caching.
The specified scope will be used as the scope during the evaluation.scriptReader - Reader object to read the script when ever neededscope - Scope to be used during the evaluationsctx - Script caching context which contains caching data. When null is passed for this, caching
will be disabled.ScriptException - If error occurred while evaluatingpublic org.mozilla.javascript.ScriptableObject exec(Reader scriptReader, ScriptCachingContext sctx) throws ScriptException
sctx is provided and cache is upto date, cached script
will be evaluated instead of the original one. Otherwise, either the cache will be updated or evaluated the
script directly without caching.scriptReader - Reader object to read the script when ever neededsctx - Script caching context which contains caching data. When null is passed for this, caching
will be disabled.ScriptException - If error occurred while evaluatingpublic void exec(Reader scriptReader, org.mozilla.javascript.ScriptableObject scope, ScriptCachingContext sctx) throws ScriptException
sctx is provided and cache is upto date, cached script
will be evaluated instead of the original one. Otherwise, either the cache will be updated or evaluated the
script directly without caching.scriptReader - Reader object to read the script when ever neededscope - Scope to be used during the executionsctx - Script caching context which contains caching data. When null is passed for this, caching
will be disabled.ScriptException - If error occurred while evaluatingpublic Object call(Reader scriptReader, String funcName, Object[] args, ScriptCachingContext sctx) throws ScriptException
scriptReader - Reader object to read the script when ever neededfuncName - Name of the function to be invokedargs - Arguments for the functions as an array of objectssctx - Script caching context which contains caching data. When null is passed for this, caching
will be disabled.ScriptException - If error occurred while invoking the functionpublic Object call(Reader scriptReader, String funcName, Object[] args, org.mozilla.javascript.ScriptableObject thiz, ScriptCachingContext sctx) throws ScriptException
scriptReader - Reader object to read the script when ever neededfuncName - Name of the function to be invokedargs - Arguments for the functions as an array of objectsthiz - this object for the functionsctx - Script caching context which contains caching data. When null is passed for this, caching
will be disabled.ScriptException - If error occurred while invoking the functionpublic Object call(Reader scriptReader, String funcName, Object[] args, org.mozilla.javascript.ScriptableObject thiz, org.mozilla.javascript.ScriptableObject scope, ScriptCachingContext sctx) throws ScriptException
scriptReader - Reader object to read the script when ever neededfuncName - Name of the function to be invokedargs - Arguments for the functions as an array of objectsthiz - this object for the functionscope - The scope where function will be executedsctx - Script caching context which contains caching data. When null is passed for this, caching
will be disabled.ScriptException - If error occurred while invoking the functionpublic org.mozilla.javascript.ScriptableObject getRuntimeScope()
throws ScriptException
ScriptExceptionpublic void unloadTenant(String tenantId)
tenantId - Tenant to be unloadedpublic static org.mozilla.javascript.Scriptable newObject(org.mozilla.javascript.ScriptableObject scope)
scope - Scope for the object constructor lookuppublic static void putContextProperty(Object key, Object value)
key - Property namevalue - Property valuepublic static Object getContextProperty(Object key)
key - Property namepublic org.mozilla.javascript.Context enterContext()
public static org.mozilla.javascript.Context enterContext(org.mozilla.javascript.ContextFactory factory)
public static org.mozilla.javascript.Context enterGlobalContext()
public static void exitContext()
Copyright © 2015 WSO2 Inc. All rights reserved.