Search in sources :

Example 11 with DefaultFileSystemManager

use of org.apache.commons.vfs2.impl.DefaultFileSystemManager in project hop by apache.

the class HopVfs method createFileSystemManager.

/**
 * Make sure to close when done using!
 *
 * @return A new standard file system manager
 * @throws HopException
 */
private static DefaultFileSystemManager createFileSystemManager() throws HopException {
    try {
        DefaultFileSystemManager fsm = new DefaultFileSystemManager();
        fsm.addProvider("ram", new org.apache.commons.vfs2.provider.ram.RamFileProvider());
        fsm.addProvider("file", new org.apache.commons.vfs2.provider.local.DefaultLocalFileProvider());
        fsm.addProvider("res", new org.apache.commons.vfs2.provider.res.ResourceFileProvider());
        fsm.addProvider("zip", new org.apache.commons.vfs2.provider.zip.ZipFileProvider());
        fsm.addProvider("gz", new org.apache.commons.vfs2.provider.gzip.GzipFileProvider());
        fsm.addProvider("jar", new org.apache.commons.vfs2.provider.jar.JarFileProvider());
        fsm.addProvider("http", new org.apache.commons.vfs2.provider.http.HttpFileProvider());
        fsm.addProvider("https", new org.apache.commons.vfs2.provider.https.HttpsFileProvider());
        fsm.addProvider("ftp", new org.apache.commons.vfs2.provider.ftp.FtpFileProvider());
        fsm.addProvider("ftps", new org.apache.commons.vfs2.provider.ftps.FtpsFileProvider());
        fsm.addProvider("war", new org.apache.commons.vfs2.provider.jar.JarFileProvider());
        fsm.addProvider("par", new org.apache.commons.vfs2.provider.jar.JarFileProvider());
        fsm.addProvider("ear", new org.apache.commons.vfs2.provider.jar.JarFileProvider());
        fsm.addProvider("sar", new org.apache.commons.vfs2.provider.jar.JarFileProvider());
        fsm.addProvider("ejb3", new org.apache.commons.vfs2.provider.jar.JarFileProvider());
        fsm.addProvider("tmp", new org.apache.commons.vfs2.provider.temp.TemporaryFileProvider());
        fsm.addProvider("tar", new org.apache.commons.vfs2.provider.tar.TarFileProvider());
        fsm.addProvider("tbz2", new org.apache.commons.vfs2.provider.tar.TarFileProvider());
        fsm.addProvider("tgz", new org.apache.commons.vfs2.provider.tar.TarFileProvider());
        fsm.addProvider("bz2", new org.apache.commons.vfs2.provider.bzip2.Bzip2FileProvider());
        fsm.addProvider("files-cache", new org.apache.commons.vfs2.provider.temp.TemporaryFileProvider());
        fsm.addExtensionMap("jar", "jar");
        fsm.addExtensionMap("zip", "zip");
        fsm.addExtensionMap("gz", "gz");
        fsm.addExtensionMap("tar", "tar");
        fsm.addExtensionMap("tbz2", "tar");
        fsm.addExtensionMap("tgz", "tar");
        fsm.addExtensionMap("bz2", "bz2");
        fsm.addMimeTypeMap("application/x-tar", "tar");
        fsm.addMimeTypeMap("application/x-gzip", "gz");
        fsm.addMimeTypeMap("application/zip", "zip");
        fsm.setFileContentInfoFactory(new FileContentInfoFilenameFactory());
        fsm.setReplicator(new DefaultFileReplicator());
        fsm.setFilesCache(new SoftRefFilesCache());
        fsm.setCacheStrategy(CacheStrategy.ON_RESOLVE);
        // Here are extra VFS plugins to register
        // 
        PluginRegistry registry = PluginRegistry.getInstance();
        List<IPlugin> plugins = registry.getPlugins(VfsPluginType.class);
        for (IPlugin plugin : plugins) {
            IVfs iVfs = registry.loadClass(plugin, IVfs.class);
            try {
                fsm.addProvider(iVfs.getUrlSchemes(), iVfs.getProvider());
            } catch (Exception e) {
                throw new HopException("Error registering provider for VFS plugin " + plugin.getIds()[0] + " : " + plugin.getName() + " : ", e);
            }
        }
        fsm.init();
        return fsm;
    } catch (Exception e) {
        throw new HopException("Error creating file system manager", e);
    }
}
Also used : IPlugin(org.apache.hop.core.plugins.IPlugin) FileContentInfoFilenameFactory(org.apache.commons.vfs2.impl.FileContentInfoFilenameFactory) SoftRefFilesCache(org.apache.commons.vfs2.cache.SoftRefFilesCache) IVfs(org.apache.hop.core.vfs.plugin.IVfs) DefaultFileSystemManager(org.apache.commons.vfs2.impl.DefaultFileSystemManager) HopException(org.apache.hop.core.exception.HopException) HopException(org.apache.hop.core.exception.HopException) HopFileException(org.apache.hop.core.exception.HopFileException) PluginRegistry(org.apache.hop.core.plugins.PluginRegistry) org.apache.commons.vfs2(org.apache.commons.vfs2) DefaultFileReplicator(org.apache.commons.vfs2.impl.DefaultFileReplicator)

