Java Code Examples for org.apache.commons.vfs2.impl.DefaultFileSystemManager

The following examples show how to use org.apache.commons.vfs2.impl.DefaultFileSystemManager. These examples are extracted from open source projects. 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 Project: hop   Source File: HopVfs.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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();
  }
}
 
Example 2
Source Project: commons-vfs   Source File: NullFilesCacheTests.java    License: 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 Project: commons-vfs   Source File: WindowsFileNameTests.java    License: Apache License 2.0 6 votes vote down vote up
public void testWindowsRoots() throws Exception {
    // valid URI forms of the filesystem root
    final String[] tests = new String[] { "file:///C:/", "file://C:/", "file:/C:/", "file:C:/" };

    for (final String name : tests) {
        final DefaultFileSystemManager manager = getManager();
        Assert.assertNotNull("Unexpected null manager for test " + this, manager);
        final FileName fn = manager.resolveFile(name).getName();

        // the following tests work for Windows file names only
        assertSame(WindowsFileName.class, fn.getClass());

        // all should result in the same FileName
        assertEquals("file:///C:/", fn.toString());
        assertEquals("/", fn.getPath());
        assertEquals("/", fn.getPathDecoded());
        assertEquals("file:///C:/", fn.getRootURI());
        assertEquals("file:///C:/", fn.getFriendlyURI());

        assertEquals("file:///C:/", fn.getRoot().toString());

        assertEquals("", fn.getExtension());
        assertEquals("", fn.getBaseName());
    }
}
 
Example 4
Source Project: commons-vfs   Source File: ProviderCacheStrategyTests.java    License: 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 5
Source Project: pentaho-kettle   Source File: ConnectionFileProviderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws Exception {
  connectionManager = ConnectionManager.getInstance();
  connectionManager.setMetastoreSupplier( () -> memoryMetaStore );
  addOne();

  DefaultFileSystemManager fsm = (DefaultFileSystemManager) KettleVFS.getInstance().getFileSystemManager();
  if ( !fsm.hasProvider( ConnectionFileProvider.SCHEME ) ) {
    fsm.addProvider( ConnectionFileProvider.SCHEME, new ConnectionFileProvider() );
  }
  if ( !fsm.hasProvider( TestFileProvider.SCHEME ) ) {
    fsm.addProvider( TestFileProvider.SCHEME, new TestFileProvider() );
  }
  if ( !fsm.hasProvider( TestFileWithDomainProvider.SCHEME ) ) {
    fsm.addProvider( TestFileWithDomainProvider.SCHEME, new TestFileWithDomainProvider() );
  }
}
 
Example 6
Source Project: spoofax   Source File: DefaultFileSystemManagerProvider.java    License: 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 7
Source Project: spoofax   Source File: DefaultFileSystemManagerProvider.java    License: Apache License 2.0 6 votes vote down vote up
protected void addProviders(DefaultFileSystemManager manager) throws FileSystemException {
    manager.addProvider("tmp", new TemporaryFileProvider());
    manager.addProvider("res", new ResourceFileProvider());
    manager.addProvider("ram", new RamFileProvider());
    manager.addProvider("zip", new ZipFileProvider());
    manager.addProvider("jar", new JarFileProvider());
    manager.addProvider("tar", new TarFileProvider());
    manager.addProvider("tgz", new TgzFileProvider());
    manager.addProvider("tbz2", new Tbz2FileProvider());
    manager.addProvider("gz", new GzipFileProvider());
    manager.addProvider("bz2", new Bzip2FileProvider());
    manager.addProvider("http", new HttpFileProvider());
    manager.addProvider("https", new HttpsFileProvider());
    manager.addProvider("ftp", new FtpFileProvider());
    manager.addProvider("ftps", new FtpsFileProvider());
}
 
Example 8
Source Project: hop   Source File: HopVfs.java    License: Apache License 2.0 5 votes vote down vote up
public static DefaultFileSystemManager getFileSystemManager() {
  lock.readLock().lock();
  try {
    if ( fsm == null ) {
      try {
        fsm = createFileSystemManager();
      } catch ( Exception e ) {
        throw new RuntimeException( "Error initializing file system manager : ", e );
      }
    }
    return fsm;
  } finally {
    lock.readLock().unlock();
  }
}
 
