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

chesnay pushed a commit to branch release-1.16
in repository https://gitbox.apache.org/repos/asf/flink.git

commit e9e072c41ad340ec0bffd54d7eb8a8d190263faa
Author: Chesnay Schepler <ches...@apache.org>
AuthorDate: Wed Sep 14 13:32:38 2022 +0200

    [FLINK-29301] loadFromResources works as jar
    
    loadFromResource implicitly relied on being called in the context of the 
java-ci-tools module, directly accessing the resources directly from the source.
    
    When packaged as a jar this approach doesn't work, because then you'd load 
files from a jar filesystem that you have to create explicitly.
    
    Instead we now load the resource as an InputStream which works in all cases.
---
 .../tools/ci/licensecheck/NoticeFileChecker.java   | 26 +++++++++++++++-------
 1 file changed, 18 insertions(+), 8 deletions(-)

diff --git 
a/tools/ci/java-ci-tools/src/main/java/org/apache/flink/tools/ci/licensecheck/NoticeFileChecker.java
 
b/tools/ci/java-ci-tools/src/main/java/org/apache/flink/tools/ci/licensecheck/NoticeFileChecker.java
index 719c9c6198e..8ea2cec2945 100644
--- 
a/tools/ci/java-ci-tools/src/main/java/org/apache/flink/tools/ci/licensecheck/NoticeFileChecker.java
+++ 
b/tools/ci/java-ci-tools/src/main/java/org/apache/flink/tools/ci/licensecheck/NoticeFileChecker.java
@@ -26,11 +26,12 @@ import com.google.common.collect.Multimap;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.BufferedReader;
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.nio.file.Files;
 import java.nio.file.Path;
-import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -38,6 +39,7 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -317,13 +319,21 @@ public class NoticeFileChecker {
 
     private static List<String> loadFromResources(String fileName) {
         try {
-            Path resource = Paths.get(NoticeFileChecker.class.getResource("/" 
+ fileName).toURI());
-            List<String> result =
-                    Files.readAllLines(resource).stream()
-                            .filter(line -> !line.startsWith("#") && 
!line.isEmpty())
-                            .collect(Collectors.toList());
-            LOG.debug("Loaded {} items from resource {}", result.size(), 
fileName);
-            return result;
+            try (BufferedReader bufferedReader =
+                    new BufferedReader(
+                            new InputStreamReader(
+                                    Objects.requireNonNull(
+                                            
NoticeFileChecker.class.getResourceAsStream(
+                                                    "/" + fileName))))) {
+
+                List<String> result =
+                        bufferedReader
+                                .lines()
+                                .filter(line -> !line.startsWith("#") && 
!line.isEmpty())
+                                .collect(Collectors.toList());
+                LOG.debug("Loaded {} items from resource {}", result.size(), 
fileName);
+                return result;
+            }
         } catch (Throwable e) {
             // wrap anything in a RuntimeException to be callable from the 
static initializer
             throw new RuntimeException("Error while loading resource", e);

Reply via email to