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:com.panet.imeta.trans.steps.mail.Mail.java

private void setAttachedFilesList(Object[] r, LogWriter log) throws Exception {
    String realSourceFileFoldername = null;
    String realSourceWildcard = null;
    FileObject sourcefile = null;
    FileObject file = null;//  w  w w .j a va2  s  .  com

    ZipOutputStream zipOutputStream = null;
    File masterZipfile = null;

    if (meta.isZipFilenameDynamic())
        data.ZipFilename = data.previousRowMeta.getString(r, data.indexOfDynamicZipFilename);

    try {

        if (meta.isDynamicFilename()) {
            // dynamic attached filenames
            if (data.indexOfSourceFilename > -1)
                realSourceFileFoldername = data.previousRowMeta.getString(r, data.indexOfSourceFilename);

            if (data.indexOfSourceWildcard > -1)
                realSourceWildcard = data.previousRowMeta.getString(r, data.indexOfSourceWildcard);

        } else {
            // static attached filenames
            realSourceFileFoldername = data.realSourceFileFoldername;
            realSourceWildcard = data.realSourceWildcard;
        }

        if (!Const.isEmpty(realSourceFileFoldername)) {
            sourcefile = KettleVFS.getFileObject(realSourceFileFoldername);
            if (sourcefile.exists()) {
                long FileSize = 0;
                FileObject list[] = null;
                if (sourcefile.getType() == FileType.FILE) {
                    list = new FileObject[1];
                    list[0] = sourcefile;
                } else
                    list = sourcefile
                            .findFiles(new TextFileSelector(sourcefile.toString(), realSourceWildcard));
                if (list.length > 0) {

                    boolean zipFiles = meta.isZipFiles();
                    if (zipFiles && data.zipFileLimit == 0) {
                        masterZipfile = new File(
                                System.getProperty("java.io.tmpdir") + Const.FILE_SEPARATOR + data.ZipFilename);

                        zipOutputStream = new ZipOutputStream(new FileOutputStream(masterZipfile));
                    }

                    for (int i = 0; i < list.length; i++) {

                        file = KettleVFS.getFileObject(KettleVFS.getFilename(list[i]));

                        if (zipFiles) {

                            if (data.zipFileLimit == 0) {
                                ZipEntry zipEntry = new ZipEntry(file.getName().getBaseName());
                                zipOutputStream.putNextEntry(zipEntry);

                                // Now put the content of this file into this archive...
                                BufferedInputStream inputStream = new BufferedInputStream(
                                        file.getContent().getInputStream());
                                int c;
                                while ((c = inputStream.read()) >= 0) {
                                    zipOutputStream.write(c);
                                }
                                inputStream.close();
                                zipOutputStream.closeEntry();
                            } else
                                FileSize += file.getContent().getSize();
                        } else {
                            addAttachedFilePart(file);
                        }
                    } // end for
                    if (zipFiles) {
                        if (log.isDebug())
                            log.logDebug(toString(), Messages.getString("Mail.Log.FileSize", "" + FileSize));
                        if (log.isDebug())
                            log.logDebug(toString(),
                                    Messages.getString("Mail.Log.LimitSize", "" + data.zipFileLimit));

                        if (data.zipFileLimit > 0 && FileSize > data.zipFileLimit) {

                            masterZipfile = new File(System.getProperty("java.io.tmpdir") + Const.FILE_SEPARATOR
                                    + data.ZipFilename);

                            zipOutputStream = new ZipOutputStream(new FileOutputStream(masterZipfile));

                            for (int i = 0; i < list.length; i++) {

                                file = KettleVFS.getFileObject(KettleVFS.getFilename(list[i]));

                                ZipEntry zipEntry = new ZipEntry(file.getName().getBaseName());
                                zipOutputStream.putNextEntry(zipEntry);

                                // Now put the content of this file into this archive...
                                BufferedInputStream inputStream = new BufferedInputStream(
                                        file.getContent().getInputStream());
                                int c;
                                while ((c = inputStream.read()) >= 0) {
                                    zipOutputStream.write(c);
                                }
                                inputStream.close();
                                zipOutputStream.closeEntry();

                            }

                        }
                        if (data.zipFileLimit > 0 && FileSize > data.zipFileLimit || data.zipFileLimit == 0) {
                            file = KettleVFS.getFileObject(masterZipfile.getAbsolutePath());
                            addAttachedFilePart(file);
                        }
                    }
                }
            } else {
                log.logError(toString(),
                        Messages.getString("Mail.Error.SourceFileFolderNotExists", realSourceFileFoldername));
            }
        }
    } catch (Exception e) {
        log.logError(toString(), e.getMessage());
    } finally {
        if (sourcefile != null) {
            try {
                sourcefile.close();
            } catch (Exception e) {
            }
        }
        if (file != null) {
            try {
                file.close();
            } catch (Exception e) {
            }
        }

        if (zipOutputStream != null) {
            try {
                zipOutputStream.finish();
                zipOutputStream.close();
            } catch (IOException e) {
                log.logError(toString(), "Unable to close attachement zip file archive : " + e.toString());
            }
        }
    }

}

From source file:com.panet.imeta.job.entries.unzip.JobEntryUnZip.java

