org.apache.commons.vfs2.FileSystem Java Examples

The following examples show how to use org.apache.commons.vfs2.FileSystem. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Webdav4sFileProvider.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a {@link FileSystem}.
 * <p>
 * If you're looking at this method and wondering how to get a FileSystemOptions object bearing the proxy host and
 * credentials configuration through to this method so it's used for resolving a
 * {@link org.apache.commons.vfs2.FileObject FileObject} in the FileSystem, then be sure to use correct signature of
 * the {@link org.apache.commons.vfs2.FileSystemManager FileSystemManager} resolveFile method.
 *
 * @see org.apache.commons.vfs2.impl.DefaultFileSystemManager#resolveFile(FileObject, String, FileSystemOptions)
 */
@Override
protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
        throws FileSystemException {
    // Create the file system
    final GenericFileName rootName = (GenericFileName) name;
    // TODO: need to check null to create a non-null here???
    final FileSystemOptions fsOpts = fileSystemOptions == null ? new FileSystemOptions() : fileSystemOptions;

    UserAuthenticationData authData = null;
    HttpClient httpClient = null;
    HttpClientContext httpClientContext = null;

    try {
        final Webdav4FileSystemConfigBuilder builder = Webdav4FileSystemConfigBuilder.getInstance();
        authData = UserAuthenticatorUtils.authenticate(fsOpts, Webdav4FileProvider.AUTHENTICATOR_TYPES);
        httpClientContext = createHttpClientContext(builder, rootName, fsOpts, authData);
        httpClient = createHttpClient(builder, rootName, fsOpts);
    } finally {
        UserAuthenticatorUtils.cleanup(authData);
    }

    return new Webdav4FileSystem(rootName, fsOpts, httpClient, httpClientContext) {
    };
}
 
Example #2
Source File: NullFilesCacheTests.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@Override
public void testBasicCacheOps() throws Exception {
    final DefaultFileSystemManager manager = getManager();
    Assert.assertNotNull("This test should not have a null DefaultFileSystemManager", manager);
    // the basic test looks different for a null cache:
    final FilesCache cache = manager.getFilesCache();
    final FileObject fo = getWriteFolder().resolveFile("dir1");
    final FileName fn = fo.getName();
    final FileSystem fs = fo.getFileSystem();

    cache.clear(fs);
    assertNull(cache.getFile(fs, fn));

    cache.putFile(fo);
    assertNull(null, cache.getFile(fs, fn));

    assertFalse(cache.putFileIfAbsent(fo)); // hmmm?
    assertNull(null, cache.getFile(fs, fn));

    cache.removeFile(fs, fn);
    assertNull(cache.getFile(fs, fn));
}
 
Example #3
Source File: ZipProviderWithCharsetTestCase.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the base folder for read tests.
 */
@Override
public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
    final FileSystemOptions opts = new FileSystemOptions();
    final ZipFileSystemConfigBuilder builder = ZipFileSystemConfigBuilder.getInstance();
    // Tests the same charset as the default but we exercise having a Charset set.
    builder.setCharset(opts, StandardCharsets.UTF_8);

    final File zipFile = AbstractVfsTestCase.getTestResource("test.zip");
    final String uri = "zip:file:" + zipFile.getAbsolutePath() + "!/";
    final FileObject resolvedFile = manager.resolveFile(uri, opts);
    final FileSystem fileSystem = resolvedFile.getFileSystem();
    Assert.assertTrue(fileSystem instanceof ZipFileSystem);
    final ZipFileSystem zipFileSystem = (ZipFileSystem) fileSystem;
    Assert.assertEquals(StandardCharsets.UTF_8, zipFileSystem.getCharset());
    return resolvedFile;
}
 
Example #4
Source File: SoftRefFilesCache.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@Override
public void clear(final FileSystem fileSystem) {
    final Map<FileName, Reference<FileObject>> files = getOrCreateFilesystemCache(fileSystem);

    lock.lock();
    try {
        final Iterator<FileSystemAndNameKey> iterKeys = refReverseMap.values().iterator();
        while (iterKeys.hasNext()) {
            final FileSystemAndNameKey key = iterKeys.next();
            if (key.getFileSystem() == fileSystem) {
                iterKeys.remove();
                files.remove(key.getFileName());
            }
        }

        if (files.size() < 1) {
            close(fileSystem);
        }
    } finally {
        lock.unlock();
    }
}
 
