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

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

Introduction

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

Prototype

public FileName getName();

Source Link

Document

Returns the name of this file.

Usage

From source file:org.pentaho.di.core.vfs.KettleVFS.java

/**
 * Get a FileInputStream for a local file. Local files can be read with NIO.
 *
 * @param fileObject/*from  ww w . j a  v a 2s. com*/
 * @return a FileInputStream
 * @throws IOException
 * @deprecated because of API change in Apache VFS. As a workaround use FileObject.getName().getPathDecoded(); Then
 *             use a regular File() object to create a File Input stream.
 */
@Deprecated
public static FileInputStream getFileInputStream(FileObject fileObject) throws IOException {

    if (!(fileObject instanceof LocalFile)) {
        // We can only use NIO on local files at the moment, so that's what we limit ourselves to.
        //
        throw new IOException(BaseMessages.getString(PKG, "FixedInput.Log.OnlyLocalFilesAreSupported"));
    }

    return new FileInputStream(fileObject.getName().getPathDecoded());
}

From source file:org.pentaho.di.core.vfs.VfsCoreTest.java

public void testWriteReadFile() throws Exception {
    // Write a text
    ////from  w  ww.  ja  va 2  s  .  com
    FileObject tempFile = KettleVFS.createTempFile("prefix", "suffix", tmpDir);
    OutputStream outputStream = KettleVFS.getOutputStream(tempFile, false);
    OutputStreamWriter writer = new OutputStreamWriter(outputStream);
    writer.write(content);
    writer.close();
    outputStream.close();

    // Read it back...
    //
    InputStream inputStream = KettleVFS.getInputStream(tempFile);
    StringBuffer buffer = new StringBuffer();
    int c;
    while ((c = inputStream.read()) >= 0) {
        buffer.append((char) c);
    }
    inputStream.close();

    assertEquals(content, buffer.toString());

    // Now open the data as a regular file...
    //
    String url = tempFile.getName().getURI();
    String textFileContent = KettleVFS.getTextFileContent(url, Const.XML_ENCODING);

    // Now delete the file...
    //
    tempFile.delete();

    assertEquals(false, tempFile.exists());

    assertEquals(content, textFileContent);
}

