|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
LiveConnect support for JRex. JRexLiveConnectSession provides a LiveConnect bridge between the JREX JVM and javascript.
JRexLiveConnectSession should only be used on a browser loaded with a web page you trust. This is because the web page will have unrestricted access to the JRex JVM. A typical usage of JRexLiveConnectSession is to use it with a custom XUL app or html page that is owned by the application.
To get LiveConnect support for JRex, be sure to edit JRex.properties and set "jrex.liveconnect" to 1 before compiling. To create an instance of JRexLiveConnectSession, create an instance of the classJRexLiveConnectSessionImpl
.
Before you can use JRexLiveConnectSession, first set the JRexCanvas using the
method setBrowser(org.mozilla.jrex.ui.JRexCanvas)
. Alternatively, you can pass the browser to the
constructor for JRexLiveConnectSessionImpl
.
The method invokeStringMethod(java.lang.String, java.lang.String)
is provided as a simple javascript invocation.
This method is used for testing. Most often the caller will want to obtain the
JSObject using the method getJSWindow()
and evaluate javascript using it.
For example, invoking getJSObject().eval("1+1+2")
will return a
java.lang.Double object with the value 4.
Note that JRex defines its own JSObject class. It is important to make sure JRex's JSObject class is loaded instead of the sun plugin's JSObject class. To be sure of this, always put plugin.jar after JRex.jar in the classpath.
By default the JRexCanvas is untrusted and therefore the javascript cannot create or access java objects in the JRex JVM. This is done to safegaurd against a malicious javascript application.
The method grantBrowserAccessToJVM()
is used to let JRexLiveConnectSession
know that it is ok for the browser to create and access java objects in the JRex JVM.
This method must be called before the method getJSWindow()
is called.
This method exists for the case that a browser window is loaded that is not trusted.
In this case, all the methods of JRexLiveConnectSession will work expect for
getJSWindow()
. Only call the method grantBrowserAccessToJVM()
if the
browser is trusted by your java application.
test.liveconnect.JRexLiveConnectSessionUnitTest
for unit tests. This class
also serves as an example of using JRexLiveConnectSession.
JRexLiveConnectSessionImpl
,
test.liveconnect.JRexLiveConnectSessionUnitTest
Method Summary | |
String |
evalUntrusted(String script)
Evaluates javascript in the browser. |
netscape.javascript.JSObject |
getJSWindow()
Retrieves a JSObject implementation for the JRexCanvas window. |
void |
grantBrowserAccessToJVM()
Makes the LiveConnectSession a 2-way session. |
String |
invokeStringMethod(String functionName,
String arg)
Invokes a javascript method which accepts a single string and returns a string. |
void |
setBrowser(JRexCanvas browser)
Sets the browser and intializes live connect for the browser. |
Method Detail |
public void setBrowser(JRexCanvas browser) throws JRexException
browser
- the JRexCanvas, which must already be initialized
JRexException
- if an error occurs while initializing live connectpublic void grantBrowserAccessToJVM() throws JRexException
getJSWindow()
. After this method is invoked, the javascript will be
able to create java objects and invoke methods on those objects, and the
caller will be able to use the method getJSWindow()
to retrieve the
JSObject instance for the javascript window.
Only call this method if the web page loaded into the JRexCanvas is trusted.
JRexException
- if an error occurs while granting access to the JRex JVMpublic String invokeStringMethod(String functionName, String arg) throws JRexException
evalUntrusted(java.lang.String)
should
be used for untrusted javascript, and the JSObject class returned from
getJSWindow()
should be used for trusted javascript.
functionName
- the name of the javascript functionarg
- the argument to pass to the javascript function
JRexException
- if an error occurs while invoking the javascriptgetJSWindow()
public String evalUntrusted(String script) throws JRexException
grantBrowserAccessToJVM()
followed by getJSWindow()
and invoke the
eval method on the JSObject instance. Only use this method if the javascript is not
trusted.
script
- the javascript to evaluate
JRexException
- if an error occurs while evaulatingpublic netscape.javascript.JSObject getJSWindow() throws JRexException, SecurityException
Note that this method will throw a security violation exception unless the
method grantBrowserAccessToJVM()
is invoked before this method.
Make sure to invoke grantBrowserAccessToJVM()
before invoking this
method.
JRexException
- if an error prevents the JSObject from being created
SecurityException
- if grantBrowserAccessToJVM has not been called yetgrantBrowserAccessToJVM()
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |