Example usage for org.apache.commons.vfs FileObject refresh

List of usage examples for org.apache.commons.vfs FileObject refresh

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileObject refresh.

Prototype

public void refresh() throws FileSystemException;

Source Link

Document

This will prepare the fileObject to get resynchronized with the underlaying filesystem if required

Usage

From source file:mondrian.spi.impl.ApacheVfsVirtualFileHandler.java

public InputStream readVirtualFile(String url) throws FileSystemException {
    // Treat catalogUrl as an Apache VFS (Virtual File System) URL.
    // VFS handles all of the usual protocols (http:, file:)
    // and then some.
    FileSystemManager fsManager = VFS.getManager();
    if (fsManager == null) {
        throw Util.newError("Cannot get virtual file system manager");
    }/*w  w  w  . ja  v  a2s .  c om*/

    // Workaround VFS bug.
    if (url.startsWith("file://localhost")) {
        url = url.substring("file://localhost".length());
    }
    if (url.startsWith("file:")) {
        url = url.substring("file:".length());
    }

    //work around for VFS bug not closing http sockets
    // (Mondrian-585)
    if (url.startsWith("http")) {
        try {
            return new URL(url).openStream();
        } catch (IOException e) {
            throw Util.newError("Could not read URL: " + url);
        }
    }

    File userDir = new File("").getAbsoluteFile();
    FileObject file = fsManager.resolveFile(userDir, url);
    FileContent fileContent = null;
    try {
        // Because of VFS caching, make sure we refresh to get the latest
        // file content. This refresh may possibly solve the following
        // workaround for defect MONDRIAN-508, but cannot be tested, so we
        // will leave the work around for now.
        file.refresh();

        // Workaround to defect MONDRIAN-508. For HttpFileObjects, verifies
        // the URL of the file retrieved matches the URL passed in.  A VFS
        // cache bug can cause it to treat URLs with different parameters
        // as the same file (e.g. http://blah.com?param=A,
        // http://blah.com?param=B)
        if (file instanceof HttpFileObject && !file.getName().getURI().equals(url)) {
            fsManager.getFilesCache().removeFile(file.getFileSystem(), file.getName());

            file = fsManager.resolveFile(userDir, url);
        }

        if (!file.isReadable()) {
            throw Util.newError("Virtual file is not readable: " + url);
        }

        fileContent = file.getContent();
    } finally {
        file.close();
    }

    if (fileContent == null) {
        throw Util.newError("Cannot get virtual file content: " + url);
    }

    return fileContent.getInputStream();
}

From source file:net.sf.vfsjfilechooser.plaf.basic.BasicVFSDirectoryModel.java

/**
 *
 *///from w w  w  .  j av  a2 s  .c  om
public void validateFileCache() {
    FileObject currentDirectory = filechooser.getCurrentDirectory();

    if (currentDirectory == null) {
        return;
    }

    try {
        currentDirectory.refresh();
    } catch (FileSystemException ex) {
    }

    if (loadThread != null) {
        loadThread.cancel(true);
    }

    setBusy(true, ++fetchID);

    loadThread = executor.submit(new LoadFilesThread(currentDirectory, fetchID));
}

From source file:egovframework.rte.fdl.filehandling.FilehandlingServiceTest.java

/**
 * ? ? , ?  ??  ?? ?/*from ww w .  ja  v  a  2s.c om*/
 * ?  ? ? ? .
 * @throws Exception
 */
@Test
public void testCaching() throws Exception {
    String testFolder = "d:/workspace/java/e-gov/eGovFramework/RTE/DEV/trunk/Foundation/egovframework.rte.fdl.filehandling/testfolder";
    FileSystemManager manager = VFS.getManager();

    FileObject scratchFolder = manager.resolveFile(testFolder);

    // testfolder ?  ? 
    scratchFolder.delete(Selectors.EXCLUDE_SELF);

    // ? Manager ?
    DefaultFileSystemManager fs = new DefaultFileSystemManager();
    fs.setFilesCache(manager.getFilesCache());

    // zip, jar, tgz, tar, tbz2, file
    if (!fs.hasProvider("file")) {
        fs.addProvider("file", new DefaultLocalFileProvider());
    }

    fs.setCacheStrategy(CacheStrategy.ON_RESOLVE);
    fs.init();

    // ? ? ?
    FileObject foBase2 = fs.resolveFile(testFolder);
    log.debug("## scratchFolder.getName().getPath() : " + scratchFolder.getName().getPath());

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

    // ??  ?
    FileObject[] fos = cachedFolder.getChildren();
    assertFalse(contains(fos, "file1.txt"));

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

    // ? 
    // BUT cachedFolder ? ??  ?
    fos = cachedFolder.getChildren();
    assertFalse(contains(fos, "file1.txt"));

    // 
    cachedFolder.refresh();
    // ?? 
    fos = cachedFolder.getChildren();
    assertTrue(contains(fos, "file1.txt"));

}

From source file:mondrian.olap.Util.java

/**
 * Gets content via Apache VFS. File must exist and have content
 *
 * @param url String// www. ja v  a2s .  c o m
 * @return Apache VFS FileContent for further processing
 * @throws FileSystemException on error
 */
