List of usage examples for io.vertx.core.file FileSystem existsBlocking
boolean existsBlocking(String path);
From source file:de.braintags.netrelay.controller.filemanager.elfinder.ElFinderController.java
License:Open Source License
@Override public void initProperties(Properties properties) { String dirs = readProperty(ROOT_DIRECTORIES_PROPERTY, null, true); String[] volSpecs = dirs.split(","); for (String volSpec : volSpecs) { String[] specs = volSpec.trim().split(":"); FileSystem fs = getVertx().fileSystem(); String volumeId = specs[0].trim(); String dirName = specs[1].trim(); if (!fs.existsBlocking(dirName)) { throw new InitException("Root directory with path '" + dirName + "' does not exist"); }/*from w w w .ja va 2s . com*/ Path path = FileSystems.getDefault().getPath(dirName); LOGGER.debug("ElFinder-Path: " + path); if (dirName.endsWith("/")) { dirName = dirName.substring(0, dirName.length() - 1); } rootVolumes.add(new VertxVolume(fs, path, volumeId, null)); } }
From source file:de.braintags.netrelay.controller.persistence.InsertAction.java
License:Open Source License
private String[] examineNewDestination(FileSystem fs, FileUpload upload) { if (upload.fileName() == null || upload.fileName().hashCode() == 0) { throw new FileNameException("The upload contains no filename"); }//from w ww . j av a2 s. com String[] destinations = new String[2]; String upDir = getPersistenceController().readProperty(PersistenceController.UPLOAD_DIRECTORY_PROP, null, true); if (!fs.existsBlocking(upDir)) { fs.mkdirsBlocking(upDir); } String relDir = getPersistenceController().readProperty(PersistenceController.UPLOAD_RELATIVE_PATH_PROP, null, true); String fileName = createUniqueName(fs, upDir, upload.fileName()); destinations[0] = upDir + (upDir.endsWith("/") ? "" : "/") + fileName; destinations[1] = relDir + (relDir.endsWith("/") ? "" : "/") + fileName; return destinations; }
From source file:de.braintags.netrelay.controller.persistence.InsertAction.java
License:Open Source License
private String createUniqueName(FileSystem fs, String upDir, String fileInName) { final String fileName = cleanFileName(fileInName); String newFileName = fileName; int counter = 0; String path = createPath(upDir, fileName); while (fs.existsBlocking(path)) { LOGGER.info("file exists already: " + path); if (fileName.indexOf('.') >= 0) { newFileName = fileName.replaceFirst("\\.", "_" + counter++ + "."); } else {/*from w ww . j a va 2 s. co m*/ newFileName = fileName + "_" + counter++; } path = createPath(upDir, newFileName); } return newFileName; }
From source file:de.braintags.netrelay.controller.persistence.PersistenceController.java
License:Open Source License
@Override protected void internalInitProperties(Properties properties) { displayAction = new DisplayAction(this); insertAction = new InsertAction(this); updateAction = new UpdateAction(this); deleteAction = new DeleteAction(this); noneAction = new NoneAction(this); mapperFactory = getNetRelay().getNetRelayMapperFactory(); String upDir = readProperty(PersistenceController.UPLOAD_DIRECTORY_PROP, null, true); FileSystem fs = getVertx().fileSystem(); if (!fs.existsBlocking(upDir)) { fs.mkdirsBlocking(upDir);//from w w w .j a v a 2 s . c o m if (!fs.existsBlocking(upDir)) { throw new InitException("could not create directory " + upDir); } else { LOGGER.info("Upload directory created: " + upDir); } } }
From source file:de.braintags.netrelay.init.Settings.java
License:Open Source License
private static Settings loadSettings(NetRelay netRelay, Vertx vertx, String path) { FileSystem fs = vertx.fileSystem(); if (fs.existsBlocking(path)) { LOGGER.info("going to load settings from " + path); Buffer buffer = fs.readFileBlocking(path); Settings settings = Json.decodeValue(buffer.toString(), Settings.class); LOGGER.info("settings successfully loaded from " + path); return settings; } else {/*from w w w. j a va2 s. com*/ LOGGER.info("creating default settings and store them in " + path); Settings settings = netRelay.createDefaultSettings(); fs.writeFileBlocking(path, Buffer.buffer(Json.encodePrettily(settings))); String message = String.format( "Settings file did not exist and was created new in path %s. NOTE: edit the file, set edited to true and restart the server", path); throw new InitException(message); } }
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 {/* w ww . j a v a 2s.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.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 a v a 2 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
Future<Void> loadRegistrationData() { Future<Void> result = Future.future(); if (getConfig().getFilename() == null) { result.fail(new IllegalStateException("device identity filename is not set")); } else {/*w ww . j a va2s .c o m*/ 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.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 ww w . j a v a 2s . 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 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 . j a v a 2s . c om 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); } } }