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.impl;
018
019import org.apache.commons.vfs2.FileName;
020import org.apache.commons.vfs2.FileObject;
021import org.apache.commons.vfs2.FileSystem;
022import org.apache.commons.vfs2.FileSystemException;
023import org.apache.commons.vfs2.FileType;
024import org.apache.commons.vfs2.provider.AbstractFileName;
025import org.apache.commons.vfs2.provider.AbstractFileSystem;
026import org.apache.commons.vfs2.provider.AbstractVfsContainer;
027
028/**
029 * A virtual filesystem provider.
030 */
031public class VirtualFileProvider extends AbstractVfsContainer {
032    /**
033     * Creates a virtual file system, with the supplied file as its root.
034     *
035     * @param rootFile The root of the file system.
036     * @return A FileObject in the FileSystem.
037     * @throws FileSystemException if an error occurs.
038     */
039    public FileObject createFileSystem(final FileObject rootFile) throws FileSystemException {
040        final AbstractFileName rootName = (AbstractFileName) getContext().getFileSystemManager()
041                .resolveName(rootFile.getName(), FileName.ROOT_PATH);
042        final VirtualFileSystem fs = new VirtualFileSystem(rootName, rootFile.getFileSystem().getFileSystemOptions());
043        addComponent(fs);
044        fs.addJunction(FileName.ROOT_PATH, rootFile);
045        return fs.getRoot();
046    }
047
048    /**
049     * Creates an empty virtual file system.
050     *
051     * @param rootUri The root of the file system.
052     * @return A FileObject in the FileSystem.
053     * @throws FileSystemException if an error occurs.
054     */
055    public FileObject createFileSystem(final String rootUri) throws FileSystemException {
056        final AbstractFileName rootName = new VirtualFileName(rootUri, FileName.ROOT_PATH, FileType.FOLDER);
057        final VirtualFileSystem fs = new VirtualFileSystem(rootName, null);
058        addComponent(fs);
059        return fs.getRoot();
060    }
061
062    /**
063     * Close a VirtualFileSystem by removing it from the {@code #components} list of this provider.
064     * <p>
065     * This gets called from DefaultFileManager#_closeFileSystem.
066     *
067     * @param filesystem the file system remembered by this provider.
068     */
069    void closeFileSystem(final FileSystem filesystem) {
070        final AbstractFileSystem fs = (AbstractFileSystem) filesystem;
071
072        removeComponent(fs);
073        fs.close();
074    }
075}