From source file:org.pentaho.di.job.entries.copyfiles.JobEntryCopyFiles.java

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

    // Clear list files to remove after copy process
    // This list is also added to result files name
    list_files_remove.clear();//  ww  w  .j av  a 2s  .c  o  m
    list_add_result.clear();

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

    try {
        sourcefilefolder = KettleVFS.getFileObject(realSourceFilefoldername, this);
        destinationfilefolder = KettleVFS.getFileObject(realDestinationFilefoldername, 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 COPY FOLDER TO FILE !!!

                    logError(BaseMessages.getString(PKG, "JobCopyFiles.Log.CanNotCopyFolderToFile",
                            realSourceFilefoldername, realDestinationFilefoldername));

                    NbrFail++;

                } else {

                    if (destinationfilefolder.getType().equals(FileType.FOLDER)
                            && sourcefilefolder.getType().equals(FileType.FILE)) {
                        // Source is a file, destination is a folder
                        // Copy the file to the destination folder

                        destinationfilefolder.copyFrom(sourcefilefolder.getParent(),
                                new TextOneFileSelector(sourcefilefolder.getParent().toString(),
                                        sourcefilefolder.getName().getBaseName(),
                                        destinationfilefolder.toString()));
                        if (isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobCopyFiles.Log.FileCopied",
                                    sourcefilefolder.getName().toString(),
                                    destinationfilefolder.getName().toString()));
                        }

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

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

                        }

                        TextFileSelector textFileSelector = new TextFileSelector(sourcefilefolder,
                                destinationfilefolder, realWildcard, parentJob);
                        try {
                            destinationfilefolder.copyFrom(sourcefilefolder, textFileSelector);
                        } finally {
                            textFileSelector.shutdown();
                        }
                    }

                    // Remove Files if needed
                    if (remove_source_files && !list_files_remove.isEmpty()) {
                        String sourceFilefoldername = sourcefilefolder.toString();
                        int trimPathLength = sourceFilefoldername.length() + 1;
                        FileObject removeFile;

                        for (Iterator<String> iter = list_files_remove.iterator(); iter.hasNext()
                                && !parentJob.isStopped();) {
                            String fileremoventry = iter.next();
                            removeFile = null; // re=null each iteration
                            // Try to get the file relative to the existing connection
                            if (fileremoventry.startsWith(sourceFilefoldername)) {
                                if (trimPathLength < fileremoventry.length()) {
                                    removeFile = sourcefilefolder
                                            .getChild(fileremoventry.substring(trimPathLength));
                                }
                            }

                            // Unable to retrieve file through existing connection; Get the file through a new VFS connection
                            if (removeFile == null) {
                                removeFile = KettleVFS.getFileObject(fileremoventry, this);
                            }

                            // Remove ONLY Files
                            if (removeFile.getType() == FileType.FILE) {
                                boolean deletefile = removeFile.delete();
                                logBasic(" ------ ");
                                if (!deletefile) {
                                    logError("      " + BaseMessages.getString(PKG,
                                            "JobCopyFiles.Error.Exception.CanRemoveFileFolder",
                                            fileremoventry));
                                } else {
                                    if (isDetailed()) {
                                        logDetailed("      " + BaseMessages.getString(PKG,
                                                "JobCopyFiles.Log.FileFolderRemoved", fileremoventry));
                                    }
                                }
                            }
                        }
                    }

                    // Add files to result files name
                    if (add_result_filesname && !list_add_result.isEmpty()) {
                        String destinationFilefoldername = destinationfilefolder.toString();
                        int trimPathLength = destinationFilefoldername.length() + 1;
                        FileObject addFile;

                        for (Iterator<String> iter = list_add_result.iterator(); iter.hasNext();) {
                            String fileaddentry = iter.next();
                            addFile = null; // re=null each iteration

                            // Try to get the file relative to the existing connection
                            if (fileaddentry.startsWith(destinationFilefoldername)) {
                                if (trimPathLength < fileaddentry.length()) {
                                    addFile = destinationfilefolder
                                            .getChild(fileaddentry.substring(trimPathLength));
                                }
                            }

                            // Unable to retrieve file through existing connection; Get the file through a new VFS connection
                            if (addFile == null) {
                                addFile = KettleVFS.getFileObject(fileaddentry, this);
                            }

                            // Add ONLY Files
                            if (addFile.getType() == FileType.FILE) {
                                ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, addFile,
                                        parentJob.getJobname(), toString());
                                result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                                if (isDetailed()) {
                                    logDetailed(" ------ ");
                                    logDetailed("      " + BaseMessages.getString(PKG,
                                            "JobCopyFiles.Log.FileAddedToResultFilesName", fileaddentry));
                                }
                            }
                        }
                    }
                }
                entrystatus = true;
            } else {
                // Destination Folder or Parent folder is missing
                logError(BaseMessages.getString(PKG, "JobCopyFiles.Error.DestinationFolderNotFound",
                        realDestinationFilefoldername));
            }
        } else {
            logError(BaseMessages.getString(PKG, "JobCopyFiles.Error.SourceFileNotExists",
                    realSourceFilefoldername));

        }
    } catch (FileSystemException fse) {
        logError(BaseMessages.getString(PKG, "JobCopyFiles.Error.Exception.CopyProcessFileSystemException",
                fse.getMessage()));
        Throwable throwable = fse.getCause();
        while (throwable != null) {
            logError(BaseMessages.getString(PKG, "JobCopyFiles.Log.CausedBy", throwable.getMessage()));
            throwable = throwable.getCause();
        }
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobCopyFiles.Error.Exception.CopyProcess",
                realSourceFilefoldername, realDestinationFilefoldername, e.getMessage()), e);
    } finally {
        if (sourcefilefolder != null) {
            try {
                sourcefilefolder.close();
                sourcefilefolder = null;
            } catch (IOException ex) { /* Ignore */
            }
        }
        if (destinationfilefolder != null) {
            try {
                destinationfilefolder.close();
                destinationfilefolder = null;
            } catch (IOException ex) { /* Ignore */
            }
        }
    }

    return entrystatus;
}

