diff options
author | Anna Koskinen <Ansku@users.noreply.github.com> | 2021-07-22 13:01:10 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-22 13:01:10 +0300 |
commit | df8bd7e0f6d2ee4dd102e02b480d82385961a130 (patch) | |
tree | 0ed636c45e95c3334f7fd984b4e34ec26eb1d78d /server | |
parent | 40e35575c8268868b38f725e3da43017e8992e87 (diff) | |
download | vaadin-framework-df8bd7e0f6d2ee4dd102e02b480d82385961a130.tar.gz vaadin-framework-df8bd7e0f6d2ee4dd102e02b480d82385961a130.zip |
fix: Reuse existing filesystem (#12346)
Modified cherry-pick of https://github.com/vaadin/flow/pull/11428
Diffstat (limited to 'server')
-rw-r--r-- | server/src/main/java/com/vaadin/server/VaadinServlet.java | 17 | ||||
-rw-r--r-- | server/src/test/java/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; @@ -322,6 +324,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 { assertTrue("Can not run concurrently with other test", |