Search in sources :

Example 1 with Selector

use of com.bluenimble.platform.api.ApiServicesManager.Selector in project serverless by bluenimble.

the class ApiImpl method describe.

@Override
public JsonObject describe(final DescribeOption... options) {
    if (options == null || options.length == 0) {
        return JsonObject.Blank;
    }
    final Map<DescribeOption.Option, DescribeOption> opts = DescribeUtils.toMap(options);
    JsonObject describe = new JsonObject();
    if (opts.containsKey(DescribeOption.Option.info)) {
        describe.set(Api.Spec.Namespace, getNamespace());
        describe.set(Api.Spec.Name, getName());
        describe.set(Api.Spec.Description, getDescription());
        describe.set(Api.Spec.Status, status().name());
        if (failure != null) {
            describe.set(Describe.Failure, failure);
        }
        describe.set(Api.Spec.Release, getRelease());
    }
    if (getSecurity() != null && opts.containsKey(DescribeOption.Option.security)) {
        describe.set(Api.Spec.Security.class.getSimpleName().toLowerCase(), getSecurity().duplicate());
    }
    if (getTracking() != null && opts.containsKey(DescribeOption.Option.tracking)) {
        describe.set(Api.Spec.Tracking.class.getSimpleName().toLowerCase(), getTracking().duplicate());
    }
    if (getFeatures() != null && opts.containsKey(DescribeOption.Option.features)) {
        describe.set(Api.Spec.Features, getFeatures().duplicate());
    }
    if (getCustom() != null && opts.containsKey(DescribeOption.Option.custom)) {
        describe.set(Api.Spec.Custom, getCustom().duplicate());
    }
    if (servicesManager.isEmpty(null)) {
        return describe;
    }
    final JsonObject failedServices = new JsonObject();
    describe.set(Describe.FailedServices, failedServices);
    JsonArray aServices = new JsonArray();
    describe.set(Describe.Services, aServices);
    final JsonArray fServices = aServices;
    final ValueHolder<Integer> apiMarkers = new ValueHolder<Integer>(0);
    servicesManager.list(new Selector() {

        @Override
        public boolean select(ApiService service) {
            if (opts.containsKey(DescribeOption.Option.services)) {
                DescribeOption opt = opts.get(DescribeOption.Option.services);
                JsonObject jService = service.toJson();
                JsonObject sDesc = jService;
                if (!opt.isVerbose()) {
                    sDesc = new JsonObject();
                    sDesc.set(ApiService.Spec.Name, jService.get(ApiService.Spec.Name));
                    sDesc.set(ApiService.Spec.Endpoint, jService.get(ApiService.Spec.Endpoint));
                    sDesc.set(ApiService.Spec.Verb, jService.get(ApiService.Spec.Verb));
                    sDesc.set(ApiService.Spec.Status, jService.get(ApiService.Spec.Status));
                    sDesc.set(ApiService.Spec.Security.class.getSimpleName().toLowerCase(), service.getSecurity());
                    JsonArray aMarkers = Json.getArray(jService, Api.Spec.Markers);
                    int markers = aMarkers != null ? aMarkers.size() : 0;
                    apiMarkers.set(apiMarkers.get() + markers);
                    if (markers > 0) {
                        sDesc.set(Api.Spec.Markers, markers);
                    }
                }
                fServices.add(sDesc);
            }
            if (ApiStatus.Failed.equals(service.status())) {
                failedServices.put(service.getVerb().name() + Lang.SPACE + Json.getString(service.toJson(), ApiService.Spec.Endpoint), service.getFailure());
            }
            return false;
        }
    });
    describe.set(Api.Spec.Markers, apiMarkers.get());
    if (fServices.isEmpty()) {
        describe.remove(Describe.Services);
    }
    if (failedServices.isEmpty()) {
        describe.remove(Describe.FailedServices);
    }
    return describe;
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) ValueHolder(com.bluenimble.platform.ValueHolder) JsonArray(com.bluenimble.platform.json.JsonArray) ApiService(com.bluenimble.platform.api.ApiService) DescribeOption(com.bluenimble.platform.api.DescribeOption) DescribeOption(com.bluenimble.platform.api.DescribeOption) Selector(com.bluenimble.platform.api.ApiServicesManager.Selector)

Example 2 with Selector

use of com.bluenimble.platform.api.ApiServicesManager.Selector 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)

Example 3 with Selector

use of com.bluenimble.platform.api.ApiServicesManager.Selector in project serverless by bluenimble.

the class ApiImpl method lockup.

protected ApiService lockup(final ApiVerb verb, String[] endpoint) {
    if (servicesManager.isEmpty(verb)) {
        return null;
    }
    String path = Lang.SLASH;
    if (endpoint != null && endpoint.length > 0) {
        path += Lang.join(endpoint, Lang.SLASH);
    }
    ApiService service = servicesManager.get(verb, path);
    if (service != null) {
        return service;
    }
    final String wildcard = path;
    final ValueHolder<ApiService> holder = new ValueHolder<ApiService>();
    servicesManager.list(new Selector() {

        @Override
        public boolean select(ApiService service) {
            WildcardMatcher matcher = new WildcardMatcher(WildcardCompiler.compile(service.getEndpoint()), wildcard);
            if (matcher.find() && service.getVerb().equals(verb)) {
                holder.set(service);
                return true;
            }
            return false;
        }
    });
    return holder.get();
}
Also used : ApiService(com.bluenimble.platform.api.ApiService) WildcardMatcher(com.bluenimble.platform.regex.WildcardMatcher) ValueHolder(com.bluenimble.platform.ValueHolder) Selector(com.bluenimble.platform.api.ApiServicesManager.Selector)

Aggregations

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