Search in sources :

Example 41 with KettleFileException

use of org.pentaho.di.core.exception.KettleFileException in project pentaho-kettle by pentaho.

the class SortRows method getBuffer.

// get sorted rows from available files in iterative manner.
// that means call to this method will continue to return rows
// till all temp files will not be read to the end.
Object[] getBuffer() throws KettleValueException {
    Object[] retval;
    // Open all files at once and read one row from each file...
    if (data.files.size() > 0 && (data.dis.size() == 0 || data.fis.size() == 0)) {
        if (log.isBasic()) {
            logBasic(BaseMessages.getString(PKG, "SortRows.Basic.OpeningTempFiles", data.files.size()));
        }
        try {
            for (int f = 0; f < data.files.size() && !isStopped(); f++) {
                FileObject fileObject = data.files.get(f);
                String filename = KettleVFS.getFilename(fileObject);
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "SortRows.Detailed.OpeningTempFile", filename));
                }
                InputStream fi = KettleVFS.getInputStream(fileObject);
                DataInputStream di;
                data.fis.add(fi);
                if (data.compressFiles) {
                    di = getDataInputStream(new GZIPInputStream(new BufferedInputStream(fi)));
                } else {
                    di = new DataInputStream(new BufferedInputStream(fi, 50000));
                }
                data.dis.add(di);
                // How long is the buffer?
                int buffersize = data.bufferSizes.get(f);
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "SortRows.Detailed.FromFileExpectingRows", filename, buffersize));
                }
                if (buffersize > 0) {
                    Object[] row = data.outputRowMeta.readData(di);
                    // new row from input stream
                    data.rowbuffer.add(row);
                    data.tempRows.add(new RowTempFile(row, f));
                }
            }
            // Sort the data row buffer
            Collections.sort(data.tempRows, data.comparator);
        } catch (Exception e) {
            logError(BaseMessages.getString(PKG, "SortRows.Error.ErrorReadingBackTempFiles"), e);
        }
    }
    if (data.files.size() == 0) {
        if (data.getBufferIndex < data.buffer.size()) {
            retval = data.buffer.get(data.getBufferIndex);
            data.getBufferIndex++;
        } else {
            retval = null;
        }
    } else {
        if (data.rowbuffer.size() == 0) {
            retval = null;
        } else {
            // 
            if (log.isRowLevel()) {
                for (int i = 0; i < data.rowbuffer.size() && !isStopped(); i++) {
                    Object[] b = data.rowbuffer.get(i);
                    logRowlevel(BaseMessages.getString(PKG, "SortRows.RowLevel.PrintRow", i, data.outputRowMeta.getString(b)));
                }
            }
            RowTempFile rowTempFile = data.tempRows.remove(0);
            retval = rowTempFile.row;
            int smallest = rowTempFile.fileNumber;
            // now get another Row for position smallest
            FileObject file = data.files.get(smallest);
            DataInputStream di = data.dis.get(smallest);
            InputStream fi = data.fis.get(smallest);
            try {
                Object[] row2 = data.outputRowMeta.readData(di);
                RowTempFile extra = new RowTempFile(row2, smallest);
                int index = Collections.binarySearch(data.tempRows, extra, data.comparator);
                if (index < 0) {
                    data.tempRows.add(index * (-1) - 1, extra);
                } else {
                    data.tempRows.add(index, extra);
                }
            } catch (KettleFileException fe) {
                // empty file or EOF mostly
                GZIPInputStream gzfi = (data.compressFiles) ? data.gzis.get(smallest) : null;
                try {
                    di.close();
                    fi.close();
                    if (gzfi != null) {
                        gzfi.close();
                    }
                    file.delete();
                } catch (IOException e) {
                    logError(BaseMessages.getString(PKG, "SortRows.Error.UnableToCloseFile", smallest, file.toString()));
                    setErrors(1);
                    stopAll();
                    return null;
                }
                data.files.remove(smallest);
                data.dis.remove(smallest);
                data.fis.remove(smallest);
                if (gzfi != null) {
                    data.gzis.remove(smallest);
                }
                // 
                for (RowTempFile rtf : data.tempRows) {
                    if (rtf.fileNumber > smallest) {
                        rtf.fileNumber--;
                    }
                }
            } catch (SocketTimeoutException e) {
                // should never happen on local files
                throw new KettleValueException(e);
            }
        }
    }
    return retval;
}
Also used : KettleFileException(org.pentaho.di.core.exception.KettleFileException) DataInputStream(java.io.DataInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) KettleException(org.pentaho.di.core.exception.KettleException) SocketTimeoutException(java.net.SocketTimeoutException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) FileSystemException(org.apache.commons.vfs2.FileSystemException) IOException(java.io.IOException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) GZIPInputStream(java.util.zip.GZIPInputStream) SocketTimeoutException(java.net.SocketTimeoutException) BufferedInputStream(java.io.BufferedInputStream) FileObject(org.apache.commons.vfs2.FileObject) FileObject(org.apache.commons.vfs2.FileObject) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Example 42 with KettleFileException