Example #5
Source File: PentahoSolutionFileProvider.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
private FileSystem createWebFileSystem( final LayeredFileName genericRootName,
                                        final FileSystemOptions fileSystemOptions ) throws FileSystemException {
  final GenericFileName outerName = (GenericFileName) genericRootName.getOuterName();
  String scheme = outerName.getScheme();
  String hostName = outerName.getHostName();
  int port = outerName.getPort();
  String userName = outerName.getUserName();
  String password = outerName.getPassword();

  HttpClientManager.HttpClientBuilderFacade clientBuilder = HttpClientManager.getInstance().createBuilder();
  if ( !StringUtil.isEmpty( hostName ) ) {
    clientBuilder.setProxy( hostName, port, scheme );
  }
  if ( !StringUtil.isEmpty( userName ) ) {
    clientBuilder.setCredentials( userName, password );
  }
  final PentahoSolutionsFileSystemConfigBuilder configBuilder = new PentahoSolutionsFileSystemConfigBuilder();
  final int timeOut = configBuilder.getTimeOut( fileSystemOptions );
  clientBuilder.setSocketTimeout( Math.max( 0, timeOut ) );

  return new WebSolutionFileSystem( genericRootName, fileSystemOptions,
    new LocalFileModel( outerName.getURI(), clientBuilder, userName, password, hostName, port )
  );
}
 
Example #6
Source File: Http5FileProvider.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@Override
protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
        throws FileSystemException {
    final GenericFileName rootName = (GenericFileName) name;

    UserAuthenticationData authData = null;
    HttpClient httpClient = null;
    HttpClientContext httpClientContext = null;

    try {
        final Http5FileSystemConfigBuilder builder = Http5FileSystemConfigBuilder.getInstance();
        authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);
        httpClientContext = createHttpClientContext(builder, rootName, fileSystemOptions, authData);
        httpClient = createHttpClient(builder, rootName, fileSystemOptions);
    } finally {
        UserAuthenticatorUtils.cleanup(authData);
    }

    return new Http5FileSystem(rootName, fileSystemOptions, httpClient, httpClientContext);
}
 
Example #7
Source File: WebdavFileProvider.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a {@link FileSystem}.
 * <p>
 * If you're looking at this method and wondering how to get a FileSystemOptions object bearing the proxy host and
 * credentials configuration through to this method so it's used for resolving a
 * {@link org.apache.commons.vfs2.FileObject FileObject} in the FileSystem, then be sure to use correct signature of
 * the {@link org.apache.commons.vfs2.FileSystemManager FileSystemManager} resolveFile method.
 * </p>
 *
 * @see org.apache.commons.vfs2.impl.DefaultFileSystemManager#resolveFile(FileObject, String, FileSystemOptions)
 */
@Override
protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
        throws FileSystemException {
    // Create the file system
    final GenericFileName rootName = (GenericFileName) name;
    final FileSystemOptions fsOpts = fileSystemOptions == null ? new FileSystemOptions() : fileSystemOptions;

    UserAuthenticationData authData = null;
    HttpClient httpClient;
    try {
        authData = UserAuthenticatorUtils.authenticate(fsOpts, AUTHENTICATOR_TYPES);

        httpClient = HttpClientFactory.createConnection(WebdavFileSystemConfigBuilder.getInstance(), "http",
                rootName.getHostName(), rootName.getPort(),
                UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
                        UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName()))),
                UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
                        UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword()))),
                fsOpts);
    } finally {
        UserAuthenticatorUtils.cleanup(authData);
    }

    return new WebdavFileSystem(rootName, httpClient, fsOpts);
}
 
