Example usage for org.apache.commons.vfs2 AllFileSelector AllFileSelector

List of usage examples for org.apache.commons.vfs2 AllFileSelector AllFileSelector

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 AllFileSelector AllFileSelector.

Prototype

AllFileSelector

Source Link

Usage

From source file:maspack.fileutil.ZipUtility.java

public static void unzip(URIx src, final File dest) throws IOException {
    dest.mkdirs();//from w w w.j ava2s .  c  om

    final FileSystemManager fileSystemManager = VFS.getManager();
    final FileObject zipFileObject = fileSystemManager.resolveFile(src.toString());

    try {
        final FileObject fileSystem = fileSystemManager.createFileSystem(zipFileObject);
        try {
            fileSystemManager.toFileObject(dest).copyFrom(fileSystem, new AllFileSelector());
        } finally {
            fileSystem.close();
        }
    } finally {
        zipFileObject.close();
    }
}

From source file:fi.mystes.synapse.mediator.vfs.VFSTestHelper.java

public static void deleteDirectory(String dirPath) throws FileSystemException {
    FileObject dir = VFS.getManager().resolveFile(dirPath);
    dir.delete(new AllFileSelector());
    dir.delete();//  w  ww.  j a  v  a  2 s.c  o m
}

From source file:com.dougchimento.at.AtManagerSimpleTest.java

@BeforeClass
public void setUp() throws Exception {
    FileSystemManager manager = VFS.getManager();
    atManager = At.getManager(new AtTestConfig("atTest.properties"));

    FileObject fakeAtResource = manager
            .resolveFile(new File(ClassLoader.getSystemResource("fakeAt.sh").getFile()).toURI().toString());
    FileObject atListOutResource = manager.resolveFile(
            new File(ClassLoader.getSystemResource("atListOutput.txt").getFile()).toURI().toString());
    FileObject atListRemovedOutResource = manager.resolveFile(
            new File(ClassLoader.getSystemResource("atListRemovedOutput.txt").getFile()).toURI().toString());
    FileObject atListAddedOutResource = manager.resolveFile(
            new File(ClassLoader.getSystemResource("atListAddedOutput.txt").getFile()).toURI().toString());

    fakeAt = manager.resolveFile("file://tmp/.at/fakeAt.sh");
    atListOut = manager.resolveFile("file://tmp/.at/atListOutput.txt");
    atListRemovedOut = manager.resolveFile("file://tmp/.at/atListRemovedOutput.txt");
    atListAddedOut = manager.resolveFile("file://tmp/.at/atListAddedOutput.txt");

    //Copy from jar resources to /tmp so we can fake at commands
    //TODO Clean up
    //TODO Permission on this file may not allow deleting
    tmpDir = new File("/tmp/.at");
    if (tmpDir.exists()) {
        tmpDir.delete();// w  w  w .j  a  v a2s  . c o m
    }
    tmpDir = new File("/tmp/.at/");
    tmpDir.mkdir();

    atListOut.copyFrom(atListOutResource, new AllFileSelector());
    atListRemovedOut.copyFrom(atListRemovedOutResource, new AllFileSelector());
    atListAddedOut.copyFrom(atListAddedOutResource, new AllFileSelector());
    fakeAt.copyFrom(fakeAtResource, new AllFileSelector());

    atConfig = new AtTestConfig("atTest.properties");
    atManager = At.getManager(atConfig);
}

From source file:com.stehno.sanctuary.core.remote.FileSystemRemoteStore.java

private void storeFile(File rootDirectory, File file, String action) {
    try {//from w  w  w .j a  v  a2 s  . c om
        FileObject remoteFile = findRemoteFile(rootDirectory, file);

        // TODO: if this fails, it could be a partial copy, save original?
        remoteFile.copyFrom(fileSystemManager.toFileObject(file), new AllFileSelector());

        if (log.isDebugEnabled())
            log.debug(action + ": " + remoteFile);

    } catch (FileSystemException e) {
        // FIXME: error
        e.printStackTrace();
    }
}

From source file:hadoopInstaller.installation.UploadConfiguration.java