From source file:org.pentaho.di.job.entries.copyfiles.JobEntryCopyFiles.java

private boolean CreateDestinationFolder(FileObject filefolder) {
    FileObject folder = null;
    try {/*from w w w .j  a v a  2  s .c o  m*/
        if (destination_is_a_file) {
            folder = filefolder.getParent();
        } else {
            folder = filefolder;
        }

        if (!folder.exists()) {
            if (create_destination_folder) {
                if (isDetailed()) {
                    logDetailed("Folder  " + folder.getName() + " does not exist !");
                }
                folder.createFolder();
                if (isDetailed()) {
                    logDetailed("Folder parent was created.");
                }
            } else {
                logError("Folder  " + folder.getName() + " does not exist !");
                return false;
            }
        }
        return true;
    } catch (Exception e) {
        logError("Couldn't created parent folder " + folder.getName(), e);
    } finally {
        if (folder != null) {
            try {
                folder.close();
                folder = null;
            } catch (Exception ex) { /* Ignore */
            }
        }
    }
    return false;
}

From source file:org.pentaho.di.job.entries.copymoveresultfilenames.JobEntryCopyMoveResultFilenames.java

public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    result.setNrErrors(1);//from   www .j  a  va2s .  com
    result.setResult(false);

    boolean deleteFile = getAction().equals("delete");

    String realdestinationFolder = null;
    if (!deleteFile) {
        realdestinationFolder = environmentSubstitute(getDestinationFolder());

        if (!CreateDestinationFolder(realdestinationFolder)) {
            return result;
        }
    }
    if (!Const.isEmpty(wildcard)) {
        wildcardPattern = Pattern.compile(environmentSubstitute(wildcard));
    }
    if (!Const.isEmpty(wildcardexclude)) {
        wildcardExcludePattern = Pattern.compile(environmentSubstitute(wildcardexclude));
    }

    if (previousResult != null) {
        NrErrors = 0;
        limitFiles = Const.toInt(environmentSubstitute(getNrErrorsLessThan()), 10);
        NrErrors = 0;
        NrSuccess = 0;
        successConditionBroken = false;
        successConditionBrokenExit = false;

        FileObject file = null;

        try {
            int size = result.getResultFiles().size();
            if (log.isBasic()) {
                logBasic(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.log.FilesFound",
                        "" + size));
            }

            List<ResultFile> resultFiles = result.getResultFilesList();
            if (resultFiles != null && resultFiles.size() > 0) {
                for (Iterator<ResultFile> it = resultFiles.iterator(); it.hasNext()
                        && !parentJob.isStopped();) {
                    if (successConditionBroken) {
                        logError(BaseMessages.getString(PKG,
                                "JobEntryCopyMoveResultFilenames.Error.SuccessConditionbroken", "" + NrErrors));
                        throw new Exception(BaseMessages.getString(PKG,
                                "JobEntryCopyMoveResultFilenames.Error.SuccessConditionbroken", "" + NrErrors));
                    }

                    ResultFile resultFile = it.next();
                    file = resultFile.getFile();
                    if (file != null && file.exists()) {
                        if (!specifywildcard
                                || (CheckFileWildcard(file.getName().getBaseName(), wildcardPattern, true)
                                        && !CheckFileWildcard(file.getName().getBaseName(),
                                                wildcardExcludePattern, false)
                                        && specifywildcard)) {
                            // Copy or Move file
                            if (!processFile(file, realdestinationFolder, result, parentJob, deleteFile)) {
                                // Update Errors
                                updateErrors();
                            }
                        }

                    } else {
                        logError(BaseMessages.getString(PKG,
                                "JobEntryCopyMoveResultFilenames.log.ErrorCanNotFindFile", file.toString()));
                        // Update Errors
                        updateErrors();
                    }
                } // end for
            }
        } catch (Exception e) {
            logError(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.Error", e.toString()));
        } finally {
            if (file != null) {
                try {
                    file.close();
                    file = null;
                } catch (Exception ex) { /* Ignore */
                }
            }
        }
    }
    // Success Condition
    result.setNrErrors(NrErrors);
    result.setNrLinesWritten(NrSuccess);
    if (getSuccessStatus()) {
        result.setResult(true);
    }

    return result;
}

