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:org.aludratest.service.file.impl.FileActionImpl.java

/** Deletes a file or folder. */
@Override//from  www  . j  av  a2 s  . c o m
public void delete(String filePath) {
    assertWritingPermitted("delete()");
    FileUtil.verifyFilePath(filePath);
    try {
        getFileObject(filePath).delete(new AllFileSelector());
        LOGGER.info("Deleted {}", filePath);
    } catch (FileSystemException e) {
        throw new TechnicalException("Error deleting file", e);
    }
}

From source file:org.aludratest.service.file.impl.FileInteractionImpl.java

/** Deletes a file or folder. */
@Override//from   ww w  . j  ava2s  .  c  om
public void delete(String filePath) {
    assertWritingPermitted("delete()");
    File.verifyFilePath(filePath);
    try {
        getFileObject(filePath).delete(new AllFileSelector());
        logger.debug("Deleted {}", filePath);
    } catch (IOException e) {
        throw new TechnicalException("Error deleting file", e);
    }
}

From source file:org.cloudifysource.esc.installer.filetransfer.VfsFileTransfer.java

@Override
public void copyFiles(final InstallationDetails details, final Set<String> excludedFiles,
        final List<File> additionalFiles, final long endTimeMillis)
        throws TimeoutException, InstallerException {

    logger.fine("Copying files to: " + host + " from local dir: " + localDir.getName().getPath() + " excluding "
            + excludedFiles.toString());

    try {/*w ww.  j  ava 2  s  .  c o  m*/

        if (remoteDir.exists()) {
            FileType type = remoteDir.getType();
            if (!type.equals(FileType.FOLDER)) {
                throw new InstallerException("The remote location: " + remoteDir.getName().getFriendlyURI()
                        + " exists but is not a directory");
            }

            if (deleteRemoteDirectoryContents) {
                logger.info("Deleting contents of remote directory: " + remoteDir.getName().getFriendlyURI());
                remoteDir.delete(new FileDepthSelector(1, Integer.MAX_VALUE));
            }
            FileObject[] children = remoteDir.getChildren();
            if (children.length > 0) {

                throw new InstallerException(
                        "The remote directory: " + remoteDir.getName().getFriendlyURI() + " is not empty");
            }
        }

        remoteDir.copyFrom(localDir, new FileSelector() {

            @Override
            public boolean includeFile(final FileSelectInfo fileInfo) throws Exception {
                if (excludedFiles.contains(fileInfo.getFile().getName().getBaseName())) {
                    logger.fine(fileInfo.getFile().getName().getBaseName() + " excluded");
                    return false;

                }
                final FileObject remoteFile = fileSystemManager.resolveFile(remoteDir,
                        localDir.getName().getRelativeName(fileInfo.getFile().getName()));

                if (!remoteFile.exists()) {
                    logger.fine(fileInfo.getFile().getName().getBaseName() + " missing on server");
                    return true;
                }

                if (fileInfo.getFile().getType() == FileType.FILE) {
                    final long remoteSize = remoteFile.getContent().getSize();
                    final long localSize = fileInfo.getFile().getContent().getSize();
                    final boolean res = localSize != remoteSize;
                    if (res) {
                        logger.fine(fileInfo.getFile().getName().getBaseName() + " different on server");
                    }
                    return res;
                }
                return false;

            }

            @Override
            public boolean traverseDescendents(final FileSelectInfo fileInfo) throws Exception {
                return true;
            }
        });

        for (final File file : additionalFiles) {
            logger.fine("copying file: " + file.getAbsolutePath() + " to remote directory");
            final FileObject fileObject = fileSystemManager.resolveFile("file:" + file.getAbsolutePath());
            final FileObject remoteFile = remoteDir.resolveFile(file.getName());
            remoteFile.copyFrom(fileObject, new AllFileSelector());
        }

        logger.fine("Copying files to: " + host + " completed.");
    } catch (final FileSystemException e) {
        throw new InstallerException("Failed to copy files to remote host " + host + ": " + e.getMessage(), e);

    }
    checkTimeout(endTimeMillis);

}

From source file:org.datacleaner.user.upgrade.DataCleanerHomeUpgrader.java

/**
 * Finds a folder to upgrade from based on the "newFolder" parameter -
 * upgrades are performed only within the same major version.
 * /*w  w w  . ja  va  2  s  .co m*/
 * @param newFolder
 *            The folder we want to upgrade to (the new version)
 * @return true if upgrade was successful, false otherwise
 */
public boolean upgrade(FileObject newFolder) {
    try {
        if (newFolder.getChildren().length != 0) {
            // if the folder is not new then we don't want to touch it
            return false;
        }

        final FileObject upgradeFromFolderCandidate = findUpgradeCandidate(newFolder);

        if (upgradeFromFolderCandidate == null) {
            logger.info("Did not find a suitable upgrade candidate");
            return false;
        }

        logger.info("Upgrading DATACLEANER_HOME from : {}", upgradeFromFolderCandidate);
        newFolder.copyFrom(upgradeFromFolderCandidate, new AllFileSelector());

        // special handling of userpreferences.dat - we only want to keep
        // the good parts ;-)
        final UserPreferencesUpgrader userPreferencesUpgrader = new UserPreferencesUpgrader(newFolder);
        userPreferencesUpgrader.upgrade();

        // Overwrite example jobs
        final List<String> allFilePaths = DemoConfiguration.getAllFilePaths();
        for (String filePath : allFilePaths) {
            overwriteFileWithDefaults(newFolder, filePath);
        }
        return true;
    } catch (FileSystemException e) {
        logger.warn("Exception occured during upgrading: {}", e);
        return false;
    }
}

