private List<ConfigGroup> getConfiguration(final Path dir, final ConfigType type) { final List<ConfigGroup> configGroups = new ArrayList<>(); if (!ioService.exists(dir)) { return configGroups; } final DirectoryStream<Path> foundConfigs = getDirectoryStreamForFilesWithParticularExtension(dir, type.getExt()); //Only load and cache if a file was found! final Iterator<Path> it = foundConfigs.iterator(); if (it.hasNext()) { while (it.hasNext()) { final String content = ioService.readAllString(it.next()); final ConfigGroup configGroup = marshaller.unmarshall(content); configGroups.add(configGroup); } return configGroups; } return null; }
@Override public List<ConfigGroup> getConfiguration(ConfigType configType) { if (ConfigType.SPACE.equals(configType)) { configType = ConfigType.ORGANIZATIONAL_UNIT; } final ConfigType type = configType; final List<ConfigGroup> configGroups = new ArrayList<>(); final DirectoryStream<Path> foundConfigs = ioService.newDirectoryStream(ioService.get(systemRepository.getUri()), entry -> { if (!Files.isDirectory(entry) && !entry.getFileName().toString().startsWith(".") && entry.getFileName().toString().endsWith(type.getExt())) { return true; } return false; } ); //Only load and cache if a file was found! final Iterator<Path> it = foundConfigs.iterator(); if (it.hasNext()) { while (it.hasNext()) { final String content = ioService.readAllString(it.next()); final ConfigGroup configGroup = marshaller.unmarshall(content); configGroups.add(configGroup); } configGroupsByTypeWithoutNamespace.put(type, configGroups); } return configGroups; }
private Path resolveConfigGroupPath(final ConfigGroup configGroup) { final ConfigType type = configGroup.getType(); final String namespace = configGroup.getNamespace(); if (type.hasNamespace() && (namespace == null || namespace.isEmpty())) { throw new RuntimeException("The ConfigType " + type.toString() + " requires a namespace."); } else if (!type.hasNamespace() && namespace != null && !namespace.isEmpty()) { throw new RuntimeException("The ConfigType " + type.toString() + " does not support namespaces."); } final String filename = configGroup.getName().replaceAll(INVALID_FILENAME_CHARS, "_"); Path path = ioService.get(systemRepository.getUri()).resolve(type.getDir()); if (type.hasNamespace()) { path = path.resolve(namespace); } return path.resolve(filename + type.getExt()); }
@Override public boolean removeConfiguration(final ConfigGroup configGroup) { //Invalidate cache if an item has been removed; otherwise cached value is stale configuration.remove(configGroup.getType()); String filename = configGroup.getName().replaceAll(INVALID_FILENAME_CHARS, "_"); final Path filePath = ioService.get(systemRepository.getUri()).resolve(filename + configGroup.getType().getExt()); // avoid duplicated writes to not cause cyclic cluster sync if (!ioService.exists(filePath)) { return true; } boolean result; try { ioService.startBatch(filePath.getFileSystem()); result = ioService.deleteIfExists(filePath); if (result) { updateLastModified(); } } catch (Exception ex) { throw new RuntimeException(ex); } finally { ioService.endBatch(); } return result; }
@Override public boolean updateConfiguration(final ConfigGroup configGroup) { if (ConfigType.SPACE.equals(configGroup.getType())) { configGroup.setType(ConfigType.ORGANIZATIONAL_UNIT); } String filename = configGroup.getName().replaceAll(INVALID_FILENAME_CHARS, "_"); final Path filePath = ioService.get(systemRepository.getUri()).resolve(filename + configGroup.getType().getExt()); final CommentedOption commentedOption = new CommentedOption(getIdentityName(), "Updated config " + filePath.getFileName()); saveConfiguration(configGroup, filePath, commentedOption); configGroupsByTypeWithoutNamespace.remove(configGroup.getType()); return true; }
@Override public boolean updateConfiguration(ConfigGroup configGroup) { String filename = configGroup.getName().replaceAll(INVALID_FILENAME_CHARS, "_"); final Path filePath = ioService.get(systemRepository.getUri()).resolve(filename + configGroup.getType().getExt()); final CommentedOption commentedOption = new CommentedOption(getIdentityName(), "Updated config " + filePath.getFileName()); try { ioService.startBatch(filePath.getFileSystem()); ioService.write(filePath, marshaller.marshall(configGroup), commentedOption); updateLastModified(); } catch (Exception ex) { throw new RuntimeException(ex); } finally { ioService.endBatch(); } //Invalidate cache if a new item has been created; otherwise cached value is stale configuration.remove(configGroup.getType()); return true; }
@Override public boolean removeConfiguration(final ConfigGroup configGroup) { if (ConfigType.SPACE.equals(configGroup.getType())) { configGroup.setType(ConfigType.ORGANIZATIONAL_UNIT); } configGroupsByTypeWithoutNamespace.remove(configGroup.getType()); String filename = configGroup.getName().replaceAll(INVALID_FILENAME_CHARS, "_"); final Path filePath = ioService.get(systemRepository.getUri()).resolve(filename + configGroup.getType().getExt()); if (!ioService.exists(filePath)) { return true; } boolean result; try { ioService.startBatch(filePath.getFileSystem()); result = ioService.deleteIfExists(filePath); if (result) { updateLastModified(); } } catch (Exception ex) { throw new RuntimeException(ex); } finally { ioService.endBatch(); } return result; }
@Override public boolean addConfiguration(final ConfigGroup configGroup) { if (ConfigType.SPACE.equals(configGroup.getType())) { configGroup.setType(ConfigType.ORGANIZATIONAL_UNIT); } String filename = configGroup.getName().replaceAll(INVALID_FILENAME_CHARS, "_"); final Path filePath = ioService.get(systemRepository.getUri()).resolve(filename + configGroup.getType().getExt()); // avoid duplicated writes to not cause cyclic cluster sync if (ioService.exists(filePath)) { return true; } final CommentedOption commentedOption = new CommentedOption(getIdentityName(), "Created config " + filePath.getFileName()); saveConfiguration(configGroup, filePath, commentedOption); configGroupsByTypeWithoutNamespace.remove(configGroup.getType()); return true; }
@Override public boolean addConfiguration(final ConfigGroup configGroup) { String filename = configGroup.getName().replaceAll(INVALID_FILENAME_CHARS, "_"); final Path filePath = ioService.get(systemRepository.getUri()).resolve(filename + configGroup.getType().getExt()); // avoid duplicated writes to not cause cyclic cluster sync if (ioService.exists(filePath)) { return true; } final CommentedOption commentedOption = new CommentedOption(getIdentityName(), "Created config " + filePath.getFileName()); try { ioService.startBatch(filePath.getFileSystem()); ioService.write(filePath, marshaller.marshall(configGroup), commentedOption); updateLastModified(); } catch (Exception ex) { throw new RuntimeException(ex); } finally { ioService.endBatch(); } //Invalidate cache if a new item has been created; otherwise cached value is stale configuration.remove(configGroup.getType()); return true; }