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

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

Introduction

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

Prototype

public FileContent getContent() throws FileSystemException;

Source Link

Document

Returns this file's content.

Usage

From source file:com.panet.imeta.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java

public static double getFileSize(Context actualContext, Scriptable actualObject, Object[] ArgList,
        Function FunctionContext) {
    try {//  ww w . j av  a 2 s  .  c o m
        if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) {
            if (ArgList[0].equals(null))
                return 0;
            FileObject file = null;

            try {
                // Source file
                file = KettleVFS.getFileObject(Context.toString(ArgList[0]));
                long filesize = 0;
                if (file.exists()) {
                    if (file.getType().equals(FileType.FILE))
                        filesize = file.getContent().getSize();
                    else
                        Context.reportRuntimeError("[" + Context.toString(ArgList[0]) + "] is not a file!");
                } else {
                    Context.reportRuntimeError("file [" + Context.toString(ArgList[0]) + "] can not be found!");
                }
                return filesize;
            } catch (IOException e) {
                throw Context
                        .reportRuntimeError("The function call getFileSize throw an error : " + e.toString());
            } finally {
                if (file != null)
                    try {
                        file.close();
                    } catch (Exception e) {
                    }
            }

        } else {
            throw Context.reportRuntimeError("The function call getFileSize is not valid.");
        }
    } catch (Exception e) {
        throw Context.reportRuntimeError(e.toString());
    }
}

From source file:com.panet.imeta.job.entries.ssh2put.JobEntrySSH2PUT.java

private boolean putFile(FileObject localFile, String remotefilename, SFTPv3Client sftpClient) {
    LogWriter log = LogWriter.getInstance();
    long filesize = -1;
    InputStream in = null;//from w ww  .  j a va 2  s . com
    BufferedInputStream inBuf = null;
    SFTPv3FileHandle sftpFileHandle = null;
    boolean retval = false;
    try {
        // Put file in the default folder
        sftpFileHandle = sftpClient.createFileTruncate(remotefilename);

        // Associate a file input stream for the current local file
        in = KettleVFS.getInputStream(localFile);
        inBuf = new BufferedInputStream(in);
        long bytesWritten = 0;
        byte[] buf = new byte[2048];
        long length = localFile.getContent().getSize();

        // Write to remote file
        while (bytesWritten != length) {
            int len = inBuf.read(buf, 0, buf.length);
            sftpClient.write(sftpFileHandle, bytesWritten, buf, 0, len);
            bytesWritten += len;
        }

        // Get File size
        filesize = getFileSize(sftpClient, remotefilename);

        if (log.isDetailed())
            log.logDetailed(toString(),
                    Messages.getString("JobSSH2PUT.Log.FileOnRemoteHost", remotefilename, "" + filesize));

        retval = true;
    } catch (Exception e) {
        // We failed to put files
        log.logError(toString(), Messages.getString("JobSSH2PUT.Log.ErrorCopyingFile", localFile.toString()));
    } finally {
        if (in != null)
            try {
                in.close();
                in = null;
            } catch (Exception ex) {
            }

        if (inBuf != null)
            try {
                inBuf.close();
                inBuf = null;
            } catch (Exception ex) {
            }
        if (sftpFileHandle != null)
            try {
                sftpClient.closeFile(sftpFileHandle);
                sftpFileHandle = null;
            } catch (Exception ex) {
            }
    }
    return retval;
}

From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java