use of org.pentaho.di.core.exception.KettleFileException in project pentaho-kettle by pentaho.

the class ScriptAddedFunctions method checkAndLoadJSFile.

// Evaluates the given ScriptFile
private static void checkAndLoadJSFile(ScriptEngine actualContext, Bindings eval_scope, String fileName) {
    Reader inStream = null;
    try {
        inStream = new InputStreamReader(KettleVFS.getInputStream(fileName));
        actualContext.eval(inStream, eval_scope);
    } catch (KettleFileException Signal) {
        /*
       * //TODO AKRETION: see if we can find better catches compatibles with JSR223 catch (FileNotFoundException Signal)
       * { new RuntimeException("Unable to open file \"" + fileName + "\" (reason: \"" + Signal.getMessage() + "\")"); }
       * catch (WrappedException Signal) { new RuntimeException("WrappedException while evaluating file \"" + fileName +
       * "\" (reason: \"" + Signal.getMessage() + "\")"); } catch (EvaluatorException Signal) { new
       * RuntimeException("EvaluatorException while evaluating file \"" + fileName + "\" (reason: \"" +
       * Signal.getMessage() + "\")"); } catch (JavaScriptException Signal) { new
       * RuntimeException("JavaScriptException while evaluating file \"" + fileName + "\" (reason: \"" +
       * Signal.getMessage() + "\")"); } catch (IOException Signal) { new RuntimeException("Error while reading file \""
       * + fileName + "\" (reason: \"" + Signal.getMessage() + "\")" ); }
       */
        new RuntimeException("Error while reading file \"" + fileName + "\" (reason: \"" + Signal.getMessage() + "\")");
    } catch (ScriptException Signal) {
        new RuntimeException("Error while reading file \"" + fileName + "\" (reason: \"" + Signal.getMessage() + "\")");
    } finally {
        try {
            if (inStream != null) {
                inStream.close();
            }
        } catch (Exception Signal) {
        // Ignore
        }
    }
}
Also used : ScriptException(javax.script.ScriptException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) KettleFileException(org.pentaho.di.core.exception.KettleFileException) ScriptException(javax.script.ScriptException) IOException(java.io.IOException)

Example 43 with KettleFileException

use of org.pentaho.di.core.exception.KettleFileException in project pentaho-kettle by pentaho.

the class ScriptValuesAddedFunctions method checkAndLoadJSFile.

// Evaluates the given ScriptFile
private static void checkAndLoadJSFile(Context actualContext, Scriptable eval_scope, String fileName) {
    Reader inStream = null;
    try {
        inStream = new InputStreamReader(KettleVFS.getInputStream(fileName));
        actualContext.evaluateReader(eval_scope, inStream, fileName, 1, null);
    } catch (FileNotFoundException Signal) {
        Context.reportError("Unable to open file \"" + fileName + "\" (reason: \"" + Signal.getMessage() + "\")");
    } catch (WrappedException Signal) {
        Context.reportError("WrappedException while evaluating file \"" + fileName + "\" (reason: \"" + Signal.getMessage() + "\")");
    } catch (EvaluatorException Signal) {
        Context.reportError("EvaluatorException while evaluating file \"" + fileName + "\" (reason: \"" + Signal.getMessage() + "\")");
    } catch (JavaScriptException Signal) {
        Context.reportError("JavaScriptException while evaluating file \"" + fileName + "\" (reason: \"" + Signal.getMessage() + "\")");
    } catch (IOException Signal) {
        Context.reportError("Error while reading file \"" + fileName + "\" (reason: \"" + Signal.getMessage() + "\")");
    } catch (KettleFileException Signal) {
        Context.reportError("Error while reading file \"" + fileName + "\" (reason: \"" + Signal.getMessage() + "\")");
    } finally {
        try {
            if (inStream != null) {
                inStream.close();
            }
        } catch (Exception Signal) {
        // Ignore
        }
    }
}
Also used : WrappedException(org.mozilla.javascript.WrappedException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) InputStreamReader(java.io.InputStreamReader) EvaluatorException(org.mozilla.javascript.EvaluatorException) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) FileNotFoundException(java.io.FileNotFoundException) WrappedException(org.mozilla.javascript.WrappedException) EvaluatorException(org.mozilla.javascript.EvaluatorException) JavaScriptException(org.mozilla.javascript.JavaScriptException) IOException(java.io.IOException) JavaScriptException(org.mozilla.javascript.JavaScriptException)

Example 44 with KettleFileException

use of org.pentaho.di.core.exception.KettleFileException in project pentaho-kettle by pentaho.

