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

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

Introduction

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

Prototype

public FileObject[] findFiles(FileSelector selector) throws FileSystemException;

Source Link

Document

Finds the set of matching descendents of this file, in depthwise order.

Usage

From source file:org.pentaho.di.job.entries.dostounix.JobEntryDosToUnix.java

private boolean processFileFolder(String sourcefilefoldername, String wildcard, int convertion, Job parentJob,
        Result result) {/*from w w  w.  j a v  a  2 s  .com*/
    boolean entrystatus = false;
    FileObject sourcefilefolder = null;
    FileObject CurrentFile = null;

    // Get real source file and wilcard
    String realSourceFilefoldername = environmentSubstitute(sourcefilefoldername);
    if (Const.isEmpty(realSourceFilefoldername)) {
        logError(BaseMessages.getString(PKG, "JobDosToUnix.log.FileFolderEmpty", sourcefilefoldername));
        // Update Errors
        updateErrors();

        return entrystatus;
    }
    String realWildcard = environmentSubstitute(wildcard);

    try {
        sourcefilefolder = KettleVFS.getFileObject(realSourceFilefoldername);

        if (sourcefilefolder.exists()) {
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobDosToUnix.Log.FileExists",
                        sourcefilefolder.toString()));
            }
            if (sourcefilefolder.getType() == FileType.FILE) {
                entrystatus = convertOneFile(sourcefilefolder, convertion, result, parentJob);

            } else if (sourcefilefolder.getType() == FileType.FOLDER) {
                FileObject[] fileObjects = sourcefilefolder.findFiles(new AllFileSelector() {
                    public boolean traverseDescendents(FileSelectInfo info) {
                        return info.getDepth() == 0 || include_subfolders;
                    }

                    public boolean includeFile(FileSelectInfo info) {

                        FileObject fileObject = info.getFile();
                        try {
                            if (fileObject == null) {
                                return false;
                            }
                            if (fileObject.getType() != FileType.FILE) {
                                return false;
                            }
                        } catch (Exception ex) {
                            // Upon error don't process the file.
                            return false;
                        } finally {
                            if (fileObject != null) {
                                try {
                                    fileObject.close();
                                } catch (IOException ex) { /* Ignore */
                                }
                            }

                        }
                        return true;
                    }
                });

                if (fileObjects != null) {
                    for (int j = 0; j < fileObjects.length && !parentJob.isStopped(); j++) {
                        if (successConditionBroken) {
                            if (!successConditionBrokenExit) {
                                logError(BaseMessages.getString(PKG,
                                        "JobDosToUnix.Error.SuccessConditionbroken", "" + nrAllErrors));
                                successConditionBrokenExit = true;
                            }
                            return false;
                        }
                        // Fetch files in list one after one ...
                        CurrentFile = fileObjects[j];

                        if (!CurrentFile.getParent().toString().equals(sourcefilefolder.toString())) {
                            // Not in the Base Folder..Only if include sub folders
                            if (include_subfolders) {
                                if (GetFileWildcard(CurrentFile.toString(), realWildcard)) {
                                    convertOneFile(CurrentFile, convertion, result, parentJob);
                                }
                            }

                        } else {
                            // In the base folder
                            if (GetFileWildcard(CurrentFile.toString(), realWildcard)) {
                                convertOneFile(CurrentFile, convertion, result, parentJob);
                            }
                        }
                    }
                }
            } else {
                logError(BaseMessages.getString(PKG, "JobDosToUnix.Error.UnknowFileFormat",
                        sourcefilefolder.toString()));
                // Update Errors
                updateErrors();
            }
        } else {
            logError(BaseMessages.getString(PKG, "JobDosToUnix.Error.SourceFileNotExists",
                    realSourceFilefoldername));
            // Update Errors
            updateErrors();
        }
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobDosToUnix.Error.Exception.Processing",
                realSourceFilefoldername.toString(), e.getMessage()));
        // Update Errors
        updateErrors();
    } finally {
        if (sourcefilefolder != null) {
            try {
                sourcefilefolder.close();
            } catch (IOException ex) {
                /* Ignore */
            }

        }
        if (CurrentFile != null) {
            try {
                CurrentFile.close();
            } catch (IOException ex) { /* Ignore */
            }
        }
    }
    return entrystatus;
}

From source file:org.pentaho.di.job.entries.evalfilesmetrics.JobEntryEvalFilesMetrics.java