Example 9
@Activate
void activate(Map<String, Object> configuration) throws VirtualFilesystemException {
    SimpleVirtualFilesystemConfig conf = Configurable.createConfigurable(SimpleVirtualFilesystemConfig.class,
            configuration);
    try {
        this.fsManager = VFS.getManager();
        File rootDirectory = new File(conf.defaultRootDirectory());
        if (!rootDirectory.exists()) {
            rootDirectory.mkdirs();
        }
        ((DefaultFileSystemManager) this.fsManager).setBaseFile(rootDirectory);
    } catch (FileSystemException e) {
        throw new VirtualFilesystemException("Issue initializing virtual file system.", e);
    }
    // Set of queues.
    tempFiles = new ArrayBlockingQueue<>(conf.maxNumberOfTempFiles());

    // Schedule our temp file cleanup service.
    this.scheduledExecutorService = Executors.newScheduledThreadPool(1);
    this.scheduledExecutorService.scheduleAtFixedRate(new CleanTempFilesRunnable(LOGGER, tempFiles),
            conf.secondsBetweenTempCleanup(), conf.secondsBetweenTempCleanup(), TimeUnit.SECONDS);
    LOGGER.debug("Configured scheduled cleanup of temp files to run every {} seconds",
            conf.secondsBetweenTempCleanup());

    // Set default temp url template
    this.baseTempUrlTemplate = conf.defaultTemporaryDirectory() != null ? conf.defaultTemporaryDirectory() :
            ("file://" + System.getProperty("java.io.tmpdir"));
    LOGGER.debug("Going to use {} for our base temp directory template", this.baseTempUrlTemplate);

    // Initialize HashFactory
    this.hashFactory = XXHashFactory.fastestInstance();
}
 
Example 10
Source Project: commons-vfs   Source File: BasicOperationsTestCase.java    License: 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 11
Source Project: commons-vfs   Source File: LRUFilesCacheTests.java    License: Apache License 2.0 5 votes vote down vote up
public void testClass() {
    @SuppressWarnings("resource")
    final DefaultFileSystemManager manager = getManager();
    Assert.assertNotNull("manager", manager);
    final FilesCache filesCache = manager.getFilesCache();
    assertTrue(Objects.toString(filesCache), filesCache instanceof LRUFilesCache);
}
 
Example 12
/**
 * Prepares the file system manager.
 */
@Override
public void prepare(final DefaultFileSystemManager manager) throws Exception {
    manager.addProvider("zip", new ZipFileProvider());
    manager.addExtensionMap("zip", "zip");
    manager.addMimeTypeMap("application/zip", "zip");
}
 
Example 13
/**
 * Prepares the file system manager.
 */
@Override
public void prepare(final DefaultFileSystemManager manager) throws Exception {
    manager.addProvider("zip", new ZipFileProvider());
    manager.addExtensionMap("zip", "zip");
    manager.addMimeTypeMap("application/zip", "zip");
}
 
Example 14
Source Project: commons-vfs   Source File: ZipProviderTestCase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Prepares the file system manager.
 */
@Override
public void prepare(final DefaultFileSystemManager manager) throws Exception {
    manager.addProvider("zip", new ZipFileProvider());
    manager.addExtensionMap("zip", "zip");
    manager.addMimeTypeMap("application/zip", "zip");
}
 
Example 15
Source Project: commons-vfs   Source File: NestedZipTestCase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Prepares the file system manager.
 */
@Override
public void prepare(final DefaultFileSystemManager manager) throws Exception {
    manager.addProvider("zip", new ZipFileProvider());
    manager.addExtensionMap("zip", "zip");
    manager.addMimeTypeMap("application/zip", "zip");
}
 
Example 16
Source Project: commons-vfs   Source File: FileNameTests.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests resolution of an absolute file name.
 */
public void testAbsoluteFileName() throws Exception {
    // Locate file by absolute file name
    final String fileName = new File("testdir").getAbsolutePath();
    final DefaultFileSystemManager manager = getManager();
    Assert.assertNotNull("Unexpected null manager for test " + this, manager);
    try (final FileObject absFile = manager.resolveFile(fileName)) {

        // Locate file by URI
        final String uri = "file://" + fileName.replace(File.separatorChar, '/');
        final FileObject uriFile = manager.resolveFile(uri);

        assertSame("file object", absFile, uriFile);
    }
}
 
Example 17
Source Project: commons-vfs   Source File: WindowsFileNameTests.java    License: Apache License 2.0 5 votes vote down vote up
public void testWindowsWrongRoots() throws Exception {
    final String[] tests = new String[] { "file:///C:", "file://C:", "file:/C:", "file:C:" };

    for (final String name : tests) {
        try {
            final DefaultFileSystemManager manager = getManager();
            Assert.assertNotNull("Unexpected null manager for test " + this, manager);
            final FileName fn = manager.resolveFile(name).getName();
            fail("should not accept root " + name);
        } catch (final FileSystemException ex) {
            assertEquals("vfs.provider/invalid-absolute-uri.error", ex.getCode());
            assertTrue(ex.toString().indexOf(name) >= 0);
        }
    }
}
 