public static InputStream readVirtualFile(String url) throws FileSystemException {
    // Treat catalogUrl as an Apache VFS (Virtual File System) URL.
    // VFS handles all of the usual protocols (http:, file:)
    // and then some.
    FileSystemManager fsManager = VFS.getManager();
    if (fsManager == null) {
        throw newError("Cannot get virtual file system manager");
    }

    // Workaround VFS bug.
    if (url.startsWith("file://localhost")) {
        url = url.substring("file://localhost".length());
    }
    if (url.startsWith("file:")) {
        url = url.substring("file:".length());
    }

    // work around for VFS bug not closing http sockets
    // (Mondrian-585)
    if (url.startsWith("http")) {
        try {
            return new URL(url).openStream();
        } catch (IOException e) {
            throw newError("Could not read URL: " + url);
        }
    }

    File userDir = new File("").getAbsoluteFile();
    FileObject file = fsManager.resolveFile(userDir, url);
    FileContent fileContent = null;
    try {
        // Because of VFS caching, make sure we refresh to get the latest
        // file content. This refresh may possibly solve the following
        // workaround for defect MONDRIAN-508, but cannot be tested, so we
        // will leave the work around for now.
        file.refresh();

        // Workaround to defect MONDRIAN-508. For HttpFileObjects, verifies
        // the URL of the file retrieved matches the URL passed in.  A VFS
        // cache bug can cause it to treat URLs with different parameters
        // as the same file (e.g. http://blah.com?param=A,
        // http://blah.com?param=B)
        if (file instanceof HttpFileObject && !file.getName().getURI().equals(url)) {
            fsManager.getFilesCache().removeFile(file.getFileSystem(), file.getName());

            file = fsManager.resolveFile(userDir, url);
        }

        if (!file.isReadable()) {
            throw newError("Virtual file is not readable: " + url);
        }

        fileContent = file.getContent();
    } finally {
        file.close();
    }

    if (fileContent == null) {
        throw newError("Cannot get virtual file content: " + url);
    }

    return fileContent.getInputStream();
}

From source file:org.objectweb.proactive.extensions.dataspaces.vfs.VFSNodeScratchSpaceImpl.java

public synchronized void close() throws IllegalStateException {
    logger.debug("Closing node scratch space");
    checkIfConfigured();// w w w . ja  va2 s .  com

    try {
        final FileObject fRuntime = partialSpaceFile.getParent();

        // rm -r node
        partialSpaceFile.delete(Selectors.SELECT_ALL);

        // try to remove runtime file
        // IMPORTANT FIXME: it seems that despite of VFS FileObject documentation,
        // looking at AbstractFileObject docs suggests that it does not implement this
        // delete-if-empty behavior! at least, it appears to be not atomic (and probably may be never atomic
        // as some protocols may not support this kind of atomic operation?)
        // refreshing file before deleting may minimize the risk of delete-when-non-empty behavior
        fRuntime.refresh();
        try {
            final boolean deleted = fRuntime.delete();
            if (deleted)
                logger.debug("Scratch directory for whole runtime was deleted (considered as empty)");
            else
                logger.debug("Scratch directory for whole runtime was not deleted (not considered as empty)");
        } catch (org.apache.commons.vfs.FileSystemException x) {
            logger.debug("Could not delete scratch directory for whole runtime - perhaps it was not empty", x);
        }

        // it is probably not needed to close files if manager is closed, but with VFS you never know...
        fRuntime.close();
        partialSpaceFile.close();
    } catch (org.apache.commons.vfs.FileSystemException x) {
        ProActiveLogger.logEatedException(logger, "Could not close correctly node scratch space", x);
    } finally {
        this.fileSystemManager.close();
    }
    logger.debug("Closed node scratch space");
}

From source file:org.sonatype.gshell.vfs.FileSystemAccessImpl.java

public FileObject resolveFile(final FileObject baseFile, final String name) throws FileSystemException {
    FileObject f = getManager().resolveFile(baseFile, name);
    FileObject d = dereference(f);
    if (d != null) {
        d.refresh();
    }/*from  w ww  . j  av  a 2 s  .c om*/
    return f;
}

From source file:org.sonatype.gshell.vfs.FileSystemAccessImpl.java

public FileObject resolveFile(final String name) throws FileSystemException {
    FileObject f = getManager().resolveFile(getCurrentDirectory(), name);
    FileObject d = dereference(f);
    if (d != null) {
        d.refresh();
    }// w  ww  .ja  va 2  s . co  m
    return f;
}

From source file:org.sonatype.gshell.vfs.FileSystemAccessImpl.java

public File getLocalFile(final FileObject file) throws FileSystemException {
    if (!isLocalFile(file)) {
        throw new FileSystemException("Unable to get local file from: " + file.getClass());
    }/*from   w  ww.  j  a va 2 s.c  o  m*/

    try {
        file.refresh();
        Field field = LocalFile.class.getDeclaredField("file");

        try {
            return (File) field.get(file);
        } catch (IllegalAccessException ignore) {
            // try again
            field.setAccessible(true);
            return (File) field.get(file);
        }
    } catch (Exception e) {
        throw new FileSystemException(e);
    }
}

From source file:org.sonatype.gshell.vfs.FileSystemAccessImpl.java

public FileObject dereference(final FileObject file) throws FileSystemException {
    assert file != null;

    if (file instanceof DelegateFileObject) {
        try {/*from w  w  w  .j a  v  a 2s.com*/
            file.refresh();
            Field field = DelegateFileObject.class.getDeclaredField("file");

            try {
                return (FileObject) field.get(file);
            } catch (IllegalAccessException ignore) {
                // try again
                field.setAccessible(true);
                return (FileObject) field.get(file);
            }
        } catch (Exception e) {
            throw new FileSystemException(e);
        }
    }

    return file;
}