From source file:org.pentaho.di.job.entries.copymoveresultfilenames.JobEntryCopyMoveResultFilenames.java

private boolean processFile(FileObject sourcefile, String destinationFolder, Result result, Job parentJob,
        boolean deleteFile) {
    boolean retval = false;

    try {// w  w w.  j a v  a 2s.com
        if (deleteFile) {
            // delete file
            if (sourcefile.delete()) {
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.log.DeletedFile",
                            sourcefile.toString()));
                }

                // Remove source file from result files list
                result.getResultFiles().remove(sourcefile.toString());
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG,
                            "JobEntryCopyMoveResultFilenames.RemovedFileFromResult", sourcefile.toString()));
                }

            } else {
                logError(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.CanNotDeletedFile",
                        sourcefile.toString()));
            }
        } else {
            // return destination short filename
            String shortfilename = getDestinationFilename(sourcefile.getName().getBaseName());
            // build full destination filename
            String destinationFilename = destinationFolder + Const.FILE_SEPARATOR + shortfilename;
            FileObject destinationfile = KettleVFS.getFileObject(destinationFilename, this);
            boolean filexists = destinationfile.exists();
            if (filexists) {
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.Log.FileExists",
                            destinationFilename));
                }
            }
            if ((!filexists) || (filexists && isOverwriteFile())) {
                if (getAction().equals("copy")) {
                    // Copy file
                    FileUtil.copyContent(sourcefile, destinationfile);
                    if (log.isDetailed()) {
                        logDetailed(
                                BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.log.CopiedFile",
                                        sourcefile.toString(), destinationFolder));
                    }
                } else {
                    // Move file
                    sourcefile.moveTo(destinationfile);
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.log.MovedFile",
                                sourcefile.toString(), destinationFolder));
                    }
                }
                if (isRemovedSourceFilename()) {
                    // Remove source file from result files list
                    result.getResultFiles().remove(sourcefile.toString());
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG,
                                "JobEntryCopyMoveResultFilenames.RemovedFileFromResult",
                                sourcefile.toString()));
                    }
                }
                if (isAddDestinationFilename()) {
                    // Add destination filename to Resultfilenames ...
                    ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL,
                            KettleVFS.getFileObject(destinationfile.toString(), this), parentJob.getJobname(),
                            toString());
                    result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                    if (log.isDetailed()) {
                        logDetailed(
                                BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.AddedFileToResult",
                                        destinationfile.toString()));
                    }
                }
            }
        }
        retval = true;
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.Log.ErrorProcessing",
                e.toString()));
    }

    return retval;
}

From source file:org.pentaho.di.job.entries.deleteresultfilenames.JobEntryDeleteResultFilenames.java

