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       * Creates a virtual file system, with the supplied file as its root.
35       *
36       * @param rootFile The root of the file system.
37       * @return A FileObject in the FileSystem.
38       * @throws FileSystemException if an error occurs.
39       */
40      public FileObject#FileObject">FileObject createFileSystem(final FileObject rootFile) throws FileSystemException {
41          final AbstractFileName../org/apache/commons/vfs2/provider/AbstractFileName.html#AbstractFileName">AbstractFileName rootName = (AbstractFileName) getContext().getFileSystemManager()
42                  .resolveName(rootFile.getName(), FileName.ROOT_PATH);
43          final VirtualFileSystemtualFileSystem.html#VirtualFileSystem">VirtualFileSystem fs = new VirtualFileSystem(rootName, rootFile.getFileSystem().getFileSystemOptions());
44          addComponent(fs);
45          fs.addJunction(FileName.ROOT_PATH, rootFile);
46          return fs.getRoot();
47      }
48  
49      /**
50       * Creates an empty virtual file system.
51       *
52       * @param rootUri The root of the file system.
53       * @return A FileObject in the FileSystem.
54       * @throws FileSystemException if an error occurs.
55       */
56      public FileObject createFileSystem(final String rootUri) throws FileSystemException {
57          final AbstractFileName rootName = new VirtualFileName(rootUri, FileName.ROOT_PATH, FileType.FOLDER);
58          final VirtualFileSystemtualFileSystem.html#VirtualFileSystem">VirtualFileSystem fs = new VirtualFileSystem(rootName, null);
59          addComponent(fs);
60          return fs.getRoot();
61      }
62  
63      /**
64       * Close a VirtualFileSystem by removing it from the {@code #components} list of this provider.
65       * <p>
66       * This gets called from DefaultFileManager#_closeFileSystem.
67       * </p>
68       *
69       * @param fileSystem the file system remembered by this provider.
70       */
71      void closeFileSystem(final FileSystem fileSystem) {
72          final AbstractFileSystem/../../org/apache/commons/vfs2/provider/AbstractFileSystem.html#AbstractFileSystem">AbstractFileSystem fs = (AbstractFileSystem) fileSystem;
73  
74          removeComponent(fs);
75          fs.close();
76      }
77  }