< prev index next >

src/java.base/share/classes/java/nio/file/FileSystems.java

Print this page
8213406: (fs) More than one instance of built-in FileSystem observed in heap
Reviewed-by: alanb, cushon, weijun


  71  * may safely access files associated with the default provider but care needs
  72  * to be taken to avoid circular loading of other installed providers. If
  73  * circular loading of installed providers is detected then an unspecified error
  74  * is thrown.
  75  *
  76  * <p> This class also defines factory methods that allow a {@link ClassLoader}
  77  * to be specified when locating a provider. As with installed providers, the
  78  * provider classes are identified by placing the provider configuration file
  79  * in the resource directory {@code META-INF/services}.
  80  *
  81  * <p> If a thread initiates the loading of the installed file system providers
  82  * and another thread invokes a method that also attempts to load the providers
  83  * then the method will block until the loading completes.
  84  *
  85  * @since 1.7
  86  */
  87 
  88 public final class FileSystems {
  89     private FileSystems() { }
  90 
  91     // Built-in file system provider
  92     private static final FileSystemProvider builtinFileSystemProvider =
  93         sun.nio.fs.DefaultFileSystemProvider.create();
  94 
  95     // built-in file system
  96     private static class BuiltinFileSystemHolder {
  97         static final FileSystem builtinFileSystem =
  98             builtinFileSystemProvider.getFileSystem(URI.create("file:///"));
  99     }
 100 
 101     // lazy initialization of default file system
 102     private static class DefaultFileSystemHolder {
 103         static final FileSystem defaultFileSystem = defaultFileSystem();
 104 
 105         // returns default file system
 106         private static FileSystem defaultFileSystem() {
 107             // load default provider
 108             FileSystemProvider provider = AccessController
 109                 .doPrivileged(new PrivilegedAction<>() {
 110                     public FileSystemProvider run() {
 111                         return getDefaultProvider();
 112                     }
 113                 });
 114 
 115             // return file system
 116             return provider.getFileSystem(URI.create("file:///"));
 117         }
 118 
 119         // returns default provider
 120         private static FileSystemProvider getDefaultProvider() {
 121             FileSystemProvider provider = builtinFileSystemProvider;

 122 
 123             // if the property java.nio.file.spi.DefaultFileSystemProvider is
 124             // set then its value is the name of the default provider (or a list)
 125             String prop = "java.nio.file.spi.DefaultFileSystemProvider";
 126             String propValue = System.getProperty(prop);
 127             if (propValue != null) {
 128                 for (String cn: propValue.split(",")) {
 129                     try {
 130                         Class<?> c = Class
 131                             .forName(cn, true, ClassLoader.getSystemClassLoader());
 132                         Constructor<?> ctor = c
 133                             .getDeclaredConstructor(FileSystemProvider.class);
 134                         provider = (FileSystemProvider)ctor.newInstance(provider);
 135 
 136                         // must be "file"
 137                         if (!provider.getScheme().equals("file"))
 138                             throw new Error("Default provider must use scheme 'file'");
 139 
 140                     } catch (Exception x) {
 141                         throw new Error(x);


 172      * then an unspecified error is thrown. URI schemes are normally compared
 173      * without regard to case but for the default provider, the scheme is
 174      * required to be {@code "file"}. The first provider class is instantiated
 175      * by invoking it with a reference to the system-default provider.
 176      * The second provider class is instantiated by invoking it with a reference
 177      * to the first provider instance. The third provider class is instantiated
 178      * by invoking it with a reference to the second instance, and so on. The
 179      * last provider to be instantiated becomes the default provider; its {@code
 180      * getFileSystem} method is invoked with the URI {@code "file:///"} to
 181      * get a reference to the default file system.
 182      *
 183      * <p> Subsequent invocations of this method return the file system that was
 184      * returned by the first invocation.
 185      *
 186      * @return  the default file system
 187      */
 188     public static FileSystem getDefault() {
 189         if (VM.isModuleSystemInited()) {
 190             return DefaultFileSystemHolder.defaultFileSystem;
 191         } else {
 192             return BuiltinFileSystemHolder.builtinFileSystem;
 193         }
 194     }
 195 
 196     /**
 197      * Returns a reference to an existing {@code FileSystem}.
 198      *
 199      * <p> This method iterates over the {@link FileSystemProvider#installedProviders()
 200      * installed} providers to locate the provider that is identified by the URI
 201      * {@link URI#getScheme scheme} of the given URI. URI schemes are compared
 202      * without regard to case. The exact form of the URI is highly provider
 203      * dependent. If found, the provider's {@link FileSystemProvider#getFileSystem
 204      * getFileSystem} method is invoked to obtain a reference to the {@code
 205      * FileSystem}.
 206      *
 207      * <p> Once a file system created by this provider is {@link FileSystem#close
 208      * closed} it is provider-dependent if this method returns a reference to
 209      * the closed file system or throws {@link FileSystemNotFoundException}.
 210      * If the provider allows a new file system to be created with the same URI
 211      * as a file system it previously created then this method throws the
 212      * exception if invoked after the file system is closed (and before a new




  71  * may safely access files associated with the default provider but care needs
  72  * to be taken to avoid circular loading of other installed providers. If
  73  * circular loading of installed providers is detected then an unspecified error
  74  * is thrown.
  75  *
  76  * <p> This class also defines factory methods that allow a {@link ClassLoader}
  77  * to be specified when locating a provider. As with installed providers, the
  78  * provider classes are identified by placing the provider configuration file
  79  * in the resource directory {@code META-INF/services}.
  80  *
  81  * <p> If a thread initiates the loading of the installed file system providers
  82  * and another thread invokes a method that also attempts to load the providers
  83  * then the method will block until the loading completes.
  84  *
  85  * @since 1.7
  86  */
  87 
  88 public final class FileSystems {
  89     private FileSystems() { }
  90 










  91     // lazy initialization of default file system
  92     private static class DefaultFileSystemHolder {
  93         static final FileSystem defaultFileSystem = defaultFileSystem();
  94 
  95         // returns default file system
  96         private static FileSystem defaultFileSystem() {
  97             // load default provider
  98             FileSystemProvider provider = AccessController
  99                 .doPrivileged(new PrivilegedAction<>() {
 100                     public FileSystemProvider run() {
 101                         return getDefaultProvider();
 102                     }
 103                 });
 104 
 105             // return file system
 106             return provider.getFileSystem(URI.create("file:///"));
 107         }
 108 
 109         // returns default provider
 110         private static FileSystemProvider getDefaultProvider() {
 111             FileSystemProvider provider =
 112                 sun.nio.fs.DefaultFileSystemProvider.FILE_SYSTEM_PROVIDER;
 113 
 114             // if the property java.nio.file.spi.DefaultFileSystemProvider is
 115             // set then its value is the name of the default provider (or a list)
 116             String prop = "java.nio.file.spi.DefaultFileSystemProvider";
 117             String propValue = System.getProperty(prop);
 118             if (propValue != null) {
 119                 for (String cn: propValue.split(",")) {
 120                     try {
 121                         Class<?> c = Class
 122                             .forName(cn, true, ClassLoader.getSystemClassLoader());
 123                         Constructor<?> ctor = c
 124                             .getDeclaredConstructor(FileSystemProvider.class);
 125                         provider = (FileSystemProvider)ctor.newInstance(provider);
 126 
 127                         // must be "file"
 128                         if (!provider.getScheme().equals("file"))
 129                             throw new Error("Default provider must use scheme 'file'");
 130 
 131                     } catch (Exception x) {
 132                         throw new Error(x);


 163      * then an unspecified error is thrown. URI schemes are normally compared
 164      * without regard to case but for the default provider, the scheme is
 165      * required to be {@code "file"}. The first provider class is instantiated
 166      * by invoking it with a reference to the system-default provider.
 167      * The second provider class is instantiated by invoking it with a reference
 168      * to the first provider instance. The third provider class is instantiated
 169      * by invoking it with a reference to the second instance, and so on. The
 170      * last provider to be instantiated becomes the default provider; its {@code
 171      * getFileSystem} method is invoked with the URI {@code "file:///"} to
 172      * get a reference to the default file system.
 173      *
 174      * <p> Subsequent invocations of this method return the file system that was
 175      * returned by the first invocation.
 176      *
 177      * @return  the default file system
 178      */
 179     public static FileSystem getDefault() {
 180         if (VM.isModuleSystemInited()) {
 181             return DefaultFileSystemHolder.defaultFileSystem;
 182         } else {
 183             return sun.nio.fs.DefaultFileSystemProvider.theFileSystem();
 184         }
 185     }
 186 
 187     /**
 188      * Returns a reference to an existing {@code FileSystem}.
 189      *
 190      * <p> This method iterates over the {@link FileSystemProvider#installedProviders()
 191      * installed} providers to locate the provider that is identified by the URI
 192      * {@link URI#getScheme scheme} of the given URI. URI schemes are compared
 193      * without regard to case. The exact form of the URI is highly provider
 194      * dependent. If found, the provider's {@link FileSystemProvider#getFileSystem
 195      * getFileSystem} method is invoked to obtain a reference to the {@code
 196      * FileSystem}.
 197      *
 198      * <p> Once a file system created by this provider is {@link FileSystem#close
 199      * closed} it is provider-dependent if this method returns a reference to
 200      * the closed file system or throws {@link FileSystemNotFoundException}.
 201      * If the provider allows a new file system to be created with the same URI
 202      * as a file system it previously created then this method throws the
 203      * exception if invoked after the file system is closed (and before a new


< prev index next >