org.apache.commons.vfs2.provider.FileProvider Java Examples

The following examples show how to use org.apache.commons.vfs2.provider.FileProvider. 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: ConcurrentFileSystemManager.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Override
public void addProvider( String[] urlSchemes, FileProvider provider ) throws FileSystemException {
  lock.writeLock().lock();
  try {
    super.addProvider( urlSchemes, provider );
  } finally {
    lock.writeLock().unlock();
  }
}
 
Example #2
Source File: BasicOperationsTestCase.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * JUnit Fixture: Prepare a simple FSM.
 *
 * @throws FileSystemException for runtime problems
 */
@Before
public void setUp() throws FileSystemException {
    manager = new DefaultFileSystemManager();
    final FileProvider fp = new DefaultLocalFileProvider();
    manager.addProvider("file", fp);
    manager.init();
}
 
Example #3
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
@Override
public URLStreamHandler createURLStreamHandler(final String protocol) {
    final FileProvider provider = providers.get(protocol);
    if (provider != null) {
        return new DefaultURLStreamHandler(context);
    }

    // Route all other calls to the default URLStreamHandlerFactory
    return new URLStreamHandlerProxy();
}
 
Example #4
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
 * </p>
 *
 * @param fileSystem The FileSystem to close.
 */
public void _closeFileSystem(final FileSystem fileSystem) {
    final FileProvider provider = providers.get(fileSystem.getRootName().getScheme());
    if (provider != null) {
        ((AbstractFileProvider) provider).closeFileSystem(fileSystem);
    } else if (fileSystem instanceof VirtualFileSystem) {
        // vfsProvider does not implement AbstractFileProvider
        vfsProvider.closeFileSystem(fileSystem);
    }
}
 
Example #5
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Resolve the uri to a file name.
 *
 * @param uri The URI to resolve.
 * @return The FileName of the file.
 * @throws FileSystemException if an error occurs.
 */
@Override
public FileName resolveURI(final String uri) throws FileSystemException {
    UriParser.checkUriEncoding(uri);

    if (uri == null) {
        throw new IllegalArgumentException();
    }

    // Extract the scheme
    final String scheme = UriParser.extractScheme(getSchemes(), uri);
    if (scheme != null) {
        // An absolute URI - locate the provider
        final FileProvider provider = providers.get(scheme);
        if (provider != null) {
            return provider.parseUri(null, uri);
        }

        // Otherwise, assume a local file
    }

    // Handle absolute file names
    if (localFileProvider != null && localFileProvider.isAbsoluteLocalName(uri)) {
        return localFileProvider.parseUri(null, uri);
    }

    if (scheme != null) {
        // An unknown scheme - hand it to the default provider
        FileSystemException.requireNonNull(defaultProvider, "vfs.impl/unknown-scheme.error", scheme, uri);
        return defaultProvider.parseUri(null, uri);
    }

    // Assume a relative name - use the supplied base file
    FileSystemException.requireNonNull(baseFile, "vfs.impl/find-rel-file.error", uri);

    return resolveName(baseFile.getName(), uri, NameScope.FILE_SYSTEM);
}
 
Example #6
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Free all resources used by unused file systems created by this manager.
 */
public void freeUnusedResources() {
    if (!init) {
        return;
    }

    // Close the providers.
    for (final FileProvider fileProvider : providers.values()) {
        final AbstractFileProvider provider = (AbstractFileProvider) fileProvider;
        provider.freeUnusedResources();
    }
    // vfsProvider does not need to free resources
}
 
Example #7
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Resolves a URI, relative to a base file with specified FileSystem configuration.
 *
 * @param baseFile The base file.
 * @param uri The file name. May be a fully qualified or relative path or a url.
 * @param fileSystemOptions Options to pass to the file system.
 * @return A FileObject representing the target file.
 * @throws FileSystemException if an error occurs accessing the file.
 */
