cookxml.cookjs.util
Class ProxyJavaAdapter

java.lang.Object
  extended bycookxml.cookjs.util.ProxyJavaAdapter
All Implemented Interfaces:
org.mozilla.javascript.IdFunctionCall

public class ProxyJavaAdapter
extends Object
implements org.mozilla.javascript.IdFunctionCall

Use Proxy class to do the task of re-directing Java function calls to Rhino interpreter. This approach requires JDK 1.3, but it avoids the annoying SecurityManager's permission exceptions, and thus allow Rhino interpreter to be run in Sandbox, allowing applications to be run in Java Web Start w/o signed jars and Applets w/o exceptions. Performance wise should be similar I think.

To override Rhino JavaScript engine's default JavaAdapter behavior, first obtain the top level scope. Then call ProxyJavaAdapter's init function. For example:

  Context cx = Context.enter ();
	// The following line is to disable the default byte code compilation, which also forces
	// Rhino to create custom ClassLoader.
  cx.setOptimizationLevel (-1);
	Scriptable scope = cx.initStandardObjects ();
	ProxyJavaAdapter.init (cx, scope, true);
 

Since:
CookJS 1.0
See Also:
Proxy, JavaAdapter, ProxyHandler

Constructor Summary
ProxyJavaAdapter()
           
 
Method Summary
 Object execIdCall(org.mozilla.javascript.IdFunctionObject f, org.mozilla.javascript.Context cx, org.mozilla.javascript.Scriptable scope, org.mozilla.javascript.Scriptable thisObj, Object[] args)
           
static void init(org.mozilla.javascript.Context cx, org.mozilla.javascript.Scriptable scope, boolean sealed)
          Call this function to initate the scope with JavaAdapter object.
static Object js_createAdapter(org.mozilla.javascript.Context cx, org.mozilla.javascript.Scriptable scope, Object[] args)
          This function creates a Scriptable object that wraps around the Java class, which is created using Proxy and handles Java function calls.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ProxyJavaAdapter

public ProxyJavaAdapter()
Method Detail

init

public static void init(org.mozilla.javascript.Context cx,
                        org.mozilla.javascript.Scriptable scope,
                        boolean sealed)
Call this function to initate the scope with JavaAdapter object.

Parameters:
cx - Context of the execution environment.
scope - the scope of JavaAdapter
sealed - the scope is sealed or not

js_createAdapter

public static Object js_createAdapter(org.mozilla.javascript.Context cx,
                                      org.mozilla.javascript.Scriptable scope,
                                      Object[] args)
This function creates a Scriptable object that wraps around the Java class, which is created using Proxy and handles Java function calls.

Parameters:
cx - Context of the execution environment.
scope - the scope of JavaAdapter
args - This is exactly like Rhino version of JavaAdapter. All except the last argument are the classes/interfaces the Proxy Java class will extend/implement. The last argument is the actual JavaScript Scriptable object which is the user implementation.
Returns:
a Scriptable object which wraps around the Proxy Java object created.

execIdCall

public Object execIdCall(org.mozilla.javascript.IdFunctionObject f,
                         org.mozilla.javascript.Context cx,
                         org.mozilla.javascript.Scriptable scope,
                         org.mozilla.javascript.Scriptable thisObj,
                         Object[] args)
Specified by:
execIdCall in interface org.mozilla.javascript.IdFunctionCall