View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.impl;
18  
19  import org.apache.commons.vfs2.FileName;
20  import org.apache.commons.vfs2.FileObject;
21  import org.apache.commons.vfs2.FileSystem;
22  import org.apache.commons.vfs2.FileSystemException;
23  import org.apache.commons.vfs2.FileType;
24  import org.apache.commons.vfs2.provider.AbstractFileName;
25  import org.apache.commons.vfs2.provider.AbstractFileSystem;
26  import org.apache.commons.vfs2.provider.AbstractVfsContainer;
27  
28  /**
29   * A virtual file system provider.
30   */
31  public class VirtualFileProvider extends AbstractVfsContainer {
32  
33      /**
34       * Constructs a new instance.
35       */
36      public VirtualFileProvider() {
37          // empty
38      }
39  
40      /**
41       * Close a VirtualFileSystem by removing it from the {@code #components} list of this provider.
42       * <p>
43       * This gets called from DefaultFileManager#_closeFileSystem.
44       * </p>
45       *
46       * @param fileSystem the file system remembered by this provider.
47       */
48      void closeFileSystem(final FileSystem fileSystem) {
49          final AbstractFileSystem fs = (AbstractFileSystem) fileSystem;
50  
51          removeComponent(fs);
52          fs.close();
53      }
54  
55      /**
56       * Creates a virtual file system, with the supplied file as its root.
57       *
58       * @param rootFile The root of the file system.
59       * @return A FileObject in the FileSystem.
60       * @throws FileSystemException if an error occurs.
61       */
62      public FileObject createFileSystem(final FileObject rootFile) throws FileSystemException {
63          final AbstractFileName rootName = (AbstractFileName) getContext().getFileSystemManager()
64                  .resolveName(rootFile.getName(), FileName.ROOT_PATH);
65          final VirtualFileSystem fs = new VirtualFileSystem(rootName, rootFile.getFileSystem().getFileSystemOptions());
66          addComponent(fs);
67          fs.addJunction(FileName.ROOT_PATH, rootFile);
68          return fs.getRoot();
69      }
70  
71      /**
72       * Creates an empty virtual file system.
73       *
74       * @param rootUri The root of the file system.
75       * @return A FileObject in the FileSystem.
76       * @throws FileSystemException if an error occurs.
77       */
78      public FileObject createFileSystem(final String rootUri) throws FileSystemException {
79          final AbstractFileName rootName = new VirtualFileName(rootUri, FileName.ROOT_PATH, FileType.FOLDER);
80          final VirtualFileSystem fs = new VirtualFileSystem(rootName, null);
81          addComponent(fs);
82          return fs.getRoot();
83      }
84  }