Helma Logo
main list history
previous version  overview  next version

Version 17 by hannes on 24. November 2006, 23:02

====Content of this page was obsoleted, What you needsee new *RhinoLoader branch|Source/browse/helma/branches/rhinoloader/*. New wiki page coming soon.

All you need is the *bare bones helma package|http://dev.helma.org/static/helma.dev/Wiki/Using%20Helma%202%20as%20Library/bare_bones_helma-0.1.zip*  or a snapshot from the *bare bones helma branch|source/helma/branches/bare-bones-branch*.

==== How it works

Helma 2 can be used as a library to instantly add scripting capability to any Java program. Scripts loaded from .js files can extend any Java class and implement any Java interface just using the following JavaScript syntax:

this.__extends__ = javaClassName;
this.__implements__ = [javaInterfaceName, ...];

The scripted class can override any Java methods and define new ones.  (Note that functions that don't have a corresponding method in a Java class or Interface can't be invoked from Java, though!) Overridden methods in the super class can be called using the super$ prefix:

  this.super$put(key, value);

==== A simple example

The Java side:

import org.helma.javascript.RhinoLoader;
import java.io.File;
import java.io.IOException;
 
public class Test {
 
    public static void main(String[] args)
            throws IOException, ClassNotFoundException {
        RhinoLoader loader = new RhinoLoader(new File("scriptdir"));
        Runnable test = (Runnable) loader.getInstance("Test");
        new Thread(test).start();
    }
}

This tries to load an instance of a class implementing java.lang.Runnable from a file called scriptdir/Test.js. The script file might look as simple as this:

this.__implements__ = "java.lang.Runnable";
 
function run() {
    java.lang.System.err.println("Running!");
}

==== Advanced features

TODO

* Pass JS <a href="http://www.mozilla.org/rhino/tutorial.html#JavaScriptHostObjects">Host objects classes</a> as second argument to to RhinoLoader constructor
* Pass global variable map as second argument to getInstance()
* Usage example in <a href="http://www.springframework.org/">Spring</a> using <a href="http://static.springframework.org/spring/docs/1.2.x/reference/beans.html#beans-instance-factory-method">instance factory method</a>

     removed
     added