    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 *      https://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.Collections;
    026import java.util.List;
    027import java.util.Objects;
    028
    029/**
    030 * Helps to work with {@link FileSystemProvider}.
    031 *
    032 * @since 2.9.0
    033 */
    034public class FileSystemProviders { // NOPMD Class will be final in 3.0.
    035
    036    private static final String SCHEME_FILE = "file";
    037    private static final FileSystemProviders INSTALLED = new FileSystemProviders(FileSystemProvider.installedProviders());
    038
    039    /**
    040     * Gets the {@link FileSystemProvider} for the given Path.
    041     *
    042     * @param path The Path to query
    043     * @return the {@link FileSystemProvider} for the given Path.
    044     */
    045    @SuppressWarnings("resource") // FileSystem is not allocated here.
    046    public static FileSystemProvider getFileSystemProvider(final Path path) {
    047        return Objects.requireNonNull(path, "path").getFileSystem().provider();
    048    }
    049
    050    /**
    051     * Returns the instance for the installed providers.
    052     *
    053     * @return the instance for the installed providers.
    054     * @see FileSystemProvider#installedProviders()
    055     */
    056    public static FileSystemProviders installed() {
    057        return INSTALLED;
    058    }
    059
    060    private final List<FileSystemProvider> providers;
    061
    062    /*
    063     * Might make public later.
    064     */
    065    private FileSystemProviders(final List<FileSystemProvider> providers) {
    066        this.providers = providers != null ? providers : Collections.emptyList();
    067    }
    068
    069    /**
    070     * Gets the {@link FileSystemProvider} for the given scheme.
    071     *
    072     * @param scheme The scheme to query.
    073     * @return the {@link FileSystemProvider} for the given URI or null.
    074     */
    075    @SuppressWarnings("resource") // FileSystems.getDefault() returns a constant.
    076    public FileSystemProvider getFileSystemProvider(final String scheme) {
    077        Objects.requireNonNull(scheme, "scheme");
    078        // Check default provider first to avoid loading of installed providers.
    079        if (scheme.equalsIgnoreCase(SCHEME_FILE)) {
    080            return FileSystems.getDefault().provider();
    081        }
    082        // Find provider.
    083        return providers.stream().filter(provider -> provider.getScheme().equalsIgnoreCase(scheme)).findFirst().orElse(null);
    084    }
    085
    086    /**
    087     * Gets the {@link FileSystemProvider} for the given URI.
    088     *
    089     * @param uri The URI to query
    090     * @return the {@link FileSystemProvider} for the given URI or null.
    091     */
    092    public FileSystemProvider getFileSystemProvider(final URI uri) {
    093        return getFileSystemProvider(Objects.requireNonNull(uri, "uri").getScheme());
    094    }
    095
    096    /**
    097     * Gets the {@link FileSystemProvider} for the given URL.
    098     *
    099     * @param url The URL to query
    100     * @return the {@link FileSystemProvider} for the given URI or null.
    101     */
    102    public FileSystemProvider getFileSystemProvider(final URL url) {
    103        return getFileSystemProvider(Objects.requireNonNull(url, "url").getProtocol());
    104    }
    105
    106}