Example usage for org.apache.commons.vfs.impl DefaultFileSystemManager close

List of usage examples for org.apache.commons.vfs.impl DefaultFileSystemManager close

Introduction

In this page you can find the example usage for org.apache.commons.vfs.impl DefaultFileSystemManager close.

Prototype

public void close() 

Source Link

Document

Closes all files created by this manager, and cleans up any temporary files.

Usage

From source file:com.threecrickets.sincerity.util.IoUtil.java

/**
 * Copies a complete subdirectory tree using Apache Commons VFS.
 * //from ww w.  j ava2s .co  m
 * @param fromDir
 *        The source directory
 * @param toDir
 *        The target directory
 * @throws IOException
 *         In case of an I/O error
 */
public static void copyRecursive(File fromDir, File toDir) throws IOException {
    DefaultFileSystemManager manager = new DefaultFileSystemManager();
    try {
        DefaultLocalFileProvider fileProvider = new DefaultLocalFileProvider();
        manager.addProvider("file", fileProvider);
        manager.setDefaultProvider(fileProvider);
        manager.setFilesCache(new DefaultFilesCache());
        manager.init();
        copyRecursive(manager, manager.resolveFile(fromDir.toURI().toString()),
                manager.resolveFile(toDir.toURI().toString()));
    } finally {
        manager.close();
    }
}

From source file:com.threecrickets.sincerity.util.IoUtil.java

/**
 * Unpacks all files in an archive using Apache Commons VFS.
 * <p>//from   w  w w. j  a  va 2 s. co  m
 * Supported formats: zip, tar.gz/tgz, tar.bz2.
 * 
 * @param archiveFile
 *        The archive file
 * @param destinationDir
 *        The destination directory
 * @param workDir
 *        The work directory
 * @throws IOException
 *         In case of an I/O error
 */
public static void unpack(File archiveFile, File destinationDir, File workDir) throws IOException {
    String scheme = null;
    String name = archiveFile.getName();
    DefaultFileSystemManager manager = new DefaultFileSystemManager();
    try {
        boolean untar = false;
        if (name.endsWith(".zip")) {
            scheme = "zip:";
            manager.addProvider("zip", new ZipFileProvider());
        } else if (name.endsWith(".tar.gz") || name.endsWith(".tgz")) {
            scheme = "gz:";
            untar = true;
            manager.addProvider("tar", new TarFileProvider());
            manager.addProvider("gz", new GzipFileProvider());
            manager.addProvider("tgz", new TgzFileProvider());
        } else if (name.endsWith(".tar.bz2")) {
            scheme = "bz2:";
            untar = true;
            manager.addProvider("tar", new TarFileProvider());
            manager.addProvider("bz2", new Bzip2FileProvider());
            manager.addProvider("tbz2", new Tbz2FileProvider());
        }

        if (scheme != null) {
            DefaultFileReplicator replicator = new DefaultFileReplicator(workDir);
            replicator.init();
            manager.setReplicator(replicator);
            manager.setTemporaryFileStore(replicator);
            DefaultLocalFileProvider fileProvider = new DefaultLocalFileProvider();
            manager.addProvider("file", fileProvider);
            manager.setDefaultProvider(fileProvider);
            manager.setFilesCache(new DefaultFilesCache());
            manager.init();

            String path = scheme + archiveFile.toURI();
            FileObject fileObject = manager.resolveFile(path);
            FileObject[] children = fileObject.getChildren();
            if (untar && children.length > 0) {
                FileObject tar = manager
                        .resolveFile(new File(workDir, children[0].getName().getBaseName()).toURI().toString());
                org.apache.commons.vfs.FileUtil.copyContent(children[0], tar);
                tar = manager.resolveFile("tar:" + tar.getName());
                children = tar.getChildren();
            }

            for (FileObject child : children)
                copyRecursive(manager, child, manager.resolveFile(destinationDir.toURI().toString()));
        }
    } finally {
        manager.close();
    }
}

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");

    /*// w  ww .  j  a va2  s .co 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:com.pongasoft.kiwidoc.builder.doclet.SourceCodeParser.java

/**
 * Parses the sources using a doclet. It is ok to provide a big number of files, since it will
 * automatically be split in chunks.//  w ww .  j ava2  s  .co  m
 *
 * @param library the library to store the results
 * @param sources the sources
 * @return the number of processed files
 * @throws IOException if there is an issue reading the files
 */
public int parseSources(LibraryModelBuilder library, FileObject sources, String overviewFilename,
        FileObject javadoc, Collection<FileObject> dependencies) throws IOException, InternalException {
    DefaultFileSystemManager dfs = new DefaultFileSystemManager();
    dfs.addProvider("file", new DefaultLocalFileProvider());
    dfs.setReplicator(new DefaultFileReplicator(IOUtils.createTempDirectory("SourceCodeParser", "")));
    dfs.init();
    try {
        File localSources = dfs.getReplicator().replicateFile(sources, SourcesSelector.INSTANCE);

        sources = dfs.toFileObject(localSources);

        FileObject[] allSources = sources.findFiles(JavaSelector.INSTANCE);

        if (allSources == null || allSources.length == 0)
            return 0;

        List<String> sourcePath = new ArrayList<String>();

        for (FileObject sourceFile : allSources) {
            //        if(javadoc != null)
            //        {
            //          String name = sources.getName().getRelativeName(sourceFile.getName());
            //          name = name.substring(0, name.length() - JAVA_EXTENSION.length());
            //          name += HTML_EXTENSION;
            //          if(javadoc.resolveFile(name, NameScope.DESCENDENT).exists())
            //            sourcePath.add(sourceFile.getName().getPath());
            //        }
            //        else
            //        {
            //          sourcePath.add(sourceFile.getName().getPath());
            //        }
            sourcePath.add(sourceFile.getName().getPath());
        }

        List<String> deps = new ArrayList<String>();

        if (dependencies != null) {
            for (FileObject dependency : dependencies) {
                if (dependency.exists()) {
                    File localDependency = dfs.getReplicator().replicateFile(dependency,
                            DependenciesSelector.INSTANCE);
                    deps.add(localDependency.getCanonicalPath());
                }
            }
        }

        FileObject overviewFile = sources.resolveFile(overviewFilename, NameScope.CHILD);

        String overviewPath = null;
        if (overviewFile.exists())
            overviewPath = overviewFile.getName().getPath();

        parseSources(library, overviewPath, sourcePath, deps);

        return sourcePath.size();
    } finally {
        dfs.close();
    }
}

From source file:org.org.eclipse.core.utils.platform.tools.ArchivesToolBox.java

private static void decompressArchiveTo(File archiveFile, File targetFolder, String archiveProtocol,
        String pathInArchive) throws IOException {
    DefaultFileSystemManager fileSystemManager = createFileSystemManager();
    FileObject fileObject = createFileObject(fileSystemManager, archiveFile, archiveProtocol, pathInArchive);
    decompressFileObjectTo(fileObject, targetFolder);
    fileSystemManager.close();
}

From source file:org.org.eclipse.core.utils.platform.tools.ArchivesToolBox.java

private static void decompressArchiveTo(File archiveFile, File targetFolder, String archiveProtocol,
        String pathInArchive, IWriteHinter writeHinter) throws IOException {
    DefaultFileSystemManager fileSystemManager = createFileSystemManager();
    FileObject fileObject = createFileObject(fileSystemManager, archiveFile, archiveProtocol, pathInArchive);
    decompressFileObjectTo(fileObject, targetFolder, writeHinter);
    fileSystemManager.close();
}