The first Server Side JavaScript(Hint:It's not Node.js)
Introduction:
Rhino was the first JavaScript engine meant to be run in a non browser environment. Introduced by NetScape in 1997 (More history at https://en.wikipedia.org/wiki/Rhino_(JavaScript_engine) ) it's meant to be run in a mixed-language environment where the host language is Java. Also it's the default scripting language bundled with J2SE 6.
Relevance of Rhino in a AJAX/REST World
What is the relevance of Rhino to the Enterprise World where the mindshare is dominated by *.JS (Insert your favorite client side JavaScript library) ?
The reality is that most of the enterprise applications (server side)are still in Java and the commitment to Java on the server is still pretty strong. What is loosening is the grip of all the server side page technologies (like JSP and ASP). We are moving to a world where the server will be still in Java the client will be written in client side JavaScript libraries with REST being the preferred data exchange format. In these kind of architectures Rhino provides the capability of running same code on the server side and client side simplifying maintenance. If you client side javascript functions don't refer to any browser objects they are fair game to be run in Rhino.
Essence of Rhino
Rhino allows you to run JavaScript code from with in Java program passing Java objects in to the script, manipulating them and return Java objects. It allows you to create JavaScript objects with in which can be returned back to the calling Java program. This allows your program to be much more dynamic than Java. (Remember that JavaScript is dynamically typed vs Java which is more statically typed).
What Rhino is not
It's not a JavaScript engine which is running in a browser. This means it doesn't know the browser world and the objects and concepts which make up that world. No DOM, No window and certainly no event handlers.
An Enterprise usage Pattern of Rhino
Following diagram shows
The above architecture pattern can be used to run validations on both server side and client side. A simple piece of code will be shown to illustrate the Rhino invocation. (The full project is located at github https://github.com/objectsmiths/rhino )
Important pseudo code snippets are shown below
// Following is code which should be executed once in the life cycle
// If you are running in a container it's better to run this one per thread
// Use of thread local variables or concurrent hash map would be preferred
Context context = org.mozilla.javascript.Context.enter() ;
//Get a reference to a context object (org.mozilla.javascript.Context.enter())
//A context represents the runtime context of an executing script
// Context a thread safe object
org.mozilla.javascript.Scriptable scope = context.initStandardObjects(null);
// Create a scope from the above context
ScriptableObject.putProperty(scope, "SomeGlobalVariableWhichShouldBeAvailableToTheRhinoScript", <some object> );
// The above pseudo code allows to pass global variables which can be refered by //the Rhino Script
String code = " Read code from a JS file in to a single string. Make sure you retain the line breaks (\n) so that when an error occurs the proper line number will be printed by the Rhino engine " ;
context.evaluateString(scope, code, "<cmd",1,null);
// Above line will compile the JS code in to the scope with the corresponding //context
// Following code will be executed one per request (say per servlet request)
// Let's say the JavaScript code has a function called "performComplexCalculation
Function func = (Function)scope.get("performComplexCalc", scope);
Object[] args = .....// Initialize the object array with the arguments to the function
Object result = func.call(context, scope, args);
The object conversion from javascript to java (and vice versa) is handled transparently.
Conclusion
In one version of ideal software world all systems are built with a single programming language/system. In the real world we have to deal with heterogeneous languages and systems. In the world of web JavaScript has acquired a singular importance thanks to the concepts of AJAX and REST services and upgraded browsers. Using Rhino we can harmonize the client JavaScript with server side JavaScript and server side Objects to achieve certain use cases which are simply very hard to achieve using any other techniques. Of course if your project is lucky enough to use Node.js then none of this applies to you.
Further Reading:
The documentation of Rhino is located at
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino#Rhino_documentation
Ram, https://en.m.wikipedia.org/wiki/Yahoo!_Query_Language and here is mention of rhino https://developer.yahoo.com/blogs/ydn/enhancing-yql-server-side-javascript-7977.html
HI Sastry, What is yql and which product was it used in ? Any information you could share would be appreciated. Ram
Thanks for the information.
yql used rhino to executeuser provided javascript in a sandbox
They have different user cases. I would use Rhino in a server side Java environment. Nodejs would completely replace Java on the server side.