Search in sources :

Example 1 with ValueHolder

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

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

the class FileSystemFolder method list.

@Override
public void list(Visitor visitor, Filter filter) throws StorageException {
    if (visitor == null) {
        return;
    }
    final ValueHolder<FileSystemStorageObject> vh = new ValueHolder<FileSystemStorageObject>();
    vh.set(new FileSystemStorageObject());
    DirectoryStream<Path> stream = null;
    try {
        stream = Files.newDirectoryStream(source.toPath(), new DirectoryStream.Filter<Path>() {

            public boolean accept(Path path) throws IOException {
                vh.get().setSource(path.toFile());
                return filter == null ? true : filter.accept(vh.get());
            }
        });
        if (stream == null) {
            return;
        }
        for (Path path : stream) {
            vh.get().setSource(path.toFile());
            if (vh.get().isFolder()) {
                try {
                    vh.set(vh.get().toFolder());
                } catch (StorageException e) {
                    throw new IOException(e.getMessage(), e);
                }
            }
            visitor.visit(vh.get());
        }
    } catch (IOException e) {
        throw new StorageException(e.getMessage(), e);
    } finally {
        try {
            if (stream != null)
                stream.close();
        } catch (IOException ex) {
        }
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) ValueHolder(com.bluenimble.platform.ValueHolder) StorageException(com.bluenimble.platform.storage.StorageException)

Example 3 with ValueHolder

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

ValueHolder (com.bluenimble.platform.ValueHolder)3 ApiService (com.bluenimble.platform.api.ApiService)2 Selector (com.bluenimble.platform.api.ApiServicesManager.Selector)2 DescribeOption (com.bluenimble.platform.api.DescribeOption)1 JsonArray (com.bluenimble.platform.json.JsonArray)1 JsonObject (com.bluenimble.platform.json.JsonObject)1 WildcardMatcher (com.bluenimble.platform.regex.WildcardMatcher)1 StorageException (com.bluenimble.platform.storage.StorageException)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1