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

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

Introduction

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

Prototype

public void close() throws FileSystemException;

Source Link

Document

Closes this file, and its content.

Usage

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 {/*  ww  w  .  ja va  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.pgpdecryptfiles.JobEntryPGPDecryptFiles.java

private boolean DecryptFile(String shortfilename, FileObject sourcefilename, String passPharse,
        FileObject destinationfilename, FileObject movetofolderfolder, Job parentJob, Result result) {

    FileObject destinationfile = null;
    boolean retval = false;
    try {/*  w  w  w.  j a  va 2  s . c o  m*/
        if (!destinationfilename.exists()) {
            gpg.decryptFile(sourcefilename, passPharse, destinationfilename);

            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.FileDecrypted",
                        sourcefilename.getName().toString(), destinationfilename.getName().toString()));
            }

            // add filename to result filename
            if (add_result_filesname && !iffileexists.equals("fail") && !iffileexists.equals("do_nothing")) {
                addFileToResultFilenames(destinationfilename.toString(), result, parentJob);
            }

            updateSuccess();

        } else {
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.FileExists",
                        destinationfilename.toString()));
            }
            if (iffileexists.equals("overwrite_file")) {
                gpg.decryptFile(sourcefilename, passPharse, destinationfilename);

                if (isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.FileOverwrite",
                            destinationfilename.getName().toString()));
                }

                // add filename to result filename
                if (add_result_filesname && !iffileexists.equals("fail")
                        && !iffileexists.equals("do_nothing")) {
                    addFileToResultFilenames(destinationfilename.toString(), result, parentJob);
                }

                updateSuccess();

            } else if (iffileexists.equals("unique_name")) {
                String short_filename = shortfilename;

                // return destination short filename
                try {
                    short_filename = getMoveDestinationFilename(short_filename, "ddMMyyyy_HHmmssSSS");
                } catch (Exception e) {
                    logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Error.GettingFilename",
                            short_filename), e);
                    return retval;
                }

                String movetofilenamefull = destinationfilename.getParent().toString() + Const.FILE_SEPARATOR
                        + short_filename;
                destinationfile = KettleVFS.getFileObject(movetofilenamefull);

                gpg.decryptFile(sourcefilename, passPharse, destinationfile);

                if (isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.FileDecrypted",
                            sourcefilename.getName().toString(), destinationfile.getName().toString()));
                }

                // add filename to result filename
                if (add_result_filesname && !iffileexists.equals("fail")
                        && !iffileexists.equals("do_nothing")) {
                    addFileToResultFilenames(destinationfile.toString(), result, parentJob);
                }

                updateSuccess();
            } else if (iffileexists.equals("delete_file")) {
                destinationfilename.delete();
                if (isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.FileDeleted",
                            destinationfilename.getName().toString()));
                }
            } else if (iffileexists.equals("move_file")) {
                String short_filename = shortfilename;
                // return destination short filename
                try {
                    short_filename = getMoveDestinationFilename(short_filename, null);
                } catch (Exception e) {
                    logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Error.GettingFilename",
                            short_filename), e);
                    return retval;
                }

                String movetofilenamefull = movetofolderfolder.toString() + Const.FILE_SEPARATOR
                        + short_filename;
                destinationfile = KettleVFS.getFileObject(movetofilenamefull);
                if (!destinationfile.exists()) {
                    sourcefilename.moveTo(destinationfile);
                    if (isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.FileDecrypted",
                                sourcefilename.getName().toString(), destinationfile.getName().toString()));
                    }

                    // add filename to result filename
                    if (add_result_filesname && !iffileexists.equals("fail")
                            && !iffileexists.equals("do_nothing")) {
                        addFileToResultFilenames(destinationfile.toString(), result, parentJob);
                    }

                } else {
                    if (ifmovedfileexists.equals("overwrite_file")) {
                        sourcefilename.moveTo(destinationfile);
                        if (isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.FileOverwrite",
                                    destinationfile.getName().toString()));
                        }

                        // add filename to result filename
                        if (add_result_filesname && !iffileexists.equals("fail")
                                && !iffileexists.equals("do_nothing")) {
                            addFileToResultFilenames(destinationfile.toString(), result, parentJob);
                        }

                        updateSuccess();
                    } else if (ifmovedfileexists.equals("unique_name")) {
                        SimpleDateFormat daf = new SimpleDateFormat();
                        Date now = new Date();
                        daf.applyPattern("ddMMyyyy_HHmmssSSS");
                        String dt = daf.format(now);
                        short_filename += "_" + dt;

                        String destinationfilenamefull = movetofolderfolder.toString() + Const.FILE_SEPARATOR
                                + short_filename;
                        destinationfile = KettleVFS.getFileObject(destinationfilenamefull);

                        sourcefilename.moveTo(destinationfile);
                        if (isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.FileDecrypted",
                                    destinationfile.getName().toString()));
                        }

                        // add filename to result filename
                        if (add_result_filesname && !iffileexists.equals("fail")
                                && !iffileexists.equals("do_nothing")) {
                            addFileToResultFilenames(destinationfile.toString(), result, parentJob);
                        }

                        updateSuccess();
                    } else if (ifmovedfileexists.equals("fail")) {
                        // Update Errors
                        updateErrors();
                    }
                }

            } else if (iffileexists.equals("fail")) {
                // Update Errors
                updateErrors();
            }

        }
    } catch (Exception e) {
        updateErrors();
        logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Error.Exception.MoveProcessError",
                sourcefilename.toString(), destinationfilename.toString(), e.getMessage()));
    } finally {
        if (destinationfile != null) {
            try {
                destinationfile.close();
            } catch (IOException ex) { /* Ignore */
            }
        }
    }
    return retval;
}

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