Example 12 with DefaultFileSystemManager

use of org.apache.commons.vfs2.impl.DefaultFileSystemManager in project hop by apache.

the class HopVfs method startsWithScheme.

/**
 * Check if filename starts with one of the known protocols like file: zip: ram: smb: jar: etc. If
 * yes, return true otherwise return false
 *
 * @param vfsFileName
 * @return boolean
 */
public static boolean startsWithScheme(String vfsFileName) {
    lock.readLock().lock();
    try {
        DefaultFileSystemManager fsManager = getFileSystemManager();
        boolean found = false;
        String[] schemes = fsManager.getSchemes();
        for (int i = 0; i < schemes.length; i++) {
            if (vfsFileName.startsWith(schemes[i] + ":")) {
                found = true;
                break;
            }
        }
        return found;
    } finally {
        lock.readLock().unlock();
    }
}
Also used : DefaultFileSystemManager(org.apache.commons.vfs2.impl.DefaultFileSystemManager)

Example 13 with DefaultFileSystemManager

use of org.apache.commons.vfs2.impl.DefaultFileSystemManager in project hop by apache.

the class S3FileObjectTest method setUp.

@Before
public void setUp() throws Exception {
    s3ServiceMock = mock(AmazonS3.class);
    S3Object s3Object = new S3Object();
    s3Object.setKey(OBJECT_NAME);
    s3Object.setBucketName(BUCKET_NAME);
    filename = new S3FileName(SCHEME, BUCKET_NAME, BUCKET_NAME, FileType.FOLDER);
    S3FileName rootFileName = new S3FileName(SCHEME, "", "", FileType.FOLDER);
    S3HopProperty s3HopProperty = mock(S3HopProperty.class);
    when(s3HopProperty.getPartSize()).thenReturn("5MB");
    S3FileSystem fileSystem = new S3FileSystem(rootFileName, new FileSystemOptions(), new StorageUnitConverter(), s3HopProperty);
    fileSystemSpy = spy(fileSystem);
    VfsComponentContext context = mock(VfsComponentContext.class);
    final DefaultFileSystemManager fsm = new DefaultFileSystemManager();
    FilesCache cache = mock(FilesCache.class);
    fsm.setFilesCache(cache);
    fsm.setCacheStrategy(CacheStrategy.ON_RESOLVE);
    when(context.getFileSystemManager()).thenReturn(fsm);
    fileSystemSpy.setContext(context);
    S3FileObject s3FileObject = new S3FileObject(filename, fileSystemSpy);
    s3FileObjectBucketSpy = spy(s3FileObject);
    s3FileObjectFileSpy = spy(new S3FileObject(new S3FileName(SCHEME, BUCKET_NAME, BUCKET_NAME + "/" + origKey, FileType.IMAGINARY), fileSystemSpy));
    S3FileObject s3FileObjectRoot = new S3FileObject(rootFileName, fileSystemSpy);
    s3FileObjectSpyRoot = spy(s3FileObjectRoot);
    // specify the behaviour of S3 Service
    // when( s3ServiceMock.getBucket( BUCKET_NAME ) ).thenReturn( testBucket );
    when(s3ServiceMock.getObject(BUCKET_NAME, OBJECT_NAME)).thenReturn(s3Object);
    when(s3ServiceMock.getObject(BUCKET_NAME, OBJECT_NAME)).thenReturn(s3Object);
    when(s3ServiceMock.listBuckets()).thenReturn(createBuckets());
    when(s3ServiceMock.doesBucketExistV2(BUCKET_NAME)).thenReturn(true);
    childObjectListing = mock(ObjectListing.class);
    when(childObjectListing.getObjectSummaries()).thenReturn(createObjectSummaries(0)).thenReturn(new ArrayList<>());
    when(childObjectListing.getCommonPrefixes()).thenReturn(new ArrayList<>()).thenReturn(createCommonPrefixes(3));
    when(childObjectListing.isTruncated()).thenReturn(true).thenReturn(false);
    when(s3ServiceMock.listObjects(any(ListObjectsRequest.class))).thenReturn(childObjectListing);
    when(s3ServiceMock.listObjects(anyString(), anyString())).thenReturn(childObjectListing);
    when(s3ServiceMock.listNextBatchOfObjects(any(ObjectListing.class))).thenReturn(childObjectListing);
    s3ObjectMock = mock(S3Object.class);
    s3ObjectInputStream = mock(S3ObjectInputStream.class);
    s3ObjectMetadata = mock(ObjectMetadata.class);
    when(s3ObjectMock.getObjectContent()).thenReturn(s3ObjectInputStream);
    when(s3ServiceMock.getObjectMetadata(anyString(), anyString())).thenReturn(s3ObjectMetadata);
    when(s3ObjectMetadata.getContentLength()).thenReturn(contentLength);
    when(s3ObjectMetadata.getLastModified()).thenReturn(testDate);
    when(s3ServiceMock.getObject(anyString(), anyString())).thenReturn(s3ObjectMock);
    when(fileSystemSpy.getS3Client()).thenReturn(s3ServiceMock);
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) StorageUnitConverter(org.apache.hop.core.util.StorageUnitConverter) ArrayList(java.util.ArrayList) S3FileSystem(org.apache.hop.vfs.s3.s3.vfs.S3FileSystem) S3FileObject(org.apache.hop.vfs.s3.s3.vfs.S3FileObject) S3FileName(org.apache.hop.vfs.s3.s3.vfs.S3FileName) VfsComponentContext(org.apache.commons.vfs2.provider.VfsComponentContext) DefaultFileSystemManager(org.apache.commons.vfs2.impl.DefaultFileSystemManager) S3HopProperty(org.apache.hop.vfs.s3.s3common.S3HopProperty) Before(org.junit.Before)