private void uploadConfiguration(FileObject remoteDirectory, Host host) throws InstallationError {
    try {/*from   ww w. jav a 2 s  . c  o m*/
        FileObject configurationDirectory = remoteDirectory.resolveFile("hadoop/etc/hadoop/"); //$NON-NLS-1$
        if (deleteOldFiles) {
            configurationDirectory.delete(new AllFileSelector());
            log.debug("HostInstallation.Upload.DeletingOldFiles", //$NON-NLS-1$
                    host.getHostname());
        } else if (!configurationDirectory.exists()) {
            throw new InstallationError("HostInstallation.Upload.NotDeployed"); //$NON-NLS-1$
        }
        configurationDirectory.copyFrom(filesToUpload, new AllFileSelector());
        modifyEnvShFile(host, configurationDirectory, InstallerConstants.ENV_FILE_HADOOP);
        modifyEnvShFile(host, configurationDirectory, InstallerConstants.ENV_FILE_YARN);
        try {
            configurationDirectory.close();
        } catch (FileSystemException ex) {
            log.warn("HostInstallation.CouldNotClose", //$NON-NLS-1$
                    configurationDirectory.getName().getURI());
        }
    } catch (FileSystemException e) {
        throw new InstallationError(e, "HostInstallation.Upload.Error", //$NON-NLS-1$
                remoteDirectory.getName().getURI());
    }
}

From source file:maspack.fileutil.FileCacher.java

public File cache(URIx uri, File cacheFile, FileTransferMonitor monitor) throws FileSystemException {

    // For atomic operation, first download to temporary directory
    File tmpCacheFile = new File(cacheFile.getAbsolutePath() + TMP_EXTENSION);
    URIx cacheURI = new URIx(cacheFile.getAbsoluteFile());
    URIx tmpCacheURI = new URIx(tmpCacheFile.getAbsoluteFile());
    FileObject localTempFile = manager.resolveFile(tmpCacheURI.toString(true));
    FileObject localCacheFile = manager.resolveFile(cacheURI.toString(true));

    FileObject remoteFile = null; // will resolve next

    // loop through authenticators until we either succeed or cancel
    boolean cancel = false;
    while (remoteFile == null && cancel == false) {
        remoteFile = resolveRemote(uri);
    }/* www. j a  v  a2 s.  c o m*/

    if (remoteFile == null || !remoteFile.exists()) {
        throw new FileSystemException("Cannot find remote file <" + uri.toString() + ">",
                new FileNotFoundException("<" + uri.toString() + ">"));
    }

    // monitor the file transfer progress
    if (monitor != null) {
        monitor.monitor(localTempFile, remoteFile, -1, cacheFile.getName());
        monitor.start();
        monitor.fireStartEvent(localTempFile);
    }

    // transfer content
    try {
        if (remoteFile.isFile()) {
            localTempFile.copyFrom(remoteFile, Selectors.SELECT_SELF);
        } else if (remoteFile.isFolder()) {
            // final FileObject fileSystem = manager.createFileSystem(remoteFile);
            localTempFile.copyFrom(remoteFile, new AllFileSelector());
            // fileSystem.close();
        }

        if (monitor != null) {
            monitor.fireCompleteEvent(localTempFile);
        }
    } catch (Exception e) {
        // try to delete local file
        localTempFile.delete();
        throw new RuntimeException(
                "Failed to complete transfer of " + remoteFile.getURL() + " to " + localTempFile.getURL(), e);
    } finally {
        // close files if we need to
        localTempFile.close();
        remoteFile.close();
        if (monitor != null) {
            monitor.release(localTempFile);
            monitor.stop();
        }

    }

    // now that the copy is complete, do a rename operation
    try {
        if (tmpCacheFile.isDirectory()) {
            SafeFileUtils.moveDirectory(tmpCacheFile, cacheFile);
        } else {
            SafeFileUtils.moveFile(tmpCacheFile, cacheFile);
        }
    } catch (Exception e) {
        localCacheFile.delete(); // delete if possible
        throw new RuntimeException("Failed to atomically move " + "to " + localCacheFile.getURL(), e);
    }

    return cacheFile;

}

From source file:net.sf.jabb.web.action.VfsTreeAction.java

/**
 * AJAX tree functions// w ww  .j av a 2  s.  co  m
 */