private boolean DecryptOneFile(FileObject Currentfile, FileObject sourcefilefolder, String passPhrase,
        String realDestinationFilefoldername, String realWildcard, Job parentJob, Result result,
        FileObject movetofolderfolder) {
    boolean entrystatus = false;
    FileObject file_name = null;

    try {//from   w  w w .  jav a  2s . com
        if (!Currentfile.toString().equals(sourcefilefolder.toString())) {
            // Pass over the Base folder itself

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

            int lenCurrent = sourceshortfilename.length();
            String short_filename_from_basefolder = shortfilename;
            if (!isDoNotKeepFolderStructure()) {
                short_filename_from_basefolder = Currentfile.toString()
                        .substring(sourcefilefolder.toString().length(), Currentfile.toString().length());
            }
            short_filename_from_basefolder = short_filename_from_basefolder.substring(0,
                    short_filename_from_basefolder.length() - lenCurrent) + shortfilename;

            // Built destination filename
            file_name = KettleVFS.getFileObject(
                    realDestinationFilefoldername + Const.FILE_SEPARATOR + short_filename_from_basefolder);

            if (!Currentfile.getParent().toString().equals(sourcefilefolder.toString())) {

                // Not in the Base Folder..Only if include sub folders
                if (include_subfolders) {
                    // Folders..only if include subfolders
                    if (Currentfile.getType() != FileType.FOLDER) {
                        if (GetFileWildcard(sourceshortfilename, realWildcard)) {
                            entrystatus = DecryptFile(shortfilename, Currentfile, passPhrase, file_name,
                                    movetofolderfolder, parentJob, result);
                        }
                    }
                }
            } else {
                // In the Base Folder...
                // Folders..only if include subfolders
                if (Currentfile.getType() != FileType.FOLDER) {
                    // file...Check if exists
                    if (GetFileWildcard(sourceshortfilename, realWildcard)) {
                        entrystatus = DecryptFile(shortfilename, Currentfile, passPhrase, file_name,
                                movetofolderfolder, parentJob, result);

                    }
                }

            }

        }
        entrystatus = true;

    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.Error", e.toString()));
    } finally {
        if (file_name != null) {
            try {
                file_name.close();

            } catch (IOException ex) { /* Ignore */
            }
        }

    }
    return entrystatus;
}

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

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

        if (!folder.exists()) {
            if (create_destination_folder) {
                if (isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.FolderNotExist",
                            folder.getName().toString()));
                }
                folder.createFolder();
                if (isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.FolderWasCreated",
                            folder.getName().toString()));
                }
            } else {
                logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.FolderNotExist",
                        folder.getName().toString()));
                return false;
            }
        }
        return true;
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.CanNotCreateParentFolder",
                folder.getName().toString()), e);

    } finally {
        if (folder != null) {
            try {
                folder.close();
            } catch (Exception ex) { /* Ignore */
            }
        }
    }
    return false;
}

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

