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.getpop.JobEntryGetPOP.java

public Result execute(Result previousResult, int nr) throws KettleException {
    Result result = previousResult;
    result.setResult(false);/*from   www  .  j a v  a2  s.c  om*/

    //FileObject fileObject = null;
    MailConnection mailConn = null;
    Date beginDate = null;
    Date endDate = null;

    SimpleDateFormat df = new SimpleDateFormat(DATE_PATTERN);

    try {

        boolean usePOP3 = getProtocol().equals(MailConnectionMeta.PROTOCOL_STRING_POP3);
        boolean moveafter = false;
        int nbrmailtoretrieve = usePOP3 ? getRetrievemails() == 2 ? Const.toInt(getFirstMails(), 0) : 0
                : Const.toInt(getFirstIMAPMails(), 0);

        String realOutputFolder = getRealOutputDirectory();
        String targetAttachmentFolder = null;
        if (getActionType() == MailConnectionMeta.ACTION_TYPE_GET) {
            FileObject fileObject = KettleVFS.getFileObject(realOutputFolder, this);
            // Check if output folder exists
            if (fileObject.exists()) {
                if (fileObject.getType() != FileType.FOLDER) {
                    throw new KettleException(BaseMessages.getString(PKG,
                            "JobGetMailsFromPOP.Error.NotAFolderNot", realOutputFolder));
                }
                if (isDebug()) {
                    logDebug(BaseMessages.getString(PKG, "JobGetMailsFromPOP.Log.OutputFolderExists",
                            realOutputFolder));
                }
            } else {
                if (isCreateLocalFolder()) {
                    if (isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobGetMailsFromPOP.Log.OutputFolderNotExist",
                                realOutputFolder));
                    }
                    // create folder
                    fileObject.createFolder();
                } else {
                    throw new KettleException(
                            BaseMessages.getString(PKG, "JobGetMailsFromPOP.FolderNotExists1.Label")
                                    + realOutputFolder
                                    + BaseMessages.getString(PKG, "JobGetMailsFromPOP.FolderNotExists2.Label"));
                }
            }

            targetAttachmentFolder = KettleVFS.getFilename(fileObject);
            // check for attachment folder
            boolean useDifferentFolderForAttachment = (isSaveAttachment() && isDifferentFolderForAttachment());

            if (useDifferentFolderForAttachment) {
                String realFolderAttachment = environmentSubstitute(getAttachmentFolder());
                if (Const.isEmpty(realFolderAttachment)) {
                    throw new KettleException(
                            BaseMessages.getString(PKG, "JobGetMailsFromPOP.Error.AttachmentFolderEmpty"));
                }
                //close old file object
                try {
                    fileObject.close();
                } catch (IOException ex) { /* Ignore */
                }
                //reuse old link
                fileObject = KettleVFS.getFileObject(realFolderAttachment, this);

                if (!fileObject.exists()) {
                    throw new KettleException(BaseMessages.getString(PKG,
                            "JobGetMailsFromPOP.Error.AttachmentFolderNotExist", realFolderAttachment));
                }

                if (fileObject.getType() != FileType.FOLDER) {
                    throw new KettleException(BaseMessages.getString(PKG,
                            "JobGetMailsFromPOP.Error.AttachmentFolderNotAFolder", realFolderAttachment));
                }
                targetAttachmentFolder = KettleVFS.getFilename(fileObject);
            }
            // Close fileObject! we don't need it anymore ...
            try {
                fileObject.close();
            } catch (IOException ex) { /* Ignore */
            }
        } // end if get

        // Check destination folder
        String realMoveToIMAPFolder = environmentSubstitute(getMoveToIMAPFolder());
        if (getProtocol().equals(MailConnectionMeta.PROTOCOL_STRING_IMAP)
                && (getActionType() == MailConnectionMeta.ACTION_TYPE_MOVE)
                || (getActionType() == MailConnectionMeta.ACTION_TYPE_GET
                        && getAfterGetIMAP() == MailConnectionMeta.AFTER_GET_IMAP_MOVE)) {
            if (Const.isEmpty(realMoveToIMAPFolder)) {
                throw new KettleException(
                        BaseMessages.getString(PKG, "JobGetMailsFromPOP.Error.MoveToIMAPFolderEmpty"));
            }
            moveafter = true;
        }

        // check search terms
        // Received Date
        switch (getConditionOnReceivedDate()) {
        case MailConnectionMeta.CONDITION_DATE_EQUAL:
        case MailConnectionMeta.CONDITION_DATE_GREATER:
        case MailConnectionMeta.CONDITION_DATE_SMALLER:
            String realBeginDate = environmentSubstitute(getReceivedDate1());
            if (Const.isEmpty(realBeginDate)) {
                throw new KettleException(
                        BaseMessages.getString(PKG, "JobGetMailsFromPOP.Error.ReceivedDateSearchTermEmpty"));
            }
            beginDate = df.parse(realBeginDate);
            break;
        case MailConnectionMeta.CONDITION_DATE_BETWEEN:
            realBeginDate = environmentSubstitute(getReceivedDate1());
            if (Const.isEmpty(realBeginDate)) {
                throw new KettleException(
                        BaseMessages.getString(PKG, "JobGetMailsFromPOP.Error.ReceivedDatesSearchTermEmpty"));
            }
            beginDate = df.parse(realBeginDate);
            String realEndDate = environmentSubstitute(getReceivedDate2());
            if (Const.isEmpty(realEndDate)) {
                throw new KettleException(
                        BaseMessages.getString(PKG, "JobGetMailsFromPOP.Error.ReceivedDatesSearchTermEmpty"));
            }
            endDate = df.parse(realEndDate);
            break;
        default:
            break;
        }

        String realserver = getRealServername();
        String realusername = getRealUsername();
        String realpassword = getRealPassword();
        String realFilenamePattern = getRealFilenamePattern();
        int realport = Const.toInt(environmentSubstitute(sslport), -1);
        String realIMAPFolder = environmentSubstitute(getIMAPFolder());
        String realProxyUsername = getRealProxyUsername();

        initVariables();
        // create a mail connection object
        mailConn = new MailConnection(log,
                MailConnectionMeta.getProtocolFromString(getProtocol(), MailConnectionMeta.PROTOCOL_IMAP),
                realserver, realport, realusername, realpassword, isUseSSL(), isUseProxy(), realProxyUsername);
        // connect
        mailConn.connect();

        if (moveafter) {
            // Set destination folder
            // Check if folder exists
            mailConn.setDestinationFolder(realMoveToIMAPFolder, isCreateMoveToFolder());
        }

        // apply search term?
        String realSearchSender = environmentSubstitute(getSenderSearchTerm());
        if (!Const.isEmpty(realSearchSender)) {
            // apply FROM
            mailConn.setSenderTerm(realSearchSender, isNotTermSenderSearch());
        }
        String realSearchReceipient = environmentSubstitute(getReceipientSearch());
        if (!Const.isEmpty(realSearchReceipient)) {
            // apply TO
            mailConn.setReceipientTerm(realSearchReceipient);
        }
        String realSearchSubject = environmentSubstitute(getSubjectSearch());
        if (!Const.isEmpty(realSearchSubject)) {
            // apply Subject
            mailConn.setSubjectTerm(realSearchSubject, isNotTermSubjectSearch());
        }
        String realSearchBody = environmentSubstitute(getBodySearch());
        if (!Const.isEmpty(realSearchBody)) {
            // apply body
            mailConn.setBodyTerm(realSearchBody, isNotTermBodySearch());
        }
        // Received Date
        switch (getConditionOnReceivedDate()) {
        case MailConnectionMeta.CONDITION_DATE_EQUAL:
            mailConn.setReceivedDateTermEQ(beginDate);
            break;
        case MailConnectionMeta.CONDITION_DATE_GREATER:
            mailConn.setReceivedDateTermGT(beginDate);
            break;
        case MailConnectionMeta.CONDITION_DATE_SMALLER:
            mailConn.setReceivedDateTermLT(beginDate);
            break;
        case MailConnectionMeta.CONDITION_DATE_BETWEEN:
            mailConn.setReceivedDateTermBetween(beginDate, endDate);
            break;
        default:
            break;
        }
        // set FlagTerm?
        if (usePOP3) {
            // retrieve messages
            if (getRetrievemails() == 1) {
                // New messages
                // POP doesn't support the concept of "new" messages!
                mailConn.setFlagTermUnread();
            }
        } else {
            switch (getValueImapList()) {
            case MailConnectionMeta.VALUE_IMAP_LIST_NEW:
                mailConn.setFlagTermNew();
                break;
            case MailConnectionMeta.VALUE_IMAP_LIST_OLD:
                mailConn.setFlagTermOld();
                break;
            case MailConnectionMeta.VALUE_IMAP_LIST_READ:
                mailConn.setFlagTermRead();
                break;
            case MailConnectionMeta.VALUE_IMAP_LIST_UNREAD:
                mailConn.setFlagTermUnread();
                break;
            case MailConnectionMeta.VALUE_IMAP_LIST_FLAGGED:
                mailConn.setFlagTermFlagged();
                break;
            case MailConnectionMeta.VALUE_IMAP_LIST_NOT_FLAGGED:
                mailConn.setFlagTermNotFlagged();
                break;
            case MailConnectionMeta.VALUE_IMAP_LIST_DRAFT:
                mailConn.setFlagTermDraft();
                break;
            case MailConnectionMeta.VALUE_IMAP_LIST_NOT_DRAFT:
                mailConn.setFlagTermNotDraft();
                break;
            default:
                break;
            }
        }
        // open folder and retrieve messages
        fetchOneFolder(mailConn, usePOP3, realIMAPFolder, realOutputFolder, targetAttachmentFolder,
                realMoveToIMAPFolder, realFilenamePattern, nbrmailtoretrieve, df);

        if (isIncludeSubFolders()) {
            // Fetch also sub folders?
            if (isDebug()) {
                logDebug(BaseMessages.getString(PKG, "JobGetPOP.FetchingSubFolders"));
            }
            String[] subfolders = mailConn.returnAllFolders();
            if (subfolders.length == 0) {
                if (isDebug()) {
                    logDebug(BaseMessages.getString(PKG, "JobGetPOP.NoSubFolders"));
                }
            } else {
                for (int i = 0; i < subfolders.length; i++) {
                    fetchOneFolder(mailConn, usePOP3, subfolders[i], realOutputFolder, targetAttachmentFolder,
                            realMoveToIMAPFolder, realFilenamePattern, nbrmailtoretrieve, df);
                }
            }
        }

        result.setResult(true);
        result.setNrFilesRetrieved(mailConn.getSavedAttachedFilesCounter());
        result.setNrLinesWritten(mailConn.getSavedMessagesCounter());
        result.setNrLinesDeleted(mailConn.getDeletedMessagesCounter());
        result.setNrLinesUpdated(mailConn.getMovedMessagesCounter());

        if (isDetailed()) {
            logDetailed("=======================================");
            logDetailed(BaseMessages.getString(PKG, "JobGetPOP.Log.Info.SavedMessages",
                    "" + mailConn.getSavedMessagesCounter()));
            logDetailed(BaseMessages.getString(PKG, "JobGetPOP.Log.Info.DeletedMessages",
                    "" + mailConn.getDeletedMessagesCounter()));
            logDetailed(BaseMessages.getString(PKG, "JobGetPOP.Log.Info.MovedMessages",
                    "" + mailConn.getMovedMessagesCounter()));
            if (getActionType() == MailConnectionMeta.ACTION_TYPE_GET && isSaveAttachment()) {
                logDetailed(BaseMessages.getString(PKG, "JobGetPOP.Log.Info.AttachedMessagesSuccess",
                        "" + mailConn.getSavedAttachedFilesCounter()));
            }
            logDetailed("=======================================");
        }
    } catch (Exception e) {
        result.setNrErrors(1);
        logError("Unexpected error: " + e.getMessage());
        logError(Const.getStackTracker(e));
    } finally {
        try {
            if (mailConn != null) {
                mailConn.disconnect();
                mailConn = null;
            }
        } catch (Exception e) { /* Ignore */
        }
    }

    return result;
}

