Example usage for io.vertx.core.json JsonArray addAll

List of usage examples for io.vertx.core.json JsonArray addAll

Introduction

In this page you can find the example usage for io.vertx.core.json JsonArray addAll.

Prototype

public JsonArray addAll(JsonArray array) 

Source Link

Document

Appends all of the elements in the specified array to the end of this JSON array.

Usage

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

License:Open Source License

private void saveToFile(final Future<Void> writeResult) {

    if (!dirty) {
        log.trace("credentials registry does not need to be persisted");
        return;//  www. j a  va 2s  .co m
    }

    final FileSystem fs = vertx.fileSystem();
    String filename = getConfig().getCredentialsFilename();

    if (!fs.existsBlocking(filename)) {
        fs.createFileBlocking(filename);
    }
    final AtomicInteger idCount = new AtomicInteger();
    JsonArray tenants = new JsonArray();
    for (Entry<String, Map<String, JsonArray>> entry : credentials.entrySet()) {
        JsonArray credentialsArray = new JsonArray();
        for (Entry<String, JsonArray> credentialEntry : entry.getValue().entrySet()) { // authId -> full json attributes object
            JsonArray singleAuthIdCredentials = credentialEntry.getValue(); // from one authId
            credentialsArray.addAll(singleAuthIdCredentials);
            idCount.incrementAndGet();
        }
        tenants.add(
                new JsonObject().put(FIELD_TENANT, entry.getKey()).put(ARRAY_CREDENTIALS, credentialsArray));
    }
    fs.writeFile(getConfig().getCredentialsFilename(), Buffer.factory.buffer(tenants.encodePrettily()),
            writeAttempt -> {
                if (writeAttempt.succeeded()) {
                    dirty = false;
                    log.trace("successfully wrote {} credentials to file {}", idCount.get(), filename);
                    writeResult.complete();
                } else {
                    log.warn("could not write credentials to file {}", filename, writeAttempt.cause());
                    writeResult.fail(writeAttempt.cause());
                }
            });
}