You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by pp...@apache.org on 2022/08/03 15:59:01 UTC

[camel-quarkus] branch main updated: Close the streams returned by Files.walk properly

This is an automated email from the ASF dual-hosted git repository.

ppalaga pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git


The following commit(s) were added to refs/heads/main by this push:
     new fba5ee47da Close the streams returned by Files.walk properly
fba5ee47da is described below

commit fba5ee47da5ceafd9efacab06e290793084d1000
Author: Peter Palaga <pp...@redhat.com>
AuthorDate: Wed Aug 3 12:29:57 2022 +0200

    Close the streams returned by Files.walk properly
---
 .../core/deployment/CamelNativeImageProcessor.java | 19 +++++----
 .../quarkus/core/deployment/util/CamelSupport.java | 37 ++++++++---------
 .../quarkus/core/deployment/util/PathFilter.java   | 10 ++++-
 .../component/xslt/deployment/XsltProcessor.java   | 47 ++++++++++++----------
 .../quarkus/component/ftp/it/FtpTestResource.java  | 11 +++--
 .../component/sftp/it/SftpTestResource.java        | 11 +++--
 ...dePatternWithAbsoluteFilePrefixDevModeTest.java | 11 +++--
 7 files changed, 83 insertions(+), 63 deletions(-)

diff --git a/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelNativeImageProcessor.java b/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelNativeImageProcessor.java
index 633bbdea75..f098b261d3 100644
--- a/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelNativeImageProcessor.java
+++ b/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelNativeImageProcessor.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.quarkus.core.deployment;
 
+import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.ArrayList;
@@ -197,14 +198,18 @@ public class CamelNativeImageProcessor {
                         continue;
                     }
 
-                    List<String> items = CamelSupport.safeWalk(resourcePath)
-                            .filter(Files::isRegularFile)
-                            .map(root::relativize)
-                            .map(Path::toString)
-                            .collect(Collectors.toList());
+                    try (Stream<Path> files = Files.walk(resourcePath)) {
+                        List<String> items = files
+                                .filter(Files::isRegularFile)
+                                .map(root::relativize)
+                                .map(Path::toString)
+                                .collect(Collectors.toList());
+                        LOGGER.debug("Register catalog json: {}", items);
+                        resources.add(new NativeImageResourceBuildItem(items));
+                    } catch (IOException e) {
+                        throw new RuntimeException("Could not walk " + resourcePath, e);
+                    }
 
-                    LOGGER.debug("Register catalog json: {}", items);
-                    resources.add(new NativeImageResourceBuildItem(items));
                 }
             }
         }
diff --git a/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/util/CamelSupport.java b/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/util/CamelSupport.java
index c7ee46a0e9..f40f463a24 100644
--- a/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/util/CamelSupport.java
+++ b/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/util/CamelSupport.java
@@ -16,7 +16,6 @@
  */
 package org.apache.camel.quarkus.core.deployment.util;
 
-import java.io.IOError;
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.reflect.Modifier;
@@ -52,14 +51,6 @@ public final class CamelSupport {
         return (ci.flags() & Modifier.PUBLIC) != 0;
     }
 
-    public static Stream<Path> safeWalk(Path p) {
-        try {
-            return Files.walk(p);
-        } catch (IOException e) {
-            throw new IOError(e);
-        }
-    }
-
     public static Stream<CamelServiceBuildItem> services(ApplicationArchivesBuildItem archives, PathFilter pathFilter) {
         final Set<CamelServiceBuildItem> answer = new HashSet<>();
         final Predicate<Path> filter = pathFilter.asPathPredicate();
@@ -72,19 +63,23 @@ public final class CamelSupport {
                     continue;
                 }
 
-                safeWalk(resourcePath).filter(Files::isRegularFile).forEach(file -> {
-                    // the root archive may point to a jar file or the absolute path of
-                    // a project's build output so we need to relativize to make the
-                    // FastFactoryFinder work as expected
-                    Path key = root.relativize(file);
-
-                    if (filter.test(key)) {
-                        String clazz = readProperties(file).getProperty("class");
-                        if (clazz != null) {
-                            answer.add(new CamelServiceBuildItem(key, clazz));
+                try (Stream<Path> files = Files.walk(resourcePath)) {
+                    files.filter(Files::isRegularFile).forEach(file -> {
+                        // the root archive may point to a jar file or the absolute path of
+                        // a project's build output so we need to relativize to make the
+                        // FastFactoryFinder work as expected
+                        Path key = root.relativize(file);
+
+                        if (filter.test(key)) {
+                            String clazz = readProperties(file).getProperty("class");
+                            if (clazz != null) {
+                                answer.add(new CamelServiceBuildItem(key, clazz));
+                            }
                         }
-                    }
-                });
+                    });
+                } catch (IOException e) {
+                    throw new RuntimeException("Could not walk " + resourcePath, e);
+                }
             }
         }
 
