diff options
author | cgdecker <cgdecker@google.com> | 2016-03-29 09:54:24 -0700 |
---|---|---|
committer | Colin Decker <cgdecker@google.com> | 2016-04-01 13:16:34 -0400 |
commit | 89aca081f3440200793b5970247403644a7acb34 (patch) | |
tree | b6569b149e40f81e239f32254c702d4b743ef07a /jimfs | |
parent | fdc12fe9e0edb4cb952a371df1f43e67d2f8f459 (diff) | |
download | jimfs-89aca081f3440200793b5970247403644a7acb34.tar.gz |
Fix a potential issue where a JimfsFileSystem would not be removed from the static cache if SystemJimfsFileSystemProvider isn't found in the installed providers list but can be loaded from its ClassLoader by ServiceLoader.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=118472638
Diffstat (limited to 'jimfs')
-rw-r--r-- | jimfs/src/main/java/com/google/common/jimfs/JimfsFileSystems.java | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/jimfs/src/main/java/com/google/common/jimfs/JimfsFileSystems.java b/jimfs/src/main/java/com/google/common/jimfs/JimfsFileSystems.java index 9b60f5a..3fc58ec 100644 --- a/jimfs/src/main/java/com/google/common/jimfs/JimfsFileSystems.java +++ b/jimfs/src/main/java/com/google/common/jimfs/JimfsFileSystems.java @@ -25,6 +25,7 @@ import java.net.URI; import java.nio.file.spi.FileSystemProvider; import java.util.HashMap; import java.util.Map; +import java.util.ServiceLoader; /** * Initializes and configures new file system instances. @@ -47,6 +48,22 @@ final class JimfsFileSystems { return provider; } } + + /* + * Jimfs.newFileSystem passes SystemJimfsFileSystemProvider.class.getClassLoader() to + * FileSystems.newFileSystem so that it will fall back to loading from that classloader if + * the provider isn't found in the installed providers. So do the same fallback here to ensure + * that we can remove file systems from the static cache on SystemJimfsFileSystemProvider if it + * gets loaded that way. + */ + ServiceLoader<FileSystemProvider> loader = ServiceLoader + .load(FileSystemProvider.class, SystemJimfsFileSystemProvider.class.getClassLoader()); + for (FileSystemProvider provider : loader) { + if (provider.getScheme().equals(URI_SCHEME)) { + return provider; + } + } + return null; } @@ -72,7 +89,7 @@ final class JimfsFileSystems { try { Method method = systemJimfsProvider.getClass().getDeclaredMethod("removeFileSystemRunnable", URI.class); - return (Runnable) method.invoke(systemJimfsProvider, uri); + return (Runnable) method.invoke(null, uri); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { throw new RuntimeException( "Unable to get Runnable for removing the FileSystem from the cache when it is closed", e); |