Search in sources :

Example 1 with Feature

use of com.bluenimble.platform.Feature 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)

Example 2 with Feature

use of com.bluenimble.platform.Feature in project serverless by bluenimble.

the class RemotePlugin method init.

@Override
public void init(final ApiServer server) throws Exception {
    Feature aFeature = Remote.class.getAnnotation(Feature.class);
    if (aFeature == null || Lang.isNullOrEmpty(aFeature.name())) {
        return;
    }
    feature = aFeature.name();
    PackageClassLoader pcl = (PackageClassLoader) RemotePlugin.class.getClassLoader();
    pcl.registerObject(Protocol.http.name(), new HttpRemote(null));
    server.addFeature(new ServerFeature() {

        private static final long serialVersionUID = -9012279234275100528L;

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

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

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

        @Override
        public Plugin implementor() {
            return RemotePlugin.this;
        }
    });
}
Also used : ServerFeature(com.bluenimble.platform.server.ServerFeature) HttpRemote(com.bluenimble.platform.remote.impls.http.HttpRemote) ApiSpace(com.bluenimble.platform.api.ApiSpace) JsonObject(com.bluenimble.platform.json.JsonObject) Feature(com.bluenimble.platform.Feature) ServerFeature(com.bluenimble.platform.server.ServerFeature) PackageClassLoader(com.bluenimble.platform.PackageClassLoader) Plugin(com.bluenimble.platform.plugins.Plugin) AbstractPlugin(com.bluenimble.platform.plugins.impls.AbstractPlugin)

Example 3 with Feature

use of com.bluenimble.platform.Feature in project serverless by bluenimble.

the class AbstractApiServer method getFeature.

@Override
public Object getFeature(ApiSpace space, Class<?> type, String name) {
    Feature aFeature = type.getAnnotation(Feature.class);
    JsonObject oFeature = Json.getObject(space.getFeatures(), aFeature.name());
    if (oFeature == null || oFeature.isEmpty()) {
        throw new FeatureNotFoundException("feature " + aFeature.name() + " not available in space " + space.getNamespace());
    }
    JsonObject oProvider = Json.getObject(oFeature, name);
    if (oProvider == null || oProvider.isEmpty()) {
        throw new FeatureNotFoundException("feature provider " + name + " not available in space " + space.getNamespace());
    }
    String provider = Json.getString(oProvider, ApiSpace.Features.Provider);
    if (Lang.isNullOrEmpty(provider)) {
        throw new FeatureNotFoundException("provider for feature " + aFeature.name() + Lang.SLASH + name + " is missing");
    }
    ServerFeature feature = features.get(aFeature.name() + FeatureProtocol + provider);
    if (feature == null) {
        throw new FeatureNotFoundException("feature " + name + Lang.COLON + aFeature.name() + FeatureProtocol + provider + " not found");
    }
    return feature.get(space, name);
}
Also used : ServerFeature(com.bluenimble.platform.server.ServerFeature) JsonObject(com.bluenimble.platform.json.JsonObject) ServerFeature(com.bluenimble.platform.server.ServerFeature) Feature(com.bluenimble.platform.Feature) FeatureNotFoundException(com.bluenimble.platform.server.FeatureNotFoundException)

Example 4 with Feature

use of com.bluenimble.platform.Feature in project serverless by bluenimble.

the class ElasticSearchPlugin method init.