public FileObject resolveFile(final FileObject baseFile, final String uri,
        final FileSystemOptions fileSystemOptions) throws FileSystemException {
    final FileObject realBaseFile;
    if (baseFile != null && VFS.isUriStyle() && baseFile.getName().isFile()) {
        realBaseFile = baseFile.getParent();
    } else {
        realBaseFile = baseFile;
    }
    // TODO: use resolveName and use this name to resolve the fileObject

    UriParser.checkUriEncoding(uri);

    if (uri == null) {
        throw new IllegalArgumentException();
    }

    // Extract the scheme
    final String scheme = UriParser.extractScheme(getSchemes(), uri);
    if (scheme != null) {
        // An absolute URI - locate the provider
        final FileProvider provider = providers.get(scheme);
        if (provider != null) {
            return provider.findFile(realBaseFile, uri, fileSystemOptions);
        }
        // Otherwise, assume a local file
    }

    // Handle absolute file names
    if (localFileProvider != null && localFileProvider.isAbsoluteLocalName(uri)) {
        return localFileProvider.findLocalFile(uri);
    }

    if (scheme != null) {
        // An unknown scheme - hand it to the default provider
        FileSystemException.requireNonNull(defaultProvider, "vfs.impl/unknown-scheme.error", scheme, uri);
        return defaultProvider.findFile(realBaseFile, uri, fileSystemOptions);
    }

    // Assume a relative name - use the supplied base file
    FileSystemException.requireNonNull(realBaseFile, "vfs.impl/find-rel-file.error", uri);

    return realBaseFile.resolveFile(uri);
}
 
Example #8
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Resolves a name, relative to the root.
 *
 * @param base the base file name
 * @param name the name
 * @param scope the {@link NameScope}
 * @return The FileName of the file.
 * @throws FileSystemException if an error occurs.
 */
@Override
public FileName resolveName(final FileName base, final String name, final NameScope scope)
        throws FileSystemException {
    FileSystemException.requireNonNull(base, "Invalid base FileName.");
    FileSystemException.requireNonNull(name, "Invalid name FileName.");
    final FileName realBase;
    if (VFS.isUriStyle() && base.isFile()) {
        realBase = base.getParent();
    } else {
        realBase = base;
    }

    final StringBuilder buffer = new StringBuilder(name);

    // Adjust separators
    UriParser.fixSeparators(buffer);
    String scheme = UriParser.extractScheme(getSchemes(), buffer.toString());

    // Determine whether to prepend the base path
    if (name.length() == 0 || (scheme == null && buffer.charAt(0) != FileName.SEPARATOR_CHAR)) {
        // Supplied path is not absolute
        if (!VFS.isUriStyle()) {
            // when using URIs the parent already do have the trailing "/"
            buffer.insert(0, FileName.SEPARATOR_CHAR);
        }
        buffer.insert(0, realBase.getPath());
    }

    // Normalise the path
    final FileType fileType = UriParser.normalisePath(buffer);

    // Check the name is ok
    final String resolvedPath = buffer.toString();
    if (!AbstractFileName.checkName(realBase.getPath(), resolvedPath, scope)) {
        throw new FileSystemException("vfs.provider/invalid-descendent-name.error", name);
    }

    String fullPath;
    if (scheme != null) {
        fullPath = resolvedPath;
    } else {
        scheme = realBase.getScheme();
        fullPath = realBase.getRootURI() + resolvedPath;
    }
    final FileProvider provider = providers.get(scheme);
    if (provider != null) {
        // TODO: extend the file name parser to be able to parse
        // only a pathname and take the missing informations from
        // the base. Then we can get rid of the string operation.
        // // String fullPath = base.getRootURI() +
        // resolvedPath.substring(1);

        return provider.parseUri(realBase, fullPath);
    }

    // An unknown scheme - hand it to the default provider - if possible
    if (scheme != null && defaultProvider != null) {
        return defaultProvider.parseUri(realBase, fullPath);
    }

    // TODO: avoid fallback to this point
    // this happens if we have a virtual filesystem (no provider for scheme)
    return ((AbstractFileName) realBase).createName(resolvedPath, fileType);
}
 
Example #9
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Closes the manager.
 * <p>
 * This will close all providers (all files), it will also close all managed components including temporary files,
 * replicator, file cache and file operations.
 * <p>
 * The manager is in uninitialized state after this method.
 */
