Search in sources :

Example 21 with HopFileException

use of org.apache.hop.core.exception.HopFileException in project hop by apache.

the class Tika method openNextFile.

private boolean openNextFile() {
    try {
        if (meta.isFileInField()) {
            // Grab another row ...
            data.readRow = getRow();
            if (// finished processing!
            data.readRow == null) {
                if (isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "Tika.Log.FinishedProcessing"));
                }
                return false;
            }
            if (first) {
                first = false;
                data.inputRowMeta = getInputRowMeta();
                data.outputRowMeta = data.inputRowMeta.clone();
                meta.getFields(data.outputRowMeta, getTransformName(), null, null, this, metadataProvider);
                // Create convert meta-data objects that will contain Date & Number formatters
                // 
                data.convertRowMeta = data.outputRowMeta.cloneToType(IValueMeta.TYPE_STRING);
                if (meta.isFileInField()) {
                    // Check is filename field is provided
                    if (StringUtils.isEmpty(meta.getDynamicFilenameField())) {
                        logError(BaseMessages.getString(PKG, "Tika.Log.NoField"));
                        throw new HopException(BaseMessages.getString(PKG, "Tika.Log.NoField"));
                    }
                    // cache the position of the field
                    if (data.indexOfFilenameField < 0) {
                        data.indexOfFilenameField = data.inputRowMeta.indexOfValue(meta.getDynamicFilenameField());
                        if (data.indexOfFilenameField < 0) {
                            // The field is unreachable !
                            logError(BaseMessages.getString(PKG, "Tika.Log.ErrorFindingField") + "[" + meta.getDynamicFilenameField() + "]");
                            throw new HopException(BaseMessages.getString(PKG, "Tika.Exception.CouldnotFindField", meta.getDynamicFilenameField()));
                        }
                    }
                    // Get the number of previous fields
                    data.totalPreviousFields = data.inputRowMeta.size();
                }
            }
            // end if first
            // get field value
            String fieldvalue = data.inputRowMeta.getString(data.readRow, data.indexOfFilenameField);
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "Tika.Log.Stream", meta.getDynamicFilenameField(), fieldvalue));
            }
            try {
                // Source is a file.
                data.file = HopVfs.getFileObject(fieldvalue);
            } catch (HopFileException e) {
                throw new HopException(e);
            } finally {
                try {
                    if (data.file != null) {
                        data.file.close();
                    }
                } catch (Exception e) {
                    logError("Error closing file", e);
                }
            }
        } else {
            if (// finished processing!
            data.fileNr >= data.files.nrOfFiles()) {
                if (isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "Tika.Log.FinishedProcessing"));
                }
                return false;
            }
            // Is this the last file?
            data.file = data.files.getFile(data.fileNr);
        }
        // Check if file is empty
        if (data.file.getContent() != null) {
            data.fileSize = data.file.getContent().getSize();
        } else {
            data.fileSize = 0;
        }
        // Move file pointer ahead!
        data.fileNr++;
        if (meta.isIgnoreEmptyFile() && data.fileSize == 0) {
            logError(BaseMessages.getString(PKG, "Tika.Error.FileSizeZero", "" + data.file.getName()));
            openNextFile();
        } else {
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "Tika.Log.OpeningFile", data.file.toString()));
            }
            data.filename = HopVfs.getFilename(data.file);
            // Add additional fields?
            if (StringUtils.isNotEmpty(meta.getShortFileFieldName())) {
                data.shortFilename = data.file.getName().getBaseName();
            }
            if (StringUtils.isNotEmpty(meta.getPathFieldName())) {
                data.path = HopVfs.getFilename(data.file.getParent());
            }
            if (StringUtils.isNotEmpty(meta.getHiddenFieldName())) {
                data.hidden = data.file.isHidden();
            }
            if (StringUtils.isNotEmpty(meta.getExtensionFieldName())) {
                data.extension = data.file.getName().getExtension();
            }
            if (StringUtils.isNotEmpty(meta.getLastModificationTimeFieldName())) {
                data.lastModificationDateTime = new Date(data.file.getContent().getLastModifiedTime());
            }
            if (StringUtils.isNotEmpty(meta.getUriFieldName())) {
                data.uriName = data.file.getName().getURI();
            }
            if (StringUtils.isNotEmpty(meta.getRootUriNameFieldName())) {
                data.rootUriName = data.file.getName().getRootURI();
            }
            // get File content
            getFileContent();
            addFileToResultFilesname(data.file);
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "Tika.Log.FileOpened", data.file.toString()));
            }
        }
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "Tika.Log.UnableToOpenFile", "" + data.fileNr, data.file.toString(), e.toString()), e);
        stopAll();
        setErrors(1);
        return false;
    }
    return true;
}
Also used : HopFileException(org.apache.hop.core.exception.HopFileException) HopException(org.apache.hop.core.exception.HopException) HopException(org.apache.hop.core.exception.HopException) HopFileException(org.apache.hop.core.exception.HopFileException) HopTransformException(org.apache.hop.core.exception.HopTransformException) Date(java.util.Date)

