Example usage for io.vertx.core.buffer Buffer toJsonArray

List of usage examples for io.vertx.core.buffer Buffer toJsonArray

Introduction

In this page you can find the example usage for io.vertx.core.buffer Buffer toJsonArray.

Prototype

JsonArray toJsonArray();

Source Link

Document

Returns a Json array representation of the Buffer.

Usage

From source file:com.hpe.sw.cms.verticle.ScanVerticle.java

License:Apache License

@Override
public void start() throws Exception {
    super.start();
    httpClient = vertx.createHttpClient();
    final String dricHost = config().getString("dric.host");
    final String swarmProtocol = config().getString("swarm.protocol");
    final String swarmHost = config().getString("swarm.host");
    final String swarmImage = config().getString("swarm.scan.image");
    final int scanMax = config().getInteger("scanner.max");

    getVertx().setPeriodic(config().getLong("scan.interval", INTERVAL), h -> {
        httpClient.getAbs(swarmProtocol + swarmHost + "/containers/json", new Handler<HttpClientResponse>() {
            @Override/*  ww w  .j ava  2 s.  c  o  m*/
            public void handle(HttpClientResponse httpClientResponse) {
                httpClientResponse.bodyHandler(new Handler<Buffer>() {
                    @Override
                    public void handle(Buffer res) {
                        JsonArray containers = res.toJsonArray();
                        int currentRunning = 0;
                        for (Object obj : containers) {
                            JsonObject container = (JsonObject) obj;
                            if (swarmImage.equals(container.getString("Image"))) {
                                currentRunning++;
                            }
                        }
                        int fetchSize = scanMax - currentRunning;
                        if (fetchSize > 0) {
                            getVertx().eventBus().send(Events.IMAGES_UPDATED.name(), fetchSize, event -> {
                                Message msg = event.result();
                                if (msg != null) {
                                    JsonArray updates = (JsonArray) msg.body();
                                    for (Object obj : updates) {
                                        try {
                                            JsonObject image = (JsonObject) obj;
                                            processImage(dricHost, image.getString("host"),
                                                    image.getString("name"), image.getString("tag"));
                                        } catch (Exception e) {
                                            LOG.error("image sent to Scan error", e);
                                        }
                                    }
                                }
                            });
                        }

                    }
                });
            }
        }).end();
    });

    //delete exited containers of scan.
    getVertx().setPeriodic(config().getLong("scan.interval", INTERVAL), h -> {
        Set containerIds = getVertx().sharedData().getLocalMap("scanContainerIds").keySet();
        containerIds.forEach(containerId -> {
            httpClient.getAbs(swarmProtocol + swarmHost + "/containers/" + containerId + "/json",
                    new Handler<HttpClientResponse>() {
                        @Override
                        public void handle(HttpClientResponse httpClientResponse) {
                            httpClientResponse.bodyHandler(new Handler<Buffer>() {
                                @Override
                                public void handle(Buffer event) {
                                    if ("exited".equals(
                                            event.toJsonObject().getJsonObject("State").getString("Status"))) {
                                        String containerId = event.toJsonObject().getString("Id");
                                        httpClient
                                                .deleteAbs("http://" + swarmHost + "/containers/" + containerId,
                                                        new Handler<HttpClientResponse>() {
                                                            @Override
                                                            public void handle(HttpClientResponse event) {
                                                                LOG.info("delete container with response code :"
                                                                        + event.statusCode());
                                                                getVertx().sharedData()
                                                                        .getLocalMap("scanContainerIds")
                                                                        .remove(containerId);
                                                            }
                                                        })
                                                .end();
                                    }
                                }
                            });
                        }
                    }).end();
        });
    });

}

From source file:org.eclipse.hono.deviceregistry.FileBasedTenantService.java

License:Open Source License

private Future<Void> addAll(final Buffer tenantsBuffer) {

    final Future<Void> result = Future.future();
    try {/*from   ww w .ja v a  2s  .c  o  m*/
        if (tenantsBuffer.length() > 0) {
            int tenantCount = 0;
            final JsonArray allObjects = tenantsBuffer.toJsonArray();
            for (final Object obj : allObjects) {
                if (JsonObject.class.isInstance(obj)) {
                    tenantCount++;
                    addTenant((JsonObject) obj);
                }
            }
            log.info("successfully loaded {} tenants from file [{}]", tenantCount, getConfig().getFilename());
        }
        result.complete();
    } catch (final DecodeException e) {
        log.warn("cannot read malformed JSON from tenants file [{}]", getConfig().getFilename());
        result.fail(e);
    }
    return result;
}