Previous Topic

Next Topic

Book Contents

Book Index

OSGi Script Engine Manager

Overview

The OSGi Script Engine Manager service (com.prosyst.mbs.services.scripting.OSGiScriptEngineManager) is the entry point interface for applications which are going to use scripting on OSGi Framework. It is similar to javax.script.ScriptEngineManager, but can be used for discovery of script engines not only on Java Classpath, but also from bundles installed on the OSGi Framework.

Script Engine Discovery

The OSGi Script Engine Manager uses the standard Jar service provider mechanism to load javax.script.ScriptEngineFactory implementations from libraries on Java Classpath and bundles installed on the OSGi Framework.

To deploy a Script Engine in a bundle implementation classes must be packaged in the bundle and the bundle must contain a text file META-INF/services/javax.script.ScriptEngineFactory, containing the full class name of the engine corresponding ScriptEngineFactory.

Script Engine Factories

The List<ScriptEngineFactory> OSGiScriptEngineManager.getEngineFactories() method can be used to list currently available Script Engines and their meta-data (name, supported extensions and mime-types, etc).

Below there is a List Script Engines Meta-Data example:

OSGiScriptEngineManager manager = ...;

List<ScriptEngineFactory> factories = manager.getEngineFactories();
for (ScriptEngineFactory factory : factories) {
   String engineName = factory.getEngineName();
  ...
}

Script Engine Instantiation

The primary usage of the OSGi Script Engine Manager is to instantiate Script Engines, which can then be used for evaluating scripts. The OSGiScriptEngineManager service has methods for creating Script Engine given engine name, script extension, mime-type or ScirptEngineFactory. All of these methods take as additional argument the Bundle on behalf of which the Script Engine will work (i.e. evaluate scripts). The bundle class loader will be used to resolve Java classes used in scripts, its BundleContext when accessing the OSGi Registry and the Bundle can be used in scripts to load packaged resources, etc.

Below there is a Create Script Engine By Extension example:

BundleContext bc = ...;
OSGiScriptEngineManager manager = ...;

OSGiScriptEngine engine = manager.getEngineByExtension("js", bc.getBundle());

...

Core

Scripts, evaluated through Script Engines created from the OSGi Script Engine Manager, have access to helper objects for working with the OSGi Registry, Functional Item Management, logging and access to the owning bundle. These objects are grouped together as fields of the Core object. This object is set as a variable in the ENGINE_SCOPE of the Script Engine under the name of "mbs" (see Core.NAME here ).

An example of the Core object is provided below:

mbs.log.info("Started")

print("Owning Bundle: " + mbs.bundle.symbolicName)

item = mbs.fim.items ['demo:fim:BinarySwitchExt:1']

print(item.UID)

mbs.registry.withService("org.osgi.service.event.EventAdmin", function(eventAdmin) {
   // do  something
});

More details and usage examples are provided in the Script Engine Access, Registry Access and FIM Access guides.