Example 14 with DefaultFileSystemManager

use of org.apache.commons.vfs2.impl.DefaultFileSystemManager in project agileway by fangjinuo.

the class AgilewayVFSManagerBootstrap method startup.

public static void startup() throws Throwable {
    FileSystemManager defaultFSManager = VFS.getManager();
    VFSUtils.addFileProvider((DefaultFileSystemManager) defaultFSManager, new SftpFileProvider(), "sftp");
}
Also used : FileSystemManager(org.apache.commons.vfs2.FileSystemManager) DefaultFileSystemManager(org.apache.commons.vfs2.impl.DefaultFileSystemManager) SftpFileProvider(com.jn.agileway.vfs.provider.sftp.SftpFileProvider)

Example 15 with DefaultFileSystemManager

use of org.apache.commons.vfs2.impl.DefaultFileSystemManager in project agileway by fangjinuo.

the class VFSUtils method removeFileProvider.

public static void removeFileProvider(final DefaultFileSystemManager fileSystemManager, String uriSchema) {
    if (fileSystemManager.hasProvider(uriSchema)) {
        FileProvider provider = getAllProviders(fileSystemManager).remove(uriSchema);
        if (provider != null) {
            FileProvider defaultProvider = Reflects.getFieldValue(defaultFileSystemManager_defaultProvider, fileSystemManager, true, false);
            if (defaultProvider == provider) {
                Reflects.setFieldValue(defaultFileSystemManager_defaultProvider, fileSystemManager, null, true, false);
            }
            List<Object> components = Reflects.getFieldValue(defaultFileSystemManager_components, fileSystemManager, true, false);
            components.remove(provider);
        }
    }
}
Also used : FileProvider(org.apache.commons.vfs2.provider.FileProvider)

Aggregations

DefaultFileSystemManager (org.apache.commons.vfs2.impl.DefaultFileSystemManager)46 File (java.io.File)13 FileSystemException (org.apache.commons.vfs2.FileSystemException)13 Test (org.junit.Test)13 FileObject (org.apache.commons.vfs2.FileObject)11 FileSystemManager (org.apache.commons.vfs2.FileSystemManager)11 IOException (java.io.IOException)8 DefaultLocalFileProvider (org.apache.commons.vfs2.provider.local.DefaultLocalFileProvider)5 Before (org.junit.Before)5 AmazonS3 (com.amazonaws.services.s3.AmazonS3)4 ArrayList (java.util.ArrayList)4 FileName (org.apache.commons.vfs2.FileName)4 FileSystemOptions (org.apache.commons.vfs2.FileSystemOptions)4 FilesCache (org.apache.commons.vfs2.FilesCache)4 SoftRefFilesCache (org.apache.commons.vfs2.cache.SoftRefFilesCache)4 StandardFileSystemManager (org.apache.commons.vfs2.impl.StandardFileSystemManager)4 VfsComponentContext (org.apache.commons.vfs2.provider.VfsComponentContext)4 RepositoryException (javax.jcr.RepositoryException)3 DefaultFileReplicator (org.apache.commons.vfs2.impl.DefaultFileReplicator)3 FileContentInfoFilenameFactory (org.apache.commons.vfs2.impl.FileContentInfoFilenameFactory)3