Example #8
Source File: SoftRefFilesCache.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@Override
public FileObject getFile(final FileSystem fileSystem, final FileName fileName) {
    final Map<FileName, Reference<FileObject>> files = getOrCreateFilesystemCache(fileSystem);

    lock.lock();
    try {
        final Reference<FileObject> ref = files.get(fileName);
        if (ref == null) {
            return null;
        }

        final FileObject fo = ref.get();
        if (fo == null) {
            removeFile(fileSystem, fileName);
        }
        return fo;
    } finally {
        lock.unlock();
    }
}
 
Example #9
Source File: SoftRefFilesCache.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
protected Map<FileName, Reference<FileObject>> getOrCreateFilesystemCache(final FileSystem fileSystem) {
    if (fileSystemCache.size() < 1) {
        startThread();
    }

    Map<FileName, Reference<FileObject>> files;

    do {
        files = fileSystemCache.get(fileSystem);
        if (files != null) {
            break;
        }
        files = new HashMap<>();
    } while (fileSystemCache.putIfAbsent(fileSystem, files) == null);

    return files;
}
 
Example #10
Source File: Http4FileProvider.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@Override
protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
        throws FileSystemException {
    final GenericFileName rootName = (GenericFileName) name;

    UserAuthenticationData authData = null;
    HttpClient httpClient = null;
    HttpClientContext httpClientContext = null;

    try {
        final Http4FileSystemConfigBuilder builder = Http4FileSystemConfigBuilder.getInstance();
        authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);
        httpClientContext = createHttpClientContext(builder, rootName, fileSystemOptions, authData);
        httpClient = createHttpClient(builder, rootName, fileSystemOptions);
    } finally {
        UserAuthenticatorUtils.cleanup(authData);
    }

    return new Http4FileSystem(rootName, fileSystemOptions, httpClient, httpClientContext);
}
 
Example #11
Source File: CanWriteFileFilter.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Checks to see if the file can be written to.
 *
 * @param fileInfo the File to check
 *
 * @return {@code true} if the file can be written to, otherwise {@code false}.
 * @throws FileSystemException Thrown for file system errors.
 */
@Override
public boolean accept(final FileSelectInfo fileInfo) throws FileSystemException {
    try (final FileObject file = fileInfo.getFile()) {
        final FileSystem fileSystem = file.getFileSystem();
        if (file.exists()) {
            if (!fileSystem.hasCapability(Capability.WRITE_CONTENT)) {
                return false;
            }
            return file.isWriteable();
        }
        if (!fileSystem.hasCapability(Capability.CREATE)) {
            return false;
        }
        return file.getParent().isWriteable();
    }
}
 
Example #12
Source File: JunctionTests.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Checks ancestors are created when a junction is created.
 */
public void testAncestors() throws Exception {
    final FileSystem fs = getManager().createVirtualFileSystem("vfs://").getFileSystem();
    final FileObject baseDir = getBaseDir();

    // Make sure the file at the junction point and its ancestors do not exist
    FileObject file = fs.resolveFile("/a/b");
    assertFalse(file.exists());
    file = file.getParent();
    assertFalse(file.exists());
    file = file.getParent();
    assertFalse(file.exists());

    // Add the junction
    fs.addJunction("/a/b", baseDir);

    // Make sure the file at the junction point and its ancestors exist
    file = fs.resolveFile("/a/b");
    assertTrue("Does not exist", file.exists());
    file = file.getParent();
    assertTrue("Does not exist", file.exists());
    file = file.getParent();
    assertTrue("Does not exist", file.exists());
}
 
Example #13
Source File: FtpsFileProvider.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the file system.
 */
@Override
protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
        throws FileSystemException {
    // Create the file system
    final GenericFileName rootName = (GenericFileName) name;

    final FtpsClientWrapper ftpClient = new FtpsClientWrapper(rootName, fileSystemOptions);

    return new FtpsFileSystem(rootName, ftpClient, fileSystemOptions);
}
 
