From bb88abaea256dd0bb16513645cae734e77a6859b Mon Sep 17 00:00:00 2001 From: Decebal Suiu Date: Mon, 9 Jul 2018 20:11:13 +0300 Subject: [PATCH] Prevent FileSystemAlreadyExistsException (#229) --- pf4j/src/main/java/org/pf4j/util/FileUtils.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/pf4j/src/main/java/org/pf4j/util/FileUtils.java b/pf4j/src/main/java/org/pf4j/util/FileUtils.java index 38fd309..a05367c 100644 --- a/pf4j/src/main/java/org/pf4j/util/FileUtils.java +++ b/pf4j/src/main/java/org/pf4j/util/FileUtils.java @@ -228,9 +228,9 @@ public class FileUtils { } public static Path getPath(URI uri, String first, String... more) throws IOException { - FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap()); - - return fileSystem.getPath(first, more); + try (FileSystem fileSystem = getFileSystem(uri)) { + return fileSystem.getPath(first, more); + } } public static Path findFile(Path directoryPath, String fileName) { @@ -253,4 +253,13 @@ public class FileUtils { return null; } + private static FileSystem getFileSystem(URI uri) throws IOException { + FileSystem fileSystem = FileSystems.getFileSystem(uri); + if (fileSystem != null) { + return fileSystem; + } + + return FileSystems.newFileSystem(uri, Collections.emptyMap()); + } + }