From df8bd7e0f6d2ee4dd102e02b480d82385961a130 Mon Sep 17 00:00:00 2001 From: Anna Koskinen Date: Thu, 22 Jul 2021 13:01:10 +0300 Subject: [PATCH] fix: Reuse existing filesystem (#12346) Modified cherry-pick of https://github.com/vaadin/flow/pull/11428 --- .../java/com/vaadin/server/VaadinServlet.java | 17 ++++++++- .../com/vaadin/server/VaadinServletTest.java | 38 +++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/com/vaadin/server/VaadinServlet.java b/server/src/main/java/com/vaadin/server/VaadinServlet.java index f0ec0427bf..ce69d9204c 100644 --- a/server/src/main/java/com/vaadin/server/VaadinServlet.java +++ b/server/src/main/java/com/vaadin/server/VaadinServlet.java @@ -39,6 +39,7 @@ import java.net.URLDecoder; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; +import java.nio.file.FileSystemAlreadyExistsException; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; @@ -1382,13 +1383,25 @@ public class VaadinServlet extends HttpServlet implements Constants { return FileSystems.getFileSystem(resourceURI); } // Opened filesystem is for the file to get the correct provider - FileSystem fileSystem = FileSystems.newFileSystem(resourceURI, - Collections.emptyMap()); + FileSystem fileSystem = getNewOrExistingFileSystem(resourceURI); OPEN_FILE_SYSTEMS.put(fileURI, 1); return fileSystem; } } + private FileSystem getNewOrExistingFileSystem(URI resourceURI) + throws IOException { + try { + return FileSystems.newFileSystem(resourceURI, + Collections.emptyMap()); + } catch (FileSystemAlreadyExistsException fsaee) { + getLogger().log(Level.FINER, + "Tried to get new filesystem, but it already existed for target uri.", + fsaee); + return FileSystems.getFileSystem(resourceURI); + } + } + // Package protected for feature verification purpose void closeFileSystem(URI resourceURI) { synchronized (FILE_SYSTEM_LOCK) { diff --git a/server/src/test/java/com/vaadin/server/VaadinServletTest.java b/server/src/test/java/com/vaadin/server/VaadinServletTest.java index 2c318e0897..4eb97d4453 100644 --- a/server/src/test/java/com/vaadin/server/VaadinServletTest.java +++ b/server/src/test/java/com/vaadin/server/VaadinServletTest.java @@ -10,11 +10,13 @@ import java.io.File; import java.io.IOException; import java.net.URISyntaxException; import java.net.URL; +import java.nio.file.FileSystem; import java.nio.file.FileSystemNotFoundException; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; @@ -321,6 +323,42 @@ public class VaadinServletTest { } } + @Test + public void openFileServerExistsForZip_openingNewDoesNotFail() + throws IOException, URISyntaxException { + assertTrue("Can not run concurrently with other test", + VaadinServlet.OPEN_FILE_SYSTEMS.isEmpty()); + + VaadinServlet servlet = new VaadinServlet(); + + final TemporaryFolder folder = TemporaryFolder.builder().build(); + folder.create(); + + try { + Path tempArchive = createJAR(folder).toPath(); + + final FileSystem fileSystem = FileSystems + .newFileSystem( + new URL("jar:file:///" + tempArchive.toString() + .replaceAll("\\\\", "/") + "!/").toURI(), + Collections.emptyMap()); + + final URL folderResourceURL = new URL("jar:file:///" + + tempArchive.toString().replaceAll("\\\\", "/") + + "!/VAADIN"); + + try { + servlet.getFileSystem(folderResourceURL.toURI()); + } finally { + servlet.closeFileSystem(folderResourceURL.toURI()); + fileSystem.close(); + } + } finally { + folder.delete(); + } + + } + @Test public void openingJarFileSystemForDifferentFilesInSameJar_existingFileSystemIsUsed() throws IOException, URISyntaxException, ServletException { -- 2.39.5