public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    result.setResult(false);/*from  w w w .  ja v  a2s . co m*/

    if (previousResult != null) {
        try {
            int size = previousResult.getResultFiles().size();
            if (log.isBasic()) {
                logBasic(
                        BaseMessages.getString(PKG, "JobEntryDeleteResultFilenames.log.FilesFound", "" + size));
            }
            if (!specifywildcard) {
                // Delete all files
                previousResult.getResultFiles().clear();
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobEntryDeleteResultFilenames.log.DeletedFiles",
                            "" + size));
                }
            } else {

                List<ResultFile> resultFiles = result.getResultFilesList();
                if (resultFiles != null && resultFiles.size() > 0) {
                    for (Iterator<ResultFile> it = resultFiles.iterator(); it.hasNext()
                            && !parentJob.isStopped();) {
                        ResultFile resultFile = it.next();
                        FileObject file = resultFile.getFile();
                        if (file != null && file.exists()) {
                            if (CheckFileWildcard(file.getName().getBaseName(), environmentSubstitute(wildcard),
                                    true)
                                    && !CheckFileWildcard(file.getName().getBaseName(),
                                            environmentSubstitute(wildcardexclude), false)) {
                                // Remove file from result files list
                                result.getResultFiles().remove(resultFile.getFile().toString());

                                if (log.isDetailed()) {
                                    logDetailed(BaseMessages.getString(PKG,
                                            "JobEntryDeleteResultFilenames.log.DeletedFile", file.toString()));
                                }
                            }

                        }
                    }
                }
            }
            result.setResult(true);
        } catch (Exception e) {
            logError(BaseMessages.getString(PKG, "JobEntryDeleteResultFilenames.Error", e.toString()));
        }
    }
    return result;
}

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

private static int getFileType(FileObject file) throws Exception {
    int aCount = 0; // occurences of LF
    int dCount = 0; // occurences of CR
    FileInputStream in = null;//  w  w w  .j  a  v a2  s.  c o  m
    try {
        in = new FileInputStream(file.getName().getPathDecoded());
        while (in.available() > 0) {
            int b = in.read();
            if (b == CR) {
                dCount++;
                if (in.available() > 0) {
                    b = in.read();
                    if (b == LF) {
                        aCount++;
                    } else {
                        return TYPE_BINAY_FILE;
                    }
                }
            } else if (b == LF) {
                aCount++;
            }
        }
    } finally {
        in.close();
    }

    if (aCount == dCount) {
        return TYPE_DOS_FILE;
    } else {
        return TYPE_UNIX_FILE;
    }
}

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