/**
 * Constructs a new GnuPG/*from ww  w  .  j  a  v a2  s . c  o  m*/
 *
 * @param gpgFilename
 *          gpg program location
 * @param logInterface
 *          LogChannelInterface
 * @throws KettleException
 */
public GPG(String gpgFilename, LogChannelInterface logInterface) throws KettleException {
    this.log = logInterface;
    this.gpgexe = gpgFilename;
    // Let's check GPG filename
    if (Const.isEmpty(getGpgExeFile())) {
        // No filename specified
        throw new KettleException(BaseMessages.getString(PKG, "GPG.GPGFilenameMissing"));
    }
    // We have a filename, we need to check
    FileObject file = null;
    try {
        file = KettleVFS.getFileObject(getGpgExeFile());

        if (!file.exists()) {
            throw new KettleException(BaseMessages.getString(PKG, "GPG.GPGFilenameNotFound"));
        }
        // The file exists
        if (!file.getType().equals(FileType.FILE)) {
            throw new KettleException(BaseMessages.getString(PKG, "GPG.GPGNotAFile", getGpgExeFile()));
        }

        // Ok we have a real file
        // Get the local filename
        this.gpgexe = KettleVFS.getFilename(file);

    } catch (Exception e) {
        throw new KettleException(BaseMessages.getString(PKG, "GPG.ErrorCheckingGPGFile", getGpgExeFile()), e);
    } finally {
        try {
            if (file != null) {
                file.close();
            }
        } catch (Exception e) {
            // Ignore close errors
        }
    }
}

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