Example #14
Source File: ValidateLoginTask.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public boolean validateLoginData() throws FileSystemException {
  if ( loginData == null ) {
    return true;
  }
  final FileObject vfsConnection = PublishUtil.createVFSConnection( VFS.getManager(), loginData );
  try {
    final FileSystem fileSystem = vfsConnection.getFileSystem();
    if ( fileSystem instanceof WebSolutionFileSystem ) {
      final WebSolutionFileSystem webSolutionFileSystem = (WebSolutionFileSystem) fileSystem;
      final Long l = (Long) webSolutionFileSystem.getAttribute( WebSolutionFileSystem.LAST_REFRESH_TIME_ATTRIBUTE );
      if ( l != null ) {
        if ( ( System.currentTimeMillis() - l ) > 500 ) {
          webSolutionFileSystem.getLocalFileModel().refresh();
        }
      }
      return true;
    }
  } catch ( FileSystemException fse ) {
    // not all file systems support attributes ..
  } catch ( IOException e ) {
    return false;
  }
  final FileType type = vfsConnection.getType();
  if ( type != FileType.FOLDER ) {
    return false;
  }
  return true;
}
 
Example #15
Source File: MimeFileProvider.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the file system.
 */
@Override
protected FileSystem doCreateFileSystem(final String scheme, final FileObject file,
        final FileSystemOptions fileSystemOptions) throws FileSystemException {
    final FileName name = new LayeredFileName(scheme, file.getName(), FileName.ROOT_PATH, FileType.FOLDER);
    return new MimeFileSystem(name, file, fileSystemOptions);
}
 
Example #16
Source File: LRUFilesCache.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
@Override
public void clear(final FileSystem filesystem) {
    final Map<FileName, FileObject> files = getOrCreateFilesystemCache(filesystem);

    writeLock.lock();
    try {
        files.clear();

        filesystemCache.remove(filesystem);
    } finally {
        writeLock.unlock();
    }
}
 
Example #17
Source File: AbstractProviderTestCase.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Runs the test. This implementation short-circuits the test if the provider being tested does not have the
 * capabilities required by this test.
 * <p>
 * TODO - Handle negative caps as well - ie, only run a test if the provider does not have certain caps.
 * </p>
 * <p>
 * TODO - Figure out how to remove the test from the TestResult if the test is skipped.
 * </p>
 */
@Override
protected void runTest() throws Throwable {
    // Check the capabilities
    final Capability[] caps = getRequiredCaps();
    if (caps != null) {
        for (final Capability cap2 : caps) {
            final Capability cap = cap2;
            final FileSystem fs = getFileSystem();
            if (!fs.hasCapability(cap)) {
                // String name = fs.getClass().getName();
                // int index = name.lastIndexOf('.');
                // String fsName = (index > 0) ? name.substring(index + 1) : name;
                // System.out.println("skipping " + getName() + " because " +
                // fsName + " does not have capability " + cap);
                return;
            }
        }
    }

    // Provider has all the capabilities - execute the test
    if (method != null) {
        try {
            method.invoke(this, (Object[]) null);
        } catch (final InvocationTargetException e) {
            throw e.getTargetException();
        }
    } else {
        super.runTest();
    }

    if (((AbstractFileSystem) readFolder.getFileSystem()).isOpen()) {
        String name = "unknown";
        if (method != null) {
            name = method.getName();
        }

        throw new IllegalStateException(getClass().getName() + ": filesystem has open streams after: " + name);
    }
}
 
Example #18
Source File: LRUFilesCache.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
@Override
public void removeFile(final FileSystem filesystem, final FileName name) {
    final Map<?, ?> files = getOrCreateFilesystemCache(filesystem);

    writeLock.lock();
    try {
        files.remove(name);

        if (files.size() < 1) {
            filesystemCache.remove(filesystem);
        }
    } finally {
        writeLock.unlock();
    }
}
 
Example #19
Source File: PentahoSolutionFileProvider.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Creates a {@link org.apache.commons.vfs2.FileSystem}.  If the returned FileSystem implements {@link
 * org.apache.commons.vfs2.provider.VfsComponent}, it will be initialised.
 *
 * @param rootName The name of the root file of the file system to create.
 */
