protected void updateLastModified() { final Path lastModifiedPath = ioService.get(systemRepository.getUri()).resolve(LAST_MODIFIED_MARKER_FILE); final CommentedOption commentedOption = new CommentedOption("system", "system repo updated"); ioService.write(lastModifiedPath, new Date().toString().getBytes(), commentedOption); // update the last value to avoid to be retriggered byt the monitor localLastModifiedValue.set(getLastModified()); }
@Override public boolean removeConfiguration(final ConfigGroup configGroup) { final Path filePath = resolveConfigGroupPath(configGroup); 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(); } invalidateCacheAfterUpdatingConfigGroup(configGroup); return result; }
@Override public boolean updateConfiguration(ConfigGroup configGroup) { final Path filePath = resolveConfigGroupPath(configGroup); final String commitMessage = "Updated config " + filePath.getFileName(); return saveConfiguration(configGroup, filePath, commitMessage, false); }
private boolean saveConfiguration(final ConfigGroup configGroup, final Path path, final String commitMessage, final boolean isNew) { // avoid duplicated writes to not cause cyclic cluster sync if (isNew && ioService.exists(path)) { return true; } final CommentedOption commentedOption = new CommentedOption(getIdentityName(), commitMessage); try { ioService.startBatch(path.getFileSystem()); ioService.write(path, marshaller.marshall(configGroup), commentedOption); updateLastModified(); } catch (Exception ex) { throw new RuntimeException(ex); } finally { ioService.endBatch(); } invalidateCacheAfterUpdatingConfigGroup(configGroup); 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; }
@PostConstruct public void setup() { Path defaultRoot = null; for (final Path path : fs.getRootDirectories()) { if (path.toUri().toString().contains("/master@")) { defaultRoot = path; break; } } if (defaultRoot == null) { throw new RuntimeException("Could not resolve 'systemFS' main root directory."); } // enable monitor by default if (System.getProperty(MONITOR_DISABLED) == null) { watchService = fs.newWatchService(); configUpdates = new CheckConfigurationUpdates(watchService); final ConfigServiceWatchServiceExecutor configServiceWatchServiceExecutor = getWatchServiceExecutor(); jobs.add(executorService.submit(new DescriptiveRunnable() { @Override public String getDescription() { return configUpdates.getDescription(); } @Override public void run() { configUpdates.execute(configServiceWatchServiceExecutor); } })); } }
@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 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; }
@Before public void setup() throws IOException { fileSystemTestingUtils.setup(); when(systemRepository.getUri()).thenReturn("git://amend-repo-test"); marshaller = new ConfigGroupMarshaller(); configurationFactory = new ConfigurationFactoryImpl(new DefaultPasswordServiceImpl()); ioService = mockIoService(); configurationService = new ConfigurationServiceImpl(systemRepository, marshaller, identity, ioService, repoChangedEvent, spaceChangedEvent, changedEvent, fileSystemTestingUtils.getFileSystem()); }
@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; }
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 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 boolean addConfiguration(final ConfigGroup configGroup) { final Path filePath = resolveConfigGroupPath(configGroup); final String commitMessage = "Created config " + filePath.getFileName(); return saveConfiguration(configGroup, filePath, commitMessage, true); }
@PostConstruct public void setup() { Path defaultRoot = null; for (final Path path : fs.getRootDirectories()) { if (path.toUri().toString().contains("/master@")) { defaultRoot = path; break; } } if (defaultRoot == null) { throw new RuntimeException("Could not resolve 'systemFS' main root directory."); } systemRepository.setRoot(convert(defaultRoot)); // enable monitor by default if (System.getProperty(MONITOR_DISABLED) == null) { watchService = fs.newWatchService(); configUpdates = new CheckConfigurationUpdates(watchService); final ConfigServiceWatchServiceExecutor configServiceWatchServiceExecutor = getWatchServiceExecutor(); jobs.add(executorService.submit(new DescriptiveRunnable() { @Override public String getDescription() { return configUpdates.getDescription(); } @Override public void run() { configUpdates.execute(configServiceWatchServiceExecutor); } })); } }
@Before public void setup() throws IOException { fileSystemTestingUtils.setup(); when(systemRepository.getUri()).thenReturn("git://amend-repo-test"); marshaller = new ConfigGroupMarshaller(); configurationFactory = new ConfigurationFactoryImpl(new DefaultPasswordServiceImpl()); ioService = mockIoService(); configurationService = new ConfigurationServiceImpl(systemRepository, marshaller, identity, ioService, repoChangedEvent, spaceChangedEvent, changedEvent, fileSystemTestingUtils.getFileSystem()); }
@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; }
protected void updateLastModified() { final Path lastModifiedPath = ioService.get(systemRepository.getUri()).resolve(LAST_MODIFIED_MARKER_FILE); final CommentedOption commentedOption = new CommentedOption("system", "system repo updated"); ioService.write(lastModifiedPath, new Date().toString().getBytes(), commentedOption); // update the last value to avoid to be re-triggered by the monitor localLastModifiedValue.set(getLastModified()); }