Example 18
Source Project: commons-vfs   Source File: CustomRamProviderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    manager = new DefaultFileSystemManager();
    manager.addProvider("ram", new RamFileProvider());
    manager.init();

    // File Systems Options
    RamFileSystemConfigBuilder.getInstance().setMaxSize(zeroSizedFso, 0L);
    RamFileSystemConfigBuilder.getInstance().setMaxSize(smallSizedFso, 10L);
}
 
Example 19
Source Project: commons-vfs   Source File: RamProviderTestCase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Prepares the file system manager.
 *
 * Imports test data from the disk.
 *
 * @throws Exception
 */
@Override
public void prepare(final DefaultFileSystemManager manager) throws Exception {
    try {
        manager.addProvider("ram", new RamFileProvider());
        manager.addProvider("file", new DefaultLocalFileProvider());
    } catch (final Exception e) {
        log.error(e);
        throw e;
    }
}
 
Example 20
Source Project: commons-vfs   Source File: Http5ProviderTestCase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Prepares the file system manager.
 */
@Override
public void prepare(final DefaultFileSystemManager manager) throws Exception {
    if (!manager.hasProvider("http5")) {
        manager.addProvider("http5", new Http5FileProvider());
    }
}
 
Example 21
Source Project: commons-vfs   Source File: NestedTgzTestCase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Prepares the file system manager.
 */
@Override
public void prepare(final DefaultFileSystemManager manager) throws Exception {
    manager.addProvider("tgz", new TarFileProvider());
    manager.addExtensionMap("tgz", "tgz");
    manager.addProvider("tar", new TarFileProvider());
}
 
Example 22
Source Project: commons-vfs   Source File: TgzProviderTestCase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Prepares the file system manager.
 */
@Override
public void prepare(final DefaultFileSystemManager manager) throws Exception {
    // manager.addProvider("tgz", new TgzFileProvider());
    manager.addProvider("tgz", new TarFileProvider());
    manager.addProvider("tar", new TarFileProvider());
}
 
Example 23
Source Project: commons-vfs   Source File: NestedTarTestCase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Prepares the file system manager.
 */
@Override
public void prepare(final DefaultFileSystemManager manager) throws Exception {
    manager.addProvider("tar", new TarFileProvider());
    manager.addExtensionMap("tar", "tar");
    manager.addMimeTypeMap("application/x-tar", "tar");
}
 
Example 24
Source Project: commons-vfs   Source File: LargeTarTestCase.java    License: 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 25
Source Project: commons-vfs   Source File: NestedTbz2TestCase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Prepares the file system manager.
 */
@Override
public void prepare(final DefaultFileSystemManager manager) throws Exception {
    manager.addProvider("tbz2", new TarFileProvider());
    manager.addExtensionMap("tbz2", "tbz2");
    manager.addProvider("tar", new TarFileProvider());
}
 
Example 26
Source Project: commons-vfs   Source File: Http4ProviderTestCase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Prepares the file system manager.
 */
@Override
public void prepare(final DefaultFileSystemManager manager) throws Exception {
    if (!manager.hasProvider("http4")) {
        manager.addProvider("http4", new Http4FileProvider());
    }
}
 
Example 27
Source Project: commons-vfs   Source File: ResourceProviderTestCase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Prepares the file system manager. This implementation does nothing.
 */
@Override
public void prepare(final DefaultFileSystemManager manager) throws Exception {
    manager.addProvider("res", new ResourceFileProvider());
    manager.addProvider("file", new UrlFileProvider());
    manager.addProvider("jar", new JarFileProvider());
}
 
Example 28
Source Project: commons-vfs   Source File: HdfsFileProviderTest.java    License: Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
    Logger.getRootLogger().setLevel(Level.ERROR);

    // Put the MiniDFSCluster directory in the target directory
    final File data = new File("target/test/hdfstestdata").getAbsoluteFile();
    data.mkdirs();
    System.setProperty("test.build.data", data.toString());
    FileUtils.cleanDirectory(data);

    // Setup HDFS
    conf = new Configuration();
    conf.set(FileSystem.FS_DEFAULT_NAME_KEY, HDFS_URI);
    conf.set("hadoop.security.token.service.use_ip", "true");
    conf.setLong(DFSConfigKeys.DFS_BLOCK_SIZE_KEY, 1024 * 1024); // 1M blocksize

    setUmask(conf);

    cluster = new MiniDFSCluster(PORT, conf, 1, true, true, true, null, null, null, null);
    cluster.waitActive();

    // Set up the VFS
    manager = new DefaultFileSystemManager();
    manager.addProvider("hdfs", new HdfsFileProvider());
    manager.init();
    hdfs = cluster.getFileSystem();
}
 
Example 29
Source Project: commons-vfs   Source File: ProviderCacheStrategyTests.java    License: 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 30
Source Project: commons-vfs   Source File: ProviderCacheStrategyTests.java    License: 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");
}