private void ProcessFileFolder(String sourcefilefoldername, String wildcard, String includeSubfolders,
        Job parentJob, Result result) {

    FileObject sourcefilefolder = null;
    FileObject CurrentFile = null;

    // Get real source file and wildcard
    String realSourceFilefoldername = environmentSubstitute(sourcefilefoldername);
    if (Const.isEmpty(realSourceFilefoldername)) {
        // Filename is empty!
        logError(BaseMessages.getString(PKG, "JobEvalFilesMetrics.log.FileFolderEmpty"));
        incrementErrors();//from www .  java  2s.c o  m
        return;
    }
    String realWildcard = environmentSubstitute(wildcard);
    final boolean include_subfolders = YES.equalsIgnoreCase(includeSubfolders);

    try {
        sourcefilefolder = KettleVFS.getFileObject(realSourceFilefoldername);

        if (sourcefilefolder.exists()) {
            // File exists
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Log.FileExists",
                        sourcefilefolder.toString()));
            }

            if (sourcefilefolder.getType() == FileType.FILE) {
                // We deals here with a file
                // let's get file size
                getFileSize(sourcefilefolder, result, parentJob);

            } else if (sourcefilefolder.getType() == FileType.FOLDER) {
                // We have a folder
                // we will fetch and extract files
                FileObject[] fileObjects = sourcefilefolder.findFiles(new AllFileSelector() {
                    public boolean traverseDescendents(FileSelectInfo info) {
                        return info.getDepth() == 0 || include_subfolders;
                    }

                    public boolean includeFile(FileSelectInfo info) {
                        FileObject fileObject = info.getFile();
                        try {
                            if (fileObject == null) {
                                return false;
                            }
                            if (fileObject.getType() != FileType.FILE) {
                                return false;
                            }
                        } catch (Exception ex) {
                            // Upon error don't process the file.
                            return false;
                        } finally {
                            if (fileObject != null) {
                                try {
                                    fileObject.close();
                                } catch (IOException ex) { /* Ignore */
                                }
                            }
                        }
                        return true;
                    }
                });

                if (fileObjects != null) {
                    for (int j = 0; j < fileObjects.length && !parentJob.isStopped(); j++) {
                        // Fetch files in list one after one ...
                        CurrentFile = fileObjects[j];

                        if (!CurrentFile.getParent().toString().equals(sourcefilefolder.toString())) {
                            // Not in the Base Folder..Only if include sub folders
                            if (include_subfolders) {
                                if (GetFileWildcard(CurrentFile.getName().getBaseName(), realWildcard)) {
                                    getFileSize(CurrentFile, result, parentJob);
                                }
                            }
                        } else {
                            // In the base folder
                            if (GetFileWildcard(CurrentFile.getName().getBaseName(), realWildcard)) {
                                getFileSize(CurrentFile, result, parentJob);
                            }
                        }
                    }
                }
            } else {
                incrementErrors();
                logError(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Error.UnknowFileFormat",
                        sourcefilefolder.toString()));
            }
        } else {
            incrementErrors();
            logError(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Error.SourceFileNotExists",
                    realSourceFilefoldername));
        }
    } catch (Exception e) {
        incrementErrors();
        logError(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Error.Exception.Processing",
                realSourceFilefoldername.toString(), e.getMessage()));

    } finally {
        if (sourcefilefolder != null) {
            try {
                sourcefilefolder.close();
            } catch (IOException ex) { /* Ignore */
            }

        }
        if (CurrentFile != null) {
            try {
                CurrentFile.close();
            } catch (IOException ex) { /* Ignore */
            }
        }
    }
}

From source file:org.pentaho.di.job.entries.folderisempty.JobEntryFolderIsEmpty.java

public Result execute(Result previousResult, int nr) {
    // see PDI-10270 for details
    boolean oldBehavior = "Y"
            .equalsIgnoreCase(getVariable(Const.KETTLE_COMPATIBILITY_SET_ERROR_ON_SPECIFIC_JOB_ENTRIES, "N"));

    Result result = previousResult;
    result.setResult(false);//w ww .j ava  2s . c o m
    result.setNrErrors(oldBehavior ? 1 : 0);

    filescount = 0;
    folderscount = 0;
    pattern = null;

    if (!Const.isEmpty(getWildcard())) {
        pattern = Pattern.compile(getRealWildcard());
    }

    if (foldername != null) {
        String realFoldername = getRealFoldername();
        FileObject folderObject = null;
        try {
            folderObject = KettleVFS.getFileObject(realFoldername, this);
            if (folderObject.exists()) {
                // Check if it's a folder
                if (folderObject.getType() == FileType.FOLDER) {
                    // File provided is a folder, so we can process ...
                    try {
                        folderObject.findFiles(new TextFileSelector(folderObject.toString()));
                    } catch (Exception ex) {
                        if (!(ex.getCause() instanceof ExpectedException)) {
                            throw ex;
                        }
                    }
                    if (log.isBasic()) {
                        log.logBasic("Total files", "We found : " + filescount + " file(s)");
                    }
                    if (filescount == 0) {
                        result.setResult(true);
                        result.setNrLinesInput(folderscount);
                    }
                } else {
                    // Not a folder, fail
                    log.logError("[" + realFoldername + "] is not a folder, failing.");
                    result.setNrErrors(1);
                }
            } else {
                // No Folder found
                if (log.isBasic()) {
                    logBasic("we can not find [" + realFoldername + "] !");
                }
                result.setNrErrors(1);
            }
        } catch (Exception e) {
            logError("Error checking folder [" + realFoldername + "]", e);
            result.setResult(false);
            result.setNrErrors(1);
        } finally {
            if (folderObject != null) {
                try {
                    folderObject.close();
                    folderObject = null;
                } catch (IOException ex) { /* Ignore */
                }
            }
        }
    } else {
        logError("No Foldername is defined.");
        result.setNrErrors(1);
    }

    return result;
}

From source file:org.pentaho.di.job.entries.folderscompare.JobEntryFoldersCompare.java