public Result execute(Result previousResult, int nr) throws KettleException {
    Result result = previousResult;
    result.setNrErrors(1);//from ww w  . j  av a  2  s .  c  o m
    result.setResult(false);

    List<RowMetaAndData> rows = result.getRows();
    RowMetaAndData resultRow = null;

    try {
        initMetrics();
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Error.Init", e.toString()));
        return result;
    }

    // Get source and destination files, also wildcard
    String[] vsourcefilefolder = source_filefolder;
    String[] vwildcard = wildcard;
    String[] vincludeSubFolders = includeSubFolders;

    switch (getSourceFiles()) {
    case SOURCE_FILES_PREVIOUS_RESULT:
        // Filenames are retrieved from previous result rows

        String realResultFieldFile = environmentSubstitute(getResultFieldFile());
        String realResultFieldWildcard = environmentSubstitute(getResultFieldWildcard());
        String realResultFieldIncluseSubfolders = environmentSubstitute(getResultFieldIncludeSubfolders());

        int indexOfResultFieldFile = -1;
        if (Const.isEmpty(realResultFieldFile)) {
            logError(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Error.ResultFieldsFileMissing"));
            return result;
        }

        int indexOfResultFieldWildcard = -1;
        int indexOfResultFieldIncludeSubfolders = -1;

        // as such we must get rows
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Log.ArgFromPrevious.Found",
                    (rows != null ? rows.size() : 0) + ""));
        }

        if (rows != null && rows.size() > 0) {
            // We get rows
            RowMetaAndData firstRow = rows.get(0);
            indexOfResultFieldFile = firstRow.getRowMeta().indexOfValue(realResultFieldFile);
            if (indexOfResultFieldFile == -1) {
                logError(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Error.CanNotFindField",
                        realResultFieldFile));
                return result;
            }
            if (!Const.isEmpty(realResultFieldWildcard)) {
                indexOfResultFieldWildcard = firstRow.getRowMeta().indexOfValue(realResultFieldWildcard);
                if (indexOfResultFieldWildcard == -1) {
                    logError(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Error.CanNotFindField",
                            realResultFieldWildcard));
                    return result;
                }
            }
            if (!Const.isEmpty(realResultFieldIncluseSubfolders)) {
                indexOfResultFieldIncludeSubfolders = firstRow.getRowMeta()
                        .indexOfValue(realResultFieldIncluseSubfolders);
                if (indexOfResultFieldIncludeSubfolders == -1) {
                    logError(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Error.CanNotFindField",
                            realResultFieldIncluseSubfolders));
                    return result;
                }
            }

            for (int iteration = 0; iteration < rows.size() && !parentJob.isStopped(); iteration++) {

                resultRow = rows.get(iteration);

                // Get source and destination file names, also wildcard
                String vsourcefilefolder_previous = resultRow.getString(indexOfResultFieldFile, null);
                String vwildcard_previous = null;
                if (indexOfResultFieldWildcard > -1) {
                    vwildcard_previous = resultRow.getString(indexOfResultFieldWildcard, null);
                }
                String vincludeSubFolders_previous = NO;
                if (indexOfResultFieldIncludeSubfolders > -1) {
                    vincludeSubFolders_previous = resultRow.getString(indexOfResultFieldIncludeSubfolders, NO);
                }

                if (isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Log.ProcessingRow",
                            vsourcefilefolder_previous, vwildcard_previous));
                }

                ProcessFileFolder(vsourcefilefolder_previous, vwildcard_previous, vincludeSubFolders_previous,
                        parentJob, result);
            }
        }

        break;
    case SOURCE_FILES_FILENAMES_RESULT:
        List<ResultFile> resultFiles = result.getResultFilesList();
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Log.ResultFilenames.Found",
                    (resultFiles != null ? resultFiles.size() : 0) + ""));
        }

        if (resultFiles != null && resultFiles.size() > 0) {
            // Let's check wildcard
            Pattern pattern = null;
            String realPattern = environmentSubstitute(getResultFilenamesWildcard());
            if (!Const.isEmpty(realPattern)) {
                pattern = Pattern.compile(realPattern);
            }

            for (Iterator<ResultFile> it = resultFiles.iterator(); it.hasNext() && !parentJob.isStopped();) {
                ResultFile resultFile = it.next();
                FileObject file = resultFile.getFile();
                try {
                    if (file != null && file.exists()) {
                        boolean getIt = true;
                        if (pattern != null) {
                            Matcher matcher = pattern.matcher(file.getName().getBaseName());
                            getIt = matcher.matches();
                        }
                        if (getIt) {
                            getFileSize(file, result, parentJob);
                        }
                    }
                } catch (Exception e) {
                    incrementErrors();
                    logError(BaseMessages.getString(PKG,
                            "JobEvalFilesMetrics.Error.GettingFileFromResultFilenames", file.toString(),
                            e.toString()));
                } finally {
                    if (file != null) {
                        try {
                            file.close();
                        } catch (Exception e) { /* Ignore */
                        }
                    }
                }
            }
        }
        break;
    default:
        // static files/folders
        // from grid entered by user
        if (vsourcefilefolder != null && vsourcefilefolder.length > 0) {
            for (int i = 0; i < vsourcefilefolder.length && !parentJob.isStopped(); i++) {

                if (isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Log.ProcessingRow",
                            vsourcefilefolder[i], vwildcard[i]));
                }

                ProcessFileFolder(vsourcefilefolder[i], vwildcard[i], vincludeSubFolders[i], parentJob, result);
            }
        } else {
            logError(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Error.FilesGridEmpty"));
            return result;
        }
        break;
    }

    result.setResult(isSuccess());
    result.setNrErrors(getNrError());
    displayResults();

    return result;
}

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();/*w  w w.j  a  v a2 s.  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 */
            }
        }
    }
}