Search in sources :

Example 1 with FIELD_DATA

use of org.eclipse.hono.util.RegistrationConstants.FIELD_DATA in project hono by eclipse.

the class FileBasedRegistrationService method saveToFile.

Future<Void> saveToFile() {
    if (!getConfig().isSaveToFile()) {
        return Future.succeededFuture();
    } else if (dirty) {
        return checkFileExists(true).compose(s -> {
            final AtomicInteger idCount = new AtomicInteger();
            final JsonArray tenants = new JsonArray();
            for (Entry<String, Map<String, JsonObject>> entry : identities.entrySet()) {
                final JsonArray devices = new JsonArray();
                for (Entry<String, JsonObject> deviceEntry : entry.getValue().entrySet()) {
                    devices.add(new JsonObject().put(FIELD_PAYLOAD_DEVICE_ID, deviceEntry.getKey()).put(FIELD_DATA, deviceEntry.getValue()));
                    idCount.incrementAndGet();
                }
                tenants.add(new JsonObject().put(FIELD_TENANT, entry.getKey()).put(ARRAY_DEVICES, devices));
            }
            Future<Void> writeHandler = Future.future();
            vertx.fileSystem().writeFile(getConfig().getFilename(), Buffer.factory.buffer(tenants.encodePrettily()), writeHandler.completer());
            return writeHandler.map(ok -> {
                dirty = false;
                log.trace("successfully wrote {} device identities to file {}", idCount.get(), getConfig().getFilename());
                return (Void) null;
            }).otherwise(t -> {
                log.warn("could not write device identities to file {}", getConfig().getFilename(), t);
                return (Void) null;
            });
        });
    } else {
        log.trace("registry does not need to be persisted");
        return Future.succeededFuture();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) FIELD_ENABLED(org.eclipse.hono.util.RequestResponseApiConstants.FIELD_ENABLED) DecodeException(io.vertx.core.json.DecodeException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) BaseRegistrationService(org.eclipse.hono.service.registration.BaseRegistrationService) Future(io.vertx.core.Future) Objects(java.util.Objects) FIELD_PAYLOAD_DEVICE_ID(org.eclipse.hono.util.RequestResponseApiConstants.FIELD_PAYLOAD_DEVICE_ID) JsonArray(io.vertx.core.json.JsonArray) Buffer(io.vertx.core.buffer.Buffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) RegistrationResult(org.eclipse.hono.util.RegistrationResult) FIELD_DATA(org.eclipse.hono.util.RegistrationConstants.FIELD_DATA) Entry(java.util.Map.Entry) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Repository(org.springframework.stereotype.Repository) Handler(io.vertx.core.Handler) JsonArray(io.vertx.core.json.JsonArray) Entry(java.util.Map.Entry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JsonObject(io.vertx.core.json.JsonObject) Future(io.vertx.core.Future)

Aggregations

AsyncResult (io.vertx.core.AsyncResult)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 Buffer (io.vertx.core.buffer.Buffer)1 DecodeException (io.vertx.core.json.DecodeException)1 JsonArray (io.vertx.core.json.JsonArray)1 JsonObject (io.vertx.core.json.JsonObject)1 HttpURLConnection (java.net.HttpURLConnection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Objects (java.util.Objects)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 BaseRegistrationService (org.eclipse.hono.service.registration.BaseRegistrationService)1 FIELD_DATA (org.eclipse.hono.util.RegistrationConstants.FIELD_DATA)1 RegistrationResult (org.eclipse.hono.util.RegistrationResult)1 FIELD_ENABLED (org.eclipse.hono.util.RequestResponseApiConstants.FIELD_ENABLED)1 FIELD_PAYLOAD_DEVICE_ID (org.eclipse.hono.util.RequestResponseApiConstants.FIELD_PAYLOAD_DEVICE_ID)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1