public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    List<RowMetaAndData> rows = result.getRows();
    RowMetaAndData resultRow = null;/*from   www.  jav a2  s  .c o  m*/
    result.setNrErrors(1);
    result.setResult(false);

    try {

        NrErrors = 0;
        NrSuccess = 0;
        successConditionBroken = false;
        successConditionBrokenExit = false;
        limitFiles = Const.toInt(environmentSubstitute(getNrErrorsLessThan()), 10);

        if (include_subfolders) {
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.IncludeSubFoldersOn"));
            }
        }

        String MoveToFolder = environmentSubstitute(destinationFolder);
        // Get source and destination files, also wildcard
        String[] vsourcefilefolder = source_filefolder;
        String[] vuserid = userid;
        String[] vdestinationfilefolder = destination_filefolder;
        String[] vwildcard = wildcard;

        if (iffileexists.equals("move_file")) {
            if (Const.isEmpty(MoveToFolder)) {
                logError(toString(),
                        BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.Error.MoveToFolderMissing"));
                return result;
            }
            FileObject folder = null;
            try {
                folder = KettleVFS.getFileObject(MoveToFolder);
                if (!folder.exists()) {
                    if (isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.Error.FolderMissing",
                                MoveToFolder));
                    }
                    if (create_move_to_folder) {
                        folder.createFolder();
                    } else {
                        logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.Error.FolderMissing",
                                MoveToFolder));
                        return result;
                    }
                }
                if (!folder.getType().equals(FileType.FOLDER)) {
                    logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.Error.NotFolder",
                            MoveToFolder));
                    return result;
                }
            } catch (Exception e) {
                logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.Error.GettingMoveToFolder",
                        MoveToFolder, e.getMessage()));
                return result;
            } finally {
                if (folder != null) {
                    try {
                        folder.close();
                    } catch (IOException ex) { /* Ignore */
                    }
                }
            }
        }

        gpg = new GPG(environmentSubstitute(gpglocation), log);

        if (arg_from_previous) {
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.ArgFromPrevious.Found",
                        (rows != null ? rows.size() : 0) + ""));
            }
        }
        if (arg_from_previous && rows != null) {
            for (int iteration = 0; iteration < rows.size(); iteration++) {
                // Success condition broken?
                if (successConditionBroken) {
                    if (!successConditionBrokenExit) {
                        logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Error.SuccessConditionbroken",
                                "" + NrErrors));
                        successConditionBrokenExit = true;
                    }
                    result.setNrErrors(NrErrors);
                    displayResults();
                    return result;
                }

                resultRow = rows.get(iteration);

                // Get source and destination file names, also wildcard
                int vactionType_previous = getActionTypeByCode(resultRow.getString(0, null));
                String vsourcefilefolder_previous = resultRow.getString(1, null);
                String vwildcard_previous = environmentSubstitute(resultRow.getString(2, null));
                String vuserid_previous = resultRow.getString(3, null);
                String vdestinationfilefolder_previous = resultRow.getString(4, null);

                if (!Const.isEmpty(vsourcefilefolder_previous)
                        && !Const.isEmpty(vdestinationfilefolder_previous)) {
                    if (isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.ProcessingRow",
                                vsourcefilefolder_previous, vdestinationfilefolder_previous,
                                vwildcard_previous));
                    }

                    if (!ProcessFileFolder(vactionType_previous, vsourcefilefolder_previous, vuserid_previous,
                            vdestinationfilefolder_previous, vwildcard_previous, parentJob, result,
                            MoveToFolder)) {
                        // The process fail
                        // Update Errors
                        updateErrors();
                    }
                } else {
                    if (isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.IgnoringRow",
                                vsourcefilefolder[iteration], vdestinationfilefolder[iteration],
                                vwildcard[iteration]));
                    }
                }
            }
        } else if (vsourcefilefolder != null && vdestinationfilefolder != null) {
            for (int i = 0; i < vsourcefilefolder.length && !parentJob.isStopped(); i++) {
                // Success condition broken?
                if (successConditionBroken) {
                    if (!successConditionBrokenExit) {
                        logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Error.SuccessConditionbroken",
                                "" + NrErrors));
                        successConditionBrokenExit = true;
                    }
                    result.setNrErrors(NrErrors);
                    displayResults();
                    return result;
                }

                if (!Const.isEmpty(vsourcefilefolder[i]) && !Const.isEmpty(vdestinationfilefolder[i])) {
                    // ok we can process this file/folder
                    if (isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.ProcessingRow",
                                vsourcefilefolder[i], vdestinationfilefolder[i], vwildcard[i]));
                    }

                    if (!ProcessFileFolder(action_type[i], vsourcefilefolder[i], vuserid[i],
                            vdestinationfilefolder[i], vwildcard[i], parentJob, result, MoveToFolder)) {
                        // Update Errors
                        updateErrors();
                    }
                } else {
                    if (isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.IgnoringRow",
                                vsourcefilefolder[i], vdestinationfilefolder[i], vwildcard[i]));
                    }
                }
            }
        }

    } catch (Exception e) {
        updateErrors();
        logError(BaseMessages.getString("JobPGPEncryptFiles.Error", e.getMessage()));
    } finally {
        if (source_filefolder != null) {
            source_filefolder = null;
        }
        if (destination_filefolder != null) {
            destination_filefolder = null;
        }
    }
    // Success Condition
    result.setNrErrors(NrErrors);
    result.setNrLinesWritten(NrSuccess);
    if (getSuccessStatus()) {
        result.setResult(true);
    }

    displayResults();

    return result;
}

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 {//from   w  w w.j  a v  a2 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, "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;
}

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

