Rhino – JavaScript for Java
Speaker: Brent Wilkins Hertz – Applications Consultant – Cobol developer – 20+ years – PowerBuilder developer – 6 years – Java developer – 10+ years – Rhino – JavaScript for Java
What is it? Rhino is an open-source implementation of JavaScript written entirely in Java. It is typically embedded into Java applications to provide scripting to end users. Rhino – JavaScript for Java
Most people who have used JavaScript before have done so by adding scripts to their HTML web pages. However, Rhino is an implementation of the core language only and doesn't contain objects or methods for manipulating HTML documents. Rhino contains: All the features of JavaScript 1.7 Allows direct scripting of Java A JavaScript shell for executing JavaScript scripts A JavaScript compiler to transform JavaScript source files into Java class files A JavaScript debugger for scripts executed with Rhino Rhino – JavaScript for Java
Outline: JavaScript context Optimization levels Initializing and scripting the basic Java objects Scripting custom objects Evaluating JavaScript expressions Rhino – JavaScript for Java
JavaScript Context The Rhino JavaScript Context object is used to store thread-specific information about the execution environment. There should be one and only one Context associated with each thread that will be executing JavaScript. Import org.mozilla.javascript.Context; To associate the current thread with a Context, simply call the enter method of Context: Context jsContext = Context.enter(); Context.exit(); Once you are done with execution, simply exit the Context: Rhino – JavaScript for Java
Optimization Levels Interpretive mode is used. No objects are compiled. 0No optimization is performed. Objects are compiled to bytecode. 1 – 9All optimizations are performed. Rhino – JavaScript for Java
Initializing and scripting the basic Java objects The basic Java objects (String, Boolean, Integer, etc.) must be initialized within the JavaScript Context. This allows for access to custom Java objects from with the Context. org.mozilla.javascript.Scriptable Scriptable script = jsContext.initStandardObjects(); Rhino – JavaScript for Java
Demo time Rhino – JavaScript for Java
Scripting custom objects Convert the object from Java to a JavaScript scriptable object Scriptable jsArgs = Context.toObject( obj, script ); Add the JavaScript object to the other Scriptable objects in the context script.put( objectName, script, jsArgs ); Rhino – JavaScript for Java
Evaluating JavaScript expressions Object result = jsContext.evaluateString( script, expression, expression, 1, null ); Determine the result type of the return object: String String stringResult = Context.toString( result ); Boolean boolean boolResult = Context.toBoolean( result );
Demo time Rhino – JavaScript for Java
Thermal Printing at Hertz Must be able to access data from the Rental Agreement Must be able to evaluate psuedocode that contains: – AND, OR – () order of operations – =, <>,, = – IN and NIN – Evaluates if a variable is in a group of values Rhino – JavaScript for Java
Thermal Printing at Hertz Must be able to access data from the Rental Agreement Must be able to evaluate psuedocode that contains: – AND, OR&&, || – () order of operations – =, <>,, ===, != – IN and NIN – Evaluates if a variable is in a group of values INJava method isInGroup( “|” + var + “|”, “|value|value|value|” ) NIN!isInGroup() Rhino – JavaScript for Java
Demo time Rhino – JavaScript for Java
Resources