| <!-- ***** BEGIN LICENSE BLOCK ***** | |
| - Version: MPL 1.1/GPL 2.0 | |
| - | |
| - The contents of this file are subject to the Mozilla Public License Version | |
| - 1.1 (the "License"); you may not use this file except in compliance with | |
| - the License. You may obtain a copy of the License at | |
| - http://www.mozilla.org/MPL/ | |
| - | |
| - Software distributed under the License is distributed on an "AS IS" basis, | |
| - WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
| - for the specific language governing rights and limitations under the | |
| - License. | |
| - | |
| - The Original Code is Rhino code, released May 6, 1999. | |
| - | |
| - The Initial Developer of the Original Code is | |
| - Netscape Communications Corporation. | |
| - Portions created by the Initial Developer are Copyright (C) 1997-1999 | |
| - the Initial Developer. All Rights Reserved. | |
| - | |
| - Contributor(s): | |
| - | |
| - Alternatively, the contents of this file may be used under the terms of | |
| - the GNU General Public License Version 2 or later (the "GPL"), in which | |
| - case the provisions of the GPL are applicable instead of those above. If | |
| - you wish to allow use of your version of this file only under the terms of | |
| - the GPL and not to allow others to use your version of this file under the | |
| - MPL, indicate your decision by deleting the provisions above and replacing | |
| - them with the notice and other provisions required by the GPL. If you do | |
| - not delete the provisions above, a recipient may use your version of this | |
| - file under either the MPL or the GPL. | |
| - | |
| - ***** END LICENSE BLOCK ***** --> | |
| <!doctype html public "-//w3c//dtd html 4.0 transitional//en"> | |
| <html> | |
| <head> | |
| <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> | |
| <meta name="Generator" content="Microsoft Word 97"> | |
| <meta name="GENERATOR" content="Mozilla/4.7 [en] (WinNT; U) [Netscape]"> | |
| <title>Embedding Scripting Host in Business Applications</title> | |
| </head> | |
| <body link="#0000FF"> | |
| <b><font face="Arial"><font size=-1>Embedding | |
| Scripting Host in Business Applications</font></font></b> | |
| <p><b><font face="Arial"><font size=-1>Madhukumar Seshadri, <a href="http://www.cognizant.com)/">www.cognizant.com</a></font></font></b> | |
| <br> | |
| <br> | |
| <p><font face="Arial"><font size=-1>With web browsers, continuing to rule | |
| the human interface for applications, creating value by being ubiquitous, | |
| few adoptions of rich technology thats getting brewed underneath, will | |
| help designing business applications even though most of the current ones | |
| are made with birds eye of the underlying technologies.</font></font> | |
| <p><font face="Arial"><font size=-1>JavaScript is a scripting language | |
| invented and developed by Netscape. The language was primarily designed | |
| for creating lightweight programming for web browser extensions by exposing | |
| the Document Object Model of an HTML page to the scripts. JavaScript is | |
| becoming object oriented and getting adopted for server-side scripting.</font></font> | |
| <p><font face="Arial"><font size=-1>JavaScript is also becoming a standard | |
| in the scripting world as Netscape is working closely with ECMA (European | |
| Computer Manufacturers Association) to make it as a standard scripting | |
| language for the script world. The standards are published as ECMA Script.</font></font> | |
| <p><font face="Arial"><font size=-1>JavaScript originally designed for | |
| exposing the DOM (Document Object Model) standardized by World Wide Web | |
| consortium (W3C), to help web page designers to control and manipulate | |
| the pages dynamically. JavaScript engines were embedded in the browsers | |
| and they execute those portions of the code embedded in the HTML pages.</font></font> | |
| <p><font face="Arial"><font size=-1>In short, JavaScript engine embedded | |
| in the browser allowed extensions or manipulations for DOM Object run time | |
| for the HTML page by executing the scripts associated with them. In other | |
| words, browser exposes its DOM object for the page to scripts for extensions | |
| and dynamic manipulations of the same, using a language that the script | |
| interpreter understands.</font></font> | |
| <p><font face="Arial"><font size=-1>Can I do the same for my application | |
| by exposing my custom business objects written in my middle-tier? Can I | |
| allow user to my write JavaScript extensions for my objects and also be | |
| host for executing those scripts?</font></font> | |
| <p><font face="Arial"><font size=-1>JavaScript host runs times are available | |
| as binaries written in major languages. Check out <a href="http://www.mozilla.org/js">www.mozilla.org/js</a>. | |
| Spider Monkey and Rhino are open source JavaScripting engines available | |
| from mozilla.</font></font> | |
| <p><font face="Arial"><font size=-1>Microsoft implementation of ECMA Script | |
| (ECMA Script is based on core JavaScript, created by Netscape) is called | |
| JScript. Microsoft binaries of jscript engine can be downloaded from http://msdn.microsoft.com/scripting/.</font></font> | |
| <p><font face="Arial"><font size=-1>This document doesnt explain the JavaScript | |
| language in detail but explains how these scripting engines can be used | |
| as host to expose business objects in the middle-tier and how the user | |
| of these applications can extend it if needed using JavaScript.</font></font> | |
| <p><font face="Arial"><font size=-1>The scripting engine Rhino (<a href="./)">www.mozilla.org/rhino)</a>, | |
| a javascript engine purely written in Java is one that I am going to use | |
| for the testing the above.</font></font> | |
| <p><font face="Arial"><font size=-1>Let us set some simple goals,</font></font> | |
| <ul> | |
| <li> | |
| <font face="Arial"><font size=-1>Execute a plain JavaScript code and | |
| use static Java object</font></font></li> | |
| <li> | |
| <font face="Arial"><font size=-1>Instantiate external objects (written | |
| in Java) from the script and use them within the script</font></font></li> | |
| </ul> | |
| <br> | |
| <br> | |
| <p><font face="Arial"><font size=-1>Let us write a simple Javscript to | |
| test the above set goals,</font></font> | |
| <p><font face="Arial"><font size=-1><b>Fig 1</b> jshosttest.js</font></font> | |
| <p><b><i><font face="Arial"><font size=-1>/* Test 1 */</font></font></i></b> | |
| <br><b><i><font face="Arial"><font size=-1>/* Use a static Java Object | |
| in the script */</font></font></i></b> | |
| <p><font face="Arial"><font size=-1>function test1() {</font></font> | |
| <br><font face="Arial"><font size=-1> var str;</font></font> | |
| <br><font face="Arial"><font size=-1> str = '"Hello World";</font></font> | |
| <br><font face="Arial"><font size=-1> return str;</font></font> | |
| <br><font face="Arial"><font size=-1>}</font></font> | |
| <p><font face="Arial"><font size=-1>var str = test1( );</font></font> | |
| <br><font face="Arial"><font size=-1>//out is expected to be Java Object | |
| exposed to the script scope</font></font> | |
| <br><font face="Arial"><font size=-1>out.println ("JavaScript - Test 1 | |
| - " + str);</font></font> | |
| <p><b><i><font face="Arial"><font size=-1>/* Test 2 */</font></font></i></b> | |
| <p><b><i><font face="Arial"><font size=-1>/* Instantiate a Javaobject for | |
| this scope and use it */</font></font></i></b> | |
| <p><font face="Arial"><font size=-1>function test2(){</font></font> | |
| <br><font face="Arial"><font size=-1> // create a Java | |
| string buffer object from JavaScript and use its java instance</font></font> | |
| <br><font face="Arial"><font size=-1> // This uses an | |
| another Java object created for creating new objects within Java and</font></font> | |
| <br><font face="Arial"><font size=-1> // brings the same | |
| for JavaScript execution scope</font></font> | |
| <br><font face="Arial"><font size=-1> // Refer _create.java | |
| for more information</font></font> | |
| <br><font face="Arial"><font size=-1> create.getInstance("java.lang.StringBuffer","buffer");</font></font> | |
| <br><font face="Arial"><font size=-1> //JavaScript refers | |
| the java object instance as buffer</font></font> | |
| <br><font face="Arial"><font size=-1> out.println(buffer.toString());</font></font> | |
| <br> <font face="Arial"><font size=-1>buffer.append("I | |
| am a javaobject dynamically created and executed in JavaScript");</font></font> | |
| <br> <font face="Arial"><font size=-1>return buffer.toString();</font></font> | |
| <br><font face="Arial"><font size=-1>}</font></font> | |
| <p><font face="Arial"><font size=-1>var str = test2();</font></font> | |
| <br><font face="Arial"><font size=-1>out.println("From JavaScript - Test | |
| 2 " + str);</font></font> | |
| <p><font face="Arial"><font size=-1>Let us write a simple Javahost Object | |
| using the Rhino engine to execute the above script,</font></font> | |
| <p><b><font face="Arial"><font size=-1>Fig 2.1 - JSHost.java</font></font></b> | |
| <p><font face="Arial"><font size=-1>/**</font></font> | |
| <br><font face="Arial"><font size=-1>* @author Madhukumar Seshadri</font></font> | |
| <br><font face="Arial"><font size=-1>* @version</font></font> | |
| <br><font face="Arial"><font size=-1>*/</font></font> | |
| <p><font face="Arial"><font size=-1>import org.mozilla.javascript.*;</font></font> | |
| <br><font face="Arial"><font size=-1>import java.io.*;</font></font> | |
| <br><font face="Arial"><font size=-1>import java.lang.*;</font></font> | |
| <br><i><font face="Arial"><font size=-1>// import com.xxx.xxx.*;</font></font></i> | |
| <p><font face="Arial"><font size=-1>public class JSHost extends Object | |
| {</font></font> | |
| <br><font face="Arial"><font size=-1> /** Creates new | |
| JSHost */</font></font> | |
| <br><font face="Arial"><font size=-1> public JSHost() | |
| {</font></font> | |
| <br><font face="Arial"><font size=-1> }</font></font> | |
| <p><i><font face="Arial"><font size=-1> /*** Executes | |
| .js file ***/</font></font></i> | |
| <br><font face="Arial"><font size=-1> public Object executeJS | |
| (String jsfname){</font></font> | |
| <br><font face="Arial"><font size=-1> //You can also | |
| use evaluateReader</font></font> | |
| <br><font face="Arial"><font size=-1> File fp = new File(jsfname);</font></font> | |
| <br><font face="Arial"><font size=-1> String str_buff | |
| =null;</font></font> | |
| <br><font face="Arial"><font size=-1> try {</font></font> | |
| <br><font face="Arial"><font size=-1> | |
| FileReader fr = new FileReader(jsfname);</font></font> | |
| <br><font face="Arial"><font size=-1> | |
| int length = (int) fp.length();</font></font> | |
| <br><font face="Arial"><font size=-1> | |
| char cbuff[] = new char[(char)length];</font></font> | |
| <br><font face="Arial"><font size=-1> | |
| fr.read(cbuff);</font></font> | |
| <br><font face="Arial"><font size=-1> | |
| str_buff = new String(cbuff);</font></font> | |
| <br><font face="Arial"><font size=-1> } catch(Exception | |
| e) {</font></font> | |
| <br><font face="Arial"><font size=-1> | |
| e.printStackTrace();</font></font> | |
| <br><font face="Arial"><font size=-1> }</font></font> | |
| <p><font face="Arial"><font size=-1> //Execute the .js | |
| file content</font></font> | |
| <br><font face="Arial"><font size=-1> return executeJSSource(str_buff);</font></font> | |
| <br><font face="Arial"><font size=-1>}</font></font> | |
| <p><i><font face="Arial"><font size=-1>/*** Executes javascript source | |
| ***/</font></font></i> | |
| <br><font face="Arial"><font size=-1>public Object executeJSSource (String | |
| jsbuff){</font></font> | |
| <br><font face="Arial"><font size=-1>Object any=null;</font></font> | |
| <br><font face="Arial"><font size=-1>try{</font></font> | |
| <br><i><font face="Arial"><font size=-1>//Enter the Context</font></font></i> | |
| <br><font face="Arial"><font size=-1><i>// Refer </i><u><font color="#0000FF">http://www.mozilla.org/rhino/tutorial.html</font></u></font></font> | |
| <br><font face="Arial"><font size=-1>Context context = Context.enter();</font></font> | |
| <br><i><font face="Arial"><font size=-1>// Get the execution scope</font></font></i> | |
| <br><font face="Arial"><font size=-1>Scriptable scope = context.initStandardObjects();</font></font> | |
| <p><i><font face="Arial"><font size=-1>//----------- For Test 1 - Get System.out | |
| in scope</font></font></i> | |
| <br><font face="Arial"><font size=-1>//Scriptable jObj1 = Context.toObject(System.out, | |
| scope);</font></font> | |
| <br><font face="Arial"><font size=-1>scope.put("out", scope, jObj1);</font></font> | |
| <p><i><font face="Arial"><font size=-1>//------------ For Test 2 - Instantiate | |
| Create Object and get that in scope</font></font></i> | |
| <br><i><font face="Arial"><font size=-1>//Allow JScript to create Java | |
| Objects</font></font></i> | |
| <br><i><font face="Arial"><font size=-1>//Bring the _create object to context</font></font></i> | |
| <br><font face="Arial"><font size=-1>_create create = new _create( );</font></font> | |
| <br><i><font face="Arial"><font size=-1>//Register this context and scope | |
| to this create object instance</font></font></i> | |
| <br><font face="Arial"><font size=-1>create.registerContext(context,scope);</font></font> | |
| <br><font face="Arial"><font size=-1>//Scriptable jObj2 = Context.toObject(_create, | |
| scope);</font></font> | |
| <br><font face="Arial"><font size=-1>scope.put("create",scope,create);</font></font> | |
| <br><i><font face="Arial"><font size=-1>//Evaluate (or execute js)</font></font></i> | |
| <br><font face="Arial"><font size=-1><i>//Refer </i><u><font color="#0000FF">http://www.mozilla.org/rhino/tutorial.html</font></u></font></font> | |
| <br><font face="Arial"><font size=-1>any = context.evaluateString(scope, | |
| jsbuff, "", 1, null);</font></font> | |
| <br><i><font face="Arial"><font size=-1>//Exit the Context</font></font></i> | |
| <br><font face="Arial"><font size=-1>context.exit( );</font></font> | |
| <br><font face="Arial"><font size=-1>}</font></font> | |
| <p><font face="Arial"><font size=-1>catch ( JavaScriptException jse) {</font></font> | |
| <br><font face="Arial"><font size=-1>jse.printStackTrace();</font></font> | |
| <br><font face="Arial"><font size=-1>}</font></font> | |
| <p><font face="Arial"><font size=-1>return any;</font></font> | |
| <p><font face="Arial"><font size=-1>}</font></font> | |
| <p><font face="Arial"><font size=-1>}</font></font> | |
| <br> | |
| <br> | |
| <p><font face="Arial"><font size=-1>Let us write a class for creating new | |
| Java objects and bringing them to this script execution scope,</font></font> | |
| <p><b><font face="Arial"><font size=-1>Fig 2.2 _create.java</font></font></b> | |
| <p><i><font face="Arial"><font size=-1>/**</font></font></i> | |
| <p><i><font face="Arial"><font size=-1>* @author Madhukumar</font></font></i> | |
| <p><i><font face="Arial"><font size=-1>*/</font></font></i> | |
| <p><font face="Arial"><font size=-1>import java.lang.Class;</font></font> | |
| <p><font face="Arial"><font size=-1>import org.mozilla.javascript.*;</font></font> | |
| <p><font face="Arial"><font size=-1>public class _create extends Object | |
| {</font></font> | |
| <p><font face="Arial"><font size=-1>static Context ptr = null;</font></font> | |
| <p><font face="Arial"><font size=-1>static Scriptable scope =null;</font></font> | |
| <p><font face="Arial"><font size=-1>public _create () { }</font></font> | |
| <p><font face="Arial"><font size=-1>public void registerContext(Context | |
| cptr, Scriptable sc){</font></font> | |
| <p><font face="Arial"><font size=-1>ptr = cptr;</font></font> | |
| <p><font face="Arial"><font size=-1>scope = sc;</font></font> | |
| <p><font face="Arial"><font size=-1>}</font></font> | |
| <p><font face="Arial"><font size=-1>public void getInstance(String classname,String | |
| jsclassname) {</font></font> | |
| <p><font face="Arial"><font size=-1>Object any=null;</font></font> | |
| <p><font face="Arial"><font size=-1>try {</font></font> | |
| <p><font face="Arial"><font size=-1>Class thisclass = Class.forName(classname);</font></font> | |
| <p><font face="Arial"><font size=-1>any = thisclass.newInstance();</font></font> | |
| <p><font face="Arial"><font size=-1>}</font></font> | |
| <p><font face="Arial"><font size=-1>catch(Exception e){</font></font> | |
| <p><font face="Arial"><font size=-1>e.printStackTrace();</font></font> | |
| <p><font face="Arial"><font size=-1>}</font></font> | |
| <p><font face="Arial"><font size=-1>if( ptr != null) {</font></font> | |
| <p><font face="Arial"><font size=-1>if (scope !=null) {</font></font> | |
| <p><i><font face="Arial"><font size=-1>//register created object for this | |
| execution scope</font></font></i> | |
| <p><font face="Arial"><font size=-1>scope.put(jsclassname,scope,any);</font></font> | |
| <p><font face="Arial"><font size=-1>}</font></font> | |
| <p><font face="Arial"><font size=-1>}</font></font> | |
| <p><font face="Arial"><font size=-1>}</font></font> | |
| <p><font face="Arial"><font size=-1>}</font></font> | |
| <br> | |
| <br> | |
| <p><font face="Arial"><font size=-1>It is time to test the code, so let | |
| us write a small object that will use the JSHost object,</font></font> | |
| <p><b><font face="Arial"><font size=-1>Fig 3 - JSHosttest.java</font></font></b> | |
| <p><font face="Arial"><font size=-1>/**</font></font> | |
| <p><font face="Arial"><font size=-1>* @author Madhukumar</font></font> | |
| <p><font face="Arial"><font size=-1>* @version</font></font> | |
| <p><font face="Arial"><font size=-1>*/</font></font> | |
| <p><font face="Arial"><font size=-1>public class JSHosttest extends Object | |
| {</font></font> | |
| <p><font face="Arial"><font size=-1>/** Creates new JSHostTest*/</font></font> | |
| <p><font face="Arial"><font size=-1>public JSHosttest() {</font></font> | |
| <p><font face="Arial"><font size=-1>}</font></font> | |
| <p><font face="Arial"><font size=-1>public static void main (String args[]){</font></font> | |
| <p><font face="Arial"><font size=-1>if (args.length < 1) {</font></font> | |
| <p><font face="Arial"><font size=-1>System.out.println("Usage - Java JSHosttest.class | |
| <js source file>");</font></font> | |
| <p><font face="Arial"><font size=-1>return;</font></font> | |
| <p><font face="Arial"><font size=-1>}</font></font> | |
| <p><font face="Arial"><font size=-1>JSHost jsh = new JSHost();</font></font> | |
| <p><font face="Arial"><font size=-1>System.out.println("Executing JavaScript | |
| file - " + args[0]);</font></font> | |
| <p><font face="Arial"><font size=-1>Object result = jsh.executeJS(args[0]);</font></font> | |
| <p><font face="Arial"><font size=-1>if (result instanceof String){</font></font> | |
| <p><font face="Arial"><font size=-1>System.out.println("Results - " + result);</font></font> | |
| <p><font face="Arial"><font size=-1>}</font></font> | |
| <p><font face="Arial"><font size=-1>}</font></font> | |
| <p><font face="Arial"><font size=-1>}</font></font> | |
| <br> | |
| <br> | |
| <p><font face="Arial"><font size=-1>For more explanations on the code execution, | |
| please refer embedding tutorial <a href="http://www.mozilla.org/rhino/tutorial.html">http://www.mozilla.org/rhino/tutorial.html</a> | |
| and for all documentation and examples on Rhino visit <a href="doc.html">http://www.mozilla.org/rhino/doc.html</a>.</font></font> | |
| <br> | |
| <br> | |
| <br> | |
| <br> | |
| <br> | |
| <br> | |
| </body> | |
| </html> |