private boolean EncryptFile(int actionType, String shortfilename, FileObject sourcefilename, String userID,
        FileObject destinationfilename, FileObject movetofolderfolder, Job parentJob, Result result) {

    FileObject destinationfile = null;
    boolean retval = false;
    try {//from w w w . j  a v  a  2 s.  c  o m
        if (!destinationfilename.exists()) {

            doJob(actionType, sourcefilename, userID, destinationfilename);
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.FileEncrypted",
                        sourcefilename.getName().toString(), destinationfilename.getName().toString()));
            }

            // add filename to result filename
            if (add_result_filesname && !iffileexists.equals("fail") && !iffileexists.equals("do_nothing")) {
                addFileToResultFilenames(destinationfilename.toString(), result, parentJob);
            }

            updateSuccess();

        } else {
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.FileExists",
                        destinationfilename.toString()));
            }
            if (iffileexists.equals("overwrite_file")) {
                doJob(actionType, sourcefilename, userID, destinationfilename);
                if (isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.FileOverwrite",
                            destinationfilename.getName().toString()));
                }

                // add filename to result filename
                if (add_result_filesname && !iffileexists.equals("fail")
                        && !iffileexists.equals("do_nothing")) {
                    addFileToResultFilenames(destinationfilename.toString(), result, parentJob);
                }

                updateSuccess();

            } else if (iffileexists.equals("unique_name")) {
                String short_filename = shortfilename;

                // return destination short filename
                try {
                    short_filename = getMoveDestinationFilename(short_filename, "ddMMyyyy_HHmmssSSS");
                } catch (Exception e) {
                    logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Error.GettingFilename",
                            short_filename), e);
                    return retval;
                }

                String movetofilenamefull = destinationfilename.getParent().toString() + Const.FILE_SEPARATOR
                        + short_filename;
                destinationfile = KettleVFS.getFileObject(movetofilenamefull);

                doJob(actionType, sourcefilename, userID, destinationfilename);
                if (isDetailed()) {
                    logDetailed(toString(), BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.FileEncrypted",
                            sourcefilename.getName().toString(), destinationfile.getName().toString()));
                }

                // add filename to result filename
                if (add_result_filesname && !iffileexists.equals("fail")
                        && !iffileexists.equals("do_nothing")) {
                    addFileToResultFilenames(destinationfile.toString(), result, parentJob);
                }

                updateSuccess();
            } else if (iffileexists.equals("delete_file")) {
                destinationfilename.delete();
                if (isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.FileDeleted",
                            destinationfilename.getName().toString()));
                }
            } else if (iffileexists.equals("move_file")) {
                String short_filename = shortfilename;
                // return destination short filename
                try {
                    short_filename = getMoveDestinationFilename(short_filename, null);
                } catch (Exception e) {
                    logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Error.GettingFilename",
                            short_filename), e);
                    return retval;
                }

                String movetofilenamefull = movetofolderfolder.toString() + Const.FILE_SEPARATOR
                        + short_filename;
                destinationfile = KettleVFS.getFileObject(movetofilenamefull);
                if (!destinationfile.exists()) {
                    sourcefilename.moveTo(destinationfile);
                    if (isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.FileEncrypted",
                                sourcefilename.getName().toString(), destinationfile.getName().toString()));
                    }

                    // add filename to result filename
                    if (add_result_filesname && !iffileexists.equals("fail")
                            && !iffileexists.equals("do_nothing")) {
                        addFileToResultFilenames(destinationfile.toString(), result, parentJob);
                    }

                } else {
                    if (ifmovedfileexists.equals("overwrite_file")) {
                        sourcefilename.moveTo(destinationfile);
                        if (isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.FileOverwrite",
                                    destinationfile.getName().toString()));
                        }

                        // add filename to result filename
                        if (add_result_filesname && !iffileexists.equals("fail")
                                && !iffileexists.equals("do_nothing")) {
                            addFileToResultFilenames(destinationfile.toString(), result, parentJob);
                        }

                        updateSuccess();
                    } else if (ifmovedfileexists.equals("unique_name")) {
                        SimpleDateFormat daf = new SimpleDateFormat();
                        Date now = new Date();
                        daf.applyPattern("ddMMyyyy_HHmmssSSS");
                        String dt = daf.format(now);
                        short_filename += "_" + dt;

                        String destinationfilenamefull = movetofolderfolder.toString() + Const.FILE_SEPARATOR
                                + short_filename;
                        destinationfile = KettleVFS.getFileObject(destinationfilenamefull);

                        sourcefilename.moveTo(destinationfile);
                        if (isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.FileEncrypted",
                                    destinationfile.getName().toString()));
                        }

                        // add filename to result filename
                        if (add_result_filesname && !iffileexists.equals("fail")
                                && !iffileexists.equals("do_nothing")) {
                            addFileToResultFilenames(destinationfile.toString(), result, parentJob);
                        }

                        updateSuccess();
                    } else if (ifmovedfileexists.equals("fail")) {
                        // Update Errors
                        updateErrors();
                    }
                }

            } else if (iffileexists.equals("fail")) {
                // Update Errors
                updateErrors();
            }

        }
    } catch (Exception e) {
        updateErrors();
        logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Error.Exception.MoveProcessError",
                sourcefilename.toString(), destinationfilename.toString(), e.getMessage()));
    } finally {
        if (destinationfile != null) {
            try {
                destinationfile.close();
            } catch (IOException ex) { /* Ignore */
            }
        }
    }
    return retval;
}

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

