From 286bd5d2e9d70e3b54fba46a73272f737fa81846 Mon Sep 17 00:00:00 2001 From: Steve Todorov Date: Sun, 4 Jun 2023 19:36:58 +0300 Subject: [PATCH] fix: FileSystems.getFileSystem does not return existing filesystem. --- .../storage/s3fs/S3FileSystemProvider.java | 11 ++++++++--- .../cloud/storage/s3fs/FileSystemsIT.java | 18 ++++++++++++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/carlspring/cloud/storage/s3fs/S3FileSystemProvider.java b/src/main/java/org/carlspring/cloud/storage/s3fs/S3FileSystemProvider.java index 82922b8f..3f81b341 100644 --- a/src/main/java/org/carlspring/cloud/storage/s3fs/S3FileSystemProvider.java +++ b/src/main/java/org/carlspring/cloud/storage/s3fs/S3FileSystemProvider.java @@ -144,7 +144,7 @@ public class S3FileSystemProvider public static final String S3_FACTORY_CLASS = "s3fs.amazon.s3.factory.class"; - private static final ConcurrentMap fileSystems = new ConcurrentHashMap<>(); + private volatile static ConcurrentMap fileSystems = new ConcurrentHashMap<>(); private static final List PROPS_TO_OVERLOAD = Arrays.asList(ACCESS_KEY, SECRET_KEY, @@ -198,8 +198,13 @@ public FileSystem newFileSystem(URI uri, String key = getFileSystemKey(uri, props); if (fileSystems.containsKey(key)) { - throw new FileSystemAlreadyExistsException( - "File system " + uri.getScheme() + ':' + key + " already exists"); + String safeName = uri.getScheme() + "://"; + String userInfo = uri.getUserInfo(); + if(userInfo != null) { + safeName += uri.getUserInfo().split(":")[0] + ":__REDACTED__@"; + } + safeName += uri.getHost() + (uri.getPort() > -1 ? ":" + uri.getPort() : "" ) + uri.getPath(); + throw new FileSystemAlreadyExistsException("File system " + safeName + " already exists"); } // create the filesystem with the final properties, store and return diff --git a/src/test/java/org/carlspring/cloud/storage/s3fs/FileSystemsIT.java b/src/test/java/org/carlspring/cloud/storage/s3fs/FileSystemsIT.java index 28b4e838..7fc8365d 100644 --- a/src/test/java/org/carlspring/cloud/storage/s3fs/FileSystemsIT.java +++ b/src/test/java/org/carlspring/cloud/storage/s3fs/FileSystemsIT.java @@ -12,8 +12,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.carlspring.cloud.storage.s3fs.util.S3EndpointConstant.S3_GLOBAL_URI_IT; -import static org.junit.jupiter.api.Assertions.assertNotSame; -import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.*; @S3IntegrationTest class FileSystemsIT extends BaseIntegrationTest @@ -73,4 +72,19 @@ void buildEnvAnotherURIReturnDifferent() assertNotSame(fileSystemAmazon, fileSystem); } + @Test + void shouldReturnExistingFileSystem() + throws IOException + { + FileSystem retrieveFileSystem = FileSystems.getFileSystem(uriGlobal); + assertSame(fileSystemAmazon, retrieveFileSystem); + } + + @Test + void shouldReturnThrowExceptionOnMissingFileSystem() + throws IOException + { + assertThrows(FileSystemNotFoundException.class,() -> FileSystems.getFileSystem(uriEurope)); + } + }