@Override
public void init(final ApiServer server) throws Exception {
    Feature aFeature = Indexer.class.getAnnotation(Feature.class);
    if (aFeature == null || Lang.isNullOrEmpty(aFeature.name())) {
        return;
    }
    feature = aFeature.name();
    server.addFeature(new ServerFeature() {

        private static final long serialVersionUID = -9012279234275100528L;

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

        @Override
        public Object get(ApiSpace space, String name) {
            String remotePluginName = Json.getString(remote, Spec.RemotePlugin);
            String remoteObjectId = Json.getString(remote, Spec.RemoteObject);
            if (Lang.isNullOrEmpty(remotePluginName) || Lang.isNullOrEmpty(remoteObjectId)) {
                return null;
            }
            Plugin pRemote = server.getPluginsRegistry().lockup(Json.getString(remote, Spec.RemotePlugin));
            if (pRemote == null) {
                return null;
            }
            PackageClassLoader pcl = (PackageClassLoader) pRemote.getClass().getClassLoader();
            Remote iRemote = (Remote) pcl.lookupObject(Json.getString(remote, Spec.RemoteObject));
            if (iRemote == null) {
                return null;
            }
            // build basic auth and url
            String token = buildAuthToken(space, name);
            if (Lang.isNullOrEmpty(token)) {
                return null;
            }
            String url = buildUrl(space, name);
            if (Lang.isNullOrEmpty(url)) {
                return null;
            }
            return new ElasticSearchIndexer(iRemote, url, token, tracer());
        }

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

        @Override
        public Plugin implementor() {
            return ElasticSearchPlugin.this;
        }
    });
}
Also used : ServerFeature(com.bluenimble.platform.server.ServerFeature) ApiSpace(com.bluenimble.platform.api.ApiSpace) Remote(com.bluenimble.platform.remote.Remote) JsonObject(com.bluenimble.platform.json.JsonObject) Feature(com.bluenimble.platform.Feature) ServerFeature(com.bluenimble.platform.server.ServerFeature) PackageClassLoader(com.bluenimble.platform.PackageClassLoader) Plugin(com.bluenimble.platform.plugins.Plugin) AbstractPlugin(com.bluenimble.platform.plugins.impls.AbstractPlugin) ElasticSearchIndexer(com.bluenimble.platform.indexer.impls.ElasticSearchIndexer)

Example 5 with Feature

use of com.bluenimble.platform.Feature in project serverless by bluenimble.

the class ApiSpaceImpl method feature.

@SuppressWarnings("unchecked")
@Override
public <T> T feature(Class<T> t, String name, ApiContext context) {
    if (context == null) {
        throw new FeatureNotFoundException("context not available");
    }
    if (Lang.isNullOrEmpty(name)) {
        throw new FeatureNotFoundException("provider can't be null or empty");
    }
    Feature aFeature = t.getAnnotation(Feature.class);
    if (aFeature == null || Lang.isNullOrEmpty(aFeature.name())) {
        throw new FeatureNotFoundException("feature " + t.getSimpleName() + " not registered in this instance");
    }
    String recyclableKey = contextRecyclableKey(aFeature.name(), name);
    Recyclable feature = context.getRecyclable(recyclableKey);
    if (feature != null) {
        return (T) feature;
    }
    T f = (T) server.getFeature(this, t, name);
    if (f == null) {
        throw new FeatureNotFoundException("feature " + name + " not found");
    }
    if (Recyclable.class.isAssignableFrom(f.getClass())) {
        context.addRecyclable(recyclableKey, (Recyclable) f);
    }
    return f;
}
Also used : Recyclable(com.bluenimble.platform.Recyclable) FeatureNotFoundException(com.bluenimble.platform.server.FeatureNotFoundException) Feature(com.bluenimble.platform.Feature)

Aggregations

Feature (com.bluenimble.platform.Feature)13 ServerFeature (com.bluenimble.platform.server.ServerFeature)12 ApiSpace (com.bluenimble.platform.api.ApiSpace)10 JsonObject (com.bluenimble.platform.json.JsonObject)10 Plugin (com.bluenimble.platform.plugins.Plugin)10 AbstractPlugin (com.bluenimble.platform.plugins.impls.AbstractPlugin)9 PackageClassLoader (com.bluenimble.platform.PackageClassLoader)3 FeatureNotFoundException (com.bluenimble.platform.server.FeatureNotFoundException)3 File (java.io.File)2 Recyclable (com.bluenimble.platform.Recyclable)1 DescribeOption (com.bluenimble.platform.api.DescribeOption)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 MemCachedCache (com.bluenimble.platform.cache.impls.memcached.MemCachedCache)1 ElasticSearchIndexer (com.bluenimble.platform.indexer.impls.ElasticSearchIndexer)1 JsonArray (com.bluenimble.platform.json.JsonArray)1 MongoDatabaseImpl (com.bluenimble.platform.plugins.database.mongodb.impls.MongoDatabaseImpl)1 OrientDatabase (com.bluenimble.platform.plugins.database.orientdb.impls.OrientDatabase)1 Remote (com.bluenimble.platform.remote.Remote)1