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

duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 22b1f54e874 Resolve the issue where "zip file closed" in versions 
prior to Spring Boot 2.3. (#31742)
22b1f54e874 is described below

commit 22b1f54e87448fe18bdd6c9a53d3c05bb10243de
Author: Cong Hu <[email protected]>
AuthorDate: Tue Jun 18 14:09:19 2024 +0800

    Resolve the issue where "zip file closed" in versions prior to Spring Boot 
2.3. (#31742)
---
 .../ClasspathResourceDirectoryReader.java          | 35 +++++++++++++++++-----
 1 file changed, 28 insertions(+), 7 deletions(-)

diff --git 
a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/directory/ClasspathResourceDirectoryReader.java
 
b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/directory/ClasspathResourceDirectoryReader.java
index 3dd2429fd3e..ff7aa03e200 100644
--- 
a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/directory/ClasspathResourceDirectoryReader.java
+++ 
b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/directory/ClasspathResourceDirectoryReader.java
@@ -39,9 +39,13 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Spliterator;
+import java.util.Spliterators;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
 
 /**
  * Classpath resource directory reader.
@@ -133,13 +137,30 @@ public final class ClasspathResourceDirectoryReader {
         if (null == jar) {
             return Stream.empty();
         }
-        try {
-            return jar.stream().filter(each -> 
each.getName().startsWith(directory) && 
!each.isDirectory()).map(JarEntry::getName);
-        } catch (final IllegalStateException ex) {
-            // todo Refactor to use JDK API to filter out closed JAR files 
used by application.
-            log.warn("Access jar file error: {}.", directoryUrl.getPath(), ex);
-            return Stream.empty();
-        }
+        return getJarEntryStream(jar).filter(each -> 
each.getName().startsWith(directory) && 
!each.isDirectory()).map(JarEntry::getName);
+    }
+    
+    /**
+     * Prior to Spring Boot 2.3, the `JarFile` class in Spring Boot did not 
override the `stream()` method, see <a 
href="https://github.com/spring-projects/spring-boot/issues/23821";>issue</a>.
+     *
+     * @param jar jar
+     * @return Stream of JarEntry
+     */
+    private static Stream<JarEntry> getJarEntryStream(final JarFile jar) {
+        Enumeration<JarEntry> entries = jar.entries();
+        return StreamSupport.stream(Spliterators.spliterator(
+                new Iterator<JarEntry>() {
+                    
+                    @Override
+                    public boolean hasNext() {
+                        return entries.hasMoreElements();
+                    }
+                    
+                    @Override
+                    public JarEntry next() {
+                        return entries.nextElement();
+                    }
+                }, jar.size(), Spliterator.ORDERED | Spliterator.DISTINCT | 
Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
     }
     
     @SneakyThrows(IOException.class)

Reply via email to