diff options
Diffstat (limited to 'common/onap-common-configuration-management/onap-configuration-management-core/src/main/java')
-rw-r--r-- | common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java index df382143c6..17e9ef048f 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java @@ -44,6 +44,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; +import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; @@ -51,7 +52,10 @@ import java.net.MalformedURLException; import java.net.URL; import java.nio.charset.Charset; import java.nio.file.Files; +import java.nio.file.FileVisitResult; import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; @@ -113,20 +117,31 @@ public class ConfigurationUtils { } public static Collection<File> getAllFiles(File file, boolean recursive, boolean onlyDirectory) { - ArrayList<File> collection = new ArrayList<>(); - if (file.isDirectory() && file.exists()) { - File[] files = file.listFiles(); - for (File innerFile : Objects.requireNonNull(files)) { - if (innerFile.isFile() && !onlyDirectory) { - collection.add(innerFile); - } else if (innerFile.isDirectory()) { - collection.add(innerFile); - if (recursive) { - collection.addAll(getAllFiles(innerFile, true, onlyDirectory)); + Path rootPath = file.toPath(); + try { + Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() { + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + super.preVisitDirectory(dir,attrs); + if (rootPath.equals(dir)) { + return FileVisitResult.CONTINUE; } + collection.add(dir.toFile()); + return recursive? FileVisitResult.CONTINUE : FileVisitResult.SKIP_SUBTREE; } - } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + super.visitFile(file, attrs); + if (!onlyDirectory) { + collection.add(file.toFile()); + } + return FileVisitResult.CONTINUE; + } + }); + } catch (IOException e) { + LOGGER.error("Failed to walk through directories starting from: {}.", file.toString(), e); } return collection; } |