@Test public void testLoadEmptyDirectory() throws IOException { DirectoryConfigSource configSource = ConfigSources.directory(folder.newFolder().getAbsolutePath()) .build(); Optional<ConfigContent.NodeContent> maybeContent = configSource.load(); assertThat(maybeContent, not(Optional.empty())); ConfigContent.NodeContent nodeContent = maybeContent.get(); Optional<Object> maybeStamp = nodeContent.stamp(); assertThat(maybeStamp, not(Optional.empty())); Instant stamp = (Instant) maybeStamp.get(); assertThat(stamp, both(greaterThan(Instant.now().minusSeconds(3))) .and(lessThan(Instant.now().plusSeconds(3)))); ObjectNode objectNode = nodeContent.data(); assertThat(objectNode.entrySet(), hasSize(0)); }
@Override public Optional<NodeContent> load() throws ConfigException { if (!Files.exists(directoryPath)) { return Optional.empty(); } try { ConfigNode.ObjectNode.Builder objectNodeRoot = ConfigNode.ObjectNode.builder(); Files.walk(directoryPath, 1, FOLLOW_LINKS) .filter(Files::isRegularFile) .forEach(path -> { String content = FileSourceHelper.safeReadContent(path); objectNodeRoot.addValue(path.getFileName().toString(), content); }); NodeContent.Builder builder = NodeContent.builder() .node(objectNodeRoot.build()); lastModifiedTime(directoryPath).ifPresent(builder::stamp); return Optional.of(builder.build()); } catch (ConfigException ex) { throw ex; } catch (Exception ex) { throw new ConfigException("Configuration at directory '" + directoryPath + "' is not accessible.", ex); } }
DirectoryConfigSource extends AbstractConfigSource implements PollableSource<Instant>,
                   WatchableSource<Path>,
                   NodeConfigSource { @Override public Optional<NodeContent> load() throws ConfigException { if (!Files.exists(directoryPath)) { return Optional.empty(); } try { ConfigNode.ObjectNode.Builder objectNodeRoot = ConfigNode.ObjectNode.builder(); Files.walk(directoryPath, 1, FOLLOW_LINKS) .filter(Files::isRegularFile) .forEach(path -> { String content = FileSourceHelper.safeReadContent(path); objectNodeRoot.addValue(path.getFileName().toString(), content); }); NodeContent.Builder builder = NodeContent.builder() .node(objectNodeRoot.build()); lastModifiedTime(directoryPath).ifPresent(builder::stamp); return Optional.of(builder.build()); } catch (ConfigException ex) { throw ex; } catch (Exception ex) { throw new ConfigException("Configuration at directory '" + directoryPath + "' is not accessible.", ex); } } }
DirectoryConfigSource extends AbstractConfigSource implements PollableSource<Instant>,
                   WatchableSource<Path>,
                   NodeConfigSource { @Override public Optional<NodeContent> load() throws ConfigException { if (!Files.exists(directoryPath)) { return Optional.empty(); } try { ConfigNode.ObjectNode.Builder objectNodeRoot = ConfigNode.ObjectNode.builder(); Files.walk(directoryPath, 1, FOLLOW_LINKS) .filter(Files::isRegularFile) .forEach(path -> { String content = FileSourceHelper.safeReadContent(path); objectNodeRoot.addValue(path.getFileName().toString(), content); }); NodeContent.Builder builder = NodeContent.builder() .node(objectNodeRoot.build()); lastModifiedTime(directoryPath).ifPresent(builder::stamp); return Optional.of(builder.build()); } catch (ConfigException ex) { throw ex; } catch (Exception ex) { throw new ConfigException("Configuration at directory '" + directoryPath + "' is not accessible.", ex); } } DirectoryConfigSource(Builder builder); }
DirectoryConfigSource extends AbstractConfigSource implements PollableSource<Instant>,
                   WatchableSource<Path>,
                   NodeConfigSource { @Override public Optional<NodeContent> load() throws ConfigException { if (!Files.exists(directoryPath)) { return Optional.empty(); } try { ConfigNode.ObjectNode.Builder objectNodeRoot = ConfigNode.ObjectNode.builder(); Files.walk(directoryPath, 1, FOLLOW_LINKS) .filter(Files::isRegularFile) .forEach(path -> { String content = FileSourceHelper.safeReadContent(path); objectNodeRoot.addValue(path.getFileName().toString(), content); }); NodeContent.Builder builder = NodeContent.builder() .node(objectNodeRoot.build()); lastModifiedTime(directoryPath).ifPresent(builder::stamp); return Optional.of(builder.build()); } catch (ConfigException ex) { throw ex; } catch (Exception ex) { throw new ConfigException("Configuration at directory '" + directoryPath + "' is not accessible.", ex); } } DirectoryConfigSource(Builder builder); static DirectoryConfigSource create(Config metaConfig); static Builder builder(); @Override boolean isModified(Instant stamp); @Override Optional<PollingStrategy> pollingStrategy(); @Override Optional<ChangeWatcher<Object>> changeWatcher(); @Override Path target(); @Override boolean exists(); @Override Class<Path> targetType(); @Override Optional<NodeContent> load(); }
DirectoryConfigSource extends AbstractConfigSource implements PollableSource<Instant>,
                   WatchableSource<Path>,
                   NodeConfigSource { @Override public Optional<NodeContent> load() throws ConfigException { if (!Files.exists(directoryPath)) { return Optional.empty(); } try { ConfigNode.ObjectNode.Builder objectNodeRoot = ConfigNode.ObjectNode.builder(); Files.walk(directoryPath, 1, FOLLOW_LINKS) .filter(Files::isRegularFile) .forEach(path -> { String content = FileSourceHelper.safeReadContent(path); objectNodeRoot.addValue(path.getFileName().toString(), content); }); NodeContent.Builder builder = NodeContent.builder() .node(objectNodeRoot.build()); lastModifiedTime(directoryPath).ifPresent(builder::stamp); return Optional.of(builder.build()); } catch (ConfigException ex) { throw ex; } catch (Exception ex) { throw new ConfigException("Configuration at directory '" + directoryPath + "' is not accessible.", ex); } } DirectoryConfigSource(Builder builder); static DirectoryConfigSource create(Config metaConfig); static Builder builder(); @Override boolean isModified(Instant stamp); @Override Optional<PollingStrategy> pollingStrategy(); @Override Optional<ChangeWatcher<Object>> changeWatcher(); @Override Path target(); @Override boolean exists(); @Override Class<Path> targetType(); @Override Optional<NodeContent> load();  }