the class AbstractFileErrorHandler method getWriter.

/**
 * returns the OutputWiter if exists. Otherwhise it will create a new one.
 *
 * @return
 * @throws KettleException
 */
Writer getWriter(Object source) throws KettleException {
    try {
        Writer outputStreamWriter = writers.get(source);
        if (outputStreamWriter != null) {
            return outputStreamWriter;
        }
        FileObject file = getReplayFilename(destinationDirectory, processingFilename, dateString, fileExtension, source);
        ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, file, baseStep.getTransMeta().getName(), baseStep.getStepname());
        baseStep.addResultFile(resultFile);
        try {
            if (encoding == null) {
                outputStreamWriter = new OutputStreamWriter(KettleVFS.getOutputStream(file, false));
            } else {
                outputStreamWriter = new OutputStreamWriter(KettleVFS.getOutputStream(file, false), encoding);
            }
        } catch (Exception e) {
            throw new KettleException(BaseMessages.getString(PKG, "AbstractFileErrorHandler.Exception.CouldNotCreateFileErrorHandlerForFile") + file.getName().getURI(), e);
        }
        writers.put(source, outputStreamWriter);
        return outputStreamWriter;
    } catch (KettleFileException e) {
        throw new KettleException(BaseMessages.getString(PKG, "AbstractFileErrorHandler.Exception.CouldNotCreateFileErrorHandlerForFile"), e);
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) OutputStreamWriter(java.io.OutputStreamWriter) FileObject(org.apache.commons.vfs2.FileObject) ResultFile(org.pentaho.di.core.ResultFile) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) KettleException(org.pentaho.di.core.exception.KettleException) IOException(java.io.IOException) KettleFileException(org.pentaho.di.core.exception.KettleFileException)

Example 45 with KettleFileException

use of org.pentaho.di.core.exception.KettleFileException in project pentaho-kettle by pentaho.

the class CsvInput method readFieldNamesFromFile.

String[] readFieldNamesFromFile(String fileName, CsvInputMeta csvInputMeta) throws KettleException {
    String delimiter = environmentSubstitute(csvInputMeta.getDelimiter());
    String enclosure = environmentSubstitute(csvInputMeta.getEnclosure());
    String realEncoding = environmentSubstitute(csvInputMeta.getEncoding());
    try (FileObject fileObject = KettleVFS.getFileObject(fileName, getTransMeta());
        BOMInputStream inputStream = new BOMInputStream(KettleVFS.getInputStream(fileObject), ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE)) {
        InputStreamReader reader = null;
        if (Utils.isEmpty(realEncoding)) {
            reader = new InputStreamReader(inputStream);
        } else {
            reader = new InputStreamReader(inputStream, realEncoding);
        }
        EncodingType encodingType = EncodingType.guessEncodingType(reader.getEncoding());
        String line = TextFileInput.getLine(log, reader, encodingType, TextFileInputMeta.FILE_FORMAT_UNIX, new StringBuilder(1000));
        String[] fieldNames = CsvInput.guessStringsFromLine(log, line, delimiter, enclosure, csvInputMeta.getEscapeCharacter());
        if (!Utils.isEmpty(csvInputMeta.getEnclosure())) {
            removeEnclosure(fieldNames, csvInputMeta.getEnclosure());
        }
        trimFieldNames(fieldNames);
        return fieldNames;
    } catch (IOException e) {
        throw new KettleFileException(BaseMessages.getString(PKG, "CsvInput.Exception.CreateFieldMappingError"), e);
    }
}
Also used : BOMInputStream(org.apache.commons.io.input.BOMInputStream) KettleFileException(org.pentaho.di.core.exception.KettleFileException) InputStreamReader(java.io.InputStreamReader) EncodingType(org.pentaho.di.trans.steps.textfileinput.EncodingType) FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException)

Aggregations

KettleFileException (org.pentaho.di.core.exception.KettleFileException)61 IOException (java.io.IOException)32 FileObject (org.apache.commons.vfs2.FileObject)30 KettleException (org.pentaho.di.core.exception.KettleException)25 FileSystemException (org.apache.commons.vfs2.FileSystemException)10 DataInputStream (java.io.DataInputStream)8 File (java.io.File)7 ResultFile (org.pentaho.di.core.ResultFile)7 SocketTimeoutException (java.net.SocketTimeoutException)6 FileInputStream (java.io.FileInputStream)5 InputStream (java.io.InputStream)5 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)5 BufferedInputStream (java.io.BufferedInputStream)4 InputStreamReader (java.io.InputStreamReader)4 GZIPInputStream (java.util.zip.GZIPInputStream)4 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)4 KettleEOFException (org.pentaho.di.core.exception.KettleEOFException)4 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)4 DataOutputStream (java.io.DataOutputStream)3 FileNotFoundException (java.io.FileNotFoundException)3