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 */
017package org.apache.commons.vfs2.provider.res;
018
019import java.net.URL;
020import java.util.Arrays;
021import java.util.Collection;
022import java.util.Collections;
023
024import org.apache.commons.vfs2.Capability;
025import org.apache.commons.vfs2.FileName;
026import org.apache.commons.vfs2.FileObject;
027import org.apache.commons.vfs2.FileSystem;
028import org.apache.commons.vfs2.FileSystemConfigBuilder;
029import org.apache.commons.vfs2.FileSystemException;
030import org.apache.commons.vfs2.FileSystemOptions;
031import org.apache.commons.vfs2.provider.AbstractFileProvider;
032
033/**
034 * The Resource provider.
035 */
036public class ResourceFileProvider extends AbstractFileProvider {
037
038    /** The provider's capabilities */
039    protected static final Collection<Capability> capabilities = Collections
040            .unmodifiableCollection(Arrays.asList(Capability.DISPATCHER));
041
042    /**
043     * Constructs a new instance.
044     */
045    public ResourceFileProvider() {
046        setFileNameParser(ResourceFileNameParser.getInstance());
047    }
048
049    @Override
050    public void closeFileSystem(final FileSystem filesystem) {
051        // no filesystem created here - so nothing to do
052    }
053
054    /**
055     * Locates a file object, by absolute URI.
056     *
057     * @param baseFile The base file.
058     * @param uri The URI of the file to locate.
059     * @param fileSystemOptions The FileSystem options.
060     * @return the FileObject.
061     * @throws FileSystemException if an error occurs.
062     */
063    @Override
064    public FileObject findFile(final FileObject baseFile, final String uri, final FileSystemOptions fileSystemOptions)
065        throws FileSystemException {
066        final FileName fileName;
067        if (baseFile != null) {
068            fileName = parseUri(baseFile.getName(), uri);
069        } else {
070            fileName = parseUri(null, uri);
071        }
072        final String resourceName = fileName.getPath();
073
074        ClassLoader classLoader = ResourceFileSystemConfigBuilder.getInstance().getClassLoader(fileSystemOptions);
075        if (classLoader == null) {
076            classLoader = getClass().getClassLoader();
077        }
078        FileSystemException.requireNonNull(classLoader, "vfs.provider.url/badly-formed-uri.error", uri);
079        final URL url = classLoader.getResource(resourceName);
080
081        FileSystemException.requireNonNull(url, "vfs.provider.url/badly-formed-uri.error", uri);
082
083        return getContext().getFileSystemManager().resolveFile(url.toExternalForm());
084    }
085
086    @Override
087    public Collection<Capability> getCapabilities() {
088        return capabilities;
089    }
090
091    @Override
092    public FileSystemConfigBuilder getConfigBuilder() {
093        return ResourceFileSystemConfigBuilder.getInstance();
094    }
095}