001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.io.file.spi;
019
020import java.net.URI;
021import java.net.URL;
022import java.nio.file.FileSystems;
023import java.nio.file.Path;
024import java.nio.file.spi.FileSystemProvider;
025import java.util.List;
026import java.util.Objects;
027
028/**
029 * Helps working with {@link FileSystemProvider}.
030 *
031 * @since 2.9.0
032 */
033public class FileSystemProviders {
034
035    private static final FileSystemProviders INSTALLED = new FileSystemProviders(FileSystemProvider.installedProviders());
036
037    /**
038     * Gets the {@link FileSystemProvider} for the given Path.
039     *
040     * @param path The Path to query
041     * @return the {@link FileSystemProvider} for the given Path.
042     */
043    @SuppressWarnings("resource") // FileSystem is not allocated here.
044    public static FileSystemProvider getFileSystemProvider(final Path path) {
045        return Objects.requireNonNull(path, "path").getFileSystem().provider();
046    }
047
048    /**
049     * Returns the instance for the installed providers.
050     *
051     * @return the instance for the installed providers.
052     * @see FileSystemProvider#installedProviders()
053     */
054    public static FileSystemProviders installed() {
055        return INSTALLED;
056    }
057
058    private final List<FileSystemProvider> providers;
059
060    /*
061     * Might make public later.
062     */
063    private FileSystemProviders(final List<FileSystemProvider> providers) {
064        this.providers = providers;
065    }
066
067    /**
068     * Gets the {@link FileSystemProvider} for the given scheme.
069     *
070     * @param scheme The scheme to query.
071     * @return the {@link FileSystemProvider} for the given URI or null.
072     */
073    @SuppressWarnings("resource") // FileSystems.getDefault() returns a constant.
074    public FileSystemProvider getFileSystemProvider(final String scheme) {
075        Objects.requireNonNull(scheme, "scheme");
076        // Check default provider first to avoid loading of installed providers.
077        if (scheme.equalsIgnoreCase("file")) {
078            return FileSystems.getDefault().provider();
079        }
080        // Find provider.
081        if (providers != null) {
082            for (final FileSystemProvider provider : providers) {
083                if (provider.getScheme().equalsIgnoreCase(scheme)) {
084                    return provider;
085                }
086            }
087        }
088        return null;
089    }
090
091    /**
092     * Gets the {@link FileSystemProvider} for the given URI.
093     *
094     * @param uri The URI to query
095     * @return the {@link FileSystemProvider} for the given URI or null.
096     */
097    public FileSystemProvider getFileSystemProvider(final URI uri) {
098        return getFileSystemProvider(Objects.requireNonNull(uri, "uri").getScheme());
099    }
100
101    /**
102     * Gets the {@link FileSystemProvider} for the given URL.
103     *
104     * @param url The URL to query
105     * @return the {@link FileSystemProvider} for the given URI or null.
106     */
107    public FileSystemProvider getFileSystemProvider(final URL url) {
108        return getFileSystemProvider(Objects.requireNonNull(url, "url").getProtocol());
109    }
110
111}