public String execute() {
    normalizeTreeRequest();
    JsTreeResult result = new JsTreeResult();

    final AllFileSelector ALL_FILES = new AllFileSelector();
    FileSystemManager fsManager = null;
    FileObject rootFile = null;
    FileObject file = null;
    FileObject referenceFile = null;
    try {
        fsManager = VfsUtility.getManager();
        rootFile = fsManager.resolveFile(rootPath, fsOptions);

        if (JsTreeRequest.OP_GET_CHILDREN.equalsIgnoreCase(requestData.getOperation())) {
            String parentPath = requestData.getId();
            List<JsTreeNodeData> nodes = null;
            try {
                nodes = getChildNodes(rootFile, parentPath);
                if (requestData.isRoot() && rootNodeName != null) { // add root node
                    JsTreeNodeData rootNode = new JsTreeNodeData();
                    rootNode.setData(rootNodeName);
                    Map<String, Object> attr = new HashMap<String, Object>();
                    rootNode.setAttr(attr);
                    attr.put("id", ".");
                    attr.put("rel", "root");
                    attr.put("fileType", FileType.FOLDER.toString());
                    rootNode.setChildren(nodes);
                    rootNode.setState(JsTreeNodeData.STATE_OPEN);
                    nodes = new LinkedList<JsTreeNodeData>();
                    nodes.add(rootNode);
                }
            } catch (Exception e) {
                log.error("Cannot get child nodes for: " + parentPath, e);
                nodes = new LinkedList<JsTreeNodeData>();
            }
            responseData = nodes;
        } else if (JsTreeRequest.OP_REMOVE_NODE.equalsIgnoreCase(requestData.getOperation())) {
            String path = requestData.getId();
            try {
                file = rootFile.resolveFile(path, NameScope.DESCENDENT);
                boolean wasDeleted = false;
                if (file.getType() == FileType.FILE) {
                    wasDeleted = file.delete();
                } else {
                    wasDeleted = file.delete(ALL_FILES) > 0;
                }
                result.setStatus(wasDeleted);
            } catch (Exception e) {
                result.setStatus(false);
                log.error("Cannot delete: " + path, e);
            }
            responseData = result;
        } else if (JsTreeRequest.OP_CREATE_NODE.equalsIgnoreCase(requestData.getOperation())) {
            String parentPath = requestData.getReferenceId();
            String name = requestData.getTitle();
            try {
                referenceFile = rootFile.resolveFile(parentPath, NameScope.DESCENDENT_OR_SELF);
                file = referenceFile.resolveFile(name, NameScope.CHILD);
                file.createFolder();
                result.setStatus(true);
                result.setId(rootFile.getName().getRelativeName(file.getName()));
            } catch (Exception e) {
                result.setStatus(false);
                log.error("Cannot create folder '" + name + "' under '" + parentPath + "'", e);
            }
            responseData = result;
        } else if (JsTreeRequest.OP_RENAME_NODE.equalsIgnoreCase(requestData.getOperation())) {
            String path = requestData.getId();
            String name = requestData.getTitle();
            try {
                referenceFile = rootFile.resolveFile(path, NameScope.DESCENDENT);
                file = referenceFile.getParent().resolveFile(name, NameScope.CHILD);
                referenceFile.moveTo(file);
                result.setStatus(true);
            } catch (Exception e) {
                result.setStatus(false);
                log.error("Cannot rename '" + path + "' to '" + name + "'", e);
            }
            responseData = result;
        } else if (JsTreeRequest.OP_MOVE_NODE.equalsIgnoreCase(requestData.getOperation())) {
            String newParentPath = requestData.getReferenceId();
            String originalPath = requestData.getId();
            try {
                referenceFile = rootFile.resolveFile(originalPath, NameScope.DESCENDENT);
                file = rootFile.resolveFile(newParentPath, NameScope.DESCENDENT_OR_SELF)
                        .resolveFile(referenceFile.getName().getBaseName(), NameScope.CHILD);
                if (requestData.isCopy()) {
                    file.copyFrom(referenceFile, ALL_FILES);
                } else {
                    referenceFile.moveTo(file);
                }
                result.setStatus(true);
            } catch (Exception e) {
                result.setStatus(false);
                log.error("Cannot move '" + originalPath + "' to '" + newParentPath + "'", e);
            }
            responseData = result;
        }
    } catch (FileSystemException e) {
        log.error("Cannot perform file operation.", e);
    } finally {
        VfsUtility.close(fsManager, file, referenceFile, rootFile);
    }
    return SUCCESS;
}

From source file:maspack.fileutil.FileCacher.java

public boolean copy(URIx from, URIx to, FileTransferMonitor monitor) throws FileSystemException {

    FileObject fromFile = null;//www .j ava  2 s.co m
    FileObject toFile = null;

    // clear authenticators
    setAuthenticator(fsOpts, null);
    setIdentityFactory(fsOpts, null);

    // loop through authenticators until we either succeed or cancel
    boolean cancel = false;
    while (toFile == null && cancel == false) {
        toFile = resolveRemote(to);
    }

    cancel = false;
    while (fromFile == null && cancel == false) {
        fromFile = resolveRemote(from);
    }

    if (fromFile == null || !fromFile.exists()) {
        throw new FileSystemException("Cannot find source file <" + from.toString() + ">",
                new FileNotFoundException("<" + from.toString() + ">"));
    }

    if (toFile == null) {
        throw new FileSystemException("Cannot find destination <" + to.toString() + ">",
                new FileNotFoundException("<" + to.toString() + ">"));
    }

    // monitor the file transfer progress
    if (monitor != null) {
        monitor.monitor(fromFile, toFile, -1, fromFile.getName().getBaseName());
        monitor.start();
        monitor.fireStartEvent(toFile);
    }

    // transfer content
    try {
        if (fromFile.isFile()) {
            toFile.copyFrom(fromFile, Selectors.SELECT_SELF);
        } else if (fromFile.isFolder()) {
            // final FileObject fileSystem = manager.createFileSystem(remoteFile);
            toFile.copyFrom(fromFile, new AllFileSelector());
            // fileSystem.close();
        }

        if (monitor != null) {
            monitor.fireCompleteEvent(toFile);
        }
    } catch (Exception e) {
        throw new FileTransferException(
                "Failed to complete transfer of " + fromFile.getURL() + " to " + toFile.getURL(), e);
    } finally {
        // close files if we need to
        fromFile.close();
        toFile.close();
        if (monitor != null) {
            monitor.release(toFile);
            monitor.stop();
        }
    }

    return true;

}

