Script Engine Access provides methods for finding and loading scripts from bundles or the default scripts directory.
Overview
Script Engine Access is one of the helper objects available to all scripts that are executed through the OSGiScriptEngine and created by the OSGiScriptEngineManager. It is available to scripts through the Core.getEngine() field and can be accessed in most script languages simply as "mbs.engine".
The Script Engine Access provides methods for finding and loading scripts from bundles or the default scripts directory.
Find Script
The findScript methods can be used to find a script URL by its path. There are two findScript methods:
The default scripts directory is defined in the configuration of Scripting Core Bundle. It is ../../../scripts by default.
The URL returned by the findScript methods can be used with methods provided by the language for loading scripts (for example the load method in Nashorn Script Engine, the JavaScript engine which comes with Java 1.8+):
myImportUrl = mbs.engine.findScript("lib/myLib.js")
load(myLibUrl)
thirdPartyLibUrl = mbs.engine.findScript("lib.js", "third.party.bundle")
load(thirdPartyLibUrl )
Load Script
The loadScript methods can be used to find and execute a script from a given bundle (or from the default scripts directory) – effectively loading/importing the script in the engine state. They use the corresponding findScript methods to obtain the script URL and then the OSGiScriptEngine, evaluating the current script is used to execute the found script.
There are two loadScript methods:
mbs.engine.load("scripts/lib/digits.js"); //load script file from "my" bundle
mbs.engine.load("scripts/lib/median.js", "com.prosyst.mbs.scripting.engine.access-demo") //load script file from bundle with given symbolic name
var digits = randomDigits(7); //randomDigits is function from digits.js
var median = median(digits); //median is function from median.js
print("The median of " + digits + " is " + median);
The Script Engine Access Demo shows how loadScipt methods can be used.