public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    result.setResult(false);/*from   w ww.  ja  v a2s  . c om*/
    boolean ok = true;

    String realFilename1 = getRealFilename1();
    String realFilename2 = getRealFilename2();

    FileObject folder1 = null;
    FileObject folder2 = null;
    FileObject filefolder1 = null;
    FileObject filefolder2 = null;

    try {
        if (filename1 != null && filename2 != null) {
            // Get Folders/Files to compare
            folder1 = KettleVFS.getFileObject(realFilename1, this);
            folder2 = KettleVFS.getFileObject(realFilename2, this);

            if (folder1.exists() && folder2.exists()) {
                if (!folder1.getType().equals(folder2.getType())) {
                    // pb...we try to compare file with folder !!!
                    logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.CanNotCompareFilesFolders"));

                    if (folder1.getType() == FileType.FILE) {
                        logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.IsAFile", realFilename1));
                    } else if (folder1.getType() == FileType.FOLDER) {
                        logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.IsAFolder", realFilename1));
                    } else {
                        logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.IsUnknownFileType",
                                realFilename1));
                    }

                    if (folder2.getType() == FileType.FILE) {
                        logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.IsAFile", realFilename2));
                    } else if (folder2.getType() == FileType.FOLDER) {
                        logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.IsAFolder", realFilename2));
                    } else {
                        logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.IsUnknownFileType",
                                realFilename2));
                    }

                } else {
                    if (folder1.getType() == FileType.FILE) {
                        // simply compare 2 files ..
                        if (equalFileContents(folder1, folder2)) {
                            result.setResult(true);
                        } else {
                            result.setResult(false);
                        }
                    } else if (folder1.getType() == FileType.FOLDER) {
                        // We compare 2 folders ...

                        FileObject[] list1 = folder1.findFiles(new TextFileSelector(folder1.toString()));
                        FileObject[] list2 = folder2.findFiles(new TextFileSelector(folder2.toString()));

                        int lenList1 = list1.length;
                        int lenList2 = list2.length;

                        if (log.isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobFoldersCompare.Log.FolderContains",
                                    realFilename1, "" + lenList1));
                            logDetailed(BaseMessages.getString(PKG, "JobFoldersCompare.Log.FolderContains",
                                    realFilename2, "" + lenList2));
                        }
                        if (lenList1 == lenList2) {

                            HashMap<String, String> collection1 = new HashMap<String, String>();
                            HashMap<String, String> collection2 = new HashMap<String, String>();

                            for (int i = 0; i < list1.length; i++) {
                                // Put files list1 in TreeMap collection1
                                collection1.put(list1[i].getName().getBaseName(), list1[i].toString());
                            }

                            for (int i = 0; i < list2.length; i++) {
                                // Put files list2 in TreeMap collection2
                                collection2.put(list2[i].getName().getBaseName(), list2[i].toString());
                            }

                            // Let's now fetch Folder1
                            // and for each entry, we will search it in Folder2
                            // if the entry exists..we will compare file entry (file or folder?)
                            // if the 2 entry are file (not folder), we will compare content
                            Set<Map.Entry<String, String>> entrees = collection1.entrySet();
                            Iterator<Map.Entry<String, String>> iterateur = entrees.iterator();

                            while (iterateur.hasNext()) {
                                Map.Entry<String, String> entree = iterateur.next();
                                if (!collection2.containsKey(entree.getKey())) {
                                    ok = false;
                                    if (log.isDetailed()) {
                                        logDetailed(BaseMessages.getString(PKG,
                                                "JobFoldersCompare.Log.FileCanNotBeFoundIn",
                                                entree.getKey().toString(), realFilename2));
                                    }
                                } else {
                                    if (log.isDebug()) {
                                        logDebug(BaseMessages.getString(PKG,
                                                "JobFoldersCompare.Log.FileIsFoundIn",
                                                entree.getKey().toString(), realFilename2));
                                    }

                                    filefolder1 = KettleVFS.getFileObject(entree.getValue().toString(), this);
                                    filefolder2 = KettleVFS
                                            .getFileObject(collection2.get(entree.getKey()).toString(), this);

                                    if (!filefolder2.getType().equals(filefolder1.getType())) {
                                        // The file1 exist in the folder2..but they don't have the same type
                                        ok = false;
                                        if (log.isDetailed()) {
                                            logDetailed(BaseMessages.getString(PKG,
                                                    "JobFoldersCompare.Log.FilesNotSameType",
                                                    filefolder1.toString(), filefolder2.toString()));
                                        }

                                        if (filefolder1.getType() == FileType.FILE) {
                                            logError(BaseMessages.getString(PKG,
                                                    "JobFoldersCompare.Log.IsAFile", filefolder1.toString()));
                                        } else if (filefolder1.getType() == FileType.FOLDER) {
                                            logError(BaseMessages.getString(PKG,
                                                    "JobFoldersCompare.Log.IsAFolder", filefolder1.toString()));
                                        } else {
                                            logError(BaseMessages.getString(PKG,
                                                    "JobFoldersCompare.Log.IsUnknownFileType",
                                                    filefolder1.toString()));
                                        }

                                        if (filefolder2.getType() == FileType.FILE) {
                                            logError(BaseMessages.getString(PKG,
                                                    "JobFoldersCompare.Log.IsAFile", filefolder2.toString()));
                                        } else if (filefolder2.getType() == FileType.FOLDER) {
                                            logError(BaseMessages.getString(PKG,
                                                    "JobFoldersCompare.Log.IsAFolder", filefolder2.toString()));
                                        } else {
                                            logError(BaseMessages.getString(PKG,
                                                    "JobFoldersCompare.Log.IsUnknownFileType",
                                                    filefolder2.toString()));
                                        }

                                    } else {
                                        // Files are the same type ...
                                        if (filefolder2.getType() == FileType.FILE) {
                                            // Let's compare file size
                                            if (comparefilesize) {
                                                long filefolder1_size = filefolder1.getContent().getSize();
                                                long filefolder2_size = filefolder2.getContent().getSize();
                                                if (filefolder1_size != filefolder2_size) {
                                                    ok = false;
                                                    if (log.isDetailed()) {
                                                        logDetailed(BaseMessages.getString(PKG,
                                                                "JobFoldersCompare.Log.FilesNotSameSize",
                                                                filefolder1.toString(),
                                                                filefolder2.toString()));
                                                        logDetailed(BaseMessages.getString(PKG,
                                                                "JobFoldersCompare.Log.SizeFileIs",
                                                                filefolder1.toString(), "" + filefolder1_size));
                                                        logDetailed(BaseMessages.getString(PKG,
                                                                "JobFoldersCompare.Log.SizeFileIs",
                                                                filefolder2.toString(), "" + filefolder2_size));
                                                    }
                                                }
                                            }

                                            if (ok) {
                                                // Let's compare files content..
                                                if (comparefilecontent) {
                                                    if (!equalFileContents(filefolder1, filefolder2)) {
                                                        ok = false;
                                                        if (log.isDetailed()) {
                                                            logDetailed(BaseMessages.getString(PKG,
                                                                    "JobFoldersCompare.Log.FilesNotSameContent",
                                                                    filefolder1.toString(),
                                                                    filefolder2.toString()));
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }

                                }
                                // logBasic(entree.getKey() + " - " + entree.getValue());
                            }

                            result.setResult(ok);
                        } else {
                            // The 2 folders don't have the same files number
                            if (log.isDetailed()) {
                                logDetailed(BaseMessages.getString(PKG,
                                        "JobFoldersCompare.Log.FoldersDifferentFiles", realFilename1.toString(),
                                        realFilename2.toString()));
                            }
                        }

                    }
                    // else: File type unknown !!
                }

            } else {
                if (!folder1.exists()) {
                    logError(BaseMessages.getString(PKG, "JobFileCompare.Log.FileNotExist", realFilename1));
                }
                if (!folder2.exists()) {
                    logError(BaseMessages.getString(PKG, "JobFileCompare.Log.FileNotExist", realFilename2));
                }
                result.setResult(false);
                result.setNrErrors(1);
            }
        } else {
            logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.Need2Files"));
        }
    } catch (Exception e) {
        result.setResult(false);
        result.setNrErrors(1);
        logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.ErrorComparing", realFilename2,
                realFilename2, e.getMessage()));
    } finally {
        try {
            if (folder1 != null) {
                folder1.close();
                folder1 = null;
            }
            if (folder2 != null) {
                folder2.close();
                folder2 = null;
            }
            if (filefolder1 != null) {
                filefolder1.close();
                filefolder1 = null;
            }
            if (filefolder2 != null) {
                filefolder2.close();
                filefolder2 = null;
            }
        } catch (IOException e) {
            // Ignore errors
        }
    }

    return result;
}

