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

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

Introduction

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

Prototype

public void moveTo(FileObject destFile) throws FileSystemException;

Source Link

Document

Move this file.

Usage

From source file:org.pentaho.di.shared.SharedObjects.java

public void saveToFile() throws IOException, KettleException {
    FileObject fileObject = KettleVFS.getFileObject(filename);

    if (fileObject.exists()) {
        // Create a backup before overwriting...
        ///* www  . ja va 2  s .  c om*/
        FileObject backupFile = KettleVFS.getFileObject(filename + ".backup");
        fileObject.moveTo(backupFile);
    }

    OutputStream outputStream = KettleVFS.getOutputStream(fileObject, false);

    PrintStream out = new PrintStream(outputStream);

    out.print(XMLHandler.getXMLHeader(Const.XML_ENCODING));
    out.println("<" + XML_TAG + ">");

    Collection<SharedObjectInterface> collection = objectsMap.values();
    for (SharedObjectInterface sharedObject : collection) {
        out.println(sharedObject.getXML());
    }

    out.println("</" + XML_TAG + ">");

    out.flush();
    out.close();
    outputStream.close();
}

From source file:org.pentaho.di.trans.steps.script.ScriptAddedFunctions.java

public static void moveFile(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {

    try {//  www  .j  a va2 s .c o  m
        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 move
                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 move the file...
                        if ((destinationExists && overwrite) || !destinationExists) {
                            fileSource.moveTo(fileDestination);
                        }

                    }
                } else {
                    new RuntimeException("file to move [" + (String) ArgList[0] + "] can not be found!");
                }
            } catch (IOException e) {
                throw new RuntimeException("The function call moveFile 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 copyFile is not valid.");
        }
    } catch (Exception e) {
        throw new RuntimeException(e.toString());
    }
}

From source file:org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java

public static void moveFile(Context actualContext, Scriptable actualObject, Object[] ArgList,
        Function FunctionContext) {

    try {/*from  w w  w  .  ja va  2 s .  c  o  m*/
        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 move
                fileSource = KettleVFS.getFileObject(Context.toString(ArgList[0]));
                // Destination filename
                fileDestination = KettleVFS.getFileObject(Context.toString(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 = Context.toBoolean(ArgList[2]);
                        }
                        boolean destinationExists = fileDestination.exists();
                        // Let's move the file...
                        if ((destinationExists && overwrite) || !destinationExists) {
                            fileSource.moveTo(fileDestination);
                        }

                    }
                } else {
                    Context.reportRuntimeError(
                            "file to move [" + Context.toString(ArgList[0]) + "] can not be found!");
                }
            } catch (IOException e) {
                throw Context.reportRuntimeError("The function call moveFile 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 Context.reportRuntimeError("The function call copyFile is not valid.");
        }
    } catch (Exception e) {
        throw Context.reportRuntimeError(e.toString());
    }
}

From source file:org.pentaho.di.trans.steps.sftpput.SFTPPut.java

protected void finishTheJob(FileObject file, String sourceData, FileObject destinationFolder)
        throws KettleException {
    try {/*ww w .  j a v a  2 s  .co m*/
        switch (meta.getAfterFTPS()) {
        case JobEntrySFTPPUT.AFTER_FTPSPUT_DELETE:
            // Delete source file
            if (!file.exists()) {
                file.delete();
                if (isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "SFTPPut.Log.DeletedFile", sourceData));
                }
            }
            break;
        case JobEntrySFTPPUT.AFTER_FTPSPUT_MOVE:
            // Move source file
            FileObject destination = null;
            try {
                destination = KettleVFS.getFileObject(destinationFolder.getName().getBaseName()
                        + Const.FILE_SEPARATOR + file.getName().getBaseName(), this);
                file.moveTo(destination);
                if (isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "SFTPPut.Log.FileMoved", file, destination));
                }
            } finally {
                if (destination != null) {
                    destination.close();
                }
            }
            break;
        default:
            if (meta.isAddFilenameResut()) {
                // Add this to the result file names...
                ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, file,
                        getTransMeta().getName(), getStepname());
                resultFile
                        .setComment(BaseMessages.getString(PKG, "SFTPPut.Log.FilenameAddedToResultFilenames"));
                addResultFile(resultFile);
                if (isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "SFTPPut.Log.FilenameAddedToResultFilenames",
                            sourceData));
                }
            }
            break;
        }
    } catch (Exception e) {
        throw new KettleException(e);
    }
}

From source file:org.pentaho.hdfs.vfs.test.HDFSVFSTest.java

@Test
public void renameFile() throws Exception {
    assertNotNull("FileSystemManager is null", fsManager);

    FileObject file = fsManager.resolveFile(buildHDFSURL("/junit/name.txt"));
    assertNotNull("File is null (could not resolve?)", file);
    assertEquals(FileType.IMAGINARY, file.getType());

    OutputStream output = file.getContent().getOutputStream();
    IOUtils.write(HELLO_HADOOP_STR, output);
    IOUtils.closeQuietly(output);/*ww  w . j a v  a2s.c o m*/
    assertEquals(FileType.FILE, file.getType());

    FileObject renamedFile = fsManager.resolveFile(buildHDFSURL("/junit/renamed.txt"));
    assertNotNull("File is null (could not resolve?)", renamedFile);
    assertEquals(FileType.IMAGINARY, renamedFile.getType());

    file.moveTo(renamedFile);
    renamedFile = fsManager.resolveFile(buildHDFSURL("/junit/renamed.txt"));
    assertNotNull("File is null (could not resolve?)", renamedFile);
    assertEquals(FileType.FILE, renamedFile.getType());
    assertEquals(true, renamedFile.delete());
}