diff --git a/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/util/PathFilter.java b/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/util/PathFilter.java
index 1333438cd5..1bca4028ac 100644
--- a/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/util/PathFilter.java
+++ b/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/util/PathFilter.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.quarkus.core.deployment.util;
 
+import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.ArrayList;
@@ -93,8 +94,13 @@ public class PathFilter {
 
     public String[] scanClassNames(Stream<Path> archiveRootDirs) {
         final Set<String> selectedPaths = new TreeSet<>();
-        archiveRootDirs.forEach(rootDir -> scanClassNames(rootDir, CamelSupport.safeWalk(rootDir),
-                Files::isRegularFile, selectedPaths::add));
+        archiveRootDirs.forEach(rootDir -> {
+            try (Stream<Path> files = Files.walk(rootDir)) {
+                scanClassNames(rootDir, files, Files::isRegularFile, selectedPaths::add);
+            } catch (IOException e) {
+                throw new RuntimeException("Could not walk " + rootDir, e);
+            }
+        });
         /* Let's add the paths without wildcards even if they did not match any Jandex class
          * A workaround for https://github.com/apache/camel-quarkus/issues/2969 */
         addNonPatternPaths(selectedPaths);
diff --git a/extensions/xslt/deployment/src/main/java/org/apache/camel/quarkus/component/xslt/deployment/XsltProcessor.java b/extensions/xslt/deployment/src/main/java/org/apache/camel/quarkus/component/xslt/deployment/XsltProcessor.java
index 11748c9355..b0ef7fe3b8 100644
--- a/extensions/xslt/deployment/src/main/java/org/apache/camel/quarkus/component/xslt/deployment/XsltProcessor.java
+++ b/extensions/xslt/deployment/src/main/java/org/apache/camel/quarkus/component/xslt/deployment/XsltProcessor.java
@@ -26,6 +26,7 @@ import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.stream.Stream;
 
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.TransformerFactory;
@@ -119,28 +120,32 @@ class XsltProcessor {
                 }
             }
 
-            Files.walk(destination)
-                    .sorted(Comparator.reverseOrder())
-                    .filter(Files::isRegularFile)
-                    .filter(path -> path.toString().endsWith(".class"))
-                    .forEach(path -> {
-                        try {
-                            final Path rel = destination.relativize(path);
-                            final String fqcn = StringUtils.removeEnd(rel.toString(), ".class").replace(File.separatorChar,
-                                    '.');
-                            final byte[] data = Files.readAllBytes(path);
-
-                            generatedClasses.produce(new GeneratedClassBuildItem(false, fqcn, data));
-                            generatedNames.produce(new XsltGeneratedClassBuildItem(fqcn));
-                        } catch (IOException e) {
-                            throw new RuntimeException(e);
-                        }
-                    });
+            try (Stream<Path> files = Files.walk(destination)) {
+                files
+                        .sorted(Comparator.reverseOrder())
+                        .filter(Files::isRegularFile)
+                        .filter(path -> path.toString().endsWith(".class"))
+                        .forEach(path -> {
+                            try {
+                                final Path rel = destination.relativize(path);
+                                final String fqcn = StringUtils.removeEnd(rel.toString(), ".class").replace(File.separatorChar,
+                                        '.');
+                                final byte[] data = Files.readAllBytes(path);
+
+                                generatedClasses.produce(new GeneratedClassBuildItem(false, fqcn, data));
+                                generatedNames.produce(new XsltGeneratedClassBuildItem(fqcn));
+                            } catch (IOException e) {
+                                throw new RuntimeException(e);
+                            }
+                        });
+            }
         } finally {
-            Files.walk(destination)
-                    .sorted(Comparator.reverseOrder())
-                    .map(Path::toFile)
-                    .forEach(File::delete);
+            try (Stream<Path> files = Files.walk(destination)) {
+                files
+                        .sorted(Comparator.reverseOrder())
+                        .map(Path::toFile)
+                        .forEach(File::delete);
+            }
         }
     }
 