private boolean EncryptOneFile(int actionType, FileObject Currentfile, FileObject sourcefilefolder,
        String userID, String realDestinationFilefoldername, String realWildcard, Job parentJob, Result result,
        FileObject movetofolderfolder) {
    boolean entrystatus = false;
    FileObject file_name = null;

    try {//w ww.  j a  va 2  s.c om
        if (!Currentfile.toString().equals(sourcefilefolder.toString())) {
            // Pass over the Base folder itself

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

            int lenCurrent = sourceshortfilename.length();
            String short_filename_from_basefolder = shortfilename;
            if (!isDoNotKeepFolderStructure()) {
                short_filename_from_basefolder = Currentfile.toString()
                        .substring(sourcefilefolder.toString().length(), Currentfile.toString().length());
            }
            short_filename_from_basefolder = short_filename_from_basefolder.substring(0,
                    short_filename_from_basefolder.length() - lenCurrent) + shortfilename;

            // Built destination filename
            file_name = KettleVFS.getFileObject(
                    realDestinationFilefoldername + Const.FILE_SEPARATOR + short_filename_from_basefolder);

            if (!Currentfile.getParent().toString().equals(sourcefilefolder.toString())) {

                // Not in the Base Folder..Only if include sub folders
                if (include_subfolders) {
                    // Folders..only if include subfolders
                    if (Currentfile.getType() != FileType.FOLDER) {

                        if (GetFileWildcard(sourceshortfilename, realWildcard)) {
                            entrystatus = EncryptFile(actionType, shortfilename, Currentfile, userID, file_name,
                                    movetofolderfolder, parentJob, result);
                        }
                    }
                }
            } else {
                // In the Base Folder...
                // Folders..only if include subfolders
                if (Currentfile.getType() != FileType.FOLDER) {
                    // file...Check if exists
                    if (GetFileWildcard(sourceshortfilename, realWildcard)) {
                        entrystatus = EncryptFile(actionType, shortfilename, Currentfile, userID, file_name,
                                movetofolderfolder, parentJob, result);

                    }
                }

            }

        }
        entrystatus = true;

    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.Error", e.toString()));
    } finally {
        if (file_name != null) {
            try {
                file_name.close();

            } catch (IOException ex) { /* Ignore */
            }
        }

    }
    return entrystatus;
}

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

private boolean CreateDestinationFolder(FileObject filefolder) {
    FileObject folder = null;
    try {/*from  w w  w.  jav a  2s.com*/
        if (destination_is_a_file) {
            folder = filefolder.getParent();
        } else {
            folder = filefolder;
        }

        if (!folder.exists()) {
            if (create_destination_folder) {
                if (isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.FolderNotExist",
                            folder.getName().toString()));
                }
                folder.createFolder();
                if (isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.FolderWasCreated",
                            folder.getName().toString()));
                }
            } else {
                logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.FolderNotExist",
                        folder.getName().toString()));
                return false;
            }
        }
        return true;
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.CanNotCreateParentFolder",
                folder.getName().toString()), e);

    } finally {
        if (folder != null) {
            try {
                folder.close();
            } catch (Exception ex) { /* Ignore */
            }
        }
    }
    return false;
}