Example usage for io.vertx.core.file FileSystem createFileBlocking

List of usage examples for io.vertx.core.file FileSystem createFileBlocking

Introduction

In this page you can find the example usage for io.vertx.core.file FileSystem createFileBlocking.

Prototype

@Fluent
FileSystem createFileBlocking(String path);

Source Link

Document

Blocking version of #createFile(String,Handler)

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;/* w w w  . j a va2s .c  o  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());
                }
            });
}

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

License:Open Source License

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

    if (!dirty) {
        log.trace("registry does not need to be persisted");
        return;/*from   w ww  . j  ava  2  s .  c o m*/
    }

    final FileSystem fs = vertx.fileSystem();
    if (!fs.existsBlocking(getConfig().getFilename())) {
        fs.createFileBlocking(getConfig().getFilename());
    }
    final AtomicInteger idCount = new AtomicInteger();
    JsonArray tenants = new JsonArray();
    for (Entry<String, Map<String, JsonObject>> entry : identities.entrySet()) {
        JsonArray devices = new JsonArray();
        for (Entry<String, JsonObject> deviceEntry : entry.getValue().entrySet()) {
            devices.add(new JsonObject().put(FIELD_DEVICE_ID, deviceEntry.getKey()).put(FIELD_DATA,
                    deviceEntry.getValue()));
            idCount.incrementAndGet();
        }
        tenants.add(new JsonObject().put(FIELD_TENANT, entry.getKey()).put(ARRAY_DEVICES, devices));
    }
    fs.writeFile(getConfig().getFilename(), Buffer.factory.buffer(tenants.encodePrettily()), writeAttempt -> {
        if (writeAttempt.succeeded()) {
            dirty = false;
            log.trace("successfully wrote {} device identities to file {}", idCount.get(),
                    getConfig().getFilename());
            writeResult.complete();
        } else {
            log.warn("could not write device identities to file {}", getConfig().getFilename(),
                    writeAttempt.cause());
            writeResult.fail(writeAttempt.cause());
        }
    });
}

From source file:org.eclipse.hono.service.credentials.impl.FileBasedCredentialsService.java

License:Open Source License

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

    if (!dirty) {
        log.trace("credentials registry does not need to be persisted");
        return;//from   w  w  w.  j  a v  a  2 s.  c  o  m
    }

    final FileSystem fs = vertx.fileSystem();
    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.add(singleAuthIdCredentials);
            idCount.incrementAndGet();
        }
        tenants.add(
                new JsonObject().put(FIELD_TENANT, entry.getKey()).put(ARRAY_CREDENTIALS, credentialsArray));
    }
    fs.writeFile(filename, 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());
        }
    });
}

From source file:org.eclipse.hono.service.registration.impl.FileBasedRegistrationService.java

License:Open Source License

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

    if (!dirty) {
        log.trace("registry does not need to be persisted");
        return;//from w  ww.  j a v a 2s  . co  m
    }

    final FileSystem fs = vertx.fileSystem();
    if (!fs.existsBlocking(filename)) {
        fs.createFileBlocking(filename);
    }
    final AtomicInteger idCount = new AtomicInteger();
    JsonArray tenants = new JsonArray();
    for (Entry<String, Map<String, JsonObject>> entry : identities.entrySet()) {
        JsonArray devices = new JsonArray();
        for (Entry<String, JsonObject> deviceEntry : entry.getValue().entrySet()) {
            devices.add(new JsonObject().put(FIELD_HONO_ID, deviceEntry.getKey()).put(FIELD_DATA,
                    deviceEntry.getValue()));
            idCount.incrementAndGet();
        }
        tenants.add(new JsonObject().put(FIELD_TENANT, entry.getKey()).put(ARRAY_DEVICES, devices));
    }
    fs.writeFile(filename, Buffer.factory.buffer(tenants.encodePrettily()), writeAttempt -> {
        if (writeAttempt.succeeded()) {
            dirty = false;
            log.trace("successfully wrote {} device identities to file {}", idCount.get(), filename);
            writeResult.complete();
        } else {
            log.warn("could not write device identities to file {}", filename, writeAttempt.cause());
            writeResult.fail(writeAttempt.cause());
        }
    });
}