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

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

Introduction

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

Prototype

@Fluent
FileSystem writeFile(String path, Buffer data, Handler<AsyncResult<Void>> handler);

Source Link

Document

Creates the file, and writes the specified Buffer data to the file represented by the path path , asynchronously.

Usage

From source file:examples.CoreExamples.java

License:Open Source License

public void exampleFuture6(Vertx vertx) {

    FileSystem fs = vertx.fileSystem();
    Future<Void> startFuture = Future.future();

    Future<Void> fut1 = Future.future();
    fs.createFile("/foo", fut1.completer());

    fut1.compose(v -> {//from   w w  w .  j  a  v a2s. c o  m
        // When the file is created (fut1), execute this:
        Future<Void> fut2 = Future.future();
        fs.writeFile("/foo", Buffer.buffer(), fut2.completer());
        return fut2;
    }).compose(v -> {
        // When the file is written (fut2), execute this:
        fs.move("/foo", "/bar", startFuture.completer());
    },
            // mark startFuture it as failed if any step fails.
            startFuture);
}

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;/*from  w ww  .  j  av a2  s.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());
                }
            });
}

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 w  w. j av a2  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;/*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  w w  .  java  2  s  .  c  om*/
    }

    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());
        }
    });
}