From source file:org.pentaho.di.job.entries.job.JobEntryJob.java

private boolean createParentFolder(String filename) {
    // Check for parent folder
    FileObject parentfolder = null;
    boolean resultat = true;
    try {/*w w w .  j a  va 2s . c o  m*/
        // Get parent folder
        parentfolder = KettleVFS.getFileObject(filename, this).getParent();
        if (!parentfolder.exists()) {
            if (createParentFolder) {
                if (log.isDebug()) {
                    log.logDebug(BaseMessages.getString(PKG, "JobJob.Log.ParentLogFolderNotExist",
                            parentfolder.getName().toString()));
                }
                parentfolder.createFolder();
                if (log.isDebug()) {
                    log.logDebug(BaseMessages.getString(PKG, "JobJob.Log.ParentLogFolderCreated",
                            parentfolder.getName().toString()));
                }
            } else {
                log.logError(BaseMessages.getString(PKG, "JobJob.Log.ParentLogFolderNotExist",
                        parentfolder.getName().toString()));
                resultat = false;
            }
        } else {
            if (log.isDebug()) {
                log.logDebug(BaseMessages.getString(PKG, "JobJob.Log.ParentLogFolderExists",
                        parentfolder.getName().toString()));
            }
        }
    } catch (Exception e) {
        resultat = false;
        log.logError(BaseMessages.getString(PKG, "JobJob.Error.ChekingParentLogFolderTitle"), BaseMessages
                .getString(PKG, "JobJob.Error.ChekingParentLogFolder", parentfolder.getName().toString()), e);
    } finally {
        if (parentfolder != null) {
            try {
                parentfolder.close();
                parentfolder = null;
            } catch (Exception ex) {
                // Ignore
            }
        }
    }

    return resultat;
}

