org.rhq.scripting.javascript.engine
Class RhinoScriptEngine
java.lang.Object
javax.script.AbstractScriptEngine
org.rhq.scripting.javascript.engine.RhinoScriptEngine
- All Implemented Interfaces:
- Compilable, Invocable, ScriptEngine
public class RhinoScriptEngine
- extends AbstractScriptEngine
- implements Invocable, Compilable
Implementation of ScriptEngine using the Mozilla Rhino
interpreter.
- Since:
- 1.6
Modified for phobos to remove some of the restrictions.
Modified to allow subclassing and preprocessing of script source code.
Modified to avoid using the RhinoTopLevel class, since that introduces
a circularity that prevents objects from being garbage collected.
- Version:
- 1.0
- Author:
- Mike Grogan, A. Sundararajan, Roberto Chinnici
Modified so that the top level scope is an ImportTopLevel instance so
that importClass and importPackage functions are available.
Modified so that the "print" and "println" functions work the same as with
the stock javascript script engine provided by the JVM.
Modified to include the "require()" function by default.
Modified to tighten the security of the script execution by running it in an
AccessControlContext active at the time of the script engine creation.
Modified to allow correct interoperability between Java and javascript string even
if represented by the custom ConsString instance., Lukas Krejci
|
Field Summary |
static boolean |
DEBUG
|
|
Constructor Summary |
RhinoScriptEngine()
|
RhinoScriptEngine(org.mozilla.javascript.commonjs.module.provider.ModuleSourceProvider moduleSourceProvider)
Creates a new instance of RhinoScriptEngine with given moduleSourceProvider as the "locator"
for the CommonJS modules. |
|
Method Summary |
static Object |
bindings(org.mozilla.javascript.Context cx,
org.mozilla.javascript.Scriptable thisObj,
Object[] args,
org.mozilla.javascript.Function funObj)
The bindings function takes a JavaScript scope object
of type ExternalScriptable and returns the underlying Bindings
instance. |
CompiledScript |
compile(Reader script)
|
CompiledScript |
compile(String script)
|
Bindings |
createBindings()
|
Object |
eval(Reader reader,
ScriptContext ctxt)
|
Object |
eval(String script,
ScriptContext ctxt)
|
ScriptEngineFactory |
getFactory()
|
|
getInterface(Class<T> clasz)
|
|
getInterface(Object thiz,
Class<T> clasz)
|
Object |
invokeFunction(String name,
Object... args)
|
Object |
invokeMethod(Object thiz,
String name,
Object... args)
|
static void |
main(String[] args)
|
protected Reader |
preProcessScriptSource(Reader reader)
|
protected void |
processAllTopLevelScripts(org.mozilla.javascript.Context cx)
|
protected void |
processTopLevelScript(String scriptName,
org.mozilla.javascript.Context cx)
|
static Object |
scope(org.mozilla.javascript.Context cx,
org.mozilla.javascript.Scriptable thisObj,
Object[] args,
org.mozilla.javascript.Function funObj)
The scope function creates a new JavaScript scope object
with given Bindings object as backing store. |
void |
setModuleSourceProvider(org.mozilla.javascript.commonjs.module.provider.ModuleSourceProvider provider)
|
static Object |
sync(org.mozilla.javascript.Context cx,
org.mozilla.javascript.Scriptable thisObj,
Object[] args,
org.mozilla.javascript.Function funObj)
The sync function creates a synchronized function (in the sense
of a Java synchronized method) from an existing function. |
| Methods inherited from class javax.script.AbstractScriptEngine |
eval, eval, eval, eval, get, getBindings, getContext, getScriptContext, put, setBindings, setContext |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
DEBUG
public static final boolean DEBUG
- See Also:
- Constant Field Values
RhinoScriptEngine
public RhinoScriptEngine()
RhinoScriptEngine
public RhinoScriptEngine(org.mozilla.javascript.commonjs.module.provider.ModuleSourceProvider moduleSourceProvider)
- Creates a new instance of RhinoScriptEngine with given moduleSourceProvider as the "locator"
for the CommonJS modules.
- Parameters:
moduleSourceProvider - the implementation able to locate sources of modules for CommonJS.
setModuleSourceProvider
public void setModuleSourceProvider(org.mozilla.javascript.commonjs.module.provider.ModuleSourceProvider provider)
eval
public Object eval(Reader reader,
ScriptContext ctxt)
throws ScriptException
- Specified by:
eval in interface ScriptEngine
- Throws:
ScriptException
eval
public Object eval(String script,
ScriptContext ctxt)
throws ScriptException
- Specified by:
eval in interface ScriptEngine
- Throws:
ScriptException
getFactory
public ScriptEngineFactory getFactory()
- Specified by:
getFactory in interface ScriptEngine
createBindings
public Bindings createBindings()
- Specified by:
createBindings in interface ScriptEngine
invokeFunction
public Object invokeFunction(String name,
Object... args)
throws ScriptException,
NoSuchMethodException
- Specified by:
invokeFunction in interface Invocable
- Throws:
ScriptException
NoSuchMethodException
invokeMethod
public Object invokeMethod(Object thiz,
String name,
Object... args)
throws ScriptException,
NoSuchMethodException
- Specified by:
invokeMethod in interface Invocable
- Throws:
ScriptException
NoSuchMethodException
getInterface
public <T> T getInterface(Class<T> clasz)
- Specified by:
getInterface in interface Invocable
getInterface
public <T> T getInterface(Object thiz,
Class<T> clasz)
- Specified by:
getInterface in interface Invocable
compile
public CompiledScript compile(String script)
throws ScriptException
- Specified by:
compile in interface Compilable
- Throws:
ScriptException
compile
public CompiledScript compile(Reader script)
throws ScriptException
- Specified by:
compile in interface Compilable
- Throws:
ScriptException
preProcessScriptSource
protected Reader preProcessScriptSource(Reader reader)
throws ScriptException
- Throws:
ScriptException
processAllTopLevelScripts
protected void processAllTopLevelScripts(org.mozilla.javascript.Context cx)
processTopLevelScript
protected void processTopLevelScript(String scriptName,
org.mozilla.javascript.Context cx)
bindings
public static Object bindings(org.mozilla.javascript.Context cx,
org.mozilla.javascript.Scriptable thisObj,
Object[] args,
org.mozilla.javascript.Function funObj)
- The bindings function takes a JavaScript scope object
of type ExternalScriptable and returns the underlying Bindings
instance.
var page = scope(pageBindings);
with (page) {
// code that uses page scope
}
var b = bindings(page);
// operate on bindings here.
scope
public static Object scope(org.mozilla.javascript.Context cx,
org.mozilla.javascript.Scriptable thisObj,
Object[] args,
org.mozilla.javascript.Function funObj)
- The scope function creates a new JavaScript scope object
with given Bindings object as backing store. This can be used
to create a script scope based on arbitrary Bindings instance.
For example, in webapp scenario, a 'page' level Bindings instance
may be wrapped as a scope and code can be run in JavaScripe 'with'
statement:
var page = scope(pageBindings);
with (page) {
// code that uses page scope
}
sync
public static Object sync(org.mozilla.javascript.Context cx,
org.mozilla.javascript.Scriptable thisObj,
Object[] args,
org.mozilla.javascript.Function funObj)
- The sync function creates a synchronized function (in the sense
of a Java synchronized method) from an existing function. The
new function synchronizes on the
this object of
its invocation.
js> var o = { f : sync(function(x) {
print("entry");
Packages.java.lang.Thread.sleep(x*1000);
print("exit");
})};
js> thread(function() {o.f(5);});
entry
js> thread(function() {o.f(5);});
js>
exit
entry
exit
main
public static void main(String[] args)
throws Exception
- Throws:
Exception
Copyright © 2008-2012 Red Hat, Inc.. All Rights Reserved.