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.FileObject;
026import org.apache.commons.vfs2.FileSystem;
027import org.apache.commons.vfs2.FileSystemConfigBuilder;
028import org.apache.commons.vfs2.FileSystemException;
029import org.apache.commons.vfs2.FileSystemOptions;
030import org.apache.commons.vfs2.provider.AbstractFileProvider;
031import org.apache.commons.vfs2.provider.UriParser;
032
033/**
034 * The Resource provider.
035 */
036public class ResourceFileProvider extends AbstractFileProvider {
037    /** The provider's capabilities */
038    protected static final Collection<Capability> capabilities = Collections
039            .unmodifiableCollection(Arrays.asList(new Capability[] { Capability.DISPATCHER }));
040
041    private static final int BUFFER_SIZE = 80;
042
043    public ResourceFileProvider() {
044        super();
045    }
046
047    /**
048     * Locates a file object, by absolute URI.
049     *
050     * @param baseFile The base file.
051     * @param uri The URI of the file to locate.
052     * @param fileSystemOptions The FileSystem options.
053     * @return the FileObject.
054     * @throws FileSystemException if an error occurs.
055     */
056    @Override
057    public FileObject findFile(final FileObject baseFile, final String uri, final FileSystemOptions fileSystemOptions)
058            throws FileSystemException {
059        final StringBuilder buf = new StringBuilder(BUFFER_SIZE);
060        UriParser.extractScheme(uri, buf);
061        final String resourceName = buf.toString();
062
063        ClassLoader cl = ResourceFileSystemConfigBuilder.getInstance().getClassLoader(fileSystemOptions);
064        if (cl == null) {
065            cl = getClass().getClassLoader();
066        }
067        final URL url = cl.getResource(resourceName);
068
069        if (url == null) {
070            throw new FileSystemException("vfs.provider.url/badly-formed-uri.error", uri);
071        }
072
073        return getContext().getFileSystemManager().resolveFile(url.toExternalForm());
074    }
075
076    @Override
077    public FileSystemConfigBuilder getConfigBuilder() {
078        return org.apache.commons.vfs2.provider.res.ResourceFileSystemConfigBuilder.getInstance();
079    }
080
081    @Override
082    public void closeFileSystem(final FileSystem filesystem) {
083        // no filesystem created here - so nothing to do
084    }
085
086    @Override
087    public Collection<Capability> getCapabilities() {
088        return capabilities;
089    }
090}