public static String getLastModifiedTime(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    try {/*from  w w  w.ja v  a  2s  .  co  m*/
        if (ArgList.length == 2 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) {
            if (ArgList[0].equals(null))
                return null;
            FileObject file = null;

            try {
                // Source file
                file = KettleVFS.getFileObject((String) ArgList[0]);
                String dateformat = (String) ArgList[1];
                if (isNull(dateformat))
                    dateformat = "yyyy-MM-dd";
                String lastmodifiedtime = null;
                if (file.exists()) {
                    java.util.Date lastmodifiedtimedate = new java.util.Date(
                            file.getContent().getLastModifiedTime());
                    java.text.DateFormat dateFormat = new SimpleDateFormat(dateformat);
                    lastmodifiedtime = dateFormat.format(lastmodifiedtimedate);

                } else {
                    new RuntimeException("file [" + (String) ArgList[0] + "] can not be found!");
                }

                return lastmodifiedtime;
            } catch (IOException e) {
                throw new RuntimeException(
                        "The function call getLastModifiedTime throw an error : " + e.toString());
            } finally {
                if (file != null)
                    try {
                        file.close();
                    } catch (Exception e) {
                    }
            }

        } else {
            throw new RuntimeException("The function call getLastModifiedTime is not valid.");
        }
    } catch (Exception e) {
        throw new RuntimeException(e.toString());
    }
}

From source file:com.panet.imeta.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java

public static String getLastModifiedTime(Context actualContext, Scriptable actualObject, Object[] ArgList,
        Function FunctionContext) {
    try {/*from  ww  w.  j  a  v  a2  s.  c o  m*/
        if (ArgList.length == 2 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) {
            if (ArgList[0].equals(null))
                return null;
            FileObject file = null;

            try {
                // Source file
                file = KettleVFS.getFileObject(Context.toString(ArgList[0]));
                String dateformat = Context.toString(ArgList[1]);
                if (isNull(dateformat))
                    dateformat = "yyyy-MM-dd";
                String lastmodifiedtime = null;
                if (file.exists()) {
                    java.util.Date lastmodifiedtimedate = new java.util.Date(
                            file.getContent().getLastModifiedTime());
                    java.text.DateFormat dateFormat = new SimpleDateFormat(dateformat);
                    lastmodifiedtime = dateFormat.format(lastmodifiedtimedate);

                } else {
                    Context.reportRuntimeError("file [" + Context.toString(ArgList[0]) + "] can not be found!");
                }

                return lastmodifiedtime;
            } catch (IOException e) {
                throw Context.reportRuntimeError(
                        "The function call getLastModifiedTime throw an error : " + e.toString());
            } finally {
                if (file != null)
                    try {
                        file.close();
                    } catch (Exception e) {
                    }
            }

        } else {
            throw Context.reportRuntimeError("The function call getLastModifiedTime is not valid.");
        }
    } catch (Exception e) {
        throw Context.reportRuntimeError(e.toString());
    }
}

From source file:com.panet.imeta.job.entries.shell.JobEntryShell.java

