Search in sources :

Example 86 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class OraBulkLoader method dispose.

public void dispose(StepMetaInterface smi, StepDataInterface sdi) {
    meta = (OraBulkLoaderMeta) smi;
    data = (OraBulkLoaderData) sdi;
    super.dispose(smi, sdi);
    // close output stream (may terminate running sqlldr)
    if (output != null) {
        // Close the output
        try {
            output.close();
        } catch (IOException e) {
            logError("Error while closing output", e);
        }
        output = null;
    }
    // running sqlldr process must be terminated
    if (sqlldrProcess != null) {
        try {
            int exitVal = sqlldrProcess.waitFor();
            sqlldrProcess = null;
            logBasic(BaseMessages.getString(PKG, "OraBulkLoader.Log.ExitValueSqlldr", "" + exitVal));
        } catch (InterruptedException e) {
            /* process should be destroyed */
            e.printStackTrace();
            if (sqlldrProcess != null) {
                sqlldrProcess.destroy();
            }
        }
    }
    if (!preview && meta.isEraseFiles()) {
        // Erase the created cfg/dat files if requested. We don't erase
        // the rest of the files because it would be "stupid" to erase them
        // right after creation. If you don't want them, don't fill them in.
        FileObject fileObject = null;
        String method = meta.getLoadMethod();
        // OraBulkLoaderMeta.METHOD_AUTO_CONCURRENT.equals(method) ||
        if (OraBulkLoaderMeta.METHOD_AUTO_END.equals(method)) {
            if (meta.getControlFile() != null) {
                try {
                    fileObject = getFileObject(meta.getControlFile(), getTransMeta());
                    fileObject.delete();
                    fileObject.close();
                } catch (Exception ex) {
                    logError("Error deleting control file \'" + getFilename(fileObject) + "\': " + ex.getMessage(), ex);
                }
            }
        }
        if (OraBulkLoaderMeta.METHOD_AUTO_END.equals(method)) {
            // In concurrent mode the data is written to the control file.
            if (meta.getDataFile() != null) {
                try {
                    fileObject = getFileObject(meta.getDataFile(), getTransMeta());
                    fileObject.delete();
                    fileObject.close();
                } catch (Exception ex) {
                    logError("Error deleting data file \'" + getFilename(fileObject) + "\': " + ex.getMessage(), ex);
                }
            }
        }
        if (OraBulkLoaderMeta.METHOD_MANUAL.equals(method)) {
            logBasic("Deletion of files is not compatible with \'manual load method\'");
        }
    }
}
Also used : IOException(java.io.IOException) FileObject(org.apache.commons.vfs2.FileObject) KettleException(org.pentaho.di.core.exception.KettleException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 87 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class ProcessFiles method processRow.

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
    meta = (ProcessFilesMeta) smi;
    data = (ProcessFilesData) sdi;
    // Get row from input rowset & set row busy!
    Object[] r = getRow();
    if (r == null) {
        // no more input to be expected...
        setOutputDone();
        return false;
    }
    if (first) {
        first = false;
        // Check is source filename field is provided
        if (Utils.isEmpty(meta.getDynamicSourceFileNameField())) {
            throw new KettleException(BaseMessages.getString(PKG, "ProcessFiles.Error.SourceFilenameFieldMissing"));
        }
        // Check is target filename field is provided
        if (meta.getOperationType() != ProcessFilesMeta.OPERATION_TYPE_DELETE && Utils.isEmpty(meta.getDynamicTargetFileNameField())) {
            throw new KettleException(BaseMessages.getString(PKG, "ProcessFiles.Error.TargetFilenameFieldMissing"));
        }
        // cache the position of the source filename field
        if (data.indexOfSourceFilename < 0) {
            data.indexOfSourceFilename = getInputRowMeta().indexOfValue(meta.getDynamicSourceFileNameField());
            if (data.indexOfSourceFilename < 0) {
                // The field is unreachable !
                throw new KettleException(BaseMessages.getString(PKG, "ProcessFiles.Exception.CouldnotFindField", meta.getDynamicSourceFileNameField()));
            }
        }
        // cache the position of the source filename field
        if (meta.getOperationType() != ProcessFilesMeta.OPERATION_TYPE_DELETE && data.indexOfTargetFilename < 0) {
            data.indexOfTargetFilename = getInputRowMeta().indexOfValue(meta.getDynamicTargetFileNameField());
            if (data.indexOfTargetFilename < 0) {
                // The field is unreachable !
                throw new KettleException(BaseMessages.getString(PKG, "ProcessFiles.Exception.CouldnotFindField", meta.getDynamicTargetFileNameField()));
            }
        }
        if (meta.simulate) {
            if (log.isBasic()) {
                logBasic(BaseMessages.getString(PKG, "ProcessFiles.Log.SimulationModeON"));
            }
        }
    }
    // End If first
    try {
        // get source filename
        String sourceFilename = getInputRowMeta().getString(r, data.indexOfSourceFilename);
        if (Utils.isEmpty(sourceFilename)) {
            logError(BaseMessages.getString(PKG, "ProcessFiles.Error.SourceFileEmpty"));
            throw new KettleException(BaseMessages.getString(PKG, "ProcessFiles.Error.SourceFileEmpty"));
        }
        data.sourceFile = KettleVFS.getFileObject(sourceFilename, getTransMeta());
        if (!data.sourceFile.exists()) {
            logError(BaseMessages.getString(PKG, "ProcessFiles.Error.SourceFileNotExist", sourceFilename));
            throw new KettleException(BaseMessages.getString(PKG, "ProcessFiles.Error.SourceFileNotExist", sourceFilename));
        }
        if (data.sourceFile.getType() != FileType.FILE) {
            logError(BaseMessages.getString(PKG, "ProcessFiles.Error.SourceFileNotFile", sourceFilename));
            throw new KettleException(BaseMessages.getString(PKG, "ProcessFiles.Error.SourceFileNotFile", sourceFilename));
        }
        String targetFilename = null;
        if (meta.getOperationType() != ProcessFilesMeta.OPERATION_TYPE_DELETE) {
            // get value for target filename
            targetFilename = getInputRowMeta().getString(r, data.indexOfTargetFilename);
            if (Utils.isEmpty(targetFilename)) {
                logError(BaseMessages.getString(PKG, "ProcessFiles.Error.TargetFileEmpty"));
                throw new KettleException(BaseMessages.getString(PKG, "ProcessFiles.Error.TargetFileEmpty"));
            }
            data.targetFile = KettleVFS.getFileObject(targetFilename, getTransMeta());
            if (data.targetFile.exists()) {
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "ProcessFiles.Log.TargetFileExists", targetFilename));
                }
                // check if target is really a file otherwise it could overwrite a complete folder by copy or move operations
                if (data.targetFile.getType() != FileType.FILE) {
                    logError(BaseMessages.getString(PKG, "ProcessFiles.Error.TargetFileNotFile", targetFilename));
                    throw new KettleException(BaseMessages.getString(PKG, "ProcessFiles.Error.TargetFileNotFile", targetFilename));
                }
            } else {
                // let's check parent folder
                FileObject parentFolder = data.targetFile.getParent();
                if (!parentFolder.exists()) {
                    if (!meta.isCreateParentFolder()) {
                        throw new KettleException(BaseMessages.getString(PKG, "ProcessFiles.Error.TargetParentFolderNotExists", parentFolder.toString()));
                    } else {
                        parentFolder.createFolder();
                    }
                }
                if (parentFolder != null) {
                    parentFolder.close();
                }
            }
        }
        switch(meta.getOperationType()) {
            case ProcessFilesMeta.OPERATION_TYPE_COPY:
                if (((meta.isOverwriteTargetFile() && data.targetFile.exists()) || !data.targetFile.exists()) && !meta.simulate) {
                    data.targetFile.copyFrom(data.sourceFile, new TextOneToOneFileSelector());
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "ProcessFiles.Log.SourceFileCopied", sourceFilename, targetFilename));
                    }
                } else {
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "ProcessFiles.Log.TargetNotOverwritten", sourceFilename, targetFilename));
                    }
                }
                break;
            case ProcessFilesMeta.OPERATION_TYPE_MOVE:
                if (((meta.isOverwriteTargetFile() && data.targetFile.exists()) || !data.targetFile.exists()) && !meta.simulate) {
                    data.sourceFile.moveTo(KettleVFS.getFileObject(targetFilename, getTransMeta()));
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "ProcessFiles.Log.SourceFileMoved", sourceFilename, targetFilename));
                    }
                } else {
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "ProcessFiles.Log.TargetNotOverwritten", sourceFilename, targetFilename));
                    }
                }
                break;
            case ProcessFilesMeta.OPERATION_TYPE_DELETE:
                if (!meta.simulate) {
                    if (!data.sourceFile.delete()) {
                        throw new KettleException(BaseMessages.getString(PKG, "ProcessFiles.Error.CanNotDeleteFile", data.sourceFile.toString()));
                    }
                }
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "ProcessFiles.Log.SourceFileDeleted", sourceFilename));
                }
                break;
            default:
                break;
        }
        // add filename to result filenames?
        if (meta.isaddTargetFileNametoResult() && meta.getOperationType() != ProcessFilesMeta.OPERATION_TYPE_DELETE && data.sourceFile.getType() == FileType.FILE) {
            // Add this to the result file names...
            ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, data.targetFile, getTransMeta().getName(), getStepname());
            resultFile.setComment(BaseMessages.getString(PKG, "ProcessFiles.Log.FileAddedResult"));
            addResultFile(resultFile);
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "ProcessFiles.Log.FilenameAddResult", data.sourceFile.toString()));
            }
        }
        // copy row to possible alternate rowset(s).
        putRow(getInputRowMeta(), r);
        if (checkFeedback(getLinesRead())) {
            if (log.isBasic()) {
                logBasic(BaseMessages.getString(PKG, "ProcessFiles.LineNumber") + getLinesRead());
            }
        }
    } catch (Exception e) {
        boolean sendToErrorRow = false;
        String errorMessage = null;
        if (getStepMeta().isDoingErrorHandling()) {
            sendToErrorRow = true;
            errorMessage = e.toString();
        } else {
            logError(BaseMessages.getString(PKG, "ProcessFiles.ErrorInStepRunning") + e.getMessage());
            setErrors(1);
            stopAll();
            // signal end to receiver(s)
            setOutputDone();
            return false;
        }
        if (sendToErrorRow) {
            // Simply add this row to the error row
            putError(getInputRowMeta(), r, 1, errorMessage, null, "ProcessFiles001");
        }
    }
    return true;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) FileObject(org.apache.commons.vfs2.FileObject) FileObject(org.apache.commons.vfs2.FileObject) ResultFile(org.pentaho.di.core.ResultFile) KettleException(org.pentaho.di.core.exception.KettleException)

