Thursday, February 12, 2009

A scripting bundle for OSGi

I've wrapped the scriptconsole4j as an OSGi bundle (tested in Apache Felix 1.4.0 and Equinox 3.4.0).

This console is a simple GUI that allows using the JSR-223 compatible scripting languages available in you Java 6 VM. The Rhino Engine (Javascript) is available by default.

If you want to execute scripts accessing the OSGi framework to retrieve services in adhoc scripts, you may find this version of the scriptconsole4j tool useful. When the bundle is started a scripting window is automatically displayed. Upon bundle termination, the window is disposed and vice-versa.

How to use it:
  • Just download it.
  • Install it on your favorite OSGi framework
  • Start the bundle
  • And its ready to be used

You can, for example, register listeners to framework objects:

var o = new Object();
o.bundleChanged = function(e) {
message = "An event happened to bundle "
+ e.getBundle().getBundleId();
//Writing in the standard output

ctx.addBundleListener(new org.osgi.framework.BundleListener(o));

Since the bundle uses DynamicImport-Package, you can use it with any exported class, so you can dynamically implement and register services:

var o = new Object();
o.sayHello = function() { java.lang.System.out.println("closed")};

r = new;
//registering the service in the OSGi Service Registry
//retrieving the service instance
service = ctx.getService(ctx.getServiceReference("foo.BarService"));

Take a look at the Rhino page showing how to use Java from Javascript, and use your imagination for the scripts that may help you.

Recently I needed the scripting functionality in OSGi on a Java 5 Virtual Machine, which does not come with the JSR-223. I've removed the JSR-223 dependant stuff from the scriptconsole4j and embedded the beanshell core with it. It worked fine but without dynamic class generation. I guess by adding the bsh-classgen it "would" work. Since we are running on OSGi, classloading is always an issue when dynamically generating stuff...
I'll test it and leave it available soon (I hope so).


Habuma said...

Just a couple of comments...first you should mention that it requires Java 1.6 (for the javax.script stuff). And, when running this on my MacBook Pro, it doesn't offer me JavaScript...instead it offers me AppleScriptEngine as the scripting language. I guess that's the default scripting implementation in Apple's implementation of Java. Easily dealt with, I suppose, but I thought I'd point it out.

Habuma said...

Sorry...just noticed that you *did* mention Java 1.6. I just didn't see it in the installation instructions.

kiev gama said...

Hello Habuma,

I've tested it on Java 6 VM hotspot versions in Windows XP and OpenSolaris. Both of them come with Rhino.
I presume the console did work in your Mac as well. I would not know if its default scripting engine allows also dynamically implementing interfaces and extending classes as Rhino does.

Dave said...

Thanks for the example. I'm doing something similar and couldn't figure out how to get Rhino to use the right class loader. I see that you set the thread context class loader to the bundle's class loader. Should have known.