You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2022/03/05 02:40:30 UTC

[groovy] branch master updated: GROOVY-10519: v9 ClassFinder closes existing FileSystems that it doesn't own

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 853d8ff  GROOVY-10519: v9 ClassFinder closes existing FileSystems that it doesn't own
853d8ff is described below

commit 853d8ff79c422cc3249a9bb5ba9c8accb510b548
Author: Paul King <pa...@asert.com.au>
AuthorDate: Fri Mar 4 11:26:10 2022 +1000

    GROOVY-10519: v9 ClassFinder closes existing FileSystems that it doesn't own
---
 .../codehaus/groovy/vmplugin/v9/ClassFinder.java   | 26 +++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/vmplugin/v9/ClassFinder.java b/src/main/java/org/codehaus/groovy/vmplugin/v9/ClassFinder.java
index f952598..9c4befe 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v9/ClassFinder.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v9/ClassFinder.java
@@ -18,6 +18,8 @@
  */
 package org.codehaus.groovy.vmplugin.v9;
 
+import groovy.lang.Tuple2;
+
 import java.io.File;
 import java.io.IOException;
 import java.net.URI;
@@ -115,8 +117,10 @@ public class ClassFinder {
         final int prefixElemCnt = prefix.trim().isEmpty() ? 0 : prefix.split(sepPatten).length;
 
         Map<String, Set<String>> result = new LinkedHashMap<>();
-        try (FileSystem fs = newFileSystem(uri)) {
-            Files.walkFileTree(fs.getPath(prefix + "/" + packageName), new SimpleFileVisitor<Path>() {
+        Tuple2<FileSystem, Boolean> fsMaybeNew = null;
+        try {
+            fsMaybeNew = maybeNewFileSystem(uri);
+            Files.walkFileTree(fsMaybeNew.getV1().getPath(prefix + "/" + packageName), new SimpleFileVisitor<Path>() {
                 @Override
                 public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes attrs) {
                     return FileVisitResult.CONTINUE;
@@ -151,16 +155,28 @@ public class ClassFinder {
                     String.format("Failed to find classes via uri: %s, prefix: %s, packageName: %s, recursive: %s",
                             uri, prefix, packageName, recursive
                     ), e);
+        } finally {
+            // we only close file systems we opened
+            if (fsMaybeNew != null && fsMaybeNew.getV2()) {
+                closeQuietly(fsMaybeNew.getV1());
+            }
         }
 
         return result;
     }
 
-    private static FileSystem newFileSystem(URI uri) throws IOException {
+    private static void closeQuietly(FileSystem fs) {
+        try {
+            fs.close();
+        } catch (IOException ignore) {
+        }
+    }
+
+    private static Tuple2<FileSystem, Boolean> maybeNewFileSystem(URI uri) throws IOException {
         try {
-            return FileSystems.newFileSystem(uri, Collections.emptyMap());
+            return new Tuple2(FileSystems.newFileSystem(uri, Collections.emptyMap()), true);
         } catch (FileSystemAlreadyExistsException e) {
-            return FileSystems.getFileSystem(uri);
+            return new Tuple2(FileSystems.getFileSystem(uri), false);
         }
     }