Example 88 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class PropertyInputMeta method exportResources.

/**
 * Since the exported transformation that runs this will reside in a ZIP file, we can't reference files relatively. So
 * what this does is turn the name of files into absolute paths OR it simply includes the resource in the ZIP file.
 * For now, we'll simply turn it into an absolute path and pray that the file is on a shared drive or something like
 * that.
 *
 * @param space
 *          the variable space to use
 * @param definitions
 * @param resourceNamingInterface
 * @param repository
 *          The repository to optionally load other resources from (to be converted to XML)
 * @param metaStore
 *          the metaStore in which non-kettle metadata could reside.
 *
 * @return the filename of the exported resource
 */
public String exportResources(VariableSpace space, Map<String, ResourceDefinition> definitions, ResourceNamingInterface resourceNamingInterface, Repository repository, IMetaStore metaStore) throws KettleException {
    try {
        // 
        if (!filefield) {
            for (int i = 0; i < fileName.length; i++) {
                FileObject fileObject = KettleVFS.getFileObject(space.environmentSubstitute(fileName[i]), space);
                fileName[i] = resourceNamingInterface.nameResource(fileObject, space, Utils.isEmpty(fileMask[i]));
            }
        }
        return null;
    } catch (Exception e) {
        throw new KettleException(e);
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) FileObject(org.apache.commons.vfs2.FileObject) KettleException(org.pentaho.di.core.exception.KettleException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleStepException(org.pentaho.di.core.exception.KettleStepException)

Example 89 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class PropertyOutput method openNewFile.

private void openNewFile() throws KettleException {
    try (FileObject newFile = KettleVFS.getFileObject(data.filename, getTransMeta())) {
        data.pro = new Properties();
        data.KeySet.clear();
        data.file = newFile;
        if (meta.isAppend() && data.file.exists()) {
            data.pro.load(KettleVFS.getInputStream(data.file));
        }
        // Create parent folder if needed...
        createParentFolder();
        // save processing file
        data.previousFileName = data.filename;
    } catch (Exception e) {
        throw new KettleException("Error opening file [" + data.filename + "]!", e);
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) FileObject(org.apache.commons.vfs2.FileObject) Properties(java.util.Properties) KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException)

Example 90 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class FileObjectContentLocation method createItem.

/**
 * Creates a new data item in the current location. This method must never return null. This method will fail if an
 * entity with the same name exists in this location.
 *
 * @param name the name of the new entity.
 * @return the newly created entity, never null.
 * @throws ContentCreationException if the item could not be created.
 */
public ContentItem createItem(final String name) throws ContentCreationException {
    String fileName = new File(name).getName();
    if (RepositoryUtilities.isInvalidPathName(fileName)) {
        throw new IllegalArgumentException("The name given is not valid.");
    }
    try {
        final FileObject file = getBackend();
        final FileObject child = file.resolveFile(fileName);
        if (child.exists()) {
            if (child.getContent().getSize() == 0) {
                // probably one of the temp files created by the pentaho-system
                return new FileObjectContentItem(this, child);
            }
            throw new ContentCreationException("File already exists: " + child);
        }
        try {
            child.createFile();
            return new FileObjectContentItem(this, child);
        } catch (IOException e) {
            throw new ContentCreationException("IOError while create", e);
        }
    } catch (FileSystemException e) {
        throw new RuntimeException(e);
    }
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) ContentCreationException(org.pentaho.reporting.libraries.repository.ContentCreationException) FileObject(org.apache.commons.vfs2.FileObject) ContentIOException(org.pentaho.reporting.libraries.repository.ContentIOException) IOException(java.io.IOException) File(java.io.File)

Aggregations

FileObject (org.apache.commons.vfs2.FileObject)646 KettleException (org.pentaho.di.core.exception.KettleException)206 IOException (java.io.IOException)203 FileSystemException (org.apache.commons.vfs2.FileSystemException)173 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)104 KettleFileException (org.pentaho.di.core.exception.KettleFileException)97 Test (org.junit.Test)82 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)68 File (java.io.File)60 InputStream (java.io.InputStream)48 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)37 KettleStepException (org.pentaho.di.core.exception.KettleStepException)36 ArrayList (java.util.ArrayList)35 ResultFile (org.pentaho.di.core.ResultFile)33 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)32 Result (org.pentaho.di.core.Result)32 OutputStream (java.io.OutputStream)29 FileName (org.apache.commons.vfs2.FileName)29 KettleValueException (org.pentaho.di.core.exception.KettleValueException)29 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)28