From source file:org.geoserver.backuprestore.utils.BackupUtils.java

/**
 * Extracts the archive file {@code archiveFile} to {@code targetFolder}; both shall previously exist.
 *
 * @param archiveFile/*  w  w  w.  ja  v a 2 s.  c  o m*/
 * @param targetFolder
 * @throws IOException
 */
public static void extractTo(Resource archiveFile, Resource targetFolder) throws IOException {
    FileSystemManager manager = VFS.getManager();
    String sourceURI = resolveArchiveURI(archiveFile);

    FileObject source = manager.resolveFile(sourceURI);
    if (manager.canCreateFileSystem(source)) {
        source = manager.createFileSystem(source);
    }
    FileObject target = manager
            .createVirtualFileSystem(manager.resolveFile(targetFolder.dir().getAbsolutePath()));

    FileSelector selector = new AllFileSelector() {
        @Override
        public boolean includeFile(FileSelectInfo fileInfo) {
            LOGGER.fine("Uncompressing " + fileInfo.getFile().getName().getFriendlyURI());
            return true;
        }
    };
    target.copyFrom(source, selector);
    source.close();
    target.close();
    manager.closeFileSystem(source.getFileSystem());
}

From source file:org.jfunktor.common.vfs.VirtualFileSystem.java

public static boolean extractContentsTo(URL fromLocation, URL toLocation)
        throws FileNotFoundException, VFSException {
    FileObject fromFile = null;// ww  w .  j a v a 2s  .c  om
    FileObject toFile = null;
    boolean retVal = false;
    if (exists(fromLocation)) {
        log.debug(fromLocation + "Exists");
        toFile = resolveFile(toLocation);
        fromFile = resolveFile(fromLocation);
        // now extract or copy the contents and children to the destination

        try {
            log.debug("About to copy from " + fromFile + " to " + toFile);
            toFile.copyFrom(fromFile, new AllFileSelector());
            retVal = true;
        } catch (FileSystemException e) {
            e.printStackTrace();
        }
    } else {
        log.debug("From Location " + fromLocation + " does not exist. Please check if it is a valid path");
        retVal = false;
    }

    return retVal;
}

From source file:org.kalypso.commons.io.VFSUtilities.java

/**
 * This function deletes the given file. If the file object is a directory, all content and the directory itself will
 * be deleted./*from  w w  w  . j a  v  a2  s .co  m*/
 *
 * @param toDel
 *          The file or directory to be deleted.
 * @return The number of deleted files. 0, if none has been deleted.
 */
public static int deleteFiles(final FileObject toDel) throws FileSystemException {
    if (FileType.FOLDER.equals(toDel.getType())) {
        /* Delete the directory. */
        KalypsoCommonsDebug.DEBUG.printf("Deleting the directory %s ...%n", toDel.getName()); //$NON-NLS-1$
        return toDel.delete(new AllFileSelector());
    } else if (FileType.FILE.equals(toDel.getType())) {
        /* Delete the file. */
        KalypsoCommonsDebug.DEBUG.printf("Deleting the file %s ...%n", toDel.getName()); //$NON-NLS-1$
        if (toDel.delete())
            return 1;

        KalypsoCommonsDebug.DEBUG.printf("Could not delete %s!%n", toDel.getName()); //$NON-NLS-1$

        return 0;
    } else {
        /* The type of the file could not be determined, or it is an imaginary one. */
        KalypsoCommonsDebug.DEBUG.printf("Could not delete %s!%n", toDel.getName()); //$NON-NLS-1$

        return 0;
    }
}

From source file:org.kalypso.commons.io.VFSUtilities.java

/**
 * Moves the complete content of one directory into another.
 *
 * @throws IOException//from   w ww.j  a  va2  s  .  c  o  m
 *           If the move failed.
 */
public static void moveContents(final File sourceDir, final File dest) throws IOException {
    final FileSystemManager vfsManager = VFSUtilities.getManager();
    final FileObject source = vfsManager.toFileObject(sourceDir);
    final FileObject destDir = vfsManager.toFileObject(dest);

    final FileObject[] findFiles = source.findFiles(new AllFileSelector());
    // Might happen, if source does not exists... shouldn't we check this?
    if (findFiles == null)
        return;

    for (final FileObject fileObject : findFiles) {
        if (FileType.FILE.equals(fileObject.getType())) {
            final String relPath = source.getName().getRelativeName(fileObject.getName());
            final FileObject destFile = destDir.resolveFile(relPath, NameScope.DESCENDENT_OR_SELF);
            final FileObject folder = destFile.getParent();
            folder.createFolder();
            fileObject.moveTo(destFile);
        }
    }
}

From source file:org.pentaho.di.job.entries.hadoopjobexecutor.JarUtilityTest.java

@BeforeClass
public static void setup() throws Exception {
    FileObject testPath = VFS.getManager().resolveFile(TEST_PATH);
    testPath.delete(new AllFileSelector());
    testPath.createFolder();//from  www. ja  va 2s.c om
}

From source file:org.pentaho.hadoop.shim.common.DistributedCacheUtilImpl.java

/**
 * Delete a directory and all of its contents
 *
 * @param dir Directory to delete//from  ww w.  j a  v a  2  s .  com
 * @return True if the directory was deleted successfully
 */
public boolean deleteDirectory(FileObject dir) throws FileSystemException {
    dir.delete(new AllFileSelector());
    return !dir.exists();
}