protected FileSystem doCreateFileSystem( final FileName rootName,
                                         final FileSystemOptions fileSystemOptions ) throws FileSystemException {
  final LayeredFileName genericRootName = (LayeredFileName) rootName;
  if ( "jcr-solution".equals( rootName.getScheme() ) ) {

    // bypass authentication if running inside server
    if ( this.bypassAuthentication ) {
      return createJCRDirectFileSystem( genericRootName, fileSystemOptions );
    }

    return createJCRFileSystem( genericRootName, fileSystemOptions );
  }
  return createWebFileSystem( genericRootName, fileSystemOptions );
}
 
Example #20
Source File: SftpFileProvider.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link FileSystem}.
 */
@Override
protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
        throws FileSystemException {
    // Create the file system
    return new SftpFileSystem((GenericFileName) name, createSession((GenericFileName) name, fileSystemOptions),
            fileSystemOptions);
}
 
Example #21
Source File: TarFileProvider.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a layered file system. This method is called if the file system is not cached.
 *
 * @param scheme The URI scheme.
 * @param file The file to create the file system on top of.
 * @return The file system.
 */
@Override
protected FileSystem doCreateFileSystem(final String scheme, final FileObject file,
        final FileSystemOptions fileSystemOptions) throws FileSystemException {
    final AbstractFileName rootName = new LayeredFileName(scheme, file.getName(), FileName.ROOT_PATH,
            FileType.FOLDER);
    return new TarFileSystem(rootName, file, fileSystemOptions);
}
 
Example #22
Source File: HttpFileProvider.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link FileSystem}.
 */
@Override
protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
        throws FileSystemException {
    // Create the file system
    final GenericFileName rootName = (GenericFileName) name;

    UserAuthenticationData authData = null;
    HttpClient httpClient;
    try {
        authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);

        final String fileScheme = rootName.getScheme();
        final char lastChar = fileScheme.charAt(fileScheme.length() - 1);
        final String internalScheme = (lastChar == 's' || lastChar == 'S') ? "https" : "http";

        httpClient = HttpClientFactory.createConnection(internalScheme, rootName.getHostName(),
                rootName.getPort(),
                UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
                        UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName()))),
                UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
                        UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword()))),
                fileSystemOptions);
    } finally {
        UserAuthenticatorUtils.cleanup(authData);
    }

    return new HttpFileSystem(rootName, httpClient, fileSystemOptions);
}
 
Example #23
Source File: ConcurrentFileSystemManager.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Override
public void _closeFileSystem( FileSystem filesystem ) {
  lock.readLock().lock();
  try {
    super._closeFileSystem( filesystem );
  } finally {
    lock.readLock().unlock();
  }
}
 
Example #24
Source File: ContentTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Tests parent identity
 */
public void testParent() throws FileSystemException {
    // Test when both exist
    FileObject folder = getReadFolder().resolveFile("dir1");
    FileObject child = folder.resolveFile("file3.txt");
    assertTrue("folder exists", folder.exists());
    assertTrue("child exists", child.exists());
    assertSame(folder, child.getParent());

    // Test when file does not exist
    child = folder.resolveFile("unknown-file");
    assertTrue("folder exists", folder.exists());
    assertFalse("child does not exist", child.exists());
    assertSame(folder, child.getParent());

    // Test when neither exists
    folder = getReadFolder().resolveFile("unknown-folder");
    child = folder.resolveFile("unknown-file");
    assertFalse("folder does not exist", folder.exists());
    assertFalse("child does not exist", child.exists());
    assertSame(folder, child.getParent());

    // Test the parent of the root of the file system
    // TODO - refactor out test cases for layered vs originating fs
    final FileSystem fileSystem = getFileSystem();
    final FileObject root = fileSystem.getRoot();
    if (fileSystem.getParentLayer() == null) {
        // No parent layer, so parent should be null
        assertNull("root has null parent", root.getParent());
    } else {
        // Parent should be parent of parent layer.
        assertSame(fileSystem.getParentLayer().getParent(), root.getParent());
    }
}
 
Example #25
Source File: DefaultFilesCache.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
@Override
public void clear(final FileSystem filesystem) {
    // avoid keeping a reference to the FileSystem (key) object
    final Map<FileName, FileObject> files = filesystemCache.remove(filesystem);
    if (files != null) {
        files.clear(); // help GC
    }
}
 