From source file:org.pentaho.di.job.entries.hadooptransjobexecutor.DistributedCacheUtil.java

/**
 * Recursively searches for all files starting at the directory provided with the extension provided. If no extension
 * is provided all files will be returned.
 *
 * @param root      Directory to start the search for files in
 * @param extension File extension to search for. If null all files will be returned.
 * @return List of absolute path names to all files found in {@code dir} and its subdirectories.
 * @throws KettleFileException// w  w  w .  j av  a2s .co  m
 * @throws FileSystemException
 */
public List<String> findFiles(FileObject root, final String extension) throws FileSystemException {
    FileObject[] files = root.findFiles(new FileSelector() {
        @Override
        public boolean includeFile(FileSelectInfo fileSelectInfo) throws Exception {
            return extension == null || extension.equals(fileSelectInfo.getFile().getName().getExtension());
        }

        @Override
        public boolean traverseDescendents(FileSelectInfo fileSelectInfo) throws Exception {
            return FileType.FOLDER.equals(fileSelectInfo.getFile().getType());
        }
    });

    if (files == null) {
        return Collections.EMPTY_LIST;
    }

    List<String> paths = new ArrayList<String>();
    for (FileObject file : files) {
        try {
            paths.add(file.getURL().toURI().getPath());
        } catch (URISyntaxException ex) {
            throw new FileSystemException("Error getting URI of file: " + file.getURL().getPath());
        }
    }
    return paths;
}

From source file:org.pentaho.di.job.entries.hadooptransjobexecutor.DistributedCacheUtil.java

/**
 * Attempts to find a plugin's installation folder on disk within all known plugin folder locations
 *
 * @param pluginFolderName Name of plugin folder
 * @return Location of the first plugin folder found as a direct descendant of one of the known plugin folder locations
 * @throws KettleFileException Error getting plugin folders
 *///from   ww w. j  a va 2 s.  c o m
public FileObject findPluginFolder(final String pluginFolderName) throws KettleFileException {
    List<PluginFolderInterface> pluginFolders = PluginFolder.populateFolders(null);
    if (pluginFolders != null) {
        for (PluginFolderInterface pluginFolder : pluginFolders) {
            FileObject folder = KettleVFS.getFileObject(pluginFolder.getFolder());

            try {
                if (folder.exists()) {
                    FileObject[] files = folder.findFiles(new FileSelector() {
                        @Override
                        public boolean includeFile(FileSelectInfo fileSelectInfo) throws Exception {
                            if (fileSelectInfo.getFile().equals(fileSelectInfo.getBaseFolder())) {
                                // Do not consider the base folders
                                return false;
                            }
                            // Determine relative name to compare
                            int baseNameLength = fileSelectInfo.getBaseFolder().getName().getPath().length()
                                    + 1;
                            String relativeName = fileSelectInfo.getFile().getName().getPath()
                                    .substring(baseNameLength);
                            // Compare plugin folder name with the relative name
                            return pluginFolderName.equals(relativeName);
                        }

                        @Override
                        public boolean traverseDescendents(FileSelectInfo fileSelectInfo) throws Exception {
                            return true;
                        }
                    });
                    if (files != null && files.length > 0) {
                        return files[0]; // Return the first match
                    }
                }
            } catch (FileSystemException ex) {
                throw new KettleFileException("Error searching for folder '" + pluginFolderName + "'", ex);
            }
        }
    }
    return null;
}