public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) {
    LogWriter log = LogWriter.getInstance();
    Result result = previousResult;
    result.setResult(false);//from   ww  w .j av  a  2 s  .  c om
    result.setEntryNr(1);

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

    String realFilenameSource = environmentSubstitute(zipFilename);
    String realWildcardSource = environmentSubstitute(wildcardSource);
    String realWildcard = environmentSubstitute(wildcard);
    String realWildcardExclude = environmentSubstitute(wildcardexclude);
    String realTargetdirectory = environmentSubstitute(targetdirectory);
    String realMovetodirectory = environmentSubstitute(movetodirectory);

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

    if (isfromprevious) {
        if (log.isDetailed())
            log.logDetailed(toString(), Messages.getString("JobUnZip.Log.ArgFromPrevious.Found",
                    (rows != null ? rows.size() : 0) + ""));

        if (rows.size() == 0)
            return result;
    } else {
        if (Const.isEmpty(zipFilename)) {
            // Zip file/folder is missing
            log.logError(toString(), Messages.getString("JobUnZip.No_ZipFile_Defined.Label"));
            return result;
        }
    }

    FileObject fileObject = null;
    FileObject targetdir = null;
    FileObject movetodir = null;

    try {

        // Let's make some checks here, before running job entry ...

        boolean exitjobentry = false;
        // Target folder
        targetdir = KettleVFS.getFileObject(realTargetdirectory);
        if (!targetdir.exists()) {
            if (createfolder) {
                targetdir.createFolder();
                if (log.isDetailed())
                    log.logDetailed(toString(),
                            Messages.getString("JobUnZip.Log.TargetFolderCreated", realTargetdirectory));

            } else {
                log.logError(Messages.getString("JobUnZip.Error.Label"),
                        Messages.getString("JobUnZip.TargetFolderNotFound.Label"));
                exitjobentry = true;
            }
        } else {
            if (!(targetdir.getType() == FileType.FOLDER)) {
                log.logError(Messages.getString("JobUnZip.Error.Label"),
                        Messages.getString("JobUnZip.TargetFolderNotFolder.Label", realTargetdirectory));
                exitjobentry = true;
            } else {
                if (log.isDetailed())
                    log.logDetailed(toString(),
                            Messages.getString("JobUnZip.TargetFolderExists.Label", realTargetdirectory));
            }
        }

        // If user want to move zip files after process
        // movetodirectory must be provided
        if (afterunzip == 2) {
            if (Const.isEmpty(movetodirectory)) {
                log.logError(Messages.getString("JobUnZip.Error.Label"),
                        Messages.getString("JobUnZip.MoveToDirectoryEmpty.Label"));
                exitjobentry = true;
            } else {
                movetodir = KettleVFS.getFileObject(realMovetodirectory);
                if (!(movetodir.exists()) || !(movetodir.getType() == FileType.FOLDER)) {
                    if (createMoveToDirectory) {
                        movetodir.createFolder();
                        if (log.isDetailed())
                            log.logDetailed(toString(), Messages.getString("JobUnZip.Log.MoveToFolderCreated",
                                    realMovetodirectory));
                    } else {
                        log.logError(Messages.getString("JobUnZip.Error.Label"),
                                Messages.getString("JobUnZip.MoveToDirectoryNotExists.Label"));
                        exitjobentry = true;
                    }
                }
            }
        }

        // We found errors...now exit
        if (exitjobentry)
            return result;

        if (isfromprevious) {
            if (rows != null) // Copy the input row to the (command line)
            // arguments
            {
                for (int iteration = 0; iteration < rows.size() && !parentJob.isStopped(); iteration++) {
                    if (successConditionBroken) {
                        if (!successConditionBrokenExit) {
                            log.logError(toString(),
                                    Messages.getString("JobUnZip.Error.SuccessConditionbroken", "" + NrErrors));
                            successConditionBrokenExit = true;
                        }
                        result.setEntryNr(NrErrors);
                        return result;
                    }

                    resultRow = rows.get(iteration);

                    // Get sourcefile/folder and wildcard
                    realFilenameSource = resultRow.getString(0, null);
                    realWildcardSource = resultRow.getString(1, null);

                    fileObject = KettleVFS.getFileObject(realFilenameSource);
                    if (fileObject.exists()) {
                        processOneFile(log, result, parentJob, fileObject, realTargetdirectory, realWildcard,
                                realWildcardExclude, movetodir, realMovetodirectory, realWildcardSource);
                    } else {
                        updateErrors();
                        log.logError(toString(),
                                Messages.getString("JobUnZip.Error.CanNotFindFile", realFilenameSource));
                    }
                }
            }
        } else {
            fileObject = KettleVFS.getFileObject(realFilenameSource);
            if (!fileObject.exists()) {
                log.logError(Messages.getString("JobUnZip.Error.Label"),
                        Messages.getString("JobUnZip.ZipFile.NotExists.Label", realFilenameSource));
                return result;
            }

            if (log.isDetailed())
                log.logDetailed(toString(),
                        Messages.getString("JobUnZip.Zip_FileExists.Label", realFilenameSource));
            if (Const.isEmpty(targetdirectory)) {
                log.logError(Messages.getString("JobUnZip.Error.Label"),
                        Messages.getString("JobUnZip.TargetFolderNotFound.Label"));
                return result;
            }

            processOneFile(log, result, parentJob, fileObject, realTargetdirectory, realWildcard,
                    realWildcardExclude, movetodir, realMovetodirectory, realWildcardSource);
        }
    } catch (Exception e) {
        log.logError(Messages.getString("JobUnZip.Error.Label"),
                Messages.getString("JobUnZip.ErrorUnzip.Label", realFilenameSource, e.getMessage()));
        updateErrors();
    } finally {
        if (fileObject != null) {
            try {
                fileObject.close();
            } catch (IOException ex) {
            }
            ;
        }
        if (targetdir != null) {
            try {
                targetdir.close();
            } catch (IOException ex) {
            }
            ;
        }
        if (movetodir != null) {
            try {
                movetodir.close();
            } catch (IOException ex) {
            }
            ;
        }
    }

    result.setNrErrors(NrErrors);
    result.setNrLinesWritten(NrSuccess);
    if (getSuccessStatus())
        result.setResult(true);
    displayResults(log);

    return result;
}

From source file:com.panet.imeta.job.entries.waitforfile.JobEntryWaitForFile.java

public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) {
    LogWriter log = LogWriter.getInstance();
    Result result = previousResult;
    result.setResult(false);//  w  w w. j  av  a2 s.c o  m

    // starttime (in seconds)
    long timeStart = System.currentTimeMillis() / 1000;

    if (filename != null) {
        FileObject fileObject = null;
        String realFilename = getRealFilename();
        try {
            fileObject = KettleVFS.getFileObject(realFilename);

            long iMaximumTimeout = Const.toInt(getRealMaximumTimeout(),
                    Const.toInt(DEFAULT_MAXIMUM_TIMEOUT, 0));
            long iCycleTime = Const.toInt(getRealCheckCycleTime(), Const.toInt(DEFAULT_CHECK_CYCLE_TIME, 0));

            //
            // Sanity check on some values, and complain on insanity
            //
            if (iMaximumTimeout < 0) {
                iMaximumTimeout = Const.toInt(DEFAULT_MAXIMUM_TIMEOUT, 0);
                if (log.isBasic())
                    log.logBasic(toString(), "Maximum timeout invalid, reset to " + iMaximumTimeout);
            }

            if (iCycleTime < 1) {
                // If lower than 1 set to the default
                iCycleTime = Const.toInt(DEFAULT_CHECK_CYCLE_TIME, 1);
                if (log.isBasic())
                    log.logBasic(toString(), "Check cycle time invalid, reset to " + iCycleTime);
            }

            if (iMaximumTimeout == 0) {
                if (log.isBasic())
                    log.logBasic(toString(), "Waiting indefinitely for file [" + realFilename + "]");
            } else {
                if (log.isBasic())
                    log.logBasic(toString(),
                            "Waiting " + iMaximumTimeout + " seconds for file [" + realFilename + "]");
            }

            boolean continueLoop = true;
            while (continueLoop && !parentJob.isStopped()) {
                fileObject = KettleVFS.getFileObject(realFilename);

                if (fileObject.exists()) {
                    // file exists, we're happy to exit
                    if (log.isBasic())
                        log.logBasic(toString(), "Detected file [" + realFilename + "] within timeout");
                    result.setResult(true);
                    continueLoop = false;

                    // add filename to result filenames
                    if (addFilenameToResult && fileObject.getType() == FileType.FILE) {
                        ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, fileObject,
                                parentJob.getJobname(), toString());
                        resultFile.setComment(Messages.getString("JobWaitForFile.FilenameAdded"));
                        result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                    }
                } else {
                    long now = System.currentTimeMillis() / 1000;

                    if ((iMaximumTimeout > 0) && (now > (timeStart + iMaximumTimeout))) {
                        continueLoop = false;

                        // file doesn't exist after timeout, either true or
                        // false
                        if (isSuccessOnTimeout()) {
                            if (log.isBasic())
                                log.logBasic(toString(),
                                        "Didn't detect file [" + realFilename + "] before timeout, success");
                            result.setResult(true);
                        } else {
                            if (log.isBasic())
                                log.logBasic(toString(),
                                        "Didn't detect file [" + realFilename + "] before timeout, failure");
                            result.setResult(false);
                        }
                    }

                    // sleep algorithm
                    long sleepTime = 0;

                    if (iMaximumTimeout == 0) {
                        sleepTime = iCycleTime;
                    } else {
                        if ((now + iCycleTime) < (timeStart + iMaximumTimeout)) {
                            sleepTime = iCycleTime;
                        } else {
                            sleepTime = iCycleTime - ((now + iCycleTime) - (timeStart + iMaximumTimeout));
                        }
                    }

                    try {
                        if (sleepTime > 0) {
                            if (log.isDetailed()) {
                                log.logDetailed(toString(), "Sleeping " + sleepTime
                                        + " seconds before next check for file [" + realFilename + "]");
                            }
                            Thread.sleep(sleepTime * 1000);
                        }
                    } catch (InterruptedException e) {
                        // something strange happened
                        result.setResult(false);
                        continueLoop = false;
                    }
                }
            }

            if (!parentJob.isStopped() && fileObject.exists() && isFileSizeCheck()) {
                long oldSize = -1;
                long newSize = fileObject.getContent().getSize();

                if (log.isDetailed())
                    log.logDetailed(toString(), "File [" + realFilename + "] is " + newSize + " bytes long");
                if (log.isBasic())
                    log.logBasic(toString(), "Waiting until file [" + realFilename + "] stops growing for "
                            + iCycleTime + " seconds");
                while (oldSize != newSize && !parentJob.isStopped()) {
                    try {
                        if (log.isDetailed()) {
                            log.logDetailed(toString(), "Sleeping " + iCycleTime
                                    + " seconds, waiting for file [" + realFilename + "] to stop growing");
                        }
                        Thread.sleep(iCycleTime * 1000);
                    } catch (InterruptedException e) {
                        // something strange happened
                        result.setResult(false);
                        continueLoop = false;
                    }
                    oldSize = newSize;
                    newSize = fileObject.getContent().getSize();
                    if (log.isDetailed()) {
                        log.logDetailed(toString(),
                                "File [" + realFilename + "] is " + newSize + " bytes long");
                    }
                }
                if (log.isBasic())
                    log.logBasic(toString(), "Stopped waiting for file [" + realFilename + "] to stop growing");
            }

            if (parentJob.isStopped()) {
                result.setResult(false);
            }
        } catch (IOException e) {
            log.logBasic(toString(), "Exception while waiting for file [" + realFilename + "] to stop growing: "
                    + e.getMessage());
        } finally {
            if (fileObject != null) {
                try {
                    fileObject.close();
                } catch (Exception e) {
                }
            }
        }
    } else {
        log.logError(toString(), "No filename is defined.");
    }

    return result;
}