Example #26
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Closes the given file system.
 * <p>
 * If you use VFS as singleton it is VERY dangerous to call this method.
 *
 * @param fileSystem The FileSystem to close.
 */
@Override
public void closeFileSystem(final FileSystem fileSystem) {
    // inform the cache ...
    getFilesCache().clear(fileSystem);

    // just in case the cache didnt call _closeFileSystem
    _closeFileSystem(fileSystem);
}
 
Example #27
Source File: ContentTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Tests root of file system exists.
 */
public void testRootURI() throws FileSystemException {
    if (!this.getProviderConfig().isFileSystemRootAccessible()) {
        return;
    }
    final FileSystem fileSystem = getFileSystem();
    final String uri = fileSystem.getRootURI();
    testRoot(getManager().resolveFile(uri, fileSystem.getFileSystemOptions()));
}
 
Example #28
Source File: TemporaryFileProvider.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Locates a file object, by absolute URI.
 *
 * @param baseFile The base FileObject.
 * @param uri The URI of the file to be located.
 * @param properties FileSystemOptions to use to locate or create the file.
 * @return The FileObject.
 * @throws FileSystemException if an error occurs.
 */
@Override
public synchronized FileObject findFile(final FileObject baseFile, final String uri,
        final FileSystemOptions properties) throws FileSystemException {
    // Parse the name
    final StringBuilder buffer = new StringBuilder(uri);
    final String scheme = UriParser.extractScheme(getContext().getFileSystemManager().getSchemes(), uri, buffer);
    UriParser.fixSeparators(buffer);
    UriParser.normalisePath(buffer);
    final String path = buffer.toString();

    // Create the temp file system if it does not exist
    // FileSystem filesystem = findFileSystem( this, (Properties) null);
    FileSystem filesystem = findFileSystem(this, properties);
    if (filesystem == null) {
        if (rootFile == null) {
            rootFile = getContext().getTemporaryFileStore().allocateFile("tempfs");
        }
        final FileName rootName = getContext().parseURI(scheme + ":" + FileName.ROOT_PATH);
        // final FileName rootName =
        // new LocalFileName(scheme, scheme + ":", FileName.ROOT_PATH);
        filesystem = new LocalFileSystem(rootName, rootFile.getAbsolutePath(), properties);
        addFileSystem(this, filesystem);
    }

    // Find the file
    return filesystem.resolveFile(path);
}
 
Example #29
Source File: AbstractLayeredFileProvider.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a layered file system.
 *
 * @param scheme The protocol to use.
 * @param file a FileObject.
 * @param fileSystemOptions Options to access the FileSystem.
 * @return A FileObject associated with the new FileSystem.
 * @throws FileSystemException if an error occurs.
 */
@Override
public synchronized FileObject createFileSystem(final String scheme, final FileObject file,
        final FileSystemOptions fileSystemOptions) throws FileSystemException {
    // Check if cached
    final FileName rootName = file.getName();
    FileSystem fs = findFileSystem(rootName, fileSystemOptions);
    if (fs == null) {
        // Create the file system
        fs = doCreateFileSystem(scheme, file, fileSystemOptions);
        addFileSystem(rootName, fs);
    }
    return fs.getRoot();
}
 
Example #30
Source File: AbstractOriginatingFileProvider.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Locates a file from its parsed URI.
 *
 * @param name The file name.
 * @param fileSystemOptions FileSystem options.
 * @return A FileObject associated with the file.
 * @throws FileSystemException if an error occurs.
 */
protected FileObject findFile(final FileName name, final FileSystemOptions fileSystemOptions)
        throws FileSystemException {
    // Check in the cache for the file system
    final FileName rootName = getContext().getFileSystemManager().resolveName(name, FileName.ROOT_PATH);

    final FileSystem fs = getFileSystem(rootName, fileSystemOptions);

    // Locate the file
    // return fs.resolveFile(name.getPath());
    return fs.resolveFile(name);
}