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

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

Introduction

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

Prototype

@Fluent
FileSystem readFile(String path, Handler<AsyncResult<Buffer>> handler);

Source Link

Document

Reads the entire file as represented by the path path as a Buffer , asynchronously.

Usage

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

License:Open Source License

Future<Void> loadCredentials() {
    Future<Void> result = Future.future();
    if (getConfig().getCredentialsFilename() == null) {
        result.fail(new IllegalStateException("credentials filename is not set"));
    } else {/*www .  ja v  a2 s  .  co  m*/
        final FileSystem fs = vertx.fileSystem();
        log.debug("trying to load credentials information from file {}", getConfig().getCredentialsFilename());

        if (fs.existsBlocking(getConfig().getCredentialsFilename())) {
            log.info("loading credentials from file [{}]", getConfig().getCredentialsFilename());
            fs.readFile(getConfig().getCredentialsFilename(), readAttempt -> {
                if (readAttempt.succeeded()) {
                    JsonArray allObjects = readAttempt.result().toJsonArray();
                    parseCredentials(allObjects);
                    result.complete();
                } else {
                    result.fail(readAttempt.cause());
                }
            });
        } else {
            log.debug("credentials file [{}] does not exist (yet)", getConfig().getCredentialsFilename());
            result.complete();
        }
    }
    return result;
}

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

License:Open Source License

Future<Void> loadRegistrationData() {
    Future<Void> result = Future.future();

    if (getConfig().getFilename() == null) {
        result.fail(new IllegalStateException("device identity filename is not set"));
    } else {//from   w w  w. j  a va 2  s . c  om

        final FileSystem fs = vertx.fileSystem();
        log.debug("trying to load device registration information from file {}", getConfig().getFilename());
        if (fs.existsBlocking(getConfig().getFilename())) {
            final AtomicInteger deviceCount = new AtomicInteger();
            fs.readFile(getConfig().getFilename(), readAttempt -> {
                if (readAttempt.succeeded()) {
                    JsonArray allObjects = new JsonArray(new String(readAttempt.result().getBytes()));
                    for (Object obj : allObjects) {
                        JsonObject tenant = (JsonObject) obj;
                        String tenantId = tenant.getString(FIELD_TENANT);
                        log.debug("loading devices for tenant [{}]", tenantId);
                        Map<String, JsonObject> deviceMap = new HashMap<>();
                        for (Object deviceObj : tenant.getJsonArray(ARRAY_DEVICES)) {
                            JsonObject device = (JsonObject) deviceObj;
                            String deviceId = device.getString(FIELD_DEVICE_ID);
                            log.debug("loading device [{}]", deviceId);
                            deviceMap.put(deviceId, device.getJsonObject(FIELD_DATA));
                            deviceCount.incrementAndGet();
                        }
                        identities.put(tenantId, deviceMap);
                    }
                    log.info("successfully loaded {} device identities from file [{}]", deviceCount.get(),
                            getConfig().getFilename());
                    result.complete();
                } else {
                    log.warn("could not load device identities from file [{}]", getConfig().getFilename());
                    result.fail(readAttempt.cause());
                }
            });
        } else {
            log.debug("device identity file [{}] does not exist (yet)", getConfig().getFilename());
            result.complete();
        }
    }
    return result;
}

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

License:Open Source License

protected void loadCredentialsData() {
    if (filename != null) {
        final FileSystem fs = vertx.fileSystem();
        log.debug("trying to load credentials information from file {}", filename);
        if (fs.existsBlocking(filename)) {
            final AtomicInteger credentialsCount = new AtomicInteger();
            fs.readFile(filename, readAttempt -> {
                if (readAttempt.succeeded()) {
                    JsonArray allObjects = new JsonArray(new String(readAttempt.result().getBytes()));
                    for (Object obj : allObjects) {
                        JsonObject tenant = (JsonObject) obj;
                        String tenantId = tenant.getString(FIELD_TENANT);
                        Map<String, JsonArray> credentialsMap = new HashMap<>();
                        for (Object credentialsObj : tenant.getJsonArray(ARRAY_CREDENTIALS)) {
                            JsonObject credentials = (JsonObject) credentialsObj;
                            JsonArray authIdCredentials;
                            if (credentialsMap.containsKey(credentials.getString(FIELD_AUTH_ID))) {
                                authIdCredentials = credentialsMap.get(credentials.getString(FIELD_AUTH_ID));
                            } else {
                                authIdCredentials = new JsonArray();
                            }/*from   w  w w.  ja va2s .  com*/
                            authIdCredentials.add(credentials);
                            credentialsMap.put(credentials.getString(FIELD_AUTH_ID), authIdCredentials);
                            credentialsCount.incrementAndGet();
                        }
                        credentials.put(tenantId, credentialsMap);
                    }
                    log.info("successfully loaded {} credentials from file [{}]", credentialsCount.get(),
                            filename);
                } else {
                    log.warn("could not load credentials from file [{}]", filename, readAttempt.cause());
                }
            });
        } else {
            log.debug("credentials file {} does not exist (yet)", filename);
        }
    }
}

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

License:Open Source License

private void loadRegistrationData() {
    if (filename != null) {
        final FileSystem fs = vertx.fileSystem();
        log.debug("trying to load device registration information from file {}", filename);
        if (fs.existsBlocking(filename)) {
            final AtomicInteger deviceCount = new AtomicInteger();
            fs.readFile(filename, readAttempt -> {
                if (readAttempt.succeeded()) {
                    JsonArray allObjects = new JsonArray(new String(readAttempt.result().getBytes()));
                    for (Object obj : allObjects) {
                        JsonObject tenant = (JsonObject) obj;
                        String tenantId = tenant.getString(FIELD_TENANT);
                        Map<String, JsonObject> deviceMap = new HashMap<>();
                        for (Object deviceObj : tenant.getJsonArray(ARRAY_DEVICES)) {
                            JsonObject device = (JsonObject) deviceObj;
                            deviceMap.put(device.getString(FIELD_HONO_ID), device.getJsonObject(FIELD_DATA));
                            deviceCount.incrementAndGet();
                        }// w  w w .  ja  v a 2  s  . c  o m
                        identities.put(tenantId, deviceMap);
                    }
                    log.info("successfully loaded {} device identities from file [{}]", deviceCount.get(),
                            filename);
                } else {
                    log.warn("could not load device identities from file [{}]", filename, readAttempt.cause());
                }
            });
        } else {
            log.debug("device identity file {} does not exist (yet)", filename);
        }
    }
}