Helma Logo
main list history
previous version  overview  next version

Version 11 by hannes on 14. October 2005, 16:14

==== What you need

All you need are the js.jar and helma.jar files you get from building the *svn snapshot|source*. When Helma 2 is released, there will be a bare bones library download option that will exclude Jetty and the Helma web framework.

==== 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

FIXME

* Pass JS Host objects classes as second argument to to RhinoLoader constructor
* Pass global variable map as second argument to getInstance()

     removed
     added