Skip to content

Commit

Permalink
Ensure the files stream is correctly closed
Browse files Browse the repository at this point in the history
  • Loading branch information
mackdk committed Oct 23, 2024
1 parent 6daa902 commit 3449e0a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -80,8 +81,7 @@ public FileConfigurationSource(List<Path> fileSourcesIn, String filePrefixIn, Li
commonFilePrefix = filePrefixIn;
fallbackNamespaces = fallbackNamespacesIn;

fileSourcesIn.stream()
.flatMap(path -> getConfigurationFiles(path))
getConfigurationStream(fileSourcesIn)
.sorted(CONFIG_PATH_COMPARATOR)
.map(file -> loadProperties(file))
.forEach(props -> configValues.putAll(props));
Expand Down Expand Up @@ -143,19 +143,34 @@ public Set<String> getPropertyNames() {
.collect(Collectors.toUnmodifiableSet());
}

private static Stream<Path> getConfigurationFiles(Path path) {
if (Files.isDirectory(path)) {
try {
return Files.list(path)
.filter(file -> file.getFileName().toString().endsWith(".conf"));
private static Stream<Path> getConfigurationStream(List<Path> locationsList) {
if (locationsList == null || locationsList.isEmpty()) {
return Stream.empty();
}

Stream.Builder<Path> streamBuilder = Stream.builder();
for (Path path : locationsList) {
if (Files.isDirectory(path) && Files.isReadable(path)) {
try (Stream<Path> filesInDirectory = Files.list(path)) {
PathMatcher configMatcher = path.getFileSystem().getPathMatcher("glob:*.conf");
filesInDirectory
.filter(file -> Files.isRegularFile(file) && Files.isReadable(file))
.filter(file -> configMatcher.matches(file.getFileName()))
.forEach(file -> streamBuilder.add(file));
}
catch (IOException ex) {
LOGGER.error("Unable to list file in directory {}", path);
}
}
else if (Files.isRegularFile(path) && Files.isReadable(path)) {
streamBuilder.add(path);
}
catch (IOException ex) {
LOGGER.error("Unable to list file in directory {}", path);
return Stream.empty();
else {
LOGGER.warn("Ignoring path {} since it's not accessible", path);
}
}

return Stream.of(path);
return streamBuilder.build();
}

private Properties loadProperties(Path file) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Ensure the files stream is correctly closed

0 comments on commit 3449e0a

Please sign in to comment.