org.apache.commons.vfs2.CacheStrategy Java Examples

The following examples show how to use org.apache.commons.vfs2.CacheStrategy. 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: ProviderCacheStrategyTests.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Test the on_call strategy
 */
public void testOnCallCache() throws Exception {
    final FileObject scratchFolder = getWriteFolder();
    if (FileObjectUtils.isInstanceOf(getBaseFolder(), RamFileObject.class)
            || scratchFolder.getFileSystem() instanceof VirtualFileSystem) {
        // cant check ram filesystem as every manager holds its own ram filesystem data
        return;
    }

    scratchFolder.delete(Selectors.EXCLUDE_SELF);

    final DefaultFileSystemManager fs = createManager();
    fs.setCacheStrategy(CacheStrategy.ON_CALL);
    fs.init();
    final FileObject foBase2 = getBaseTestFolder(fs);

    final FileObject cachedFolder = foBase2.resolveFile(scratchFolder.getName().getPath());

    FileObject[] fos = cachedFolder.getChildren();
    assertContainsNot(fos, "file1.txt");

    scratchFolder.resolveFile("file1.txt").createFile();

    fos = cachedFolder.getChildren();
    assertContains(fos, "file1.txt");
}
 
Example #2
Source File: DefaultFileSystemManagerProvider.java    From spoofax with Apache License 2.0 6 votes vote down vote up
@Override public FileSystemManager get() {
    try {
        final DefaultFileSystemManager manager = new DefaultFileSystemManager();

        manager.setFilesCache(new DefaultFilesCache());
        manager.setCacheStrategy(CacheStrategy.ON_RESOLVE);

        final String baseTmpDir = System.getProperty("java.io.tmpdir");
        final File tempDir = new File(baseTmpDir, "vfs_cache" + new Random().nextLong()).getAbsoluteFile();
        final DefaultFileReplicator replicator = new DefaultFileReplicator(tempDir);
        manager.setTemporaryFileStore(replicator);
        manager.setReplicator(replicator);

        addDefaultProvider(manager);
        addProviders(manager);
        setBaseFile(manager);

        manager.init();

        return manager;
    } catch(FileSystemException e) {
        throw new RuntimeException("Cannot initialize resource service: " + e.getMessage(), e);
    }
}
 
Example #3
Source File: GoogleDriveFileSystem.java    From hop with Apache License 2.0 5 votes vote down vote up
private synchronized FileObject processFile( FileName name, boolean useCache ) throws FileSystemException {
  if ( !super.getRootName().getRootURI().equals( name.getRootURI() ) ) {
    throw new FileSystemException( "vfs.provider/mismatched-fs-for-name.error",
        new Object[] { name, super.getRootName(), name.getRootURI() } );
  } else {
    FileObject file;
    if ( useCache ) {
      file = super.getFileFromCache( name );
    } else {
      file = null;
    }

    if ( file == null ) {
      try {
        file = this.createFile( (AbstractFileName) name );
      } catch ( Exception e ) {
        throw new FileSystemException( "Unable to get Google Drive file object for '"+name+"'", e );
      }

      file = super.decorateFileObject( file );
      if ( useCache ) {
        super.putFileToCache( file );
      }
    }

    if ( super.getFileSystemManager().getCacheStrategy().equals( CacheStrategy.ON_RESOLVE ) ) {
      file.refresh();
    }

    return file;
  }
}
 
Example #4
Source File: StorageObject.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
private FileSystemManager getFileSystemManager() throws Exception {
	if (FILESYSTEMANAGERINSTANCE == null) {
		synchronized (StorageObject.class) {
			if (FILESYSTEMANAGERINSTANCE == null) {
				StandardFileSystemManager fs = new StandardFileSystemManager();
				fs.setFilesCache(new NullFilesCache());
				fs.setCacheStrategy(CacheStrategy.ON_RESOLVE);
				fs.init();
				FILESYSTEMANAGERINSTANCE = fs;
			}
		}
	}
	return FILESYSTEMANAGERINSTANCE;
}
 
Example #5
Source File: AbstractFileSystem.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
private synchronized FileObject resolveFile(final FileName name, final boolean useCache)
        throws FileSystemException {
    if (!rootName.getRootURI().equals(name.getRootURI())) {
        throw new FileSystemException("vfs.provider/mismatched-fs-for-name.error", name, rootName,
                name.getRootURI());
    }

    // [email protected] ==> use getFileFromCache
    FileObject file;
    if (useCache) {
        file = getFileFromCache(name);
    } else {
        file = null;
    }

    if (file == null) {
        try {
            file = createFile((AbstractFileName) name);
        } catch (final Exception e) {
            throw new FileSystemException("vfs.provider/resolve-file.error", name, e);
        }

        file = decorateFileObject(file);

        // [email protected] ==> use putFileToCache
        if (useCache) {
            putFileToCache(file);
        }
    }

    /**
     * resync the file information if requested
     */
    if (getFileSystemManager().getCacheStrategy().equals(CacheStrategy.ON_RESOLVE)) {
        file.refresh();
    }
    return file;
}
 