@Override
public void close() {
    if (!init) {
        return;
    }

    // make sure all discovered components in
    // org.apache.commons.vfs2.impl.StandardFileSystemManager.configure(Element)
    // are closed here

    // Close the file system providers.
    for (final FileProvider provider : providers.values()) {
        closeComponent(provider);
    }

    // Close the other components
    closeComponent(vfsProvider);
    closeComponent(fileReplicator);
    closeComponent(tempFileStore);
    closeComponent(filesCache);
    closeComponent(defaultProvider);


    // unregister all providers here, so if any components have local file references
    // they can still resolve against the supported schemes
    providers.clear();

    // FileOperations are components, too
    for (final List<FileOperationProvider> opproviders : operationProviders.values()) {
        for (final FileOperationProvider p : opproviders) {
            closeComponent(p);
        }
    }
    // unregister all
    operationProviders.clear();

    // collections with add()
    typeMap.clear();

    // should not happen, but make debugging easier:
    if (!components.isEmpty()) {
        log.warn("DefaultFilesystemManager.close: not all components are closed: " + components.toString());
    }
    components.clear();

    // managed components
    vfsProvider = null;

    // virtual schemas
    virtualFileSystemSchemes.clear();

    // setters and derived state
    defaultProvider = null;
    baseFile = null;
    fileObjectDecorator = null;
    fileObjectDecoratorConst = null;
    localFileProvider = null;
    fileReplicator = null;
    tempFileStore = null;
    // setters with init() defaults
    filesCache = null;
    fileCacheStrategy = null;
    fileContentInfoFactory = null;

    init = false;
}
 
Example #10
Source File: S3VfsPlugin.java    From hop with Apache License 2.0 4 votes vote down vote up
@Override public FileProvider getProvider() {
  return new S3FileProvider();
}
 
Example #11
Source File: GoogleDriveVfsPlugin.java    From hop with Apache License 2.0 4 votes vote down vote up
@Override public FileProvider getProvider() {
  return new GoogleDriveFileProvider();
}
 
Example #12
Source File: AzureVfsPlugin.java    From hop with Apache License 2.0 4 votes vote down vote up
@Override public FileProvider getProvider() {
  AzureFileProvider fileProvider = new AzureFileProvider();
  return fileProvider;
}
 
Example #13
Source File: GoogleCloudStorageVfsPlugin.java    From hop with Apache License 2.0 4 votes vote down vote up
@Override public FileProvider getProvider() {
  return new GoogleStorageFileProvider();
}
 
Example #14
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a layered file system.
 *
 * @param scheme The scheme to use.
 * @param file The FileObject.
 * @return The layered FileObject.
 * @throws FileSystemException if an error occurs.
 */
@Override
public FileObject createFileSystem(final String scheme, final FileObject file) throws FileSystemException {
    final FileProvider provider = providers.get(scheme);
    FileSystemException.requireNonNull(provider, "vfs.impl/unknown-provider.error", scheme, file);
    return provider.createFileSystem(scheme, file, file.getFileSystem().getFileSystemOptions());
}
 
Example #15
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 3 votes vote down vote up
/**
 * Get the capabilities for a given scheme.
 *
 * @param scheme The scheme to located.
 * @return A Collection of capabilities.
 * @throws FileSystemException if the given scheme is not konwn
 */
@Override
public Collection<Capability> getProviderCapabilities(final String scheme) throws FileSystemException {
    final FileProvider provider = providers.get(scheme);
    FileSystemException.requireNonNull(provider, "vfs.impl/unknown-scheme.error", scheme);
    return provider.getCapabilities();
}
 
Example #16
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 3 votes vote down vote up
/**
 * Get the configuration builder for the given scheme.
 *
 * @param scheme The scheme to locate.
 * @return The FileSystemConfigBuilder for the scheme.
 * @throws FileSystemException if the given scheme is not konwn
 */
@Override
public FileSystemConfigBuilder getFileSystemConfigBuilder(final String scheme) throws FileSystemException {
    final FileProvider provider = providers.get(scheme);
    FileSystemException.requireNonNull(provider, "vfs.impl/unknown-scheme.error", scheme);
    return provider.getConfigBuilder();
}
 
Example #17
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the default provider. This is the provider that will handle URI with unknown schemes. The manager takes care
 * of all lifecycle management.
 *
 * @param provider The FileProvider.
 * @throws FileSystemException if an error occurs setting the provider.
 */
public void setDefaultProvider(final FileProvider provider) throws FileSystemException {
    setupComponent(provider);
    defaultProvider = provider;
}
 
Example #18
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 2 votes vote down vote up
/**
 * Registers a file system provider.
 * <p>
 * The manager takes care of all lifecycle management. A provider may be registered multiple times. The first
 * {@link LocalFileProvider} added will be remembered for {@link #getLocalFileProvider()}.
 *
 * @param urlScheme The scheme the provider will handle.
 * @param provider The provider.
 * @throws FileSystemException if an error occurs adding the provider.
 */
public void addProvider(final String urlScheme, final FileProvider provider) throws FileSystemException {
    addProvider(new String[] { urlScheme }, provider);
}
 
Example #19
Source File: IVfs.java    From hop with Apache License 2.0 votes vote down vote up
FileProvider getProvider();