diff --git a/integration-tests/ftp/src/test/java/org/apache/camel/quarkus/component/ftp/it/FtpTestResource.java b/integration-tests/ftp/src/test/java/org/apache/camel/quarkus/component/ftp/it/FtpTestResource.java
index 64ab8cf390..e2713b8a1a 100644
--- a/integration-tests/ftp/src/test/java/org/apache/camel/quarkus/component/ftp/it/FtpTestResource.java
+++ b/integration-tests/ftp/src/test/java/org/apache/camel/quarkus/component/ftp/it/FtpTestResource.java
@@ -23,6 +23,7 @@ import java.util.ArrayList;
 import java.util.Comparator;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Stream;
 
 import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
 import org.apache.camel.quarkus.test.AvailablePortFinder;
@@ -120,10 +121,12 @@ public class FtpTestResource implements QuarkusTestResourceLifecycleManager {
 
         try {
             if (ftpRoot != null) {
-                Files.walk(ftpRoot)
-                        .sorted(Comparator.reverseOrder())
-                        .map(Path::toFile)
-                        .forEach(File::delete);
+                try (Stream<Path> files = Files.walk(ftpRoot)) {
+                    files
+                            .sorted(Comparator.reverseOrder())
+                            .map(Path::toFile)
+                            .forEach(File::delete);
+                }
             }
         } catch (Exception e) {
             LOGGER.warn("Failed delete ftp root: {}, {}", ftpRoot, e);
diff --git a/integration-tests/ftp/src/test/java/org/apache/camel/quarkus/component/sftp/it/SftpTestResource.java b/integration-tests/ftp/src/test/java/org/apache/camel/quarkus/component/sftp/it/SftpTestResource.java
index a27f151cd4..a98764d4db 100644
--- a/integration-tests/ftp/src/test/java/org/apache/camel/quarkus/component/sftp/it/SftpTestResource.java
+++ b/integration-tests/ftp/src/test/java/org/apache/camel/quarkus/component/sftp/it/SftpTestResource.java
@@ -23,6 +23,7 @@ import java.nio.file.Path;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.Map;
+import java.util.stream.Stream;
 
 import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
 import org.apache.camel.quarkus.test.AvailablePortFinder;
@@ -89,10 +90,12 @@ public class SftpTestResource implements QuarkusTestResourceLifecycleManager {
 
         try {
             if (sftpHome != null) {
-                Files.walk(sftpHome)
-                        .sorted(Comparator.reverseOrder())
-                        .map(Path::toFile)
-                        .forEach(File::delete);
+                try (Stream<Path> files = Files.walk(sftpHome)) {
+                    files
+                            .sorted(Comparator.reverseOrder())
+                            .map(Path::toFile)
+                            .forEach(File::delete);
+                }
             }
         } catch (Exception e) {
             LOGGER.warn("Failed delete sftp home: {}, {}", sftpHome, e);
diff --git a/integration-tests/main-devmode/src/test/java/org/apache/camel/quarkus/main/CamelMainRoutesIncludePatternWithAbsoluteFilePrefixDevModeTest.java b/integration-tests/main-devmode/src/test/java/org/apache/camel/quarkus/main/CamelMainRoutesIncludePatternWithAbsoluteFilePrefixDevModeTest.java
index 39ae1853ab..5905c01725 100644
--- a/integration-tests/main-devmode/src/test/java/org/apache/camel/quarkus/main/CamelMainRoutesIncludePatternWithAbsoluteFilePrefixDevModeTest.java
+++ b/integration-tests/main-devmode/src/test/java/org/apache/camel/quarkus/main/CamelMainRoutesIncludePatternWithAbsoluteFilePrefixDevModeTest.java
@@ -27,6 +27,7 @@ import java.util.Comparator;
 import java.util.Properties;
 import java.util.concurrent.TimeUnit;
 import java.util.function.Consumer;
+import java.util.stream.Stream;
 
 import io.quarkus.test.QuarkusDevModeTest;
 import io.restassured.RestAssured;
@@ -75,10 +76,12 @@ public class CamelMainRoutesIncludePatternWithAbsoluteFilePrefixDevModeTest {
             deleteFile = File::deleteOnExit;
         }
 
-        Files.walk(BASE)
-                .sorted(Comparator.reverseOrder())
-                .map(Path::toFile)
-                .forEach(deleteFile);
+        try (Stream<Path> files = Files.walk(BASE)) {
+            files
+                    .sorted(Comparator.reverseOrder())
+                    .map(Path::toFile)
+                    .forEach(deleteFile);
+        }
     }
 
     public static Asset applicationProperties() {