From source file:org.pentaho.di.job.entries.hadooptransjobexecutor.DistributedCacheUtilTest.java

@Test
public void extractToTemp() throws Exception {
    DistributedCacheUtil ch = new DistributedCacheUtil();

    FileObject archive = KettleVFS.getFileObject("test-res/pentaho-mapreduce-sample.jar");
    FileObject extracted = ch.extractToTemp(archive);

    assertNotNull(extracted);//from www.  j  a va  2s.com
    assertTrue(extracted.exists());
    try {
        // There should be 3 files and 5 directories inside the root folder (which is the 9th entry)
        assertTrue(extracted.findFiles(new AllFileSelector()).length == 9);
    } finally {
        // clean up after ourself
        ch.deleteDirectory(extracted);
    }
}

From source file:org.pentaho.di.job.entries.movefiles.JobEntryMoveFiles.java

private boolean ProcessFileFolder(String sourcefilefoldername, String destinationfilefoldername,
        String wildcard, Job parentJob, Result result, String MoveToFolder) {
    boolean entrystatus = false;
    FileObject sourcefilefolder = null;
    FileObject destinationfilefolder = null;
    FileObject movetofolderfolder = null;
    FileObject Currentfile = null;

    // Get real source, destination file and wildcard
    String realSourceFilefoldername = environmentSubstitute(sourcefilefoldername);
    String realDestinationFilefoldername = environmentSubstitute(destinationfilefoldername);
    String realWildcard = environmentSubstitute(wildcard);

    try {/*  ww w . jav  a2 s. com*/
        sourcefilefolder = KettleVFS.getFileObject(realSourceFilefoldername, this);
        destinationfilefolder = KettleVFS.getFileObject(realDestinationFilefoldername, this);
        if (!Const.isEmpty(MoveToFolder)) {
            movetofolderfolder = KettleVFS.getFileObject(MoveToFolder, this);
        }

        if (sourcefilefolder.exists()) {

            // Check if destination folder/parent folder exists !
            // If user wanted and if destination folder does not exist
            // PDI will create it
            if (CreateDestinationFolder(destinationfilefolder)) {

                // Basic Tests
                if (sourcefilefolder.getType().equals(FileType.FOLDER) && destination_is_a_file) {
                    // Source is a folder, destination is a file
                    // WARNING !!! CAN NOT MOVE FOLDER TO FILE !!!

                    log.logError(BaseMessages.getString(PKG, "JobMoveFiles.Log.Forbidden"),
                            BaseMessages.getString(PKG, "JobMoveFiles.Log.CanNotMoveFolderToFile",
                                    realSourceFilefoldername, realDestinationFilefoldername));

                    // Update Errors
                    updateErrors();
                } else {
                    if (destinationfilefolder.getType().equals(FileType.FOLDER)
                            && sourcefilefolder.getType().equals(FileType.FILE)) {
                        // Source is a file, destination is a folder
                        // return destination short filename
                        String shortfilename = sourcefilefolder.getName().getBaseName();

                        try {
                            shortfilename = getDestinationFilename(shortfilename);
                        } catch (Exception e) {
                            logError(BaseMessages.getString(PKG,
                                    BaseMessages.getString(PKG, "JobMoveFiles.Error.GettingFilename",
                                            sourcefilefolder.getName().getBaseName(), e.toString())));
                            return entrystatus;
                        }
                        // Move the file to the destination folder

                        String destinationfilenamefull = KettleVFS.getFilename(destinationfilefolder)
                                + Const.FILE_SEPARATOR + shortfilename;
                        FileObject destinationfile = KettleVFS.getFileObject(destinationfilenamefull, this);

                        entrystatus = MoveFile(shortfilename, sourcefilefolder, destinationfile,
                                movetofolderfolder, parentJob, result);
                        return entrystatus;
                    } else if (sourcefilefolder.getType().equals(FileType.FILE) && destination_is_a_file) {
                        // Source is a file, destination is a file

                        FileObject destinationfile = KettleVFS.getFileObject(realDestinationFilefoldername,
                                this);

                        // return destination short filename
                        String shortfilename = destinationfile.getName().getBaseName();
                        try {
                            shortfilename = getDestinationFilename(shortfilename);
                        } catch (Exception e) {
                            logError(BaseMessages.getString(PKG,
                                    BaseMessages.getString(PKG, "JobMoveFiles.Error.GettingFilename",
                                            sourcefilefolder.getName().getBaseName(), e.toString())));
                            return entrystatus;
                        }

                        String destinationfilenamefull = KettleVFS.getFilename(destinationfile.getParent())
                                + Const.FILE_SEPARATOR + shortfilename;
                        destinationfile = KettleVFS.getFileObject(destinationfilenamefull, this);

                        entrystatus = MoveFile(shortfilename, sourcefilefolder, destinationfile,
                                movetofolderfolder, parentJob, result);
                        return entrystatus;
                    } else {
                        // Both source and destination are folders
                        if (log.isDetailed()) {
                            logDetailed("  ");
                            logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.FetchFolder",
                                    sourcefilefolder.toString()));
                        }

                        FileObject[] fileObjects = sourcefilefolder.findFiles(new AllFileSelector() {
                            public boolean traverseDescendents(FileSelectInfo info) {
                                return true;
                            }

                            public boolean includeFile(FileSelectInfo info) {
                                FileObject fileObject = info.getFile();
                                try {
                                    if (fileObject == null) {
                                        return false;
                                    }
                                } catch (Exception ex) {
                                    // Upon error don't process the file.
                                    return false;
                                } finally {
                                    if (fileObject != null) {
                                        try {
                                            fileObject.close();
                                        } catch (IOException ex) { /* Ignore */
                                        }
                                    }

                                }
                                return true;
                            }
                        });

                        if (fileObjects != null) {
                            for (int j = 0; j < fileObjects.length && !parentJob.isStopped(); j++) {
                                // Success condition broken?
                                if (successConditionBroken) {
                                    if (!successConditionBrokenExit) {
                                        logError(BaseMessages.getString(PKG,
                                                "JobMoveFiles.Error.SuccessConditionbroken", "" + NrErrors));
                                        successConditionBrokenExit = true;
                                    }
                                    return false;
                                }
                                // Fetch files in list one after one ...
                                Currentfile = fileObjects[j];

                                if (!MoveOneFile(Currentfile, sourcefilefolder, realDestinationFilefoldername,
                                        realWildcard, parentJob, result, movetofolderfolder)) {
                                    // Update Errors
                                    updateErrors();
                                }

                            }
                        }
                    }

                }
                entrystatus = true;
            } else {
                // Destination Folder or Parent folder is missing
                logError(BaseMessages.getString(PKG, "JobMoveFiles.Error.DestinationFolderNotFound",
                        realDestinationFilefoldername));
            }
        } else {
            logError(BaseMessages.getString(PKG, "JobMoveFiles.Error.SourceFileNotExists",
                    realSourceFilefoldername));
        }
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobMoveFiles.Error.Exception.MoveProcess",
                realSourceFilefoldername.toString(), destinationfilefolder.toString(), e.getMessage()));
    } finally {
        if (sourcefilefolder != null) {
            try {
                sourcefilefolder.close();
            } catch (IOException ex) {
                /* Ignore */
            }
        }
        if (destinationfilefolder != null) {
            try {
                destinationfilefolder.close();
            } catch (IOException ex) {
                /* Ignore */
            }
        }
        if (Currentfile != null) {
            try {
                Currentfile.close();
            } catch (IOException ex) {
                /* Ignore */
            }
        }
        if (movetofolderfolder != null) {
            try {
                movetofolderfolder.close();
            } catch (IOException ex) {
                /* Ignore */
            }
        }
    }
    return entrystatus;
}