From source file:com.panet.imeta.job.entries.xslt.JobEntryXSLT.java

public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) {
    LogWriter log = LogWriter.getInstance();
    Result result = previousResult;
    result.setResult(false);//from  w w w.  ja va2s  . co m

    String realxmlfilename = getRealxmlfilename();
    String realxslfilename = getRealxslfilename();
    String realoutputfilename = getRealoutputfilename();

    FileObject xmlfile = null;
    FileObject xslfile = null;
    FileObject outputfile = null;

    try

    {

        if (xmlfilename != null && xslfilename != null && outputfilename != null) {
            xmlfile = KettleVFS.getFileObject(realxmlfilename);
            xslfile = KettleVFS.getFileObject(realxslfilename);
            outputfile = KettleVFS.getFileObject(realoutputfilename);

            if (xmlfile.exists() && xslfile.exists()) {
                if (outputfile.exists() && iffileexists == 2) {
                    //Output file exists
                    // User want to fail
                    log.logError(toString(), Messages.getString("JobEntryXSLT.OuputFileExists1.Label")
                            + realoutputfilename + Messages.getString("JobEntryXSLT.OuputFileExists2.Label"));
                    result.setResult(false);
                    result.setNrErrors(1);
                }

                else if (outputfile.exists() && iffileexists == 1) {
                    // Do nothing
                    if (log.isDebug())
                        log.logDebug(toString(),
                                Messages.getString("JobEntryXSLT.OuputFileExists1.Label") + realoutputfilename
                                        + Messages.getString("JobEntryXSLT.OuputFileExists2.Label"));
                    result.setResult(true);
                } else {
                    if (outputfile.exists() && iffileexists == 0) {
                        // the output file exists and user want to create new one with unique name
                        //Format Date

                        // Try to clean filename (without wildcard)
                        String wildcard = realoutputfilename.substring(realoutputfilename.length() - 4,
                                realoutputfilename.length());
                        if (wildcard.substring(0, 1).equals(".")) {
                            // Find wildcard
                            realoutputfilename = realoutputfilename.substring(0,
                                    realoutputfilename.length() - 4) + "_"
                                    + StringUtil.getFormattedDateTimeNow(true) + wildcard;
                        } else {
                            // did not find wildcard
                            realoutputfilename = realoutputfilename + "_"
                                    + StringUtil.getFormattedDateTimeNow(true);
                        }
                        if (log.isDebug())
                            log.logDebug(toString(),
                                    Messages.getString("JobEntryXSLT.OuputFileExists1.Label")
                                            + realoutputfilename
                                            + Messages.getString("JobEntryXSLT.OuputFileExists2.Label"));
                        log.logDebug(toString(),
                                Messages.getString("JobEntryXSLT.OuputFileNameChange1.Label")
                                        + realoutputfilename
                                        + Messages.getString("JobEntryXSLT.OuputFileNameChange2.Label"));
                    }

                    // Create transformer factory
                    TransformerFactory factory = TransformerFactory.newInstance();

                    if (xsltfactory.equals(FACTORY_SAXON)) {
                        // Set the TransformerFactory to the SAXON implementation.
                        factory = new net.sf.saxon.TransformerFactoryImpl();
                    }

                    if (log.isDetailed())
                        log.logDetailed(Messages.getString("JobEntryXSL.Log.TransformerFactoryInfos"), Messages
                                .getString("JobEntryXSL.Log.TransformerFactory", factory.getClass().getName()));

                    // Use the factory to create a template containing the xsl file
                    Templates template = factory
                            .newTemplates(new StreamSource(KettleVFS.getInputStream(xslfile)));

                    // Use the template to create a transformer
                    Transformer xformer = template.newTransformer();

                    if (log.isDetailed())
                        log.logDetailed(Messages.getString("JobEntryXSL.Log.TransformerClassInfos"), Messages
                                .getString("JobEntryXSL.Log.TransformerClass", xformer.getClass().getName()));

                    // Prepare the input and output files
                    Source source = new StreamSource(KettleVFS.getInputStream(xmlfile));
                    StreamResult resultat = new StreamResult(KettleVFS.getOutputStream(outputfile, false));

                    // Apply the xsl file to the source file and write the result to the output file
                    xformer.transform(source, resultat);

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

                    // Everything is OK
                    result.setResult(true);
                }
            } else {

                if (!xmlfile.exists()) {
                    log.logError(toString(), Messages.getString("JobEntryXSLT.FileDoesNotExist1.Label")
                            + realxmlfilename + Messages.getString("JobEntryXSLT.FileDoesNotExist2.Label"));
                }
                if (!xslfile.exists()) {
                    log.logError(toString(), Messages.getString("JobEntryXSLT.FileDoesNotExist1.Label")
                            + realxslfilename + Messages.getString("JobEntryXSLT.FileDoesNotExist2.Label"));
                }
                result.setResult(false);
                result.setNrErrors(1);
            }

        } else {
            log.logError(toString(), Messages.getString("JobEntryXSLT.AllFilesNotNull.Label"));
            result.setResult(false);
            result.setNrErrors(1);
        }
    } catch (Exception e) {
        log.logError(toString(),
                Messages.getString("JobEntryXSLT.ErrorXLST.Label")
                        + Messages.getString("JobEntryXSLT.ErrorXLSTXML1.Label") + realxmlfilename
                        + Messages.getString("JobEntryXSLT.ErrorXLSTXML2.Label")
                        + Messages.getString("JobEntryXSLT.ErrorXLSTXSL1.Label") + realxslfilename
                        + Messages.getString("JobEntryXSLT.ErrorXLSTXSL2.Label") + e.getMessage());
        result.setResult(false);
        result.setNrErrors(1);
    } finally {
        try {
            if (xmlfile != null)
                xmlfile.close();

            if (xslfile != null)
                xslfile.close();
            if (outputfile != null)
                outputfile.close();

            // file object is not properly garbaged collected and thus the file cannot
            // be deleted anymore. This is a known problem in the JVM.

            System.gc();
        } catch (IOException e) {
        }
    }

    return result;
}