Example 22 with HopFileException

use of org.apache.hop.core.exception.HopFileException in project hop by apache.

the class TestUtils method createRamFile.

public static String createRamFile(String path, IVariables variables) {
    if (variables == null) {
        variables = new Variables();
        variables.initializeFrom(null);
    }
    try {
        FileObject file = HopVfs.getFileObject("ram://" + path);
        file.createFile();
        return file.getName().getURI();
    } catch (FileSystemException | HopFileException e) {
        throw new RuntimeException(e);
    }
}
Also used : IVariables(org.apache.hop.core.variables.IVariables) Variables(org.apache.hop.core.variables.Variables) FileSystemException(org.apache.commons.vfs2.FileSystemException) HopFileException(org.apache.hop.core.exception.HopFileException) FileObject(org.apache.commons.vfs2.FileObject)

Example 23 with HopFileException

use of org.apache.hop.core.exception.HopFileException in project hop by apache.

the class HopImportBase method setValidateOutputFolder.

@Override
public void setValidateOutputFolder(String outputFolderName) throws HopException {
    this.outputFolder = HopVfs.getFileObject(outputFolderName);
    try {
        if (!outputFolder.exists() || !outputFolder.isFolder()) {
            log.logBasic("output folder '" + outputFolderName + "' doesn't exist or is not a folder.");
            outputFolder.createFolder();
        }
        this.outputFolderName = outputFolder.getName().getURI();
    } catch (Exception e) {
        throw new HopFileException("Error setting output folder " + outputFolderName, e);
    }
}
Also used : HopFileException(org.apache.hop.core.exception.HopFileException) HopException(org.apache.hop.core.exception.HopException) HopFileException(org.apache.hop.core.exception.HopFileException)

Example 24 with HopFileException

use of org.apache.hop.core.exception.HopFileException in project hop by apache.

the class UIGit method open.

public InputStream open(String file, String commitId) throws HopException {
    if (commitId.equals(WORKINGTREE)) {
        String baseDirectory = getDirectory();
        String filePath = baseDirectory + Const.FILE_SEPARATOR + file;
        try {
            return HopVfs.getInputStream(filePath);
        } catch (HopFileException e) {
            throw new HopException("Unable to find working tree file '" + filePath + "'", e);
        }
    }
    RevCommit commit = resolve(commitId);
    RevTree tree = commit.getTree();
    try (TreeWalk tw = new TreeWalk(git.getRepository())) {
        tw.addTree(tree);
        tw.setFilter(PathFilter.create(file));
        tw.setRecursive(true);
        tw.next();
        ObjectLoader loader = git.getRepository().open(tw.getObjectId(0));
        return loader.openStream();
    } catch (MissingObjectException e) {
        throw new HopException("Unable to find file '" + file + "' for commit ID '" + commitId + "", e);
    } catch (IncorrectObjectTypeException e) {
        throw new HopException("Incorrect object type error for file '" + file + "' for commit ID '" + commitId + "", e);
    } catch (CorruptObjectException e) {
        throw new HopException("Corrupt object error for file '" + file + "' for commit ID '" + commitId + "", e);
    } catch (IOException e) {
        throw new HopException("Error reading git file '" + file + "' for commit ID '" + commitId + "", e);
    }
}
Also used : HopFileException(org.apache.hop.core.exception.HopFileException) HopException(org.apache.hop.core.exception.HopException) IOException(java.io.IOException) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 25 with HopFileException

use of org.apache.hop.core.exception.HopFileException in project hop by apache.

the class BlockingTransform method getBuffer.

