Is OSGi fundamentally incompatible with JSR-223 Scripting Language Discovery?

后端 未结 3 1324
生来不讨喜
生来不讨喜 2020-12-05 21:13

I\'ve recently written a small specialist scripting language and used the Maven to export an OSGi compliant bundle that also exports a service descriptor into the \"ME

3条回答
  •  庸人自扰
    2020-12-05 21:24

    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

提交回复
热议问题