From source file:org.pentaho.di.job.entries.pgpdecryptfiles.JobEntryPGPDecryptFiles.java

private boolean ProcessFileFolder(String sourcefilefoldername, String passPhrase,
        String destinationfilefoldername, String wildcard, Job parentJob, Result result, String MoveToFolder) {
    boolean entrystatus = false;
    FileObject sourcefilefolder = null;
    FileObject destinationfilefolder = null;
    FileObject movetofolderfolder = null;
    FileObject Currentfile = null;

    // Get real source, destination file and wildcard
    String realSourceFilefoldername = environmentSubstitute(sourcefilefoldername);
    String realDestinationFilefoldername = environmentSubstitute(destinationfilefoldername);
    String realWildcard = environmentSubstitute(wildcard);

    try {//from w  w  w.j  a v a 2 s.c  o m

        sourcefilefolder = KettleVFS.getFileObject(realSourceFilefoldername);
        destinationfilefolder = KettleVFS.getFileObject(realDestinationFilefoldername);
        if (!Const.isEmpty(MoveToFolder)) {
            movetofolderfolder = KettleVFS.getFileObject(MoveToFolder);
        }

        if (sourcefilefolder.exists()) {

            // Check if destination folder/parent folder exists !
            // If user wanted and if destination folder does not exist
            // PDI will create it
            if (CreateDestinationFolder(destinationfilefolder)) {

                // Basic Tests
                if (sourcefilefolder.getType().equals(FileType.FOLDER) && destination_is_a_file) {
                    // Source is a folder, destination is a file
                    // WARNING !!! CAN NOT MOVE FOLDER TO FILE !!!

                    logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.Forbidden"),
                            BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.CanNotMoveFolderToFile",
                                    realSourceFilefoldername, realDestinationFilefoldername));

                    // Update Errors
                    updateErrors();
                } else {
                    if (destinationfilefolder.getType().equals(FileType.FOLDER)
                            && sourcefilefolder.getType().equals(FileType.FILE)) {
                        // Source is a file, destination is a folder
                        // return destination short filename
                        String shortfilename = sourcefilefolder.getName().getBaseName();

                        try {
                            shortfilename = getDestinationFilename(sourcefilefolder.getName().getBaseName());
                        } catch (Exception e) {
                            logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Error.GettingFilename",
                                    sourcefilefolder.getName().getBaseName(), e.toString()));
                            return entrystatus;
                        }
                        // Move the file to the destination folder

                        String destinationfilenamefull = destinationfilefolder.toString() + Const.FILE_SEPARATOR
                                + shortfilename;
                        FileObject destinationfile = KettleVFS.getFileObject(destinationfilenamefull);

                        entrystatus = DecryptFile(shortfilename, sourcefilefolder, passPhrase, destinationfile,
                                movetofolderfolder, parentJob, result);

                    } else if (sourcefilefolder.getType().equals(FileType.FILE) && destination_is_a_file) {
                        // Source is a file, destination is a file

                        FileObject destinationfile = KettleVFS.getFileObject(realDestinationFilefoldername);

                        // return destination short filename
                        String shortfilename = destinationfile.getName().getBaseName();
                        try {
                            shortfilename = getDestinationFilename(destinationfile.getName().getBaseName());
                        } catch (Exception e) {
                            logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Error.GettingFilename",
                                    sourcefilefolder.getName().getBaseName(), e.toString()));
                            return entrystatus;
                        }

                        String destinationfilenamefull = destinationfilefolder.getParent().toString()
                                + Const.FILE_SEPARATOR + shortfilename;
                        destinationfile = KettleVFS.getFileObject(destinationfilenamefull);

                        entrystatus = DecryptFile(shortfilename, sourcefilefolder, passPhrase, destinationfile,
                                movetofolderfolder, parentJob, result);

                    } else {
                        // Both source and destination are folders
                        if (isDetailed()) {
                            logDetailed("  ");
                            logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.FetchFolder",
                                    sourcefilefolder.toString()));
                        }

                        FileObject[] fileObjects = sourcefilefolder.findFiles(new AllFileSelector() {
                            public boolean traverseDescendents(FileSelectInfo info) {
                                return info.getDepth() == 0 || include_subfolders;
                            }

                            public boolean includeFile(FileSelectInfo info) {
                                FileObject fileObject = info.getFile();
                                try {
                                    if (fileObject == null) {
                                        return false;
                                    }
                                } catch (Exception ex) {
                                    // Upon error don't process the file.
                                    return false;
                                } finally {
                                    if (fileObject != null) {
                                        try {
                                            fileObject.close();
                                            fileObject = null;
                                        } catch (IOException ex) { /* Ignore */
                                        }
                                    }
                                }
                                return true;
                            }
                        });

                        if (fileObjects != null) {
                            for (int j = 0; j < fileObjects.length && !parentJob.isStopped(); j++) {
                                // Success condition broken?
                                if (successConditionBroken) {
                                    if (!successConditionBrokenExit) {
                                        logError(BaseMessages.getString(PKG,
                                                "JobPGPDecryptFiles.Error.SuccessConditionbroken",
                                                "" + NrErrors));
                                        successConditionBrokenExit = true;
                                    }
                                    return false;
                                }
                                // Fetch files in list one after one ...
                                Currentfile = fileObjects[j];

                                if (!DecryptOneFile(Currentfile, sourcefilefolder, passPhrase,
                                        realDestinationFilefoldername, realWildcard, parentJob, result,
                                        movetofolderfolder)) {
                                    // Update Errors
                                    updateErrors();
                                }

                            }
                        }
                    }
                }
                entrystatus = true;
            } else {
                // Destination Folder or Parent folder is missing
                logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Error.DestinationFolderNotFound",
                        realDestinationFilefoldername));
            }
        } else {
            logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Error.SourceFileNotExists",
                    realSourceFilefoldername));
        }
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Error.Exception.MoveProcess",
                realSourceFilefoldername.toString(), destinationfilefolder.toString(), e.getMessage()));
        // Update Errors
        updateErrors();
    } finally {
        if (sourcefilefolder != null) {
            try {
                sourcefilefolder.close();
            } catch (IOException ex) { /* Ignore */
            }
        }
        if (destinationfilefolder != null) {
            try {
                destinationfilefolder.close();
            } catch (IOException ex) { /* Ignore */
            }
        }
        if (Currentfile != null) {
            try {
                Currentfile.close();
            } catch (IOException ex) { /* Ignore */
            }
        }
        if (movetofolderfolder != null) {
            try {
                movetofolderfolder.close();
            } catch (IOException ex) { /* Ignore */
            }
        }
    }
    return entrystatus;
}

