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;
private VaadinServletService servletService;
- // Mapped uri is for the jar file
- static final Map<URI, Integer> openFileSystems = new HashMap<>();
+ /**
+ * Mapped uri is for the jar file.
+ * <p>
+ * FOR INTERNAL USE ONLY, may get renamed or removed.
+ */
+ static final Map<URI, Integer> OPEN_FILE_SYSTEMS = new HashMap<>();
private static final Object fileSystemLock = new Object();
/**
+ " and will determine this as not a folder");
}
- Integer locks = openFileSystems.computeIfPresent(fileURI,
+ Integer locks = OPEN_FILE_SYSTEMS.computeIfPresent(fileURI,
(key, value) -> value + 1);
if (locks != null) {
// Get filesystem is for the file to get the correct provider
return FileSystems.getFileSystem(resourceURI);
}
// Opened filesystem is for the file to get the correct provider
- FileSystem fileSystem = FileSystems.newFileSystem(resourceURI,
- Collections.emptyMap());
- openFileSystems.put(fileURI, 1);
+ 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 (fileSystemLock) {
try {
URI fileURI = getFileURI(resourceURI);
- Integer locks = openFileSystems.computeIfPresent(fileURI,
+ Integer locks = OPEN_FILE_SYSTEMS.computeIfPresent(fileURI,
(key, value) -> value - 1);
if (locks != null && locks == 0) {
- openFileSystems.remove(fileURI);
+ OPEN_FILE_SYSTEMS.remove(fileURI);
// Get filesystem is for the file to get the correct
// provider
FileSystems.getFileSystem(resourceURI).close();
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;
public void isAllowedVAADINResource_jarWarFileScheme_detectsAsStaticResources()
throws IOException, URISyntaxException, ServletException {
assertTrue("Can not run concurrently with other test",
- VaadinServlet.openFileSystems.isEmpty());
+ VaadinServlet.OPEN_FILE_SYSTEMS.isEmpty());
VaadinServlet servlet = new VaadinServlet();
servlet.init(new MockServletConfig());
public void isAllowedVAADINResource_jarInAJar_detectsAsStaticResources()
throws IOException, URISyntaxException, ServletException {
assertTrue("Can not run concurrently with other test",
- VaadinServlet.openFileSystems.isEmpty());
+ VaadinServlet.OPEN_FILE_SYSTEMS.isEmpty());
VaadinServlet servlet = new VaadinServlet();
servlet.init(new MockServletConfig());
}
}
+ @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",
- VaadinServlet.openFileSystems.isEmpty());
+ VaadinServlet.OPEN_FILE_SYSTEMS.isEmpty());
VaadinServlet servlet = new VaadinServlet();
servlet.init(new MockServletConfig());
servlet.getFileSystem(fileResourceURL.toURI());
assertEquals("Same file should be marked for both resources",
- (Integer) 2, VaadinServlet.openFileSystems.entrySet()
+ (Integer) 2, VaadinServlet.OPEN_FILE_SYSTEMS.entrySet()
.iterator().next().getValue());
servlet.closeFileSystem(folderResourceURL.toURI());
assertEquals("Closing resource should be removed from jar uri",
- (Integer) 1, VaadinServlet.openFileSystems.entrySet()
+ (Integer) 1, VaadinServlet.OPEN_FILE_SYSTEMS.entrySet()
.iterator().next().getValue());
servlet.closeFileSystem(fileResourceURL.toURI());
assertTrue("Closing last resource should clear marking",
- VaadinServlet.openFileSystems.isEmpty());
+ VaadinServlet.OPEN_FILE_SYSTEMS.isEmpty());
try {
FileSystems.getFileSystem(folderResourceURL.toURI());
throws IOException, InterruptedException, ExecutionException,
URISyntaxException, ServletException {
assertTrue("Can not run concurrently with other test",
- VaadinServlet.openFileSystems.isEmpty());
+ VaadinServlet.OPEN_FILE_SYSTEMS.isEmpty());
VaadinServlet servlet = new VaadinServlet();
servlet.init(new MockServletConfig());
private void ensureFileSystemsCleared(URL fileResourceURL)
throws URISyntaxException {
assertFalse("URI should have been cleared",
- VaadinServlet.openFileSystems
+ VaadinServlet.OPEN_FILE_SYSTEMS
.containsKey(fileResourceURL.toURI()));
try {
FileSystems.getFileSystem(fileResourceURL.toURI());