From source file:be.ibridge.kettle.job.entry.getpop.JobEntryGetPOP.java

public Result execute(Result prev_result, int nr, Repository rep, Job parentJob) {
    LogWriter log = LogWriter.getInstance();
    Result result = new Result(nr);
    result.setResult(false);/*from  w ww . j ava  2s  .  c o m*/
    result.setNrErrors(1);

    FileObject fileObject = null;

    //Get system properties

    //Properties prop = System.getProperties();
    Properties prop = new Properties();

    //Create session object
    //Session sess = Session.getDefaultInstance(prop,null);
    Session sess = Session.getInstance(prop, null);
    sess.setDebug(true);

    try {

        int nbrmailtoretrieve = Const.toInt(firstmails, 0);
        fileObject = KettleVFS.getFileObject(getRealOutputDirectory());

        // Check if output folder exists
        if (!fileObject.exists()) {
            log.logError(toString(),
                    Messages.getString("JobGetMailsFromPOP.FolderNotExists1.Label") + getRealOutputDirectory()
                            + Messages.getString("JobGetMailsFromPOP.FolderNotExists2.Label"));
        } else {

            String host = getRealServername();
            String user = getRealUsername();
            String pwd = getRealPassword();

            Store st = null;

            if (!getUseSSL()) {

                //Create POP3 object               
                st = sess.getStore("pop3");

                // Try to connect to the server
                st.connect(host, user, pwd);
            } else {
                // Ssupports POP3 connection with SSL, the connection is established via SSL.

                String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

                //Properties pop3Props = new Properties();

                prop.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
                prop.setProperty("mail.pop3.socketFactory.fallback", "false");
                prop.setProperty("mail.pop3.port", getRealSSLPort());
                prop.setProperty("mail.pop3.socketFactory.port", getRealSSLPort());

                URLName url = new URLName("pop3", host, Const.toInt(getRealSSLPort(), 995), "", user, pwd);

                st = new POP3SSLStore(sess, url);

                st.connect();

            }

            log.logDetailed(toString(), Messages.getString("JobGetMailsFromPOP.LoggedWithUser.Label") + user);

            //Open default folder   INBOX 
            Folder f = st.getFolder("INBOX");
            f.open(Folder.READ_ONLY);

            if (f == null) {
                log.logError(toString(), Messages.getString("JobGetMailsFromPOP.InvalidFolder.Label"));

            } else {

                log.logDetailed(toString(),
                        Messages.getString("JobGetMailsFromPOP.TotalMessagesFolder1.Label") + f.getName()
                                + Messages.getString("JobGetMailsFromPOP.TotalMessagesFolder2.Label")
                                + f.getMessageCount());
                log.logDetailed(toString(),
                        Messages.getString("JobGetMailsFromPOP.TotalNewMessagesFolder1.Label") + f.getName()
                                + Messages.getString("JobGetMailsFromPOP.TotalNewMessagesFolder2.Label")
                                + f.getNewMessageCount());

                // Get emails 
                Message msg_list[] = getPOPMessages(f, retrievemails);

                if (msg_list.length > 0) {
                    List current_file_POP = new ArrayList();
                    List current_filepath_POP = new ArrayList();
                    int nb_email_POP = 1;
                    DateFormat dateFormat = new SimpleDateFormat("hhmmss_mmddyyyy");

                    String startpattern = "name";
                    if (!Const.isEmpty(getRealFilenamePattern())) {
                        startpattern = getRealFilenamePattern();
                    }

                    for (int i = 0; i < msg_list.length; i++)

                    {

                        /*if(msg[i].isMimeType("text/plain"))
                         {
                         log.logDetailed(toString(), "Expediteur: "+msg[i].getFrom()[0]);
                         log.logDetailed(toString(), "Sujet: "+msg[i].getSubject());
                         log.logDetailed(toString(), "Texte: "+(String)msg[i].getContent());
                                
                         }*/

                        if ((nb_email_POP <= nbrmailtoretrieve && retrievemails == 2) || (retrievemails != 2)) {

                            Message msg_POP = msg_list[i];
                            log.logDetailed(toString(), Messages.getString("JobGetMailsFromPOP.EmailFrom.Label")
                                    + msg_list[i].getFrom()[0]);
                            log.logDetailed(toString(),
                                    Messages.getString("JobGetMailsFromPOP.EmailSubject.Label")
                                            + msg_list[i].getSubject());

                            String localfilename_message = startpattern + "_" + dateFormat.format(new Date())
                                    + "_" + (i + 1) + ".mail";

                            log.logDetailed(toString(),
                                    Messages.getString("JobGetMailsFromPOP.LocalFilename1.Label")
                                            + localfilename_message
                                            + Messages.getString("JobGetMailsFromPOP.LocalFilename2.Label"));

                            File filename_message = new File(getRealOutputDirectory(), localfilename_message);
                            OutputStream os_filename = new FileOutputStream(filename_message);
                            Enumeration enums_POP = msg_POP.getAllHeaders();
                            while (enums_POP.hasMoreElements())

                            {
                                Header header_POP = (Header) enums_POP.nextElement();
                                os_filename.write(new StringBuffer(header_POP.getName()).append(": ")
                                        .append(header_POP.getValue()).append("\r\n").toString().getBytes());
                            }
                            os_filename.write("\r\n".getBytes());
                            InputStream in_POP = msg_POP.getInputStream();
                            byte[] buffer_POP = new byte[1024];
                            int length_POP = 0;
                            while ((length_POP = in_POP.read(buffer_POP, 0, 1024)) != -1) {
                                os_filename.write(buffer_POP, 0, length_POP);
                            }
                            os_filename.close();
                            nb_email_POP++;
                            current_file_POP.add(filename_message);
                            current_filepath_POP.add(filename_message.getPath());

                            if (delete) {
                                log.logDetailed(toString(),
                                        Messages.getString("JobGetMailsFromPOP.DeleteEmail.Label"));
                                msg_POP.setFlag(javax.mail.Flags.Flag.DELETED, true);
                            }
                        }

                    }
                }
                // Close and exit 
                if (f != null)
                    f.close(false);
                if (st != null)
                    st.close();

                f = null;
                st = null;
                sess = null;

                result.setNrErrors(0);
                result.setResult(true);

            }
        }

    }

    catch (NoSuchProviderException e) {
        log.logError(toString(), "provider error: " + e.getMessage());
    } catch (MessagingException e) {
        log.logError(toString(), "Message error: " + e.getMessage());
    }

    catch (Exception e) {
        log.logError(toString(), "Inexpected error: " + e.getMessage());
    }

    finally {
        if (fileObject != null) {
            try {
                fileObject.close();
            } catch (IOException ex) {
            }
            ;
        }
        sess = null;

    }

    return result;
}