From source file:com.app.server.SARDeployer.java

public CopyOnWriteArrayList<String> unpack(final FileObject unpackFileObject, final File outputDir,
        StandardFileSystemManager fileSystemManager) throws IOException {
    outputDir.mkdirs();// w  ww  .j  a va  2s.com
    URLClassLoader webClassLoader;
    CopyOnWriteArrayList<String> classPath = new CopyOnWriteArrayList<String>();
    final FileObject packFileObject = fileSystemManager
            .resolveFile("jar:" + unpackFileObject.toString() + "!/");
    try {
        FileObject outputDirFileObject = fileSystemManager.toFileObject(outputDir);
        outputDirFileObject.copyFrom(packFileObject, new AllFileSelector());
        FileObject[] libs = outputDirFileObject.findFiles(new FileSelector() {

            public boolean includeFile(FileSelectInfo arg0) throws Exception {
                return arg0.getFile().getName().getBaseName().toLowerCase().endsWith(".jar");
            }

            public boolean traverseDescendents(FileSelectInfo arg0) throws Exception {
                // TODO Auto-generated method stub
                return true;
            }

        });
        /*String replaceString="file:///"+outputDir.getAbsolutePath().replace("\\","/");
        replaceString=replaceString.endsWith("/")?replaceString:replaceString+"/";*/
        // System.out.println(replaceString);
        for (FileObject lib : libs) {
            // System.out.println(outputDir.getAbsolutePath());
            // System.out.println(jsp.getName().getFriendlyURI());
            classPath.add(lib.getName().getFriendlyURI());
            // System.out.println(relJspName);
        }
    } finally {
        packFileObject.close();
    }
    return classPath;
}

From source file:com.app.server.WarDeployer.java

public Vector<URL> unpack(final FileObject unpackFileObject, final File outputDir,
        StandardFileSystemManager fileSystemManager, ConcurrentHashMap<String, String> jsps)
        throws IOException {
    outputDir.mkdirs();/*from   w  w  w  . j a  v  a2  s .c  o m*/
    URLClassLoader webClassLoader;
    Vector<URL> libraries = new Vector<URL>();
    final FileObject packFileObject = fileSystemManager.resolveFile(unpackFileObject.toString());
    try {
        FileObject outputDirFileObject = fileSystemManager.toFileObject(outputDir);
        outputDirFileObject.copyFrom(packFileObject, new AllFileSelector());
        FileObject[] jspFiles = outputDirFileObject.findFiles(new FileSelector() {

            public boolean includeFile(FileSelectInfo arg0) throws Exception {
                return arg0.getFile().getName().getBaseName().toLowerCase().endsWith(".jsp")
                        || arg0.getFile().getName().getBaseName().toLowerCase().endsWith(".jar");
            }

            public boolean traverseDescendents(FileSelectInfo arg0) throws Exception {
                // TODO Auto-generated method stub
                return true;
            }

        });
        String replaceString = "file:///" + outputDir.getAbsolutePath().replace("\\", "/");
        replaceString = replaceString.endsWith("/") ? replaceString : replaceString + "/";
        // System.out.println(replaceString);
        for (FileObject jsplibs : jspFiles) {
            // System.out.println(outputDir.getAbsolutePath());
            // System.out.println(jsp.getName().getFriendlyURI());
            if (jsplibs.getName().getBaseName().endsWith(".jar")) {
                libraries.add(new URL(jsplibs.getName().getFriendlyURI()));
            } else {
                String relJspName = jsplibs.getName().getFriendlyURI().replace(replaceString, "");
                jsps.put(relJspName, relJspName);
            }
            // System.out.println(relJspName);
        }
    } finally {
        packFileObject.close();
    }
    return libraries;
}