private void executeShell(Result result, List<RowMetaAndData> cmdRows, String[] args) {
    LogWriter log = LogWriter.getInstance();
    FileObject fileObject = null;
    String realScript = null;/* www  .  ja  v  a 2 s. com*/
    FileObject tempFile = null;

    try {
        // What's the exact command?
        String base[] = null;
        List<String> cmds = new ArrayList<String>();

        if (log.isBasic())
            log.logBasic(toString(), Messages.getString("JobShell.RunningOn", Const.getOS()));

        if (insertScript) {
            realScript = environmentSubstitute(script);
        } else {
            String realFilename = environmentSubstitute(getFilename());
            fileObject = KettleVFS.getFileObject(realFilename);
        }

        if (Const.getOS().equals("Windows 95")) {
            base = new String[] { "command.com", "/C" };
        } else if (Const.getOS().startsWith("Windows")) {
            base = new String[] { "cmd.exe", "/C" };
        } else {
            if (!insertScript) {
                // Just set the command to the script we need to execute...
                //
                base = new String[] { KettleVFS.getFilename(fileObject) };
            } else {
                // Create a unique new temporary filename in the working directory, put the script in there
                // Set the permissions to execute and then run it...
                //
                try {
                    tempFile = KettleVFS.createTempFile("kettle", "shell", workDirectory);
                    tempFile.createFile();
                    OutputStream outputStream = tempFile.getContent().getOutputStream();
                    outputStream.write(realScript.getBytes());
                    outputStream.close();
                    String tempFilename = KettleVFS.getFilename(tempFile);
                    // Now we have to make this file executable...
                    // On Unix-like systems this is done using the command "/bin/chmod +x filename"
                    //
                    ProcessBuilder procBuilder = new ProcessBuilder("chmod", "+x", tempFilename);
                    Process proc = procBuilder.start();
                    // Eat/log stderr/stdout all messages in a different thread...
                    StreamLogger errorLogger = new StreamLogger(proc.getErrorStream(),
                            toString() + " (stderr)");
                    StreamLogger outputLogger = new StreamLogger(proc.getInputStream(),
                            toString() + " (stdout)");
                    new Thread(errorLogger).start();
                    new Thread(outputLogger).start();
                    proc.waitFor();

                    // Now set this filename as the base command...
                    //
                    base = new String[] { tempFilename };
                } catch (Exception e) {
                    throw new Exception("Unable to create temporary file to execute script", e);
                }
            }
        }

        // Construct the arguments...
        if (argFromPrevious && cmdRows != null) {
            // Add the base command...
            for (int i = 0; i < base.length; i++)
                cmds.add(base[i]);

            if (Const.getOS().equals("Windows 95") || Const.getOS().startsWith("Windows")) {
                // for windows all arguments including the command itself
                // need to be
                // included in 1 argument to cmd/command.

                StringBuffer cmdline = new StringBuffer(300);

                cmdline.append('"');
                if (insertScript)
                    cmdline.append(realScript);
                else
                    cmdline.append(optionallyQuoteField(KettleVFS.getFilename(fileObject), "\""));
                // Add the arguments from previous results...
                for (int i = 0; i < cmdRows.size(); i++) // Normally just
                // one row, but
                // once in a
                // while to
                // remain
                // compatible we
                // have
                // multiple.
                {
                    RowMetaAndData r = (RowMetaAndData) cmdRows.get(i);
                    for (int j = 0; j < r.size(); j++) {
                        cmdline.append(' ');
                        cmdline.append(optionallyQuoteField(r.getString(j, null), "\""));
                    }
                }
                cmdline.append('"');
                cmds.add(cmdline.toString());
            } else {
                // Add the arguments from previous results...
                for (int i = 0; i < cmdRows.size(); i++) // Normally just
                // one row, but
                // once in a
                // while to
                // remain
                // compatible we
                // have
                // multiple.
                {
                    RowMetaAndData r = (RowMetaAndData) cmdRows.get(i);
                    for (int j = 0; j < r.size(); j++) {
                        cmds.add(optionallyQuoteField(r.getString(j, null), "\""));
                    }
                }
            }
        } else if (args != null) {
            // Add the base command...
            for (int i = 0; i < base.length; i++)
                cmds.add(base[i]);

            if (Const.getOS().equals("Windows 95") || Const.getOS().startsWith("Windows")) {
                // for windows all arguments including the command itself
                // need to be
                // included in 1 argument to cmd/command.

                StringBuffer cmdline = new StringBuffer(300);

                cmdline.append('"');
                if (insertScript)
                    cmdline.append(realScript);
                else
                    cmdline.append(optionallyQuoteField(KettleVFS.getFilename(fileObject), "\""));

                for (int i = 0; i < args.length; i++) {
                    cmdline.append(' ');
                    cmdline.append(optionallyQuoteField(args[i], "\""));
                }
                cmdline.append('"');
                cmds.add(cmdline.toString());
            } else {
                for (int i = 0; i < args.length; i++) {
                    cmds.add(args[i]);
                }
            }
        }

        StringBuffer command = new StringBuffer();

        Iterator<String> it = cmds.iterator();
        boolean first = true;
        while (it.hasNext()) {
            if (!first)
                command.append(' ');
            else
                first = false;
            command.append((String) it.next());
        }
        if (log.isBasic())
            log.logBasic(toString(), Messages.getString("JobShell.ExecCommand", command.toString()));

        // Build the environment variable list...
        ProcessBuilder procBuilder = new ProcessBuilder(cmds);
        Map<String, String> env = procBuilder.environment();
        String[] variables = listVariables();
        for (int i = 0; i < variables.length; i++) {
            env.put(variables[i], getVariable(variables[i]));
        }

        if (getWorkDirectory() != null && !Const.isEmpty(Const.rtrim(getWorkDirectory()))) {
            String vfsFilename = environmentSubstitute(getWorkDirectory());
            File file = new File(KettleVFS.getFilename(KettleVFS.getFileObject(vfsFilename)));
            procBuilder.directory(file);
        }
        Process proc = procBuilder.start();

        // any error message?
        StreamLogger errorLogger = new StreamLogger(proc.getErrorStream(), toString() + " (stderr)");

        // any output?
        StreamLogger outputLogger = new StreamLogger(proc.getInputStream(), toString() + " (stdout)");

        // kick them off
        new Thread(errorLogger).start();
        new Thread(outputLogger).start();

        proc.waitFor();
        if (log.isDetailed())
            log.logDetailed(toString(), Messages.getString("JobShell.CommandFinished", command.toString()));

        // What's the exit status?
        result.setExitStatus(proc.exitValue());
        if (result.getExitStatus() != 0) {
            if (log.isDetailed())
                log.logDetailed(toString(), Messages.getString("JobShell.ExitStatus",
                        environmentSubstitute(getFilename()), "" + result.getExitStatus()));

            result.setNrErrors(1);
        }

        // close the streams
        // otherwise you get "Too many open files, java.io.IOException" after a lot of iterations
        proc.getErrorStream().close();
        proc.getOutputStream().close();

    } catch (IOException ioe) {
        log.logError(toString(), Messages.getString("JobShell.ErrorRunningShell",
                environmentSubstitute(getFilename()), ioe.toString()));
        result.setNrErrors(1);
    } catch (InterruptedException ie) {
        log.logError(toString(), Messages.getString("JobShell.Shellinterupted",
                environmentSubstitute(getFilename()), ie.toString()));
        result.setNrErrors(1);
    } catch (Exception e) {
        log.logError(toString(), Messages.getString("JobShell.UnexpectedError",
                environmentSubstitute(getFilename()), e.toString()));
        result.setNrErrors(1);
    } finally {
        // If we created a temporary file, remove it...
        //
        if (tempFile != null) {
            try {
                tempFile.delete();
            } catch (Exception e) {
                Messages.getString("JobShell.UnexpectedError", tempFile.toString(), e.toString());
            }
        }
    }

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

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

private boolean takeThisFile(LogWriter log, FileObject sourceFile, String destinationFile)
        throws FileSystemException {
    boolean retval = false;
    File destination = new File(destinationFile);
    if (!destination.exists()) {
        if (log.isDebug())
            log.logDebug(toString(), Messages.getString("JobUnZip.Log.CanNotFindFile", destinationFile));
        return true;
    }//www .  ja  v  a 2  s.c  o  m
    if (log.isDebug())
        log.logDebug(toString(), Messages.getString("JobUnZip.Log.FileExists", destinationFile));
    if (iffileexist == IF_FILE_EXISTS_SKIP) {
        if (log.isDebug())
            log.logDebug(toString(), Messages.getString("JobUnZip.Log.FileSkip", destinationFile));
        return false;
    }
    if (iffileexist == IF_FILE_EXISTS_FAIL) {
        updateErrors();
        log.logError(toString(), Messages.getString("JobUnZip.Log.FileError", destinationFile, "" + NrErrors));
        return false;
    }

    if (iffileexist == IF_FILE_EXISTS_OVERWRITE) {
        if (log.isDebug())
            log.logDebug(toString(), Messages.getString("JobUnZip.Log.FileOverwrite", destinationFile));
        return true;
    }

    Long entrySize = sourceFile.getContent().getSize();
    Long destinationSize = destination.length();

    if (iffileexist == IF_FILE_EXISTS_OVERWRITE_DIFF_SIZE) {
        if (entrySize != destinationSize) {
            if (log.isDebug())
                log.logDebug(toString(), Messages.getString("JobUnZip.Log.FileDiffSize.Diff",
                        sourceFile.getName().getURI(), "" + entrySize, destinationFile, "" + destinationSize));
            return true;
        } else {
            if (log.isDebug())
                log.logDebug(toString(), Messages.getString("JobUnZip.Log.FileDiffSize.Same",
                        sourceFile.getName().getURI(), "" + entrySize, destinationFile, "" + destinationSize));
            return false;
        }
    }
    if (iffileexist == IF_FILE_EXISTS_OVERWRITE_EQUAL_SIZE) {
        if (entrySize == destinationSize) {
            if (log.isDebug())
                log.logDebug(toString(), Messages.getString("JobUnZip.Log.FileEqualSize.Same",
                        sourceFile.getName().getURI(), "" + entrySize, destinationFile, "" + destinationSize));
            return true;
        } else {
            if (log.isDebug())
                log.logDebug(toString(), Messages.getString("JobUnZip.Log.FileEqualSize.Diff",
                        sourceFile.getName().getURI(), "" + entrySize, destinationFile, "" + destinationSize));
            return false;
        }
    }
    if (iffileexist == IF_FILE_EXISTS_OVERWRITE_ZIP_BIG) {
        if (entrySize > destinationSize) {
            if (log.isDebug())
                log.logDebug(toString(), Messages.getString("JobUnZip.Log.FileBigSize.Big",
                        sourceFile.getName().getURI(), "" + entrySize, destinationFile, "" + destinationSize));
            return true;
        } else {
            if (log.isDebug())
                log.logDebug(toString(), Messages.getString("JobUnZip.Log.FileBigSize.Small",
                        sourceFile.getName().getURI(), "" + entrySize, destinationFile, "" + destinationSize));
            return false;
        }
    }
    if (iffileexist == IF_FILE_EXISTS_OVERWRITE_ZIP_BIG_EQUAL) {
        if (entrySize >= destinationSize) {
            if (log.isDebug())
                log.logDebug(toString(), Messages.getString("JobUnZip.Log.FileBigEqualSize.Big",
                        sourceFile.getName().getURI(), "" + entrySize, destinationFile, "" + destinationSize));
            return true;
        } else {
            if (log.isDebug())
                log.logDebug(toString(), Messages.getString("JobUnZip.Log.FileBigEqualSize.Small",
                        sourceFile.getName().getURI(), "" + entrySize, destinationFile, "" + destinationSize));
            return false;
        }
    }
    if (iffileexist == IF_FILE_EXISTS_OVERWRITE_ZIP_SMALL) {
        if (entrySize < destinationSize) {
            if (log.isDebug())
                log.logDebug(toString(), Messages.getString("JobUnZip.Log.FileSmallSize.Small",
                        sourceFile.getName().getURI(), "" + entrySize, destinationFile, "" + destinationSize));
            return true;
        } else {
            if (log.isDebug())
                log.logDebug(toString(), Messages.getString("JobUnZip.Log.FileSmallSize.Big",
                        sourceFile.getName().getURI(), "" + entrySize, destinationFile, "" + destinationSize));
            return false;
        }
    }
    if (iffileexist == IF_FILE_EXISTS_OVERWRITE_ZIP_SMALL_EQUAL) {
        if (entrySize <= destinationSize) {
            if (log.isDebug())
                log.logDebug(toString(), Messages.getString("JobUnZip.Log.FileSmallEqualSize.Small",
                        sourceFile.getName().getURI(), "" + entrySize, destinationFile, "" + destinationSize));
            return true;
        } else {
            if (log.isDebug())
                log.logDebug(toString(), Messages.getString("JobUnZip.Log.FileSmallEqualSize.Big",
                        sourceFile.getName().getURI(), "" + entrySize, destinationFile, "" + destinationSize));
            return false;
        }
    }
    if (iffileexist == IF_FILE_EXISTS_UNIQ) {
        // Create file with unique name
        return true;
    }

    return retval;
}

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;

    ZipOutputStream zipOutputStream = null;
    File masterZipfile = null;/*from ww  w . j  a  va2 s .c  o m*/

    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:be.ibridge.kettle.job.entry.mail.JobEntryMail.java

public Result execute(Result result, int nr, Repository rep, Job parentJob) {
    LogWriter log = LogWriter.getInstance();

    File masterZipfile = null;/*from   w  ww.j  ava  2s  . co m*/

    // Send an e-mail...
    // create some properties and get the default Session
    Properties props = new Properties();
    if (Const.isEmpty(server)) {
        log.logError(toString(),
                "Unable to send the mail because the mail-server (SMTP host) is not specified");
        result.setNrErrors(1L);
        result.setResult(false);
        return result;
    }

    String protocol = "smtp";
    if (usingSecureAuthentication) {
        protocol = "smtps";
    }

    props.put("mail." + protocol + ".host", StringUtil.environmentSubstitute(server));
    if (!Const.isEmpty(port))
        props.put("mail." + protocol + ".port", StringUtil.environmentSubstitute(port));
    boolean debug = log.getLogLevel() >= LogWriter.LOG_LEVEL_DEBUG;

    if (debug)
        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(debug);

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

        String email_address = StringUtil.environmentSubstitute(replyAddress);
        if (!Const.isEmpty(email_address)) {
            msg.setFrom(new InternetAddress(email_address));
        } else {
            throw new MessagingException("reply e-mail address is not filled in");
        }

        // Split the mail-address: space separated
        String destinations[] = StringUtil.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);

        if (!Const.isEmpty(destinationCc)) {
            // Split the mail-address Cc: space separated
            String destinationsCc[] = StringUtil.environmentSubstitute(destinationCc).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);
        }

        if (!Const.isEmpty(destinationBCc)) {
            // Split the mail-address BCc: space separated
            String destinationsBCc[] = StringUtil.environmentSubstitute(destinationBCc).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);
        }

        msg.setSubject(StringUtil.environmentSubstitute(subject));
        msg.setSentDate(new Date());
        StringBuffer messageText = new StringBuffer();

        if (comment != null) {
            messageText.append(StringUtil.environmentSubstitute(comment)).append(Const.CR).append(Const.CR);
        }

        if (!onlySendComment) {
            messageText.append("Job:").append(Const.CR);
            messageText.append("-----").append(Const.CR);
            messageText.append("Name       : ").append(parentJob.getJobMeta().getName()).append(Const.CR);
            messageText.append("Directory  : ").append(parentJob.getJobMeta().getDirectory()).append(Const.CR);
            messageText.append("JobEntry   : ").append(getName()).append(Const.CR);
            messageText.append(Const.CR);
        }

        if (includeDate) {
            Value date = new Value("date", new Date());
            messageText.append("Message date: ").append(date.toString()).append(Const.CR).append(Const.CR);
        }
        if (!onlySendComment && result != null) {
            messageText.append("Previous result:").append(Const.CR);
            messageText.append("-----------------").append(Const.CR);
            messageText.append("Job entry nr         : ").append(result.getEntryNr()).append(Const.CR);
            messageText.append("Errors               : ").append(result.getNrErrors()).append(Const.CR);
            messageText.append("Lines read           : ").append(result.getNrLinesRead()).append(Const.CR);
            messageText.append("Lines written        : ").append(result.getNrLinesWritten()).append(Const.CR);
            messageText.append("Lines input          : ").append(result.getNrLinesInput()).append(Const.CR);
            messageText.append("Lines output         : ").append(result.getNrLinesOutput()).append(Const.CR);
            messageText.append("Lines updated        : ").append(result.getNrLinesUpdated()).append(Const.CR);
            messageText.append("Script exit status   : ").append(result.getExitStatus()).append(Const.CR);
            messageText.append("Result               : ").append(result.getResult()).append(Const.CR);
            messageText.append(Const.CR);
        }

        if (!onlySendComment && (!Const.isEmpty(StringUtil.environmentSubstitute(contactPerson))
                || !Const.isEmpty(StringUtil.environmentSubstitute(contactPhone)))) {
            messageText.append("Contact information :").append(Const.CR);
            messageText.append("---------------------").append(Const.CR);
            messageText.append("Person to contact : ").append(StringUtil.environmentSubstitute(contactPerson))
                    .append(Const.CR);
            messageText.append("Telephone number  : ").append(StringUtil.environmentSubstitute(contactPhone))
                    .append(Const.CR);
            messageText.append(Const.CR);
        }

        // Include the path to this job entry...
        if (!onlySendComment) {
            JobTracker jobTracker = parentJob.getJobTracker();
            if (jobTracker != null) {
                messageText.append("Path to this job entry:").append(Const.CR);
                messageText.append("------------------------").append(Const.CR);

                addBacktracking(jobTracker, messageText);
            }
        }

        Multipart parts = new MimeMultipart();
        MimeBodyPart part1 = new MimeBodyPart(); // put the text in the
        // 1st part
        part1.setText(messageText.toString());
        parts.addBodyPart(part1);
        if (includingFiles && result != null) {
            List resultFiles = result.getResultFilesList();
            if (resultFiles != null && resultFiles.size() > 0) {
                if (!zipFiles) {
                    // Add all files to the message...
                    //
                    for (Iterator iter = resultFiles.iterator(); iter.hasNext();) {
                        ResultFile resultFile = (ResultFile) iter.next();
                        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(fds.getName());
                                // add the part with the file in the BodyPart();
                                parts.addBodyPart(files);

                                log.logBasic(toString(),
                                        "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
                            + StringUtil.environmentSubstitute(zipFilename));
                    ZipOutputStream zipOutputStream = null;
                    try {
                        zipOutputStream = new ZipOutputStream(new FileOutputStream(masterZipfile));

                        for (Iterator iter = resultFiles.iterator(); iter.hasNext();) {
                            ResultFile resultFile = (ResultFile) iter.next();

                            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().getURI());
                                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();

                                log.logBasic(toString(), "Added file '" + file.getName().getURI()
                                        + "' to the mail message in a zip archive.");
                            }
                        }
                    } catch (Exception e) {
                        log.logError(toString(), "Error zipping attachement files into file ["
                                + masterZipfile.getPath() + "] : " + e.toString());
                        log.logError(toString(), Const.getStackTracker(e));
                        result.setNrErrors(1);
                    } finally {
                        if (zipOutputStream != null) {
                            try {
                                zipOutputStream.finish();
                                zipOutputStream.close();
                            } catch (IOException e) {
                                log.logError(toString(),
                                        "Unable to close attachement zip file archive : " + e.toString());
                                log.logError(toString(), 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 th e data source
                        files.setFileName(fds.getName());
                        // add the part with the file in the BodyPart();
                        parts.addBodyPart(files);
                    }
                }
            }
        }
        msg.setContent(parts);

        Transport transport = null;
        try {
            transport = session.getTransport(protocol);
            if (usingAuthentication) {
                if (!Const.isEmpty(port)) {
                    transport.connect(StringUtil.environmentSubstitute(Const.NVL(server, "")),
                            Integer.parseInt(StringUtil.environmentSubstitute(Const.NVL(port, ""))),
                            StringUtil.environmentSubstitute(Const.NVL(authenticationUser, "")),
                            StringUtil.environmentSubstitute(Const.NVL(authenticationPassword, "")));
                } else {
                    transport.connect(StringUtil.environmentSubstitute(Const.NVL(server, "")),
                            StringUtil.environmentSubstitute(Const.NVL(authenticationUser, "")),
                            StringUtil.environmentSubstitute(Const.NVL(authenticationPassword, "")));
                }
            } else {
                transport.connect();
            }
            transport.sendMessage(msg, msg.getAllRecipients());
        } finally {
            if (transport != null)
                transport.close();
        }
    } catch (IOException e) {
        log.logError(toString(), "Problem while sending message: " + e.toString());
        result.setNrErrors(1);
    } catch (MessagingException mex) {
        log.logError(toString(), "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) {
                    log.logError(toString(), "    ** Invalid Addresses");
                    for (int i = 0; i < invalid.length; i++) {
                        log.logError(toString(), "         " + invalid[i]);
                        result.setNrErrors(1);
                    }
                }

                Address[] validUnsent = sfex.getValidUnsentAddresses();
                if (validUnsent != null) {
                    log.logError(toString(), "    ** ValidUnsent Addresses");
                    for (int i = 0; i < validUnsent.length; i++) {
                        log.logError(toString(), "         " + 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++) {
                        log.logError(toString(), "         " + 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:mondrian.olap.Util.java

/**
 * Gets content via Apache VFS. File must exist and have content
 *
 * @param url String//from   www .  j  av a 2  s.c o m
 * @return Apache VFS FileContent for further processing
 * @throws FileSystemException on error
 */
public static InputStream readVirtualFile(String url) throws FileSystemException {
    // Treat catalogUrl as an Apache VFS (Virtual File System) URL.
    // VFS handles all of the usual protocols (http:, file:)
    // and then some.
    FileSystemManager fsManager = VFS.getManager();
    if (fsManager == null) {
        throw newError("Cannot get virtual file system manager");
    }

    // Workaround VFS bug.
    if (url.startsWith("file://localhost")) {
        url = url.substring("file://localhost".length());
    }
    if (url.startsWith("file:")) {
        url = url.substring("file:".length());
    }

    // work around for VFS bug not closing http sockets
    // (Mondrian-585)
    if (url.startsWith("http")) {
        try {
            return new URL(url).openStream();
        } catch (IOException e) {
            throw newError("Could not read URL: " + url);
        }
    }

    File userDir = new File("").getAbsoluteFile();
    FileObject file = fsManager.resolveFile(userDir, url);
    FileContent fileContent = null;
    try {
        // Because of VFS caching, make sure we refresh to get the latest
        // file content. This refresh may possibly solve the following
        // workaround for defect MONDRIAN-508, but cannot be tested, so we
        // will leave the work around for now.
        file.refresh();

        // Workaround to defect MONDRIAN-508. For HttpFileObjects, verifies
        // the URL of the file retrieved matches the URL passed in.  A VFS
        // cache bug can cause it to treat URLs with different parameters
        // as the same file (e.g. http://blah.com?param=A,
        // http://blah.com?param=B)
        if (file instanceof HttpFileObject && !file.getName().getURI().equals(url)) {
            fsManager.getFilesCache().removeFile(file.getFileSystem(), file.getName());

            file = fsManager.resolveFile(userDir, url);
        }

        if (!file.isReadable()) {
            throw newError("Virtual file is not readable: " + url);
        }

        fileContent = file.getContent();
    } finally {
        file.close();
    }

    if (fileContent == null) {
        throw newError("Cannot get virtual file content: " + url);
    }

    return fileContent.getInputStream();
}

From source file:be.ibridge.kettle.job.entry.waitforfile.JobEntryWaitForFile.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   ww  w .j ava  2s.c om

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

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

            fileObject = KettleVFS.getFileObject(realFilename);

            long iMaximumTimeout = Const.toInt(getMaximumTimeout(), Const.toInt(DEFAULT_MAXIMUM_TIMEOUT, 0));
            long iCycleTime = Const.toInt(getCheckCycleTime(), 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);
                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);
                log.logBasic(toString(), "Check cycle time invalid, reset to " + iCycleTime);
            }

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

            boolean continueLoop = true;
            while (continueLoop && !parentJob.isStopped()) {
                if (fileObject.exists()) {
                    // file exists, we're happy to exit
                    log.logBasic(toString(), "Detected file [" + realFilename + "] within timeout");
                    result.setResult(true);
                    continueLoop = false;
                } 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()) {
                            log.logBasic(toString(),
                                    "Didn't detect file [" + realFilename + "] before timeout, success");
                            result.setResult(true);
                        } else {
                            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();

                log.logDetailed(toString(), "File [" + realFilename + "] is " + newSize + " bytes long");
                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");
                    }
                }
                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());
        }
    } else {
        log.logError(toString(), "No filename is defined.");
    }

    return result;
}