Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/nio/file/FileSystems.java
38918 views
/*1* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.file;2627import java.nio.file.spi.FileSystemProvider;28import java.net.URI;29import java.io.IOException;30import java.security.AccessController;31import java.security.PrivilegedAction;32import java.util.*;33import java.lang.reflect.Constructor;3435/**36* Factory methods for file systems. This class defines the {@link #getDefault37* getDefault} method to get the default file system and factory methods to38* construct other types of file systems.39*40* <p> The first invocation of any of the methods defined by this class causes41* the default {@link FileSystemProvider provider} to be loaded. The default42* provider, identified by the URI scheme "file", creates the {@link FileSystem}43* that provides access to the file systems accessible to the Java virtual44* machine. If the process of loading or initializing the default provider fails45* then an unspecified error is thrown.46*47* <p> The first invocation of the {@link FileSystemProvider#installedProviders48* installedProviders} method, by way of invoking any of the {@code49* newFileSystem} methods defined by this class, locates and loads all50* installed file system providers. Installed providers are loaded using the51* service-provider loading facility defined by the {@link ServiceLoader} class.52* Installed providers are loaded using the system class loader. If the53* system class loader cannot be found then the extension class loader is used;54* if there is no extension class loader then the bootstrap class loader is used.55* Providers are typically installed by placing them in a JAR file on the56* application class path or in the extension directory, the JAR file contains a57* provider-configuration file named {@code java.nio.file.spi.FileSystemProvider}58* in the resource directory {@code META-INF/services}, and the file lists one or59* more fully-qualified names of concrete subclass of {@link FileSystemProvider}60* that have a zero argument constructor.61* The ordering that installed providers are located is implementation specific.62* If a provider is instantiated and its {@link FileSystemProvider#getScheme()63* getScheme} returns the same URI scheme of a provider that was previously64* instantiated then the most recently instantiated duplicate is discarded. URI65* schemes are compared without regard to case. During construction a provider66* may safely access files associated with the default provider but care needs67* to be taken to avoid circular loading of other installed providers. If68* circular loading of installed providers is detected then an unspecified error69* is thrown.70*71* <p> This class also defines factory methods that allow a {@link ClassLoader}72* to be specified when locating a provider. As with installed providers, the73* provider classes are identified by placing the provider configuration file74* in the resource directory {@code META-INF/services}.75*76* <p> If a thread initiates the loading of the installed file system providers77* and another thread invokes a method that also attempts to load the providers78* then the method will block until the loading completes.79*80* @since 1.781*/8283public final class FileSystems {84private FileSystems() {85}8687// lazy initialization of default file system88private static class DefaultFileSystemHolder {89static final FileSystem defaultFileSystem = defaultFileSystem();9091// returns default file system92private static FileSystem defaultFileSystem() {93// load default provider94FileSystemProvider provider = AccessController95.doPrivileged(new PrivilegedAction<FileSystemProvider>() {96public FileSystemProvider run() {97return getDefaultProvider();98}99});100101// return file system102return provider.getFileSystem(URI.create("file:///"));103}104105// returns default provider106private static FileSystemProvider getDefaultProvider() {107FileSystemProvider provider = sun.nio.fs.DefaultFileSystemProvider.create();108109// if the property java.nio.file.spi.DefaultFileSystemProvider is110// set then its value is the name of the default provider (or a list)111String propValue = System112.getProperty("java.nio.file.spi.DefaultFileSystemProvider");113if (propValue != null) {114for (String cn: propValue.split(",")) {115try {116Class<?> c = Class117.forName(cn, true, ClassLoader.getSystemClassLoader());118Constructor<?> ctor = c119.getDeclaredConstructor(FileSystemProvider.class);120provider = (FileSystemProvider)ctor.newInstance(provider);121122// must be "file"123if (!provider.getScheme().equals("file"))124throw new Error("Default provider must use scheme 'file'");125126} catch (Exception x) {127throw new Error(x);128}129}130}131return provider;132}133}134135/**136* Returns the default {@code FileSystem}. The default file system creates137* objects that provide access to the file systems accessible to the Java138* virtual machine. The <em>working directory</em> of the file system is139* the current user directory, named by the system property {@code user.dir}.140* This allows for interoperability with the {@link java.io.File java.io.File}141* class.142*143* <p> The first invocation of any of the methods defined by this class144* locates the default {@link FileSystemProvider provider} object. Where the145* system property {@code java.nio.file.spi.DefaultFileSystemProvider} is146* not defined then the default provider is a system-default provider that147* is invoked to create the default file system.148*149* <p> If the system property {@code java.nio.file.spi.DefaultFileSystemProvider}150* is defined then it is taken to be a list of one or more fully-qualified151* names of concrete provider classes identified by the URI scheme152* {@code "file"}. Where the property is a list of more than one name then153* the names are separated by a comma. Each class is loaded, using the system154* class loader, and instantiated by invoking a one argument constructor155* whose formal parameter type is {@code FileSystemProvider}. The providers156* are loaded and instantiated in the order they are listed in the property.157* If this process fails or a provider's scheme is not equal to {@code "file"}158* then an unspecified error is thrown. URI schemes are normally compared159* without regard to case but for the default provider, the scheme is160* required to be {@code "file"}. The first provider class is instantiated161* by invoking it with a reference to the system-default provider.162* The second provider class is instantiated by invoking it with a reference163* to the first provider instance. The third provider class is instantiated164* by invoking it with a reference to the second instance, and so on. The165* last provider to be instantiated becomes the default provider; its {@code166* getFileSystem} method is invoked with the URI {@code "file:///"} to167* get a reference to the default file system.168*169* <p> Subsequent invocations of this method return the file system that was170* returned by the first invocation.171*172* @return the default file system173*/174public static FileSystem getDefault() {175return DefaultFileSystemHolder.defaultFileSystem;176}177178/**179* Returns a reference to an existing {@code FileSystem}.180*181* <p> This method iterates over the {@link FileSystemProvider#installedProviders()182* installed} providers to locate the provider that is identified by the URI183* {@link URI#getScheme scheme} of the given URI. URI schemes are compared184* without regard to case. The exact form of the URI is highly provider185* dependent. If found, the provider's {@link FileSystemProvider#getFileSystem186* getFileSystem} method is invoked to obtain a reference to the {@code187* FileSystem}.188*189* <p> Once a file system created by this provider is {@link FileSystem#close190* closed} it is provider-dependent if this method returns a reference to191* the closed file system or throws {@link FileSystemNotFoundException}.192* If the provider allows a new file system to be created with the same URI193* as a file system it previously created then this method throws the194* exception if invoked after the file system is closed (and before a new195* instance is created by the {@link #newFileSystem newFileSystem} method).196*197* <p> If a security manager is installed then a provider implementation198* may require to check a permission before returning a reference to an199* existing file system. In the case of the {@link FileSystems#getDefault200* default} file system, no permission check is required.201*202* @param uri the URI to locate the file system203*204* @return the reference to the file system205*206* @throws IllegalArgumentException207* if the pre-conditions for the {@code uri} parameter are not met208* @throws FileSystemNotFoundException209* if the file system, identified by the URI, does not exist210* @throws ProviderNotFoundException211* if a provider supporting the URI scheme is not installed212* @throws SecurityException213* if a security manager is installed and it denies an unspecified214* permission215*/216public static FileSystem getFileSystem(URI uri) {217String scheme = uri.getScheme();218for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {219if (scheme.equalsIgnoreCase(provider.getScheme())) {220return provider.getFileSystem(uri);221}222}223throw new ProviderNotFoundException("Provider \"" + scheme + "\" not found");224}225226/**227* Constructs a new file system that is identified by a {@link URI}228*229* <p> This method iterates over the {@link FileSystemProvider#installedProviders()230* installed} providers to locate the provider that is identified by the URI231* {@link URI#getScheme scheme} of the given URI. URI schemes are compared232* without regard to case. The exact form of the URI is highly provider233* dependent. If found, the provider's {@link FileSystemProvider#newFileSystem(URI,Map)234* newFileSystem(URI,Map)} method is invoked to construct the new file system.235*236* <p> Once a file system is {@link FileSystem#close closed} it is237* provider-dependent if the provider allows a new file system to be created238* with the same URI as a file system it previously created.239*240* <p> <b>Usage Example:</b>241* Suppose there is a provider identified by the scheme {@code "memory"}242* installed:243* <pre>244* Map<String,String> env = new HashMap<>();245* env.put("capacity", "16G");246* env.put("blockSize", "4k");247* FileSystem fs = FileSystems.newFileSystem(URI.create("memory:///?name=logfs"), env);248* </pre>249*250* @param uri251* the URI identifying the file system252* @param env253* a map of provider specific properties to configure the file system;254* may be empty255*256* @return a new file system257*258* @throws IllegalArgumentException259* if the pre-conditions for the {@code uri} parameter are not met,260* or the {@code env} parameter does not contain properties required261* by the provider, or a property value is invalid262* @throws FileSystemAlreadyExistsException263* if the file system has already been created264* @throws ProviderNotFoundException265* if a provider supporting the URI scheme is not installed266* @throws IOException267* if an I/O error occurs creating the file system268* @throws SecurityException269* if a security manager is installed and it denies an unspecified270* permission required by the file system provider implementation271*/272public static FileSystem newFileSystem(URI uri, Map<String,?> env)273throws IOException274{275return newFileSystem(uri, env, null);276}277278/**279* Constructs a new file system that is identified by a {@link URI}280*281* <p> This method first attempts to locate an installed provider in exactly282* the same manner as the {@link #newFileSystem(URI,Map) newFileSystem(URI,Map)}283* method. If none of the installed providers support the URI scheme then an284* attempt is made to locate the provider using the given class loader. If a285* provider supporting the URI scheme is located then its {@link286* FileSystemProvider#newFileSystem(URI,Map) newFileSystem(URI,Map)} is287* invoked to construct the new file system.288*289* @param uri290* the URI identifying the file system291* @param env292* a map of provider specific properties to configure the file system;293* may be empty294* @param loader295* the class loader to locate the provider or {@code null} to only296* attempt to locate an installed provider297*298* @return a new file system299*300* @throws IllegalArgumentException301* if the pre-conditions for the {@code uri} parameter are not met,302* or the {@code env} parameter does not contain properties required303* by the provider, or a property value is invalid304* @throws FileSystemAlreadyExistsException305* if the URI scheme identifies an installed provider and the file306* system has already been created307* @throws ProviderNotFoundException308* if a provider supporting the URI scheme is not found309* @throws ServiceConfigurationError310* when an error occurs while loading a service provider311* @throws IOException312* an I/O error occurs creating the file system313* @throws SecurityException314* if a security manager is installed and it denies an unspecified315* permission required by the file system provider implementation316*/317public static FileSystem newFileSystem(URI uri, Map<String,?> env, ClassLoader loader)318throws IOException319{320String scheme = uri.getScheme();321322// check installed providers323for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {324if (scheme.equalsIgnoreCase(provider.getScheme())) {325return provider.newFileSystem(uri, env);326}327}328329// if not found, use service-provider loading facility330if (loader != null) {331ServiceLoader<FileSystemProvider> sl = ServiceLoader332.load(FileSystemProvider.class, loader);333for (FileSystemProvider provider: sl) {334if (scheme.equalsIgnoreCase(provider.getScheme())) {335return provider.newFileSystem(uri, env);336}337}338}339340throw new ProviderNotFoundException("Provider \"" + scheme + "\" not found");341}342343/**344* Constructs a new {@code FileSystem} to access the contents of a file as a345* file system.346*347* <p> This method makes use of specialized providers that create pseudo file348* systems where the contents of one or more files is treated as a file349* system.350*351* <p> This method iterates over the {@link FileSystemProvider#installedProviders()352* installed} providers. It invokes, in turn, each provider's {@link353* FileSystemProvider#newFileSystem(Path,Map) newFileSystem(Path,Map)} method354* with an empty map. If a provider returns a file system then the iteration355* terminates and the file system is returned. If none of the installed356* providers return a {@code FileSystem} then an attempt is made to locate357* the provider using the given class loader. If a provider returns a file358* system then the lookup terminates and the file system is returned.359*360* @param path361* the path to the file362* @param loader363* the class loader to locate the provider or {@code null} to only364* attempt to locate an installed provider365*366* @return a new file system367*368* @throws ProviderNotFoundException369* if a provider supporting this file type cannot be located370* @throws ServiceConfigurationError371* when an error occurs while loading a service provider372* @throws IOException373* if an I/O error occurs374* @throws SecurityException375* if a security manager is installed and it denies an unspecified376* permission377*/378public static FileSystem newFileSystem(Path path,379ClassLoader loader)380throws IOException381{382if (path == null)383throw new NullPointerException();384Map<String,?> env = Collections.emptyMap();385386// check installed providers387for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {388try {389return provider.newFileSystem(path, env);390} catch (UnsupportedOperationException uoe) {391}392}393394// if not found, use service-provider loading facility395if (loader != null) {396ServiceLoader<FileSystemProvider> sl = ServiceLoader397.load(FileSystemProvider.class, loader);398for (FileSystemProvider provider: sl) {399try {400return provider.newFileSystem(path, env);401} catch (UnsupportedOperationException uoe) {402}403}404}405406throw new ProviderNotFoundException("Provider not found");407}408}409410411