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

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

Introduction

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

Prototype

public boolean exists() throws FileSystemException;

Source Link

Document

Determines if this file exists.

Usage

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

public Result execute(Result previousResult, int nr) throws KettleException {
    try {//  w  w  w. j  a  va  2  s . c om
        Result result = previousResult;
        List<RowMetaAndData> rows = result.getRows();
        RowMetaAndData resultRow = null;
        result.setNrErrors(1);
        result.setResult(false);

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

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

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

        if (iffileexists.equals("move_file")) {
            if (Const.isEmpty(MoveToFolder)) {
                logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.Error.MoveToFolderMissing"));
                return result;
            }
            FileObject folder = null;
            try {
                folder = KettleVFS.getFileObject(MoveToFolder);
                if (!folder.exists()) {
                    if (isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.Error.FolderMissing",
                                MoveToFolder));
                    }
                    if (create_move_to_folder) {
                        folder.createFolder();
                    } else {
                        logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.Error.FolderMissing",
                                MoveToFolder));
                        return result;
                    }
                }
                if (!folder.getType().equals(FileType.FOLDER)) {
                    logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.Error.NotFolder",
                            MoveToFolder));
                    return result;
                }
            } catch (Exception e) {
                logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.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, "JobPGPDecryptFiles.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, "JobPGPDecryptFiles.Error.SuccessConditionbroken",
                                "" + NrErrors));
                        successConditionBrokenExit = true;
                    }
                    result.setNrErrors(NrErrors);
                    displayResults();
                    return result;
                }

                resultRow = rows.get(iteration);

                // Get source and destination file names, also wildcard
                String vsourcefilefolder_previous = resultRow.getString(0, null);
                String vwildcard_previous = Encr.decryptPasswordOptionallyEncrypted(
                        environmentSubstitute(resultRow.getString(1, null)));
                String vpassphrase_previous = resultRow.getString(2, null);
                String vdestinationfilefolder_previous = resultRow.getString(3, null);

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

                    if (!ProcessFileFolder(vsourcefilefolder_previous, vpassphrase_previous,
                            vdestinationfilefolder_previous, vwildcard_previous, parentJob, result,
                            MoveToFolder)) {
                        // The move process fail
                        // Update Errors
                        updateErrors();
                    }
                } else {
                    if (isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.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, "JobPGPDecryptFiles.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, "JobPGPDecryptFiles.Log.ProcessingRow",
                                vsourcefilefolder[i], vdestinationfilefolder[i], vwildcard[i]));
                    }

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

        // Success Condition
        result.setNrErrors(NrErrors);
        result.setNrLinesWritten(NrSuccess);
        if (getSuccessStatus()) {
            result.setResult(true);
        }

        displayResults();

        return result;
    } finally {
        if (source_filefolder != null) {
            source_filefolder = null;
        }
        if (destination_filefolder != null) {
            destination_filefolder = null;
        }
    }
}

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

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

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

    try {//from   w ww . ja  va  2 s.  co  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 {/*from   ww w  .  jav a2s. co  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 CreateDestinationFolder(FileObject filefolder) {
    FileObject folder = null;
    try {//from  ww w.java  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(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  a  2  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  ww  w.j av  a  2  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 www  . ja v  a 2 s.  c  om

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

        if (sourcefilefolder.exists()) {

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

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

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

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

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

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

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

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

                        // Source is a file, destination is a file

                        FileObject destinationfile = KettleVFS.getFileObject(realDestinationFilefoldername);

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

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

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

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

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

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

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

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

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

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 a2  s  . co 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 CreateDestinationFolder(FileObject filefolder) {
    FileObject folder = null;
    try {//from w  w  w  .ja  va 2  s  .  co  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, "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;
}

From source file:org.pentaho.di.job.entries.pgpverify.JobEntryPGPVerify.java

/**
 * Exports the object to a flat-file system, adding content with filename keys to a set of definitions. The supplied
 * resource naming interface allows the object to name appropriately without worrying about those parts of the
 * implementation specific details./*from  ww w .j a  v a  2  s . c om*/
 *
 * @param space
 *          The variable space to resolve (environment) variables with.
 * @param definitions
 *          The map containing the filenames and content
 * @param namingInterface
 *          The resource naming interface allows the object to be named appropriately
 * @param repository
 *          The repository to load resources from
 * @param metaStore
 *          the metaStore to load external metadata from
 *
 * @return The filename for this object. (also contained in the definitions map)
 * @throws KettleException
 *           in case something goes wrong during the export
 */
public String exportResources(VariableSpace space, Map<String, ResourceDefinition> definitions,
        ResourceNamingInterface namingInterface, Repository repository, IMetaStore metaStore)
        throws KettleException {
    try {
        // The object that we're modifying here is a copy of the original!
        // So let's change the gpglocation from relative to absolute by grabbing the file object...
        // In case the name of the file comes from previous steps, forget about this!
        //
        if (!Const.isEmpty(gpglocation)) {
            // From : ${FOLDER}/../foo/bar.csv
            // To : /home/matt/test/files/foo/bar.csv
            //
            FileObject fileObject = KettleVFS.getFileObject(space.environmentSubstitute(gpglocation), space);

            // If the file doesn't exist, forget about this effort too!
            //
            if (fileObject.exists()) {
                // Convert to an absolute path...
                //
                gpglocation = namingInterface.nameResource(fileObject, space, true);

                return gpglocation;
            }
        }
        return null;
    } catch (Exception e) {
        throw new KettleException(e);
    }
}