Example usage for org.apache.commons.vfs FileSystemManager getFilesCache

List of usage examples for org.apache.commons.vfs FileSystemManager getFilesCache

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileSystemManager getFilesCache.

Prototype

public FilesCache getFilesCache();

Source Link

Document

Get the cache used to cache fileobjects.

Usage

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

/**
 * ? ? , ?  ??  ?? ?/*  ww  w.  ja v  a 2s  .c  o  m*/
 * ?  ? ? ? .
 * @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:egovframework.rte.fdl.filehandling.FilehandlingServiceTest.java

@Test
public void testCaching1() throws Exception {
    String testFolder = "d:/workspace/java/e-gov/eGovFramework/RTE/DEV/trunk/Foundation/egovframework.rte.fdl.filehandling/test";

    FileSystemManager manager = VFS.getManager();

    EgovFileUtil.writeFile(testFolder + "/file1.txt", text, "UTF-8");

    /*//from  www.j a  va2  s. c  o  m
     * ? Manager ?
     * CacheStrategy.MANUAL      : Deal with cached data manually. Call FileObject.refresh() to refresh the object data.
     * CacheStrategy.ON_RESOLVE : Refresh the data every time you request a file from FileSystemManager.resolveFile
     * CacheStrategy.ON_CALL   : Refresh the data every time you call a method on the fileObject. You'll use this only if you really need the latest info as this setting is a major performance loss. 
     */
    DefaultFileSystemManager fs = new DefaultFileSystemManager();
    fs.setFilesCache(manager.getFilesCache());

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

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

    // ? ? ?
    //FileObject foBase2 = fs.resolveFile(testFolder);
    log.debug("####1");
    FileObject cachedFile = fs.toFileObject(new File(testFolder + "/file1.txt"));
    log.debug("####2");

    FilesCache filesCache = fs.getFilesCache();
    log.debug("####3");
    filesCache.putFile(cachedFile);
    FileObject obj = filesCache.getFile(cachedFile.getFileSystem(), cachedFile.getName());

    //FileObject baseFile = fs.getBaseFile();
    //        log.debug("### cachedFile.getContent().getSize() is " + cachedFile.getContent().getSize());

    //        long fileSize = cachedFile.getContent().getSize();
    //        log.debug("#########size is " + fileSize);
    //FileObject cachedFile1 = cachedFile.resolveFile("file2.txt");

    //       FileObject scratchFolder = manager.resolveFile(testFolder);
    //       scratchFolder.delete(Selectors.EXCLUDE_SELF);

    EgovFileUtil.delete(new File(testFolder + "/file1.txt"));

    //       obj.createFile();

    //        log.debug("#########obj is " + obj.toString());
    //        log.debug("#########size is " + obj.getContent().getSize());
    log.debug("#########file is " + obj.exists());

    fs.close();
}

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");
    }/*from  ww  w. j  a v  a2s .co m*/

    // 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:mondrian.olap.Util.java

/**
 * Gets content via Apache VFS. File must exist and have content
 *
 * @param url String// w w  w  . j  a va  2 s.  com
 * @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.eclim.plugin.core.command.archive.ArchiveReadCommand.java

/**
 * {@inheritDoc}/*www  .  ja  v a 2  s.c  om*/
 */
public String execute(CommandLine commandLine) throws Exception {
    InputStream in = null;
    OutputStream out = null;
    FileSystemManager fsManager = null;
    try {
        String file = commandLine.getValue(Options.FILE_OPTION);

        fsManager = VFS.getManager();
        FileObject fileObject = fsManager.resolveFile(file);
        FileObject tempFile = fsManager
                .resolveFile(SystemUtils.JAVA_IO_TMPDIR + "/eclim/" + fileObject.getName().getPath());

        // the vfs file cache isn't very intelligent, so clear it.
        fsManager.getFilesCache().clear(fileObject.getFileSystem());
        fsManager.getFilesCache().clear(tempFile.getFileSystem());

        // NOTE: FileObject.getName().getPath() does not include the drive
        // information.
        String path = tempFile.getName().getURI().substring(URI_PREFIX.length());
        // account for windows uri which has an extra '/' in front of the drive
        // letter (file:///C:/blah/blah/blah).
        if (WIN_PATH.matcher(path).matches()) {
            path = path.substring(1);
        }

        //if(!tempFile.exists()){
        tempFile.createFile();

        in = fileObject.getContent().getInputStream();
        out = tempFile.getContent().getOutputStream();
        IOUtils.copy(in, out);

        new File(path).deleteOnExit();
        //}

        return path;
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
}