Search in sources :

Example 51 with FileObject

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

the class SharedObjects method writeToFile.

/**
 * Write sharedObjects to file. In case of an exception are caught save backup file instead of new one.
 *
 * @param fileObject     is file for writing
 * @param backupFileName is backup file name
 * @throws IOException
 * @throws KettleException
 */
@VisibleForTesting
protected void writeToFile(FileObject fileObject, String backupFileName) throws IOException, KettleException {
    OutputStream outputStream = null;
    PrintStream out = null;
    try {
        outputStream = initOutputStreamUsingKettleVFS(fileObject);
        out = new PrintStream(outputStream);
        out.print(XMLHandler.getXMLHeader(Const.XML_ENCODING));
        out.println("<" + XML_TAG + ">");
        Collection<SharedObjectInterface> collection = objectsMap.values();
        for (SharedObjectInterface sharedObject : collection) {
            String xmlContent = sharedObject.getXML();
            out.println(xmlContent);
        }
        out.println("</" + XML_TAG + ">");
    } catch (Exception e) {
        // restore file if something wrong
        boolean isRestored = false;
        if (backupFileName != null) {
            restoreFileFromBackup(backupFileName);
            isRestored = true;
        }
        throw new KettleException(BaseMessages.getString(PKG, "SharedOjects.WriteToFile.ErrorWritingFile", isRestored), e);
    } finally {
        if (out != null) {
            out.flush();
        }
        if (out != null) {
            out.close();
        }
        if (out != null) {
            outputStream.close();
        }
    }
}
Also used : PrintStream(java.io.PrintStream) KettleException(org.pentaho.di.core.exception.KettleException) OutputStream(java.io.OutputStream) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) FileSystemException(org.apache.commons.vfs2.FileSystemException) IOException(java.io.IOException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 52 with FileObject

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

the class SharedObjects method saveToFile.

public void saveToFile() throws IOException, KettleException {
    FileObject fileObject = getFileObjectFromKettleVFS(filename);
    String backupFileName = createOrGetFileBackup(fileObject);
    writeToFile(fileObject, backupFileName);
}
Also used : FileObject(org.apache.commons.vfs2.FileObject)

Example 53 with FileObject

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

the class ScriptAddedFunctions method getFileExtension.

public static String getFileExtension(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) {
    try {
        if (ArgList.length == 1 && !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 Extension = null;
                if (file.exists()) {
                    Extension = file.getName().getExtension();
                } else {
                    throw new RuntimeException("file [" + ArgList[0] + "] can not be found!");
                }
                return Extension;
            } catch (IOException e) {
                throw new RuntimeException("The function call getFileExtension throw an error : " + e.toString());
            } finally {
                if (file != null) {
                    try {
                        file.close();
                    } catch (Exception e) {
                    // Ignore errors
                    }
                }
            }
        } else {
            throw new RuntimeException("The function call getFileExtension is not valid.");
        }
    } catch (Exception e) {
        throw new RuntimeException(e.toString());
    }
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) ScriptException(javax.script.ScriptException) IOException(java.io.IOException)

Example 54 with FileObject

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

the class ScriptAddedFunctions method getShortFilename.

public static String getShortFilename(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) {
    try {
        if (ArgList.length == 1 && !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 Filename = null;
                if (file.exists()) {
                    Filename = file.getName().getBaseName();
                } else {
                    throw new RuntimeException("file [" + ArgList[0] + "] can not be found!");
                }
                return Filename;
            } catch (IOException e) {
                throw new RuntimeException("The function call getShortFilename throw an error : " + e.toString());
            } finally {
                if (file != null) {
                    try {
                        file.close();
                    } catch (Exception e) {
                    // Ignore errors
                    }
                }
            }
        } else {
            throw new RuntimeException("The function call getShortFilename is not valid.");
        }
    } catch (Exception e) {
        throw new RuntimeException(e.toString());
    }
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) ScriptException(javax.script.ScriptException) IOException(java.io.IOException)

Example 55 with FileObject

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

the class ScriptAddedFunctions method copyFile.

public static void copyFile(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) {
    try {
        if (ArgList.length == 3 && !isNull(ArgList[0]) && !isNull(ArgList[1]) && !isUndefined(ArgList[0]) && !isUndefined(ArgList[1])) {
            FileObject fileSource = null, fileDestination = null;
            try {
                // Source file to copy
                fileSource = KettleVFS.getFileObject((String) ArgList[0]);
                // Destination filename
                fileDestination = KettleVFS.getFileObject((String) ArgList[1]);
                if (fileSource.exists()) {
                    // Source file exists...
                    if (fileSource.getType() == FileType.FILE) {
                        // Great..source is a file ...
                        boolean overwrite = false;
                        if (!ArgList[1].equals(null)) {
                            overwrite = (Boolean) ArgList[2];
                        }
                        boolean destinationExists = fileDestination.exists();
                        // Let's copy the file...
                        if ((destinationExists && overwrite) || !destinationExists) {
                            FileUtil.copyContent(fileSource, fileDestination);
                        }
                    }
                } else {
                    throw new RuntimeException("file to copy [" + ArgList[0] + "] can not be found!");
                }
            } catch (IOException e) {
                throw new RuntimeException("The function call copyFile throw an error : " + e.toString());
            } finally {
                if (fileSource != null) {
                    try {
                        fileSource.close();
                    } catch (Exception e) {
                    // Ignore errors
                    }
                }
                if (fileDestination != null) {
                    try {
                        fileDestination.close();
                    } catch (Exception e) {
                    // Ignore errors
                    }
                }
            }
        } else {
            throw new RuntimeException("The function call copyFileis not valid.");
        }
    } catch (Exception e) {
        throw new RuntimeException(e.toString());
    }
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) ScriptException(javax.script.ScriptException) IOException(java.io.IOException)

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