private Object[] getBuffer() {
    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, "BlockingTransform.Log.Openfiles"));
        }
        try {
            FileObject fileObject = data.files.get(0);
            String filename = HopVfs.getFilename(fileObject);
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "BlockingTransform.Log.Openfilename1") + filename + BaseMessages.getString(PKG, "BlockingTransform.Log.Openfilename2"));
            }
            InputStream fi = HopVfs.getInputStream(fileObject);
            DataInputStream di;
            data.fis.add(fi);
            if (meta.isCompressFiles()) {
                GZIPInputStream gzfi = new GZIPInputStream(new BufferedInputStream(fi));
                di = new DataInputStream(gzfi);
                data.gzis.add(gzfi);
            } else {
                di = new DataInputStream(fi);
            }
            data.dis.add(di);
            // How long is the buffer?
            int buffersize = di.readInt();
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "BlockingTransform.Log.BufferSize1") + filename + BaseMessages.getString(PKG, "BlockingTransform.Log.BufferSize2") + buffersize + " " + BaseMessages.getString(PKG, "BlockingTransform.Log.BufferSize3"));
            }
            if (buffersize > 0) {
                // Read a row from temp-file
                data.rowbuffer.add(data.outputRowMeta.readData(di));
            }
        } catch (Exception e) {
            logError(BaseMessages.getString(PKG, "BlockingTransformMeta.ErrorReadingFile") + e.toString());
            logError(Const.getStackTracker(e));
        }
    }
    if (data.files.size() == 0) {
        if (data.buffer.size() > 0) {
            retval = data.buffer.get(0);
            data.buffer.remove(0);
        } else {
            retval = null;
        }
    } else {
        if (data.rowbuffer.size() == 0) {
            retval = null;
        } else {
            retval = data.rowbuffer.get(0);
            data.rowbuffer.remove(0);
            // now get another
            FileObject file = data.files.get(0);
            DataInputStream di = data.dis.get(0);
            InputStream fi = data.fis.get(0);
            GZIPInputStream gzfi = (meta.isCompressFiles()) ? data.gzis.get(0) : null;
            try {
                data.rowbuffer.add(0, data.outputRowMeta.readData(di));
            } catch (SocketTimeoutException e) {
                logError(BaseMessages.getString(PKG, "System.Log.UnexpectedError") + " : " + e.toString());
                logError(Const.getStackTracker(e));
                setErrors(1);
                stopAll();
            } catch (HopFileException fe) {
                // empty file or EOF mostly
                try {
                    di.close();
                    fi.close();
                    if (gzfi != null) {
                        gzfi.close();
                    }
                    file.delete();
                } catch (IOException e) {
                    logError(BaseMessages.getString(PKG, "BlockingTransformMeta.UnableDeleteFile") + file.toString());
                    setErrors(1);
                    stopAll();
                    return null;
                }
                data.files.remove(0);
                data.dis.remove(0);
                data.fis.remove(0);
                if (gzfi != null) {
                    data.gzis.remove(0);
                }
            }
        }
    }
    return retval;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) FileSystemException(org.apache.commons.vfs2.FileSystemException) HopException(org.apache.hop.core.exception.HopException) SocketTimeoutException(java.net.SocketTimeoutException) HopFileException(org.apache.hop.core.exception.HopFileException) GZIPInputStream(java.util.zip.GZIPInputStream) SocketTimeoutException(java.net.SocketTimeoutException) HopFileException(org.apache.hop.core.exception.HopFileException) FileObject(org.apache.commons.vfs2.FileObject) FileObject(org.apache.commons.vfs2.FileObject)

Aggregations

HopFileException (org.apache.hop.core.exception.HopFileException)49 HopException (org.apache.hop.core.exception.HopException)21 IOException (java.io.IOException)16 FileObject (org.apache.commons.vfs2.FileObject)14 SocketTimeoutException (java.net.SocketTimeoutException)8 ResultFile (org.apache.hop.core.ResultFile)7 File (java.io.File)6 HopEofException (org.apache.hop.core.exception.HopEofException)6 DataInputStream (java.io.DataInputStream)4 EOFException (java.io.EOFException)4 GZIPInputStream (java.util.zip.GZIPInputStream)4 FileSystemException (org.apache.commons.vfs2.FileSystemException)4 LocalFile (org.apache.commons.vfs2.provider.local.LocalFile)4 HopTransformException (org.apache.hop.core.exception.HopTransformException)4 HopValueException (org.apache.hop.core.exception.HopValueException)4 IRowMeta (org.apache.hop.core.row.IRowMeta)4 IValueMeta (org.apache.hop.core.row.IValueMeta)4 RowMeta (org.apache.hop.core.row.RowMeta)4 InputStream (java.io.InputStream)3 JarFile (java.util.jar.JarFile)3