Example #6
Source File: LargeTarTestCase.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    manager = new DefaultFileSystemManager();

    manager.setFilesCache(new SoftRefFilesCache());
    manager.setCacheStrategy(CacheStrategy.ON_RESOLVE);

    manager.addProvider("file", new DefaultLocalFileProvider());
    manager.addProvider("tgz", new TarFileProvider());
    manager.addProvider("tar", new TarFileProvider());

    new File(baseDir).mkdir(); // if test is run standalone
    createLargeFile(largeFilePath, largeFileName);
}
 
Example #7
Source File: ProviderCacheStrategyTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Test the manual cache strategy
 */
public void testManualCache() throws Exception {
    final FileObject scratchFolder = getWriteFolder();
    if (FileObjectUtils.isInstanceOf(getBaseFolder(), RamFileObject.class)
            || scratchFolder.getFileSystem() instanceof VirtualFileSystem) {
        // cant check ram filesystem as every manager holds its own ram filesystem data
        return;
    }

    scratchFolder.delete(Selectors.EXCLUDE_SELF);

    final DefaultFileSystemManager fs = createManager();
    fs.setCacheStrategy(CacheStrategy.MANUAL);
    fs.init();
    final FileObject foBase2 = getBaseTestFolder(fs);

    final FileObject cachedFolder = foBase2.resolveFile(scratchFolder.getName().getPath());

    FileObject[] fos = cachedFolder.getChildren();
    assertContainsNot(fos, "file1.txt");

    scratchFolder.resolveFile("file1.txt").createFile();

    fos = cachedFolder.getChildren();
    assertContainsNot(fos, "file1.txt");

    cachedFolder.refresh();
    fos = cachedFolder.getChildren();
    assertContains(fos, "file1.txt");
}
 
Example #8
Source File: ProviderCacheStrategyTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Test the on_resolve strategy
 */
public void testOnResolveCache() throws Exception {
    final FileObject scratchFolder = getWriteFolder();
    if (FileObjectUtils.isInstanceOf(getBaseFolder(), RamFileObject.class)
            || scratchFolder.getFileSystem() instanceof VirtualFileSystem) {
        // cant check ram filesystem as every manager holds its own ram filesystem data
        return;
    }

    scratchFolder.delete(Selectors.EXCLUDE_SELF);

    final DefaultFileSystemManager fs = createManager();
    fs.setCacheStrategy(CacheStrategy.ON_RESOLVE);
    fs.init();
    final FileObject foBase2 = getBaseTestFolder(fs);

    FileObject cachedFolder = foBase2.resolveFile(scratchFolder.getName().getPath());

    FileObject[] fos = cachedFolder.getChildren();
    assertContainsNot(fos, "file1.txt");

    scratchFolder.resolveFile("file1.txt").createFile();

    fos = cachedFolder.getChildren();
    assertContainsNot(fos, "file1.txt");

    cachedFolder = foBase2.resolveFile(scratchFolder.getName().getPath());
    fos = cachedFolder.getChildren();
    assertContains(fos, "file1.txt");
}
 
Example #9
Source File: GoogleDriveFileSystem.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private synchronized FileObject processFile( FileName name, boolean useCache ) throws FileSystemException {
  if ( !super.getRootName().getRootURI().equals( name.getRootURI() ) ) {
    throw new FileSystemException( "vfs.provider/mismatched-fs-for-name.error",
        new Object[] { name, super.getRootName(), name.getRootURI() } );
  } else {
    FileObject file;
    if ( useCache ) {
      file = super.getFileFromCache( name );
    } else {
      file = null;
    }

    if ( file == null ) {
      try {
        file = this.createFile( (AbstractFileName) name );
      } catch ( Exception var5 ) {
        return null;
      }

      file = super.decorateFileObject( file );
      if ( useCache ) {
        super.putFileToCache( file );
      }
    }

    if ( super.getFileSystemManager().getCacheStrategy().equals( CacheStrategy.ON_RESOLVE ) ) {
      file.refresh();
    }

    return file;
  }
}
 
Example #10
Source File: S3FileObjectTest.java    From hop with Apache License 2.0 4 votes vote down vote up
@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 );
}
 
Example #11
Source File: S3NFileObjectTest.java    From hop with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {

  s3ServiceMock = mock( AmazonS3.class );
  S3Object s3Object = new S3Object();
  s3Object.setKey( OBJECT_NAME );
  s3Object.setBucketName( BUCKET_NAME );

  filename = new S3NFileName( SCHEME, BUCKET_NAME, "/" + BUCKET_NAME, FileType.FOLDER );
  S3NFileName rootFileName = new S3NFileName( SCHEME, BUCKET_NAME, "", FileType.FOLDER );
  S3NFileSystem fileSystem = new S3NFileSystem( rootFileName, new FileSystemOptions() );
  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 );

  S3NFileObject s3FileObject = new S3NFileObject( filename, fileSystemSpy );
  s3FileObjectBucketSpy = spy( s3FileObject );

  s3FileObjectFileSpy = spy( new S3NFileObject(
    new S3NFileName( SCHEME, BUCKET_NAME, "/" + BUCKET_NAME + "/" + origKey, FileType.IMAGINARY ), fileSystemSpy ) );

  S3NFileObject s3FileObjectRoot = new S3NFileObject( 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 );
}
 
