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 List<ConfigGroup> getConfiguration(ConfigType type, final String namespace) { if (ConfigType.SPACE.equals(type)) { type = ConfigType.ORGANIZATIONAL_UNIT; } if (!ConfigType.REPOSITORY.equals(type)) { return Collections.emptyList(); } return getConfiguration(type).stream() .filter(repoConfig -> namespace.equals(repoConfig.getConfigItemValue("space"))) .collect(Collectors.toList()); }
@Override public List<ConfigGroup> getConfiguration(final ConfigType type) { if (type.hasNamespace()) { throw new RuntimeException("The ConfigType " + type.toString() + " requires a namespace."); } if (configGroupsByTypeWithoutNamespace.containsKey(type)) { return configGroupsByTypeWithoutNamespace.get(type); } final Path typeDir = ioService.get(systemRepository.getUri()).resolve(type.getDir()); final List<ConfigGroup> configGroups = getConfiguration(typeDir, type); if (configGroups != null) { configGroupsByTypeWithoutNamespace.put(type, configGroups); } else { return Collections.emptyList(); } return configGroups; }
@Override public ConfigGroup newConfigGroup(final ConfigType type, final String name, final String description) { if (type.hasNamespace()) { throw new RuntimeException("The ConfigType " + type.toString() + " requires a namespace."); } final ConfigGroup configGroup = new ConfigGroup(); configGroup.setDescription(description); configGroup.setName(name); configGroup.setType(type); configGroup.setEnabled(true); return configGroup; }
@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 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; }
private void invalidateCacheAfterUpdatingConfigGroup(final ConfigGroup configGroup) { final ConfigType type = configGroup.getType(); if (!type.hasNamespace()) { configGroupsByTypeWithoutNamespace.remove(type); } else { if (configGroupsByTypeWithNamespace.containsKey(type)) { configGroupsByTypeWithNamespace.get(type).remove(configGroup.getNamespace()); } } }
@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 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 List<ConfigGroup> getConfiguration(final ConfigType type, final String namespace) { if (!type.hasNamespace() && namespace != null && !namespace.isEmpty()) { throw new RuntimeException("The ConfigType " + type.toString() + " does not support namespaces."); } if (configGroupsByTypeWithNamespace.containsKey(type)) { final Map<String, List<ConfigGroup>> configGroupsByNamespace = configGroupsByTypeWithNamespace.get(type); if (configGroupsByNamespace.containsKey(namespace)) { return configGroupsByNamespace.get(namespace); } } final Path typeDir = ioService.get(systemRepository.getUri()).resolve(type.getDir()); final Path namespaceDir = typeDir.resolve(namespace); final List<ConfigGroup> configGroups = getConfiguration(namespaceDir, type); if (configGroups != null) { if (!configGroupsByTypeWithNamespace.containsKey(type)) { configGroupsByTypeWithNamespace.put(type, new ConcurrentHashMap<>()); } final Map<String, List<ConfigGroup>> configGroupsByNamespace = configGroupsByTypeWithNamespace.get(type); configGroupsByNamespace.put(namespace, configGroups); } else { return Collections.emptyList(); } return configGroups; }
@Override public ConfigGroup newConfigGroup(final ConfigType type, final String namespace, final String name, final String description) { if (!type.hasNamespace() && namespace != null && !namespace.isEmpty()) { throw new RuntimeException("The ConfigType " + type.toString() + " does not support namespaces."); } final ConfigGroup configGroup = new ConfigGroup(); configGroup.setDescription(description); configGroup.setName(name); configGroup.setType(type); configGroup.setNamespace(namespace); configGroup.setEnabled(true); return configGroup; }
@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 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 Map<String, List<ConfigGroup>> getConfigurationByNamespace(final ConfigType type) { if (!type.hasNamespace()) { throw new RuntimeException("The ConfigType " + type.toString() + " does not support namespaces."); } final Path typeDir = ioService.get(systemRepository.getUri()).resolve(type.getDir()); if (!ioService.exists(typeDir)) { return Collections.emptyMap(); } final DirectoryStream<Path> foundNamespaces = getDirectoryStreamForDirectories(typeDir); // Force cache update for all namespaces in that type final Iterator<Path> it = foundNamespaces.iterator(); while (it.hasNext()) { final String namespace = Paths.convert(it.next()).getFileName(); getConfiguration(type, namespace); } // Return the updated cache return configGroupsByTypeWithNamespace.get(type); }
@Override public Map<String, List<ConfigGroup>> getConfigurationByNamespace(ConfigType type) { if (ConfigType.SPACE.equals(type)) { type = ConfigType.ORGANIZATIONAL_UNIT; } if (!ConfigType.REPOSITORY.equals(type)) { return Collections.emptyMap(); } final Map<String, List<ConfigGroup>> repoConfigsBySpace = new HashMap<>(); final List<ConfigGroup> repoConfigs = getConfiguration(type); for (ConfigGroup repoConfig : repoConfigs) { final String space = repoConfig.getConfigItemValue("space"); if (space != null) { if (!repoConfigsBySpace.containsKey(space)) { repoConfigsBySpace.put(space, new ArrayList<>()); } repoConfigsBySpace.get(space).add(repoConfig); } } return repoConfigsBySpace; }
@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) { 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; }
@Override public ConfigGroup newConfigGroup(ConfigType type, final String name, final String description) { if (ConfigType.SPACE.equals(type)) { type = ConfigType.ORGANIZATIONAL_UNIT; } final ConfigGroup configGroup = new ConfigGroup(); configGroup.setDescription(description); configGroup.setName(name); configGroup.setType(type); configGroup.setEnabled(true); return configGroup; }