Skip to content

Commit

Permalink
fix: FileSystems.getFileSystem does not return existing filesystem. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-todorov authored Jul 25, 2023
1 parent 64c5ed8 commit 66f3cdf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public class S3FileSystemProvider

public static final String S3_FACTORY_CLASS = "s3fs.amazon.s3.factory.class";

private static final ConcurrentMap<String, S3FileSystem> fileSystems = new ConcurrentHashMap<>();
private volatile static ConcurrentMap<String, S3FileSystem> fileSystems = new ConcurrentHashMap<>();

private static final List<String> PROPS_TO_OVERLOAD = Arrays.asList(ACCESS_KEY,
SECRET_KEY,
Expand Down Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions src/test/java/org/carlspring/cloud/storage/s3fs/FileSystemsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}

}

0 comments on commit 66f3cdf

Please sign in to comment.