Example #12
Source File: VFSClassloaderUtil.java    From metron with Apache License 2.0 4 votes vote down vote up
/**
 * Create a FileSystem manager suitable for our purposes.
 * This manager supports files of the following types:
 * * res - resource files
 * * jar
 * * tar
 * * bz2
 * * tgz
 * * zip
 * * HDFS
 * * FTP
 * * HTTP/S
 * * file
 * @return vfs
 * @throws FileSystemException
 */
public static FileSystemManager generateVfs() throws FileSystemException {
  DefaultFileSystemManager vfs = new DefaultFileSystemManager();
  vfs.addProvider("res", new org.apache.commons.vfs2.provider.res.ResourceFileProvider());
  vfs.addProvider("zip", new org.apache.commons.vfs2.provider.zip.ZipFileProvider());
  vfs.addProvider("gz", new org.apache.commons.vfs2.provider.gzip.GzipFileProvider());
  vfs.addProvider("ram", new org.apache.commons.vfs2.provider.ram.RamFileProvider());
  vfs.addProvider("file", new org.apache.commons.vfs2.provider.local.DefaultLocalFileProvider());
  vfs.addProvider("jar", new org.apache.commons.vfs2.provider.jar.JarFileProvider());
  vfs.addProvider("http", new org.apache.commons.vfs2.provider.http.HttpFileProvider());
  vfs.addProvider("https", new org.apache.commons.vfs2.provider.https.HttpsFileProvider());
  vfs.addProvider("ftp", new org.apache.commons.vfs2.provider.ftp.FtpFileProvider());
  vfs.addProvider("ftps", new org.apache.commons.vfs2.provider.ftps.FtpsFileProvider());
  vfs.addProvider("war", new org.apache.commons.vfs2.provider.jar.JarFileProvider());
  vfs.addProvider("par", new org.apache.commons.vfs2.provider.jar.JarFileProvider());
  vfs.addProvider("ear", new org.apache.commons.vfs2.provider.jar.JarFileProvider());
  vfs.addProvider("sar", new org.apache.commons.vfs2.provider.jar.JarFileProvider());
  vfs.addProvider("ejb3", new org.apache.commons.vfs2.provider.jar.JarFileProvider());
  vfs.addProvider("tmp", new org.apache.commons.vfs2.provider.temp.TemporaryFileProvider());
  vfs.addProvider("tar", new org.apache.commons.vfs2.provider.tar.TarFileProvider());
  vfs.addProvider("tbz2", new org.apache.commons.vfs2.provider.tar.TarFileProvider());
  vfs.addProvider("tgz", new org.apache.commons.vfs2.provider.tar.TarFileProvider());
  vfs.addProvider("bz2", new org.apache.commons.vfs2.provider.bzip2.Bzip2FileProvider());
  vfs.addProvider("hdfs", new HdfsFileProvider());
  vfs.addExtensionMap("jar", "jar");
  vfs.addExtensionMap("zip", "zip");
  vfs.addExtensionMap("gz", "gz");
  vfs.addExtensionMap("tar", "tar");
  vfs.addExtensionMap("tbz2", "tar");
  vfs.addExtensionMap("tgz", "tar");
  vfs.addExtensionMap("bz2", "bz2");
  vfs.addMimeTypeMap("application/x-tar", "tar");
  vfs.addMimeTypeMap("application/x-gzip", "gz");
  vfs.addMimeTypeMap("application/zip", "zip");
  vfs.setFileContentInfoFactory(new FileContentInfoFilenameFactory());
  vfs.setFilesCache(new SoftRefFilesCache());
  vfs.setReplicator(new UniqueFileReplicator(new File(System.getProperty("java.io.tmpdir"))));
  vfs.setCacheStrategy(CacheStrategy.ON_RESOLVE);
  vfs.init();
  return vfs;
}
 
Example #13
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 3 votes vote down vote up
/**
 * Set the cache strategy to use when dealing with file object data.
 * <p>
 * Can only be set before the FileSystemManager is initialized.
 * <p>
 * The default is {@link CacheStrategy#ON_RESOLVE}
 *
 * @param fileCacheStrategy The CacheStrategy to use.
 * @throws FileSystemException if this is not possible. e.g. it is already set.
 */
public void setCacheStrategy(final CacheStrategy fileCacheStrategy) throws FileSystemException {
    if (init) {
        throw new FileSystemException("vfs.impl/already-inited.error");
    }

    this.fileCacheStrategy = fileCacheStrategy;
}
 
Example #14
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 2 votes vote down vote up
/**
 * Get the cache strategy used.
 *
 * @return The CacheStrategy.
 */
@Override
public CacheStrategy getCacheStrategy() {
    return fileCacheStrategy;
}