Helma Logo
main list history
previous version  overview  next version

Version 2 by hannes on 16. May 2008, 11:30

The *helma.rhino module|http://dev.helma.org/trac/helma/browser/helma-ng/trunk/modules/helma/rhino.js* provides methods to access the Helma NG Rhino engine.

=== Setting up Host objects

By default, Helma NG hasn't got does not contain any web specific custom host classesobjects. This is useful to allow makes sense when using Helma NG in other contexts than the typical web application. The helma.rhino module provides a utility method called <code>initWebApp()</code> that sets up the *host objects|Helma NG Core API* used for web applications.

You can also register *your own host objects|http://www.mozilla.org/rhino/tutorial.html#javaScriptHostObjects* using the helma.rhino.addHostObject() method, passing the host object java class as only argument.

=== Scripting Java classes

Helma NG allows you to add methods and properties to existing Java classes.

The helma.rhino.extendJavaClass() takes a java class as argument and returns an extended java class wrapper. You can add Javascript functions to the java class like it was a Javascript constructor, using the class wrapper's prototype property.

  var HashMap = helma.rhino.extendJavaClass(java.util.HashMap);
  HashMap.prototype.dump = function() { res.write(this); }

Note that while the class wrapper works as constructor for the extended class, the added functions and properties are available also on objects created using the standard java constructor and even objects created and returned from java code. The Helma NG object wrapper takes care of this.

Note that this is different from Rhino's *JavaAdapter|http://www.mozilla.org/rhino/scriptjava.html#extending*. JavaAdapter allows you to create new java classes in javascript, extending existing classes and implementing existing interfaces. The Helma NG java class extension feature allows you to add scripted functions to existing java classes without creating a new class.

You can also script java superclasses and interfaces. However, Helma NG currently does not implement extended class merging or chaining. In other words, if both java.util.HashMap, java.util.Map and java.lang.Object classes are extended, java instances will only have the "nearest" extension: HashMap instances will only have the extended properties of java.util.HashMap, TreeMap objects those of java.util.Map, and ArrayList those of java.lang.Object.

=== Managing callbacks

helma.rhino provides *addCallback(), removeCallback(), and invokeCallback() methods|http://dev.helma.org/trac/helma/browser/helma-ng/trunk/modules/helma/rhino.js#L34* to register, remove and invoke *callbacks*.

=== Setting the Rhino optimization level

The helma.rhino.setRhinoOptimizationLevel() method allows you to set the Rhino optimization level. Accepted values are integer numbers between -1 and 9. The default level is 0 and implements basic Javascript to Java bytecode compilation. Level -1 disables the bytecode compiler and sets Rhino to interpreter mode, which is required for some features such as *continuations*. Levels 1 to 9 add some optimizations that may or may not be detrimental to your health.

Note that Helma NG can run in both Java-compiled and interpreted mode in the same application, so you can call this on a per-request basis in the onInvoke *callback*. Also note that setting the optimization level in the 0..9 range does not cause scripts that have already been compiled and cached to be recompiled.

=== Going further

The rhino module provides utility methods getRhinoContext() and getRhinoEngine() to access the current *Rhino Context|http://www.mozilla.org/rhino/apidocs/org/mozilla/javascript/Context.html* and *Rhino Engine|http://dev.helma.org/static/ng/api/org/helma/javascript/RhinoEngine.html* instances. These classes provide a lot of Javascript related functionality that can be easily used from application code.