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