From source file:org.pentaho.di.job.entries.pgpencryptfiles.JobEntryPGPEncryptFiles.java

private boolean ProcessFileFolder(int actionType, String sourcefilefoldername, String userID,
        String destinationfilefoldername, String wildcard, Job parentJob, Result result, String MoveToFolder) {
    boolean entrystatus = false;
    FileObject sourcefilefolder = null;
    FileObject destinationfilefolder = null;
    FileObject movetofolderfolder = null;
    FileObject Currentfile = null;

    // Get real source, destination file and wildcard
    String realSourceFilefoldername = environmentSubstitute(sourcefilefoldername);
    String realuserID = environmentSubstitute(userID);
    String realDestinationFilefoldername = environmentSubstitute(destinationfilefoldername);
    String realWildcard = environmentSubstitute(wildcard);

    try {//  w w  w  .jav  a2s .  c  om

        sourcefilefolder = KettleVFS.getFileObject(realSourceFilefoldername);
        destinationfilefolder = KettleVFS.getFileObject(realDestinationFilefoldername);
        if (!Const.isEmpty(MoveToFolder)) {
            movetofolderfolder = KettleVFS.getFileObject(MoveToFolder);
        }

        if (sourcefilefolder.exists()) {

            // Check if destination folder/parent folder exists !
            // If user wanted and if destination folder does not exist
            // PDI will create it
            if (CreateDestinationFolder(destinationfilefolder)) {

                // Basic Tests
                if (sourcefilefolder.getType().equals(FileType.FOLDER) && destination_is_a_file) {
                    // Source is a folder, destination is a file
                    // WARNING !!! CAN NOT MOVE FOLDER TO FILE !!!

                    logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.Forbidden"),
                            BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.CanNotMoveFolderToFile",
                                    realSourceFilefoldername, realDestinationFilefoldername));

                    // Update Errors
                    updateErrors();
                } else {
                    if (destinationfilefolder.getType().equals(FileType.FOLDER)
                            && sourcefilefolder.getType().equals(FileType.FILE)) {
                        // Source is a file, destination is a folder
                        // return destination short filename
                        String shortfilename = sourcefilefolder.getName().getBaseName();

                        try {
                            shortfilename = getDestinationFilename(sourcefilefolder.getName().getBaseName());
                        } catch (Exception e) {
                            logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Error.GettingFilename",
                                    sourcefilefolder.getName().getBaseName(), e.toString()));
                            return entrystatus;
                        }
                        // Move the file to the destination folder

                        String destinationfilenamefull = destinationfilefolder.toString() + Const.FILE_SEPARATOR
                                + shortfilename;
                        FileObject destinationfile = KettleVFS.getFileObject(destinationfilenamefull);

                        entrystatus = EncryptFile(actionType, shortfilename, sourcefilefolder, realuserID,
                                destinationfile, movetofolderfolder, parentJob, result);

                    } else if (sourcefilefolder.getType().equals(FileType.FILE) && destination_is_a_file) {

                        // Source is a file, destination is a file

                        FileObject destinationfile = KettleVFS.getFileObject(realDestinationFilefoldername);

                        // return destination short filename
                        String shortfilename = destinationfile.getName().getBaseName();
                        try {
                            shortfilename = getDestinationFilename(destinationfile.getName().getBaseName());
                        } catch (Exception e) {
                            logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Error.GettingFilename",
                                    sourcefilefolder.getName().getBaseName(), e.toString()));
                            return entrystatus;
                        }

                        String destinationfilenamefull = destinationfilefolder.getParent().toString()
                                + Const.FILE_SEPARATOR + shortfilename;
                        destinationfile = KettleVFS.getFileObject(destinationfilenamefull);

                        entrystatus = EncryptFile(actionType, shortfilename, sourcefilefolder, realuserID,
                                destinationfile, movetofolderfolder, parentJob, result);

                    } else {
                        // Both source and destination are folders
                        if (isDetailed()) {
                            logDetailed("  ");
                            logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.FetchFolder",
                                    sourcefilefolder.toString()));
                        }

                        FileObject[] fileObjects = sourcefilefolder.findFiles(new AllFileSelector() {
                            public boolean traverseDescendents(FileSelectInfo info) {
                                return info.getDepth() == 0 || include_subfolders;
                            }

                            public boolean includeFile(FileSelectInfo info) {
                                FileObject fileObject = info.getFile();
                                try {
                                    if (fileObject == null) {
                                        return false;
                                    }
                                } catch (Exception ex) {
                                    // Upon error don't process the file.
                                    return false;
                                } finally {
                                    if (fileObject != null) {
                                        try {
                                            fileObject.close();
                                            fileObject = null;
                                        } catch (IOException ex) { /* Ignore */
                                        }
                                    }
                                }
                                return true;
                            }
                        });

                        if (fileObjects != null) {
                            for (int j = 0; j < fileObjects.length && !parentJob.isStopped(); j++) {
                                // Success condition broken?
                                if (successConditionBroken) {
                                    if (!successConditionBrokenExit) {
                                        logError(BaseMessages.getString(PKG,
                                                "JobPGPEncryptFiles.Error.SuccessConditionbroken",
                                                "" + NrErrors));
                                        successConditionBrokenExit = true;
                                    }
                                    return false;
                                }
                                // Fetch files in list one after one ...
                                Currentfile = fileObjects[j];

                                if (!EncryptOneFile(actionType, Currentfile, sourcefilefolder, realuserID,
                                        realDestinationFilefoldername, realWildcard, parentJob, result,
                                        movetofolderfolder)) {
                                    // Update Errors
                                    updateErrors();
                                }

                            }
                        }
                    }
                }
                entrystatus = true;
            } else {
                // Destination Folder or Parent folder is missing
                logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Error.DestinationFolderNotFound",
                        realDestinationFilefoldername));
            }
        } else {
            logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Error.SourceFileNotExists",
                    realSourceFilefoldername));
        }
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Error.Exception.MoveProcess",
                realSourceFilefoldername.toString(), destinationfilefolder.toString(), e.getMessage()));
        // Update Errors
        updateErrors();
    } finally {
        if (sourcefilefolder != null) {
            try {
                sourcefilefolder.close();
            } catch (IOException ex) { /* Ignore */
            }
        }
        if (destinationfilefolder != null) {
            try {
                destinationfilefolder.close();
            } catch (IOException ex) { /* Ignore */
            }
        }
        if (Currentfile != null) {
            try {
                Currentfile.close();
            } catch (IOException ex) { /* Ignore */
            }
        }
        if (movetofolderfolder != null) {
            try {
                movetofolderfolder.close();
            } catch (IOException ex) { /* Ignore */
            }
        }
    }
    return entrystatus;
}