Search in sources :

Example 1 with ScriptableApiSpi

use of com.bluenimble.platform.api.impls.scripting.ScriptableApiSpi in project serverless by bluenimble.

the class ScriptingPlugin method init.

@Override
public void init(final ApiServer server) throws Exception {
    if (Lang.isNullOrEmpty(vmArgs)) {
        masterEngine = Manager.getScriptEngine(ScriptingPlugin.class.getClassLoader());
    } else {
        masterEngine = Manager.getScriptEngine(Lang.split(vmArgs, Lang.SPACE, true), ScriptingPlugin.class.getClassLoader());
    }
    Feature aFeature = ScriptingEngine.class.getAnnotation(Feature.class);
    if (aFeature == null || Lang.isNullOrEmpty(aFeature.name())) {
        return;
    }
    PackageClassLoader pcl = (PackageClassLoader) ScriptingPlugin.class.getClassLoader();
    pcl.registerObject(Registered.ApiSpi, new ScriptableApiSpi());
    pcl.registerObject(Registered.ServiceSpi, new ScriptableApiServiceSpi());
    File platform = new File(home, "platform");
    // load platform
    Reader pReader = null;
    try {
        pReader = new FileReader(new File(platform, "Platform.js"));
        Bindings bindings = new SimpleBindings();
        bindings.put(Vars.Core, new File(platform, Vars.Core).getAbsolutePath());
        bindings.put(Vars.Tools, new File(platform, Vars.Tools).getAbsolutePath());
        shared = new DefaultScriptingEngine(this, (ScriptObjectMirror) masterEngine.eval(pReader, bindings), server.getMapProvider());
    } finally {
        IOUtils.closeQuietly(pReader);
    }
    // add features
    server.addFeature(new ServerFeature() {

        private static final long serialVersionUID = 2626039344401539390L;

        @Override
        public Class<?> type() {
            return ScriptingEngine.class;
        }

        @Override
        public Object get(ApiSpace space, String name) {
            return shared;
        }

        @Override
        public String provider() {
            return ScriptingPlugin.this.getName();
        }

        @Override
        public Plugin implementor() {
            return ScriptingPlugin.this;
        }
    });
}
Also used : ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) DefaultScriptingEngine(com.bluenimble.platform.api.scripting.impls.DefaultScriptingEngine) Reader(java.io.Reader) FileReader(java.io.FileReader) Feature(com.bluenimble.platform.Feature) ServerFeature(com.bluenimble.platform.server.ServerFeature) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) ScriptableApiServiceSpi(com.bluenimble.platform.api.impls.scripting.ScriptableApiServiceSpi) ServerFeature(com.bluenimble.platform.server.ServerFeature) ApiSpace(com.bluenimble.platform.api.ApiSpace) SimpleBindings(javax.script.SimpleBindings) ScriptableApiSpi(com.bluenimble.platform.api.impls.scripting.ScriptableApiSpi) FileReader(java.io.FileReader) File(java.io.File) PackageClassLoader(com.bluenimble.platform.PackageClassLoader) Plugin(com.bluenimble.platform.plugins.Plugin) AbstractPlugin(com.bluenimble.platform.plugins.impls.AbstractPlugin)

Aggregations

Feature (com.bluenimble.platform.Feature)1 PackageClassLoader (com.bluenimble.platform.PackageClassLoader)1 ApiSpace (com.bluenimble.platform.api.ApiSpace)1 ScriptableApiServiceSpi (com.bluenimble.platform.api.impls.scripting.ScriptableApiServiceSpi)1 ScriptableApiSpi (com.bluenimble.platform.api.impls.scripting.ScriptableApiSpi)1 DefaultScriptingEngine (com.bluenimble.platform.api.scripting.impls.DefaultScriptingEngine)1 Plugin (com.bluenimble.platform.plugins.Plugin)1 AbstractPlugin (com.bluenimble.platform.plugins.impls.AbstractPlugin)1 ServerFeature (com.bluenimble.platform.server.ServerFeature)1 File (java.io.File)1 FileReader (java.io.FileReader)1 Reader (java.io.Reader)1 Bindings (javax.script.Bindings)1 SimpleBindings (javax.script.SimpleBindings)1 ScriptObjectMirror (jdk.nashorn.api.scripting.ScriptObjectMirror)1