From source file:com.panet.imeta.job.entries.movefiles.JobEntryMoveFiles.java

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

    FileObject destinationfile = null;
    boolean retval = false;
    try {//from w w w . ja va2 s.  c o  m
        if (!destinationfilename.exists()) {
            if (!simulate)
                sourcefilename.moveTo(destinationfilename);
            if (log.isDetailed())
                log.logDetailed(toString(), Messages.getString("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(), log, result, parentJob);

            updateSuccess();

        } else {
            if (log.isDetailed())
                log.logDetailed(toString(),
                        Messages.getString("JobMoveFiles.Log.FileExists", destinationfilename.toString()));
            if (iffileexists.equals("overwrite_file")) {
                if (!simulate)
                    sourcefilename.moveTo(destinationfilename);
                if (log.isDetailed())
                    log.logDetailed(toString(), Messages.getString("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(), log, 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) {
                    log.logError(toString(), Messages.getString(
                            Messages.getString("JobMoveFiles.Error.GettingFilename", short_filename)));
                    return retval;
                }

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

                if (!simulate)
                    sourcefilename.moveTo(destinationfile);
                if (log.isDetailed())
                    log.logDetailed(toString(), Messages.getString("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(), log, result, parentJob);

                updateSuccess();
            } else if (iffileexists.equals("delete_file")) {
                if (!simulate)
                    destinationfilename.delete();
                if (log.isDetailed())
                    log.logDetailed(toString(), Messages.getString("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) {
                    log.logError(toString(), Messages.getString(
                            Messages.getString("JobMoveFiles.Error.GettingFilename", short_filename)));
                    return retval;
                }

                String movetofilenamefull = movetofolderfolder.toString() + Const.FILE_SEPARATOR
                        + short_filename;
                destinationfile = KettleVFS.getFileObject(movetofilenamefull);
                if (!destinationfile.exists()) {
                    if (!simulate)
                        sourcefilename.moveTo(destinationfile);
                    if (log.isDetailed())
                        log.logDetailed(toString(), Messages.getString("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(), log, result, parentJob);

                } else {
                    if (ifmovedfileexists.equals("overwrite_file")) {
                        if (!simulate)
                            sourcefilename.moveTo(destinationfile);
                        if (log.isDetailed())
                            log.logDetailed(toString(), Messages.getString("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(), log, 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);

                        if (!simulate)
                            sourcefilename.moveTo(destinationfile);
                        if (log.isDetailed())
                            log.logDetailed(toString(), Messages.getString("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(), log, result, parentJob);

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

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

        }
    } catch (Exception e) {
        log.logError(toString(), Messages.getString("JobMoveFiles.Error.Exception.MoveProcessError",
                sourcefilename.toString(), destinationfilename.toString(), e.getMessage()));
    } finally {
        if (destinationfile != null) {
            try {
                destinationfile.close();
            } catch (IOException ex) {
            }
            ;
        }
    }
    return retval;
}

From source file:com.panet.imeta.job.entries.copyfiles.JobEntryCopyFiles.java

private boolean ProcessFileFolder(String sourcefilefoldername, String destinationfilefoldername,
        String wildcard, Job parentJob, Result result) {

    LogWriter log = LogWriter.getInstance();
    boolean entrystatus = false;
    FileObject sourcefilefolder = null;
    FileObject destinationfilefolder = null;

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

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

    try {

        // Here gc() is explicitly called if e.g. createfile is used in the
        // same
        // job for the same file. The problem is that after creating the
        // file the
        // file object is not properly garbaged collected and thus the file
        // cannot
        // be deleted anymore. This is a known problem in the JVM.

        System.gc();

        sourcefilefolder = KettleVFS.getFileObject(realSourceFilefoldername);
        destinationfilefolder = KettleVFS.getFileObject(realDestinationFilefoldername);

        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)// destinationfilefolder.getType().equals(FileType.FILE))
                {
                    // Source is a folder, destination is a file
                    // WARNING !!! CAN NOT COPY FOLDER TO FILE !!!

                    log.logError(Messages.getString("JobCopyFiles.Log.Forbidden"),
                            Messages.getString("JobCopyFiles.Log.CanNotCopyFolderToFile",
                                    realSourceFilefoldername, realDestinationFilefoldername));

                    NbrFail++;

                } else {

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

                        destinationfilefolder.copyFrom(sourcefilefolder.getParent(),
                                new TextOneFileSelector(sourcefilefolder.getParent().toString(),
                                        sourcefilefolder.getName().getBaseName(),
                                        destinationfilefolder.toString()));
                        if (log.isDetailed())
                            log.logDetailed(Messages.getString("JobCopyFiles.Log.FileCopiedInfos"),
                                    Messages.getString("JobCopyFiles.Log.FileCopied",
                                            sourcefilefolder.getName().toString(),
                                            destinationfilefolder.getName().toString()));

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

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

                        }
                        destinationfilefolder.copyFrom(sourcefilefolder,
                                new TextFileSelector(sourcefilefolder.toString(),
                                        destinationfilefolder.toString(), realWildcard, parentJob));
                    }

                    // Remove Files if needed
                    if (remove_source_files && !list_files_remove.isEmpty()) {
                        for (Iterator<String> iter = list_files_remove.iterator(); iter.hasNext()
                                && !parentJob.isStopped();) {
                            String fileremoventry = (String) iter.next();
                            // Remove ONLY Files
                            if (KettleVFS.getFileObject(fileremoventry).getType() == FileType.FILE) {
                                boolean deletefile = KettleVFS.getFileObject(fileremoventry).delete();
                                log.logBasic("", " ------ ");
                                if (!deletefile) {
                                    log.logError("      " + Messages.getString("JobCopyFiles.Log.Error"),
                                            Messages.getString(
                                                    "JobCopyFiles.Error.Exception.CanRemoveFileFolder",
                                                    fileremoventry));
                                } else {
                                    if (log.isDetailed())
                                        log.logDetailed(
                                                "      " + Messages
                                                        .getString("JobCopyFiles.Log.FileFolderRemovedInfos"),
                                                Messages.getString("JobCopyFiles.Log.FileFolderRemoved",
                                                        fileremoventry));
                                }
                            }
                        }
                    }

                    // Add files to result files name
                    if (add_result_filesname && !list_add_result.isEmpty()) {
                        for (Iterator<String> iter = list_add_result.iterator(); iter.hasNext();) {
                            String fileaddentry = (String) iter.next();
                            // Add ONLY Files
                            if (KettleVFS.getFileObject(fileaddentry).getType() == FileType.FILE) {
                                ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL,
                                        KettleVFS.getFileObject(fileaddentry), parentJob.getJobname(),
                                        toString());
                                result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                                if (log.isDetailed()) {
                                    log.logDetailed("", " ------ ");
                                    log.logDetailed(
                                            "      " + Messages.getString("JobCopyFiles.Log.ResultFilesName"),
                                            Messages.getString("JobCopyFiles.Log.FileAddedToResultFilesName",
                                                    fileaddentry));
                                }
                            }
                        }
                    }
                }
                entrystatus = true;
            } else {
                // Destination Folder or Parent folder is missing
                log.logError(toString(), Messages.getString("JobCopyFiles.Error.DestinationFolderNotFound",
                        realDestinationFilefoldername));
            }
        } else {
            log.logError(toString(),
                    Messages.getString("JobCopyFiles.Error.SourceFileNotExists", realSourceFilefoldername));

        }
    } catch (IOException e) {
        log.logError("Error", Messages.getString("JobCopyFiles.Error.Exception.CopyProcess",
                realSourceFilefoldername.toString(), destinationfilefolder.toString(), e.getMessage()));
    } finally {
        if (sourcefilefolder != null) {
            try {
                sourcefilefolder.close();

            } catch (IOException ex) {
            }
            ;
        }
        if (destinationfilefolder != null) {
            try {
                destinationfilefolder.close();

            } catch (IOException ex) {
            }
            ;
        }
    }

    return entrystatus;
}

From source file:com.panet.imeta.job.entries.getpop.JobEntryGetPOP.java

@SuppressWarnings({ "unchecked" })
public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) {
    LogWriter log = LogWriter.getInstance();
    Result result = previousResult;
    result.setResult(false);/* www .  j  ava 2  s.  c o  m*/
    result.setNrErrors(1);

    //Get system properties
    Properties prop = new Properties();
    prop.setProperty("mail.pop3s.rsetbeforequit", "true"); //$NON-NLS-1$ //$NON-NLS-2$
    prop.setProperty("mail.pop3.rsetbeforequit", "true"); //$NON-NLS-1$ //$NON-NLS-2$

    //Create session object
    Session sess = Session.getDefaultInstance(prop, null);
    sess.setDebug(true);

    FileObject fileObject = null;
    Store st = null;
    Folder f = null;
    try {
        int nbrmailtoretrieve = Const.toInt(firstmails, 0);
        String realOutputFolder = getRealOutputDirectory();
        fileObject = KettleVFS.getFileObject(realOutputFolder);

        // Check if output folder exists
        if (!fileObject.exists()) {
            log.logError(toString(),
                    Messages.getString("JobGetMailsFromPOP.FolderNotExists.Label", realOutputFolder)); //$NON-NLS-1$
        } else {
            if (fileObject.getType() == FileType.FOLDER) {
                String host = getRealServername();
                String user = getRealUsername();
                String pwd = getRealPassword();

                if (!getUseSSL()) {
                    //Create POP3 object
                    st = sess.getStore("pop3"); //$NON-NLS-1$

                    // Try to connect to the server
                    st.connect(host, user, pwd);
                } else {
                    // Ssupports POP3 connection with SSL, the connection is established via SSL.

                    String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; //$NON-NLS-1$
                    prop.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY); //$NON-NLS-1$
                    prop.setProperty("mail.pop3.socketFactory.fallback", "false"); //$NON-NLS-1$ //$NON-NLS-2$
                    prop.setProperty("mail.pop3.port", getRealSSLPort()); //$NON-NLS-1$
                    prop.setProperty("mail.pop3.socketFactory.port", getRealSSLPort()); //$NON-NLS-1$

                    URLName url = new URLName("pop3", host, Const.toInt(getRealSSLPort(), 995), "", user, pwd); //$NON-NLS-1$ //$NON-NLS-2$
                    st = new POP3SSLStore(sess, url);
                    st.connect();
                }
                if (log.isDetailed())
                    log.logDetailed(toString(),
                            Messages.getString("JobGetMailsFromPOP.LoggedWithUser.Label") + user); //$NON-NLS-1$

                //Open the INBOX FOLDER
                // For POP3, the only folder available is the INBOX.
                f = st.getFolder("INBOX"); //$NON-NLS-1$

                if (f == null) {
                    log.logError(toString(), Messages.getString("JobGetMailsFromPOP.InvalidFolder.Label")); //$NON-NLS-1$

                } else {
                    // Open folder
                    if (delete)
                        f.open(Folder.READ_WRITE);
                    else
                        f.open(Folder.READ_ONLY);

                    Message messageList[] = f.getMessages();
                    if (log.isDetailed()) {
                        log.logDetailed(toString(),
                                Messages.getString("JobGetMailsFromPOP.TotalMessagesFolder.Label", f.getName(), //$NON-NLS-1$
                                        String.valueOf(messageList.length)));
                        log.logDetailed(toString(),
                                Messages.getString("JobGetMailsFromPOP.TotalUnreadMessagesFolder.Label", //$NON-NLS-1$
                                        f.getName(), String.valueOf(f.getUnreadMessageCount())));
                    }
                    // Get emails
                    Message msg_list[] = getPOPMessages(f, retrievemails);

                    if (msg_list.length > 0) {
                        List<File> current_file_POP = new ArrayList<File>();
                        List<String> current_filepath_POP = new ArrayList<String>();
                        int nb_email_POP = 1;

                        String startpattern = "name"; //$NON-NLS-1$
                        if (!Const.isEmpty(getRealFilenamePattern())) {
                            startpattern = getRealFilenamePattern();
                        }

                        for (int i = 0; i < msg_list.length; i++) {
                            if ((nb_email_POP <= nbrmailtoretrieve && retrievemails == 2)
                                    || (retrievemails != 2)) {
                                Message msg_POP = msg_list[i];
                                if (log.isDetailed()) {
                                    log.logDetailed(toString(),
                                            Messages.getString("JobGetMailsFromPOP.EmailFrom.Label", //$NON-NLS-1$
                                                    msg_list[i].getFrom()[0].toString()));
                                    log.logDetailed(toString(), Messages.getString(
                                            "JobGetMailsFromPOP.EmailSubject.Label", msg_list[i].getSubject())); //$NON-NLS-1$
                                }
                                String localfilename_message = startpattern + "_" //$NON-NLS-1$
                                        + StringUtil.getFormattedDateTimeNow(true) + "_" + (i + 1) + ".mail"; //$NON-NLS-1$ //$NON-NLS-2$
                                if (log.isDetailed())
                                    log.logDetailed(toString(), Messages.getString(
                                            "JobGetMailsFromPOP.LocalFilename.Label", localfilename_message)); //$NON-NLS-1$

                                File filename_message = new File(realOutputFolder, localfilename_message);
                                OutputStream os_filename = new FileOutputStream(filename_message);
                                Enumeration<Header> enums_POP = msg_POP.getAllHeaders();
                                while (enums_POP.hasMoreElements())

                                {
                                    Header header_POP = enums_POP.nextElement();
                                    os_filename.write(new StringBuffer(header_POP.getName()).append(": ") //$NON-NLS-1$
                                            .append(header_POP.getValue()).append("\r\n").toString().getBytes()); //$NON-NLS-1$
                                }
                                os_filename.write("\r\n".getBytes()); //$NON-NLS-1$
                                InputStream in_POP = msg_POP.getInputStream();
                                byte[] buffer_POP = new byte[1024];
                                int length_POP = 0;
                                while ((length_POP = in_POP.read(buffer_POP, 0, 1024)) != -1) {
                                    os_filename.write(buffer_POP, 0, length_POP);

                                }
                                os_filename.close();
                                nb_email_POP++;
                                current_file_POP.add(filename_message);
                                current_filepath_POP.add(filename_message.getPath());

                                // Check attachments
                                Object content = msg_POP.getContent();
                                if (content instanceof Multipart) {
                                    handleMultipart(realOutputFolder, (Multipart) content);
                                }

                                // Check if mail has to be deleted
                                if (delete) {
                                    if (log.isDetailed())
                                        log.logDetailed(toString(),
                                                Messages.getString("JobGetMailsFromPOP.DeleteEmail.Label")); //$NON-NLS-1$
                                    msg_POP.setFlag(javax.mail.Flags.Flag.DELETED, true);
                                }
                            }
                        }
                    }

                    result.setNrErrors(0);
                    result.setResult(true);
                }

            } else {
                log.logError(toString(),
                        Messages.getString("JobGetMailsFromPOP.Error.NotAFolder", realOutputFolder));
            }
        }
    } catch (NoSuchProviderException e) {
        log.logError(toString(), Messages.getString("JobEntryGetPOP.ProviderException", e.getMessage())); //$NON-NLS-1$
    } catch (MessagingException e) {
        log.logError(toString(), Messages.getString("JobEntryGetPOP.MessagingException", e.getMessage())); //$NON-NLS-1$
    } catch (Exception e) {
        log.logError(toString(), Messages.getString("JobEntryGetPOP.GeneralException", e.getMessage())); //$NON-NLS-1$
    } finally {
        if (fileObject != null) {
            try {
                fileObject.close();
            } catch (IOException ex) {
            }
            ;
        }
        //close the folder, passing in a true value to expunge the deleted message
        try {
            if (f != null)
                f.close(true);
            if (st != null)
                st.close();
        } catch (Exception e) {
            log.logError(toString(), e.getMessage());
        }
        // free memory
        f = null;
        st = null;
        sess = null;
    }

    return result;
}

From source file:be.ibridge.kettle.job.entry.zipfile.JobEntryZipFile.java

public Result execute(Result prev_result, int nr, Repository rep, Job parentJob) {
    LogWriter log = LogWriter.getInstance();
    Result result = new Result(nr);
    result.setResult(false);//from w w w.  ja v  a 2 s .co  m
    boolean Fileexists = false;

    String realZipfilename = StringUtil.environmentSubstitute(zipFilename);
    String realWildcard = StringUtil.environmentSubstitute(wildcard);
    String realWildcardExclude = StringUtil.environmentSubstitute(wildcardexclude);
    String realTargetdirectory = StringUtil.environmentSubstitute(sourcedirectory);
    String realMovetodirectory = StringUtil.environmentSubstitute(movetodirectory);

    if (realZipfilename != null) {
        FileObject fileObject = null;
        try {
            fileObject = KettleVFS.getFileObject(realZipfilename);
            // Check if Zip File exists
            if (fileObject.exists()) {
                Fileexists = true;
                log.logDebug(toString(), Messages.getString("JobZipFiles.Zip_FileExists1.Label")
                        + realZipfilename + Messages.getString("JobZipFiles.Zip_FileExists2.Label"));
            } else {
                Fileexists = false;
            }

            // Let's start the process now
            if (ifzipfileexists == 3 && Fileexists) {
                // the zip file exists and user want to Fail
                result.setResult(false);
                result.setNrErrors(1);

            } else if (ifzipfileexists == 2 && Fileexists) {
                // the zip file exists and user want to do nothing
                result.setResult(true);

            } else if (afterzip == 2 && realMovetodirectory == null) {
                // After Zip, Move files..User must give a destination Folder
                result.setResult(false);
                result.setNrErrors(1);
                log.logError(toString(),
                        Messages.getString("JobZipFiles.AfterZip_No_DestinationFolder_Defined.Label"));

            } else
            // After Zip, Move files..User must give a destination Folder
            {

                if (ifzipfileexists == 0 && Fileexists) {

                    // the zip file exists and user want to create new one with unique name
                    //Format Date

                    DateFormat dateFormat = new SimpleDateFormat("hhmmss_mmddyyyy");
                    realZipfilename = realZipfilename + "_" + dateFormat.format(new Date()) + ".zip";
                    log.logDebug(toString(), Messages.getString("JobZipFiles.Zip_FileNameChange1.Label")
                            + realZipfilename + Messages.getString("JobZipFiles.Zip_FileNameChange1.Label"));

                } else if (ifzipfileexists == 1 && Fileexists) {
                    log.logDebug(toString(), Messages.getString("JobZipFiles.Zip_FileAppend1.Label")
                            + realZipfilename + Messages.getString("JobZipFiles.Zip_FileAppend2.Label"));
                }

                // Get all the files in the directory...

                File f = new File(realTargetdirectory);

                String[] filelist = f.list();

                log.logDetailed(toString(),
                        Messages.getString("JobZipFiles.Files_Found1.Label") + filelist.length
                                + Messages.getString("JobZipFiles.Files_Found2.Label") + realTargetdirectory
                                + Messages.getString("JobZipFiles.Files_Found3.Label"));

                Pattern pattern = null;
                if (!Const.isEmpty(realWildcard)) {
                    pattern = Pattern.compile(realWildcard);

                }
                Pattern patternexclude = null;
                if (!Const.isEmpty(realWildcardExclude)) {
                    patternexclude = Pattern.compile(realWildcardExclude);

                }

                // Prepare Zip File
                byte[] buffer = new byte[18024];

                FileOutputStream dest = new FileOutputStream(realZipfilename);
                BufferedOutputStream buff = new BufferedOutputStream(dest);
                ZipOutputStream out = new ZipOutputStream(buff);

                // Set the method
                out.setMethod(ZipOutputStream.DEFLATED);

                // Set the compression level
                if (compressionrate == 0) {
                    out.setLevel(Deflater.NO_COMPRESSION);
                } else if (compressionrate == 1) {
                    out.setLevel(Deflater.DEFAULT_COMPRESSION);
                }
                if (compressionrate == 2) {
                    out.setLevel(Deflater.BEST_COMPRESSION);
                }
                if (compressionrate == 3) {
                    out.setLevel(Deflater.BEST_SPEED);
                }

                // Specify Zipped files (After that we will move,delete them...)
                String[] ZippedFiles = new String[filelist.length];
                int FileNum = 0;

                // Get the files in the list...
                for (int i = 0; i < filelist.length && !parentJob.isStopped(); i++) {
                    boolean getIt = true;
                    boolean getItexclude = false;

                    // First see if the file matches the regular expression!
                    if (pattern != null) {
                        Matcher matcher = pattern.matcher(filelist[i]);
                        getIt = matcher.matches();
                    }

                    if (patternexclude != null) {
                        Matcher matcherexclude = patternexclude.matcher(filelist[i]);
                        getItexclude = matcherexclude.matches();
                    }

                    // Get processing File
                    String targetFilename = realTargetdirectory + Const.FILE_SEPARATOR + filelist[i];
                    File file = new File(targetFilename);

                    if (getIt && !getItexclude && !file.isDirectory()) {

                        // We can add the file to the Zip Archive

                        log.logDebug(toString(),
                                Messages.getString("JobZipFiles.Add_FilesToZip1.Label") + filelist[i]
                                        + Messages.getString("JobZipFiles.Add_FilesToZip2.Label")
                                        + realTargetdirectory
                                        + Messages.getString("JobZipFiles.Add_FilesToZip3.Label"));

                        // Associate a file input stream for the current file
                        FileInputStream in = new FileInputStream(targetFilename);

                        // Add ZIP entry to output stream.
                        out.putNextEntry(new ZipEntry(filelist[i]));

                        int len;
                        while ((len = in.read(buffer)) > 0) {
                            out.write(buffer, 0, len);
                        }

                        out.closeEntry();

                        // Close the current file input stream
                        in.close();

                        // Get Zipped File
                        ZippedFiles[FileNum] = filelist[i];
                        FileNum = FileNum + 1;
                    }
                }

                // Close the ZipOutPutStream
                out.close();

                //-----Get the list of Zipped Files and Move or Delete Them
                if (afterzip == 1 || afterzip == 2) {
                    // iterate through the array of Zipped files
                    for (int i = 0; i < ZippedFiles.length; i++) {
                        if (ZippedFiles[i] != null) {
                            // Delete File
                            FileObject fileObjectd = KettleVFS
                                    .getFileObject(realTargetdirectory + Const.FILE_SEPARATOR + ZippedFiles[i]);

                            // Here we can move, delete files
                            if (afterzip == 1) {
                                // Delete File
                                boolean deleted = fileObjectd.delete();
                                if (!deleted) {
                                    result.setResult(false);
                                    result.setNrErrors(1);
                                    log.logError(toString(),
                                            Messages.getString("JobZipFiles.Cant_Delete_File1.Label")
                                                    + realTargetdirectory + Const.FILE_SEPARATOR
                                                    + ZippedFiles[i] + Messages
                                                            .getString("JobZipFiles.Cant_Delete_File2.Label"));

                                }
                                // File deleted
                                log.logDebug(toString(),
                                        Messages.getString("JobZipFiles.File_Deleted1.Label")
                                                + realTargetdirectory + Const.FILE_SEPARATOR + ZippedFiles[i]
                                                + Messages.getString("JobZipFiles.File_Deleted2.Label"));
                            } else if (afterzip == 2) {
                                // Move File   
                                try {
                                    FileObject fileObjectm = KettleVFS.getFileObject(
                                            realMovetodirectory + Const.FILE_SEPARATOR + ZippedFiles[i]);
                                    fileObjectd.moveTo(fileObjectm);
                                } catch (IOException e) {
                                    log.logError(toString(),
                                            Messages.getString("JobZipFiles.Cant_Move_File1.Label")
                                                    + ZippedFiles[i]
                                                    + Messages.getString("JobZipFiles.Cant_Move_File2.Label")
                                                    + e.getMessage());
                                    result.setResult(false);
                                    result.setNrErrors(1);
                                }
                                // File moved
                                log.logDebug(toString(), Messages.getString("JobZipFiles.File_Moved1.Label")
                                        + ZippedFiles[i] + Messages.getString("JobZipFiles.File_Moved2.Label"));
                            }
                        }
                    }
                }
                result.setResult(true);
            }
        } catch (IOException e) {
            log.logError(toString(),
                    Messages.getString("JobZipFiles.Cant_CreateZipFile1.Label") + realZipfilename
                            + Messages.getString("JobZipFiles.Cant_CreateZipFile2.Label") + e.getMessage());
            result.setResult(false);
            result.setNrErrors(1);
        } finally {
            if (fileObject != null) {
                try {
                    fileObject.close();
                } catch (IOException ex) {
                }
                ;
            }
        }
    } else {
        result.setResult(false);
        result.setNrErrors(1);
        log.logError(toString(), Messages.getString("JobZipFiles.No_ZipFile_Defined.Label"));
    }

    return result;
}

From source file:com.panet.imeta.job.entries.mssqlbulkload.JobEntryMssqlBulkLoad.java

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

    LogWriter log = LogWriter.getInstance();

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

    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);
            if (!(fileObject instanceof LocalFile)) {
                // MSSQL BUKL INSERT can only use local files, so that's
                // what we limit ourselves to.
                //
                throw new KettleException(
                        Messages.getString("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())
                    log.logDetailed(toString(),
                            Messages.getString("JobMssqlBulkLoad.FileExists.Label", realFilename));

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

                    if (db.getDatabaseMeta().getDatabaseType() != DatabaseMeta.TYPE_DATABASE_MSSQL) {
                        log.logError(toString(), Messages.getString("JobMssqlBulkLoad.Error.DbNotMSSQL",
                                connection.getDatabaseName()));
                        return result;
                    }
                    db.shareVariablesWith(this);
                    try {
                        db.connect();
                        // Get schemaname
                        String realSchemaname = environmentSubstitute(schemaname);
                        // Get tablename
                        String realTablename = environmentSubstitute(tablename);

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

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

                            // FIELDTERMINATOR
                            String Fieldterminator = getRealFieldTerminator();
                            if (Const.isEmpty(Fieldterminator)
                                    && (datafiletype.equals("char") || datafiletype.equals("widechar"))) {
                                log.logError(toString(),
                                        Messages.getString("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")) {
                                if (specificcodepage.length() < 0) {
                                    log.logError(toString(), Messages
                                            .getString("JobMssqlBulkLoad.Error.SpecificCodePageMissing"));
                                    return result;

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

                            // Check Error file
                            if (errorfilename != null) {
                                File errorfile = new File(errorfilename);
                                if (errorfile.exists() && !adddatetime) {
                                    // The error file is created when the
                                    // command is executed. An error occurs
                                    // if the file already exists.
                                    log.logError(toString(),
                                            Messages.getString("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 ='" + errorfilename + "_" + d + "'";
                                } else
                                    ErrorfileName = "ERRORFILE ='" + errorfilename + "'";
                            }

                            // 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 " + 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;
                            if (formatfilename != null)
                                SQLBULKLOAD = SQLBULKLOAD + ", FORMATFILE='" + formatfilename + "'";
                            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.execStatements(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), parentJob.getJobname(),
                                            toString());
                                    result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                                }

                                result.setResult(true);
                            } catch (KettleDatabaseException je) {
                                result.setNrErrors(1);
                                log.logError(toString(),
                                        "An error occurred executing this job entry : " + je.getMessage());
                            } catch (IOException e) {
                                log.logError(toString(),
                                        "An error occurred executing this job entry : " + e.getMessage());
                                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())
                                log.logDetailed(toString(), Messages
                                        .getString("JobMssqlBulkLoad.Error.TableNotExists", realTablename));
                        }
                    } catch (KettleDatabaseException dbe) {
                        db.disconnect();
                        result.setNrErrors(1);
                        log.logError(toString(), "An error occurred executing this entry: " + dbe.getMessage());
                    }
                } else {
                    // No database connection is defined
                    result.setNrErrors(1);
                    log.logError(toString(), Messages.getString("JobMssqlBulkLoad.Nodatabase.Label"));
                }
            } else {
                // the file doesn't exist
                result.setNrErrors(1);
                log.logError(toString(),
                        Messages.getString("JobMssqlBulkLoad.Error.FileNotExists", realFilename));
            }
        } catch (Exception e) {
            // An unexpected error occurred
            result.setNrErrors(1);
            log.logError(toString(), Messages.getString("JobMssqlBulkLoad.UnexpectedError.Label"), e);
        } finally {
            try {
                if (fileObject != null)
                    fileObject.close();
            } catch (Exception e) {
            }
        }
    } else {
        // No file was specified
        result.setNrErrors(1);
        log.logError(toString(), Messages.getString("JobMssqlBulkLoad.Nofilename.Label"));
    }
    return result;
}