Matt F. blogged about an alternative solution [1]
When providing scripting in a Java application, scripting engines conforming to
JSR 223 (e.g. Groovy, JRuby, Scala, ...) can easily be embedded using something
along the lines of
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine scriptEngine = scriptEngineManager.getByName("groovy");
However, in an OSGi-based application, the ScriptEngineManager fails to discover
scripting engines located in installed bundles, due to the way it
discovers engines available on the class path. Luckily, the Apache Felix project
has already solved this problem, there are
- OSGiScriptEngineManager [2]
- OSGiScriptEngineFactory [3]
- OSGiScriptEngine [4]
which provide an OSGi-compliant way to discover and load scripting engines
installed as OSGi bundles.
ScriptEngineManager scriptEngineManager = new OSGiScriptEngineManager(bundleContext);
ScriptEngine scriptEngine = scriptEngineManager.getByName("groovy");
Now that we've had several years of scripting and OSGi experience, one challenge is simplifying script access to OSGi services. Using the ServiceTracker api [5] seems to be the only approach; but this effort is high for simple scripts. We've worked towards a means for scripts to express which OSGi services they want and automate the ServiceTracker calls on behalf of the scripts but it is fragile. Looking forward to the OSGi specification offering better support in the future.
[1] http://devnotesblog.wordpress.com/2011/09/07/scripting-using-jsr-223-in-an-osgi-environment/
[2] http://svn.apache.org/repos/asf/felix/trunk/mishell/src/main/java/org/apache/felix/mishell/OSGiScriptEngineManager.java
[3] http://svn.apache.org/repos/asf/felix/trunk/mishell/src/main/java/org/apache/felix/mishell/OSGiScriptEngineFactory.java
[4] http://svn.apache.org/repos/asf/felix/trunk/mishell/src/main/java/org/apache/felix/mishell/OSGiScriptEngine.java
[5] https://osgi.org/javadoc/osgi.core/7.0.0/org/osgi/util/tracker/ServiceTracker.html