From source file:org.pentaho.di.job.entries.mail.JobEntryMail.java

public Result execute(Result result, int nr) {
    File masterZipfile = null;//w ww .j a  v  a2 s.  co  m

    // Send an e-mail...
    // create some properties and get the default Session
    Properties props = new Properties();
    if (Const.isEmpty(server)) {
        logError(BaseMessages.getString(PKG, "JobMail.Error.HostNotSpecified"));

        result.setNrErrors(1L);
        result.setResult(false);
        return result;
    }

    String protocol = "smtp";
    if (usingSecureAuthentication) {
        if (secureConnectionType.equals("TLS")) {
            // Allow TLS authentication
            props.put("mail.smtp.starttls.enable", "true");
        } else {

            protocol = "smtps";
            // required to get rid of a SSL exception :
            // nested exception is:
            // javax.net.ssl.SSLException: Unsupported record version Unknown
            props.put("mail.smtps.quitwait", "false");
        }

    }

    props.put("mail." + protocol + ".host", environmentSubstitute(server));
    if (!Const.isEmpty(port)) {
        props.put("mail." + protocol + ".port", environmentSubstitute(port));
    }

    if (log.isDebug()) {
        props.put("mail.debug", "true");
    }

    if (usingAuthentication) {
        props.put("mail." + protocol + ".auth", "true");

        /*
         * authenticator = new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new
         * PasswordAuthentication( StringUtil.environmentSubstitute(Const.NVL(authenticationUser, "")),
         * StringUtil.environmentSubstitute(Const.NVL(authenticationPassword, "")) ); } };
         */
    }

    Session session = Session.getInstance(props);
    session.setDebug(log.isDebug());

    try {
        // create a message
        Message msg = new MimeMessage(session);

        // set message priority
        if (usePriority) {
            String priority_int = "1";
            if (priority.equals("low")) {
                priority_int = "3";
            }
            if (priority.equals("normal")) {
                priority_int = "2";
            }

            msg.setHeader("X-Priority", priority_int); // (String)int between 1= high and 3 = low.
            msg.setHeader("Importance", importance);
            // seems to be needed for MS Outlook.
            // where it returns a string of high /normal /low.
            msg.setHeader("Sensitivity", sensitivity);
            // Possible values are normal, personal, private, company-confidential

        }

        // Set Mail sender (From)
        String sender_address = environmentSubstitute(replyAddress);
        if (!Const.isEmpty(sender_address)) {
            String sender_name = environmentSubstitute(replyName);
            if (!Const.isEmpty(sender_name)) {
                sender_address = sender_name + '<' + sender_address + '>';
            }
            msg.setFrom(new InternetAddress(sender_address));
        } else {
            throw new MessagingException(BaseMessages.getString(PKG, "JobMail.Error.ReplyEmailNotFilled"));
        }

        // set Reply to addresses
        String reply_to_address = environmentSubstitute(replyToAddresses);
        if (!Const.isEmpty(reply_to_address)) {
            // Split the mail-address: space separated
            String[] reply_Address_List = environmentSubstitute(reply_to_address).split(" ");
            InternetAddress[] address = new InternetAddress[reply_Address_List.length];
            for (int i = 0; i < reply_Address_List.length; i++) {
                address[i] = new InternetAddress(reply_Address_List[i]);
            }
            msg.setReplyTo(address);
        }

        // Split the mail-address: space separated
        String[] destinations = environmentSubstitute(destination).split(" ");
        InternetAddress[] address = new InternetAddress[destinations.length];
        for (int i = 0; i < destinations.length; i++) {
            address[i] = new InternetAddress(destinations[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, address);

        String realCC = environmentSubstitute(getDestinationCc());
        if (!Const.isEmpty(realCC)) {
            // Split the mail-address Cc: space separated
            String[] destinationsCc = realCC.split(" ");
            InternetAddress[] addressCc = new InternetAddress[destinationsCc.length];
            for (int i = 0; i < destinationsCc.length; i++) {
                addressCc[i] = new InternetAddress(destinationsCc[i]);
            }

            msg.setRecipients(Message.RecipientType.CC, addressCc);
        }

        String realBCc = environmentSubstitute(getDestinationBCc());
        if (!Const.isEmpty(realBCc)) {
            // Split the mail-address BCc: space separated
            String[] destinationsBCc = realBCc.split(" ");
            InternetAddress[] addressBCc = new InternetAddress[destinationsBCc.length];
            for (int i = 0; i < destinationsBCc.length; i++) {
                addressBCc[i] = new InternetAddress(destinationsBCc[i]);
            }

            msg.setRecipients(Message.RecipientType.BCC, addressBCc);
        }
        String realSubject = environmentSubstitute(subject);
        if (!Const.isEmpty(realSubject)) {
            msg.setSubject(realSubject);
        }

        msg.setSentDate(new Date());
        StringBuffer messageText = new StringBuffer();
        String endRow = isUseHTML() ? "<br>" : Const.CR;
        String realComment = environmentSubstitute(comment);
        if (!Const.isEmpty(realComment)) {
            messageText.append(realComment).append(Const.CR).append(Const.CR);
        }
        if (!onlySendComment) {

            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.Job")).append(endRow);
            messageText.append("-----").append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.JobName") + "    : ")
                    .append(parentJob.getJobMeta().getName()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.JobDirectory") + "  : ")
                    .append(parentJob.getJobMeta().getRepositoryDirectory()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.JobEntry") + "   : ")
                    .append(getName()).append(endRow);
            messageText.append(Const.CR);
        }

        if (includeDate) {
            messageText.append(endRow).append(BaseMessages.getString(PKG, "JobMail.Log.Comment.MsgDate") + ": ")
                    .append(XMLHandler.date2string(new Date())).append(endRow).append(endRow);
        }
        if (!onlySendComment && result != null) {
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.PreviousResult") + ":")
                    .append(endRow);
            messageText.append("-----------------").append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.JobEntryNr") + "         : ")
                    .append(result.getEntryNr()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.Errors") + "               : ")
                    .append(result.getNrErrors()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.LinesRead") + "           : ")
                    .append(result.getNrLinesRead()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.LinesWritten") + "        : ")
                    .append(result.getNrLinesWritten()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.LinesInput") + "          : ")
                    .append(result.getNrLinesInput()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.LinesOutput") + "         : ")
                    .append(result.getNrLinesOutput()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.LinesUpdated") + "        : ")
                    .append(result.getNrLinesUpdated()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.LinesRejected") + "       : ")
                    .append(result.getNrLinesRejected()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.Status") + "  : ")
                    .append(result.getExitStatus()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.Result") + "               : ")
                    .append(result.getResult()).append(endRow);
            messageText.append(endRow);
        }

        if (!onlySendComment && (!Const.isEmpty(environmentSubstitute(contactPerson))
                || !Const.isEmpty(environmentSubstitute(contactPhone)))) {
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.ContactInfo") + " :")
                    .append(endRow);
            messageText.append("---------------------").append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.PersonToContact") + " : ")
                    .append(environmentSubstitute(contactPerson)).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.Tel") + "  : ")
                    .append(environmentSubstitute(contactPhone)).append(endRow);
            messageText.append(endRow);
        }

        // Include the path to this job entry...
        if (!onlySendComment) {
            JobTracker jobTracker = parentJob.getJobTracker();
            if (jobTracker != null) {
                messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.PathToJobentry") + ":")
                        .append(endRow);
                messageText.append("------------------------").append(endRow);

                addBacktracking(jobTracker, messageText);
                if (isUseHTML()) {
                    messageText.replace(0, messageText.length(),
                            messageText.toString().replace(Const.CR, endRow));
                }
            }
        }

        MimeMultipart parts = new MimeMultipart();
        MimeBodyPart part1 = new MimeBodyPart(); // put the text in the
        // Attached files counter
        int nrattachedFiles = 0;

        // 1st part

        if (useHTML) {
            if (!Const.isEmpty(getEncoding())) {
                part1.setContent(messageText.toString(), "text/html; " + "charset=" + getEncoding());
            } else {
                part1.setContent(messageText.toString(), "text/html; " + "charset=ISO-8859-1");
            }
        } else {
            part1.setText(messageText.toString());
        }

        parts.addBodyPart(part1);

        if (includingFiles && result != null) {
            List<ResultFile> resultFiles = result.getResultFilesList();
            if (resultFiles != null && !resultFiles.isEmpty()) {
                if (!zipFiles) {
                    // Add all files to the message...
                    //
                    for (ResultFile resultFile : resultFiles) {
                        FileObject file = resultFile.getFile();
                        if (file != null && file.exists()) {
                            boolean found = false;
                            for (int i = 0; i < fileType.length; i++) {
                                if (fileType[i] == resultFile.getType()) {
                                    found = true;
                                }
                            }
                            if (found) {
                                // create a data source
                                MimeBodyPart files = new MimeBodyPart();
                                URLDataSource fds = new URLDataSource(file.getURL());

                                // get a data Handler to manipulate this file type;
                                files.setDataHandler(new DataHandler(fds));
                                // include the file in the data source
                                files.setFileName(file.getName().getBaseName());

                                // insist on base64 to preserve line endings
                                files.addHeader("Content-Transfer-Encoding", "base64");

                                // add the part with the file in the BodyPart();
                                parts.addBodyPart(files);
                                nrattachedFiles++;
                                logBasic("Added file '" + fds.getName() + "' to the mail message.");
                            }
                        }
                    }
                } else {
                    // create a single ZIP archive of all files
                    masterZipfile = new File(System.getProperty("java.io.tmpdir") + Const.FILE_SEPARATOR
                            + environmentSubstitute(zipFilename));
                    ZipOutputStream zipOutputStream = null;
                    try {
                        zipOutputStream = new ZipOutputStream(new FileOutputStream(masterZipfile));

                        for (ResultFile resultFile : resultFiles) {
                            boolean found = false;
                            for (int i = 0; i < fileType.length; i++) {
                                if (fileType[i] == resultFile.getType()) {
                                    found = true;
                                }
                            }
                            if (found) {
                                FileObject file = resultFile.getFile();
                                ZipEntry zipEntry = new ZipEntry(file.getName().getBaseName());
                                zipOutputStream.putNextEntry(zipEntry);

                                // Now put the content of this file into this archive...
                                BufferedInputStream inputStream = new BufferedInputStream(
                                        KettleVFS.getInputStream(file));
                                try {
                                    int c;
                                    while ((c = inputStream.read()) >= 0) {
                                        zipOutputStream.write(c);
                                    }
                                } finally {
                                    inputStream.close();
                                }
                                zipOutputStream.closeEntry();
                                nrattachedFiles++;
                                logBasic("Added file '" + file.getName().getURI()
                                        + "' to the mail message in a zip archive.");
                            }
                        }
                    } catch (Exception e) {
                        logError("Error zipping attachement files into file [" + masterZipfile.getPath()
                                + "] : " + e.toString());
                        logError(Const.getStackTracker(e));
                        result.setNrErrors(1);
                    } finally {
                        if (zipOutputStream != null) {
                            try {
                                zipOutputStream.finish();
                                zipOutputStream.close();
                            } catch (IOException e) {
                                logError("Unable to close attachement zip file archive : " + e.toString());
                                logError(Const.getStackTracker(e));
                                result.setNrErrors(1);
                            }
                        }
                    }

                    // Now attach the master zip file to the message.
                    if (result.getNrErrors() == 0) {
                        // create a data source
                        MimeBodyPart files = new MimeBodyPart();
                        FileDataSource fds = new FileDataSource(masterZipfile);
                        // get a data Handler to manipulate this file type;
                        files.setDataHandler(new DataHandler(fds));
                        // include the file in the data source
                        files.setFileName(fds.getName());
                        // add the part with the file in the BodyPart();
                        parts.addBodyPart(files);
                    }
                }
            }
        }

        int nrEmbeddedImages = 0;
        if (embeddedimages != null && embeddedimages.length > 0) {
            FileObject imageFile = null;
            for (int i = 0; i < embeddedimages.length; i++) {
                String realImageFile = environmentSubstitute(embeddedimages[i]);
                String realcontenID = environmentSubstitute(contentids[i]);
                if (messageText.indexOf("cid:" + realcontenID) < 0) {
                    if (log.isDebug()) {
                        log.logDebug("Image [" + realImageFile + "] is not used in message body!");
                    }
                } else {
                    try {
                        boolean found = false;
                        imageFile = KettleVFS.getFileObject(realImageFile, this);
                        if (imageFile.exists() && imageFile.getType() == FileType.FILE) {
                            found = true;
                        } else {
                            log.logError("We can not find [" + realImageFile + "] or it is not a file");
                        }
                        if (found) {
                            // Create part for the image
                            MimeBodyPart messageBodyPart = new MimeBodyPart();
                            // Load the image
                            URLDataSource fds = new URLDataSource(imageFile.getURL());
                            messageBodyPart.setDataHandler(new DataHandler(fds));
                            // Setting the header
                            messageBodyPart.setHeader("Content-ID", "<" + realcontenID + ">");
                            // Add part to multi-part
                            parts.addBodyPart(messageBodyPart);
                            nrEmbeddedImages++;
                            log.logBasic("Image '" + fds.getName() + "' was embedded in message.");
                        }
                    } catch (Exception e) {
                        log.logError(
                                "Error embedding image [" + realImageFile + "] in message : " + e.toString());
                        log.logError(Const.getStackTracker(e));
                        result.setNrErrors(1);
                    } finally {
                        if (imageFile != null) {
                            try {
                                imageFile.close();
                            } catch (Exception e) { /* Ignore */
                            }
                        }
                    }
                }
            }
        }

        if (nrEmbeddedImages > 0 && nrattachedFiles == 0) {
            // If we need to embedd images...
            // We need to create a "multipart/related" message.
            // otherwise image will appear as attached file
            parts.setSubType("related");
        }
        // put all parts together
        msg.setContent(parts);

        Transport transport = null;
        try {
            transport = session.getTransport(protocol);
            String authPass = getPassword(authenticationPassword);

            if (usingAuthentication) {
                if (!Const.isEmpty(port)) {
                    transport.connect(environmentSubstitute(Const.NVL(server, "")),
                            Integer.parseInt(environmentSubstitute(Const.NVL(port, ""))),
                            environmentSubstitute(Const.NVL(authenticationUser, "")), authPass);
                } else {
                    transport.connect(environmentSubstitute(Const.NVL(server, "")),
                            environmentSubstitute(Const.NVL(authenticationUser, "")), authPass);
                }
            } else {
                transport.connect();
            }
            transport.sendMessage(msg, msg.getAllRecipients());
        } finally {
            if (transport != null) {
                transport.close();
            }
        }
    } catch (IOException e) {
        logError("Problem while sending message: " + e.toString());
        result.setNrErrors(1);
    } catch (MessagingException mex) {
        logError("Problem while sending message: " + mex.toString());
        result.setNrErrors(1);

        Exception ex = mex;
        do {
            if (ex instanceof SendFailedException) {
                SendFailedException sfex = (SendFailedException) ex;

                Address[] invalid = sfex.getInvalidAddresses();
                if (invalid != null) {
                    logError("    ** Invalid Addresses");
                    for (int i = 0; i < invalid.length; i++) {
                        logError("         " + invalid[i]);
                        result.setNrErrors(1);
                    }
                }

                Address[] validUnsent = sfex.getValidUnsentAddresses();
                if (validUnsent != null) {
                    logError("    ** ValidUnsent Addresses");
                    for (int i = 0; i < validUnsent.length; i++) {
                        logError("         " + validUnsent[i]);
                        result.setNrErrors(1);
                    }
                }

                Address[] validSent = sfex.getValidSentAddresses();
                if (validSent != null) {
                    // System.out.println("    ** ValidSent Addresses");
                    for (int i = 0; i < validSent.length; i++) {
                        logError("         " + validSent[i]);
                        result.setNrErrors(1);
                    }
                }
            }
            if (ex instanceof MessagingException) {
                ex = ((MessagingException) ex).getNextException();
            } else {
                ex = null;
            }
        } while (ex != null);
    } finally {
        if (masterZipfile != null && masterZipfile.exists()) {
            masterZipfile.delete();
        }
    }

    if (result.getNrErrors() > 0) {
        result.setResult(false);
    } else {
        result.setResult(true);
    }

    return result;
}

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

public Result execute(Result previousResult, int nr) throws KettleException {
    Result result = previousResult;
    List<RowMetaAndData> rows = result.getRows();
    RowMetaAndData resultRow = null;/*from   w w w . j  av  a2s  .c o m*/
    result.setNrErrors(1);
    result.setResult(false);

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

    if (log.isDetailed()) {
        if (simulate) {
            logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.SimulationOn"));
        }
        if (include_subfolders) {
            logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.IncludeSubFoldersOn"));
        }
    }

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

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

    if (arg_from_previous) {
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.ArgFromPrevious.Found",
                    (rows != null ? rows.size() : 0) + ""));
        }
    }
    if (arg_from_previous && rows != null) {
        for (int iteration = 0; iteration < rows.size() && !parentJob.isStopped(); iteration++) {
            // Success condition broken?
            if (successConditionBroken) {
                if (!successConditionBrokenExit) {
                    logError(BaseMessages.getString(PKG, "JobMoveFiles.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 vdestinationfilefolder_previous = resultRow.getString(1, null);
            String vwildcard_previous = resultRow.getString(2, null);

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

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

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

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

    displayResults();

    return result;
}

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

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

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

    try {/*from   w w w. j  a v a  2 s  . c  om*/
        sourcefilefolder = KettleVFS.getFileObject(realSourceFilefoldername, this);
        destinationfilefolder = KettleVFS.getFileObject(realDestinationFilefoldername, this);
        if (!Const.isEmpty(MoveToFolder)) {
            movetofolderfolder = KettleVFS.getFileObject(MoveToFolder, this);
        }

        if (sourcefilefolder.exists()) {

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

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

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

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

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

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

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

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

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

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

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

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

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

                                }
                                return true;
                            }
                        });

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

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

                            }
                        }
                    }

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

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

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

    FileObject destinationfile = null;
    boolean retval = false;
    try {//from w  w w  .  ja  v  a2 s. c o  m
        if (!destinationfilename.exists()) {
            if (!simulate) {
                sourcefilename.moveTo(destinationfilename);
            }
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.FileMoved",
                        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();
            retval = true;

        } else {
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.FileExists",
                        destinationfilename.toString()));
            }
            if (iffileexists.equals("overwrite_file")) {
                if (!simulate) {
                    sourcefilename.moveTo(destinationfilename);
                }
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.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();
                retval = true;

            } 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,
                            BaseMessages.getString(PKG, "JobMoveFiles.Error.GettingFilename", short_filename)),
                            e);
                    return retval;
                }

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

                if (!simulate) {
                    sourcefilename.moveTo(destinationfile);
                }
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.FileMoved",
                            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();
                retval = true;
            } else if (iffileexists.equals("delete_file")) {
                if (!simulate) {
                    destinationfilename.delete();
                }
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.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,
                            BaseMessages.getString(PKG, "JobMoveFiles.Error.GettingFilename", short_filename)),
                            e);
                    return retval;
                }

                String movetofilenamefull = movetofolderfolder.toString() + Const.FILE_SEPARATOR
                        + short_filename;
                destinationfile = KettleVFS.getFileObject(movetofilenamefull, this);
                if (!destinationfile.exists()) {
                    if (!simulate) {
                        sourcefilename.moveTo(destinationfile);
                    }
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.FileMoved",
                                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")) {
                        if (!simulate) {
                            sourcefilename.moveTo(destinationfile);
                        }
                        if (log.isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.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();
                        retval = true;
                    } 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, this);

                        if (!simulate) {
                            sourcefilename.moveTo(destinationfile);
                        }
                        if (log.isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.FileMoved",
                                    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();
                        retval = true;
                    } else if (ifmovedfileexists.equals("fail")) {
                        // Update Errors
                        updateErrors();
                    }
                }

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

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

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

    try {/* w w w  .j a v  a2s  .  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,
                        BaseMessages.getString(PKG, "JobMoveFiles.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,
                    this);

            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 (include_subfolders && move_empty_folders && Const.isEmpty(wildcard)) {
                            entrystatus = MoveFile(shortfilename, Currentfile, file_name, movetofolderfolder,
                                    parentJob, result);
                        }
                    } else {

                        if (GetFileWildcard(sourceshortfilename, realWildcard)) {
                            entrystatus = MoveFile(shortfilename, Currentfile, file_name, movetofolderfolder,
                                    parentJob, result);
                        }
                    }
                }
            } else {
                // In the Base Folder...
                // Folders..only if include subfolders
                if (Currentfile.getType() == FileType.FOLDER) {
                    if (include_subfolders && move_empty_folders && Const.isEmpty(wildcard)) {
                        entrystatus = MoveFile(shortfilename, Currentfile, file_name, movetofolderfolder,
                                parentJob, result);
                    }
                } else {

                    // file...Check if exists
                    if (GetFileWildcard(sourceshortfilename, realWildcard)) {
                        entrystatus = MoveFile(shortfilename, Currentfile, file_name, movetofolderfolder,
                                parentJob, result);

                    }
                }

            }

        }
        entrystatus = true;

    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobMoveFiles.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.movefiles.JobEntryMoveFiles.java

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

        if (!folder.exists()) {
            if (create_destination_folder) {
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.FolderNotExist",
                            folder.getName().toString()));
                }
                folder.createFolder();
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.FolderWasCreated",
                            folder.getName().toString()));
                }
            } else {
                logError(BaseMessages.getString(PKG, "JobMoveFiles.Log.FolderNotExist",
                        folder.getName().toString()));
                return false;
            }
        }
        return true;
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobMoveFiles.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.mssqlbulkload.JobEntryMssqlBulkLoad.java

public Result execute(Result previousResult, int nr) {
    String TakeFirstNbrLines = "";
    String LineTerminatedby = "";
    String FieldTerminatedby = "";
    boolean useFieldSeparator = false;
    String UseCodepage = "";
    String ErrorfileName = "";

    Result result = previousResult;
    result.setResult(false);//from  ww w  .j a  v a  2  s. c om

    String vfsFilename = environmentSubstitute(filename);
    FileObject fileObject = null;
    // Let's check the filename ...
    if (!Const.isEmpty(vfsFilename)) {
        try {
            // User has specified a file, We can continue ...
            //
            // This is running over VFS but we need a normal file.
            // As such, we're going to verify that it's a local file...
            // We're also going to convert VFS FileObject to File
            //
            fileObject = KettleVFS.getFileObject(vfsFilename, this);
            if (!(fileObject instanceof LocalFile)) {
                // MSSQL BUKL INSERT can only use local files, so that's what we limit ourselves to.
                //
                throw new KettleException(BaseMessages.getString(PKG,
                        "JobMssqlBulkLoad.Error.OnlyLocalFileSupported", vfsFilename));
            }

            // Convert it to a regular platform specific file name
            //
            String realFilename = KettleVFS.getFilename(fileObject);

            // Here we go... back to the regular scheduled program...
            //
            File file = new File(realFilename);
            if (file.exists() && file.canRead()) {
                // User has specified an existing file, We can continue ...
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobMssqlBulkLoad.FileExists.Label", realFilename));
                }

                if (connection != null) {
                    // User has specified a connection, We can continue ...
                    Database db = new Database(this, connection);

                    if (!(db.getDatabaseMeta().getDatabaseInterface() instanceof MSSQLServerDatabaseMeta)) {
                        logError(BaseMessages.getString(PKG, "JobMssqlBulkLoad.Error.DbNotMSSQL",
                                connection.getDatabaseName()));
                        return result;
                    }
                    db.shareVariablesWith(this);
                    try {
                        db.connect(parentJob.getTransactionId(), null);
                        // Get schemaname
                        String realSchemaname = environmentSubstitute(schemaname);
                        // Get tablename
                        String realTablename = environmentSubstitute(tablename);

                        // Add schemaname (Most the time Schemaname.Tablename)
                        if (schemaname != null) {
                            realTablename = realSchemaname + "." + realTablename;
                        }

                        if (db.checkTableExists(realTablename)) {
                            // The table existe, We can continue ...
                            if (log.isDetailed()) {
                                logDetailed(BaseMessages.getString(PKG, "JobMssqlBulkLoad.TableExists.Label",
                                        realTablename));
                            }

                            // FIELDTERMINATOR
                            String Fieldterminator = getRealFieldTerminator();
                            if (Const.isEmpty(Fieldterminator)
                                    && (datafiletype.equals("char") || datafiletype.equals("widechar"))) {
                                logError(BaseMessages.getString(PKG,
                                        "JobMssqlBulkLoad.Error.FieldTerminatorMissing"));
                                return result;
                            } else {
                                if (datafiletype.equals("char") || datafiletype.equals("widechar")) {
                                    useFieldSeparator = true;
                                    FieldTerminatedby = "FIELDTERMINATOR='" + Fieldterminator + "'";
                                }
                            }
                            // Check Specific Code page
                            if (codepage.equals("Specific")) {
                                String realCodePage = environmentSubstitute(codepage);
                                if (specificcodepage.length() < 0) {
                                    logError(BaseMessages.getString(PKG,
                                            "JobMssqlBulkLoad.Error.SpecificCodePageMissing"));
                                    return result;

                                } else {
                                    UseCodepage = "CODEPAGE = '" + realCodePage + "'";
                                }
                            } else {
                                UseCodepage = "CODEPAGE = '" + codepage + "'";
                            }

                            // Check Error file
                            String realErrorFile = environmentSubstitute(errorfilename);
                            if (realErrorFile != null) {
                                File errorfile = new File(realErrorFile);
                                if (errorfile.exists() && !adddatetime) {
                                    // The error file is created when the command is executed. An error occurs if the file already
                                    // exists.
                                    logError(BaseMessages.getString(PKG,
                                            "JobMssqlBulkLoad.Error.ErrorFileExists"));
                                    return result;
                                }
                                if (adddatetime) {
                                    // Add date time to filename...
                                    SimpleDateFormat daf = new SimpleDateFormat();
                                    Date now = new Date();
                                    daf.applyPattern("yyyMMdd_HHmmss");
                                    String d = daf.format(now);

                                    ErrorfileName = "ERRORFILE ='" + realErrorFile + "_" + d + "'";
                                } else {
                                    ErrorfileName = "ERRORFILE ='" + realErrorFile + "'";
                                }
                            }

                            // ROWTERMINATOR
                            String Rowterminator = getRealLineterminated();
                            if (!Const.isEmpty(Rowterminator)) {
                                LineTerminatedby = "ROWTERMINATOR='" + Rowterminator + "'";
                            }

                            // Start file at
                            if (startfile > 0) {
                                TakeFirstNbrLines = "FIRSTROW=" + startfile;
                            }

                            // End file at
                            if (endfile > 0) {
                                TakeFirstNbrLines = "LASTROW=" + endfile;
                            }

                            // Truncate table?
                            String SQLBULKLOAD = "";
                            if (truncate) {
                                SQLBULKLOAD = "TRUNCATE TABLE " + realTablename + ";";
                            }

                            // Build BULK Command
                            SQLBULKLOAD = SQLBULKLOAD + "BULK INSERT " + realTablename + " FROM " + "'"
                                    + realFilename.replace('\\', '/') + "'";
                            SQLBULKLOAD = SQLBULKLOAD + " WITH (";
                            if (useFieldSeparator) {
                                SQLBULKLOAD = SQLBULKLOAD + FieldTerminatedby;
                            } else {
                                SQLBULKLOAD = SQLBULKLOAD + "DATAFILETYPE ='" + datafiletype + "'";
                            }

                            if (LineTerminatedby.length() > 0) {
                                SQLBULKLOAD = SQLBULKLOAD + "," + LineTerminatedby;
                            }
                            if (TakeFirstNbrLines.length() > 0) {
                                SQLBULKLOAD = SQLBULKLOAD + "," + TakeFirstNbrLines;
                            }
                            if (UseCodepage.length() > 0) {
                                SQLBULKLOAD = SQLBULKLOAD + "," + UseCodepage;
                            }
                            String realFormatFile = environmentSubstitute(formatfilename);
                            if (realFormatFile != null) {
                                SQLBULKLOAD = SQLBULKLOAD + ", FORMATFILE='" + realFormatFile + "'";
                            }
                            if (firetriggers) {
                                SQLBULKLOAD = SQLBULKLOAD + ",FIRE_TRIGGERS";
                            }
                            if (keepnulls) {
                                SQLBULKLOAD = SQLBULKLOAD + ",KEEPNULLS";
                            }
                            if (keepidentity) {
                                SQLBULKLOAD = SQLBULKLOAD + ",KEEPIDENTITY";
                            }
                            if (checkconstraints) {
                                SQLBULKLOAD = SQLBULKLOAD + ",CHECK_CONSTRAINTS";
                            }
                            if (tablock) {
                                SQLBULKLOAD = SQLBULKLOAD + ",TABLOCK";
                            }
                            if (orderby != null) {
                                SQLBULKLOAD = SQLBULKLOAD + ",ORDER ( " + orderby + " " + orderdirection + ")";
                            }
                            if (ErrorfileName.length() > 0) {
                                SQLBULKLOAD = SQLBULKLOAD + ", " + ErrorfileName;
                            }
                            if (maxerrors > 0) {
                                SQLBULKLOAD = SQLBULKLOAD + ", MAXERRORS=" + maxerrors;
                            }
                            if (batchsize > 0) {
                                SQLBULKLOAD = SQLBULKLOAD + ", BATCHSIZE=" + batchsize;
                            }
                            if (rowsperbatch > 0) {
                                SQLBULKLOAD = SQLBULKLOAD + ", ROWS_PER_BATCH=" + rowsperbatch;
                            }
                            // End of Bulk command
                            SQLBULKLOAD = SQLBULKLOAD + ")";

                            try {
                                // Run the SQL
                                db.execStatement(SQLBULKLOAD);

                                // Everything is OK...we can disconnect now
                                db.disconnect();

                                if (isAddFileToResult()) {
                                    // Add filename to output files
                                    ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL,
                                            KettleVFS.getFileObject(realFilename, this), parentJob.getJobname(),
                                            toString());
                                    result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                                }

                                result.setResult(true);
                            } catch (KettleDatabaseException je) {
                                result.setNrErrors(1);
                                logError("An error occurred executing this job entry : " + je.getMessage(), je);
                            } catch (KettleFileException e) {
                                logError("An error occurred executing this job entry : " + e.getMessage(), e);
                                result.setNrErrors(1);
                            } finally {
                                if (db != null) {
                                    db.disconnect();
                                    db = null;
                                }
                            }
                        } else {
                            // Of course, the table should have been created already before the bulk load operation
                            db.disconnect();
                            result.setNrErrors(1);
                            if (log.isDetailed()) {
                                logDetailed(BaseMessages.getString(PKG, "JobMssqlBulkLoad.Error.TableNotExists",
                                        realTablename));
                            }
                        }
                    } catch (KettleDatabaseException dbe) {
                        db.disconnect();
                        result.setNrErrors(1);
                        logError("An error occurred executing this entry: " + dbe.getMessage());
                    }
                } else {
                    // No database connection is defined
                    result.setNrErrors(1);
                    logError(BaseMessages.getString(PKG, "JobMssqlBulkLoad.Nodatabase.Label"));
                }
            } else {
                // the file doesn't exist
                result.setNrErrors(1);
                logError(BaseMessages.getString(PKG, "JobMssqlBulkLoad.Error.FileNotExists", realFilename));
            }
        } catch (Exception e) {
            // An unexpected error occurred
            result.setNrErrors(1);
            logError(BaseMessages.getString(PKG, "JobMssqlBulkLoad.UnexpectedError.Label"), e);
        } finally {
            try {
                if (fileObject != null) {
                    fileObject.close();
                }
            } catch (Exception e) {
                // Ignore errors
            }
        }
    } else {
        // No file was specified
        result.setNrErrors(1);
        logError(BaseMessages.getString(PKG, "JobMssqlBulkLoad.Nofilename.Label"));
    }
    return result;
}

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

public Result execute(Result previousResult, int nr) throws KettleException {
    try {/*w ww.  j  a v a2 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;
        }
    }
}