Search in sources :

Example 1 with ApiContext

use of com.bluenimble.platform.api.ApiContext in project serverless by bluenimble.

the class ApiImpl method stop.

public void stop(boolean saveStatus) {
    ApiContext context = new DefaultApiContext();
    // call services manager onStop
    try {
        servicesManager.onStop(context);
    } catch (Exception ex) {
        space.tracer().log(Tracer.Level.Error, "ServiceManager.stop casued an error", ex);
    }
    // call spi onStop
    if (spi != null) {
        try {
            spi.onStop(this, context);
        } catch (Exception ex) {
            space.tracer().log(Tracer.Level.Error, "ApiSpi.onStop casued an error", ex);
        }
    }
    // recycle context
    context.recycle();
    // clear messages
    if (!i18n.isEmpty()) {
        Iterator<String> langs = i18n.keySet().iterator();
        while (langs.hasNext()) {
            i18n.get(langs.next()).clear();
        }
    }
    i18n.clear();
    if (classLoader != null) {
        try {
            classLoader.clear();
        } catch (IOException e) {
            space.tracer().log(Tracer.Level.Error, Lang.BLANK, e);
        }
    }
    // update/save status
    setStatus(ApiStatus.Stopped, saveStatus);
    space.tracer().log(Tracer.Level.Info, "\tapi {0} {1}", getNamespace(), status);
    tracer.onShutdown(this);
}
Also used : ApiContext(com.bluenimble.platform.api.ApiContext) IOException(java.io.IOException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) IOException(java.io.IOException) ApiServiceValidatorException(com.bluenimble.platform.api.validation.ApiServiceValidatorException)

Example 2 with ApiContext

use of com.bluenimble.platform.api.ApiContext in project serverless by bluenimble.

the class ApiImpl method start.

public void start(boolean pause) {
    // create classLoader
    try {
        if (new File(home, ConfigKeys.Folders.Lib).exists() || Json.getArray(descriptor, ConfigKeys.Classpath) != null) {
            this.classLoader = new ApiClassLoader(space.getNamespace() + Lang.DOT + getNamespace(), InstallUtils.toUrls(home, Json.getArray(descriptor, ConfigKeys.Classpath)));
        }
    } catch (Exception ex) {
        failed(ex);
    }
    // start resources manager
    try {
        resourcesManager.onStart();
    } catch (Exception ex) {
        failed(ex);
    }
    // load messages
    try {
        loadI18n();
    } catch (Exception ex) {
        failed(ex);
    }
    // create spi
    try {
        spi = (ApiSpi) BeanUtils.create(this.getClassLoader(), Json.getObject(descriptor, ConfigKeys.Spi), space.getServer().getPluginsRegistry());
    } catch (Exception ex) {
        failed(ex);
    }
    if (spi == null) {
        spi = DefaultApiSpi;
    }
    space.tracer().log(Tracer.Level.Info, "\t  Namespace: {0}", getNamespace());
    space.tracer().log(Tracer.Level.Info, "\t       Name: {0}", getName());
    space.tracer().log(Tracer.Level.Info, "\tDescription: {0}", getDescription());
    // init tracer
    JsonObject oTracer = Json.getObject(descriptor, ConfigKeys.Tracer);
    if (!Json.isNullOrEmpty(oTracer)) {
        try {
            tracer = (Tracer) BeanUtils.create(this.getClassLoader(), oTracer, space.getServer().getPluginsRegistry());
        } catch (Exception ex) {
            failed(ex);
        }
    }
    tracer.onInstall(this);
    ApiContext context = new DefaultApiContext();
    try {
        // start spi
        try {
            // initialize any linked datasource
            JsonArray datasources = Json.getArray(getRuntime(), DataSources);
            if (datasources != null && !datasources.isEmpty()) {
                // change classloader
                for (int i = 0; i < datasources.count(); i++) {
                    Recyclable recyclable = space.getRecyclable(RemoteDataSource.class.getAnnotation(Feature.class).name() + Lang.DOT + space.getNamespace() + Lang.DOT + (String) datasources.get(i));
                    if (recyclable == null) {
                        continue;
                    }
                    // create factory
                    recyclable.set(space, this.getClassLoader(), (String) datasources.get(i));
                }
            }
            // start api
            spi.onStart(this, context);
        } catch (Exception ex) {
            failed(ex);
        }
        // start services manager
        try {
            servicesManager.onStart(context);
        } catch (Exception ex) {
            failed(ex);
        }
    } finally {
        context.recycle();
    }
    // update/save status
    if (!ApiStatus.Failed.equals(status)) {
        setStatus(pause ? ApiStatus.Paused : ApiStatus.Running, true);
        if (Lang.isDebugMode()) {
            final StringBuilder sb = new StringBuilder(space.getNamespace() + Lang.SLASH + getNamespace() + " available services\n");
            servicesManager.list(new Selector() {

                @Override
                public boolean select(ApiService service) {
                    sb.append(Lang.TAB).append(Lang.PARENTH_OPEN).append(service.status().name().substring(0, 1)).append(Lang.PARENTH_CLOSE).append(Lang.SPACE).append(service.getVerb()).append(Lang.COLON).append(Json.getString(service.toJson(), ApiService.Spec.Endpoint));
                    if (ApiStatus.Failed.equals(service.status())) {
                        sb.append(Lang.ENDLN).append(service.getFailure().toString(2));
                    }
                    sb.append(Lang.ENDLN);
                    return false;
                }
            });
            space.tracer().log(Tracer.Level.Info, sb);
            sb.setLength(0);
        }
    }
    space.tracer().log(Tracer.Level.Info, "\tapi {0} {1}", getNamespace(), status);
}
Also used : ApiClassLoader(com.bluenimble.platform.server.impls.ApiClassLoader) JsonObject(com.bluenimble.platform.json.JsonObject) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) IOException(java.io.IOException) ApiServiceValidatorException(com.bluenimble.platform.api.validation.ApiServiceValidatorException) JsonArray(com.bluenimble.platform.json.JsonArray) ApiContext(com.bluenimble.platform.api.ApiContext) ApiService(com.bluenimble.platform.api.ApiService) Recyclable(com.bluenimble.platform.Recyclable) File(java.io.File) Selector(com.bluenimble.platform.api.ApiServicesManager.Selector)

Aggregations

ApiContext (com.bluenimble.platform.api.ApiContext)2 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)2 ApiServiceValidatorException (com.bluenimble.platform.api.validation.ApiServiceValidatorException)2 IOException (java.io.IOException)2 Recyclable (com.bluenimble.platform.Recyclable)1 ApiService (com.bluenimble.platform.api.ApiService)1 Selector (com.bluenimble.platform.api.ApiServicesManager.Selector)1 JsonArray (com.bluenimble.platform.json.JsonArray)1 JsonObject (com.bluenimble.platform.json.JsonObject)1 ApiClassLoader (com.bluenimble.platform.server.impls.ApiClassLoader)1 File (java.io.File)1