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

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

Introduction

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

Prototype

public void close() throws FileSystemException;

Source Link

Document

Closes this file, and its content.

Usage

From source file:com.panet.imeta.job.entries.movefiles.JobEntryMoveFiles.java

private boolean CreateDestinationFolder(FileObject filefolder) {
    LogWriter log = LogWriter.getInstance();
    FileObject folder = null;
    try {//from   w ww  .java 2s.  c o  m
        if (destination_is_a_file)
            folder = filefolder.getParent();
        else
            folder = filefolder;

        if (!folder.exists()) {
            if (create_destination_folder) {
                if (log.isDetailed())
                    log.logDetailed(toString(),
                            Messages.getString("JobMoveFiles.Log.FolderNotExist", folder.getName().toString()));
                folder.createFolder();
                if (log.isDetailed())
                    log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FolderWasCreated",
                            folder.getName().toString()));
            } else {
                log.logError(toString(),
                        Messages.getString("JobMoveFiles.Log.FolderNotExist", folder.getName().toString()));
                return false;
            }
        }
        return true;
    } catch (Exception e) {
        log.logError(toString(),
                Messages.getString("JobMoveFiles.Log.CanNotCreateParentFolder", folder.getName().toString()));

    } finally {
        if (folder != null) {
            try {
                folder.close();
            } catch (Exception ex) {
            }
            ;
        }
    }
    return false;
}

From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java

public static String getLastModifiedTime(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    try {//  w ww  .j  av a  2  s . c  o  m
        if (ArgList.length == 2 && !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 dateformat = (String) ArgList[1];
                if (isNull(dateformat))
                    dateformat = "yyyy-MM-dd";
                String lastmodifiedtime = null;
                if (file.exists()) {
                    java.util.Date lastmodifiedtimedate = new java.util.Date(
                            file.getContent().getLastModifiedTime());
                    java.text.DateFormat dateFormat = new SimpleDateFormat(dateformat);
                    lastmodifiedtime = dateFormat.format(lastmodifiedtimedate);

                } else {
                    new RuntimeException("file [" + (String) ArgList[0] + "] can not be found!");
                }

                return lastmodifiedtime;
            } catch (IOException e) {
                throw new RuntimeException(
                        "The function call getLastModifiedTime throw an error : " + e.toString());
            } finally {
                if (file != null)
                    try {
                        file.close();
                    } catch (Exception e) {
                    }
            }

        } else {
            throw new RuntimeException("The function call getLastModifiedTime is not valid.");
        }
    } catch (Exception e) {
        throw new RuntimeException(e.toString());
    }
}

From source file:com.panet.imeta.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java

public static String getLastModifiedTime(Context actualContext, Scriptable actualObject, Object[] ArgList,
        Function FunctionContext) {
    try {/*from w  ww .j a v a 2  s .  com*/
        if (ArgList.length == 2 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) {
            if (ArgList[0].equals(null))
                return null;
            FileObject file = null;

            try {
                // Source file
                file = KettleVFS.getFileObject(Context.toString(ArgList[0]));
                String dateformat = Context.toString(ArgList[1]);
                if (isNull(dateformat))
                    dateformat = "yyyy-MM-dd";
                String lastmodifiedtime = null;
                if (file.exists()) {
                    java.util.Date lastmodifiedtimedate = new java.util.Date(
                            file.getContent().getLastModifiedTime());
                    java.text.DateFormat dateFormat = new SimpleDateFormat(dateformat);
                    lastmodifiedtime = dateFormat.format(lastmodifiedtimedate);

                } else {
                    Context.reportRuntimeError("file [" + Context.toString(ArgList[0]) + "] can not be found!");
                }

                return lastmodifiedtime;
            } catch (IOException e) {
                throw Context.reportRuntimeError(
                        "The function call getLastModifiedTime throw an error : " + e.toString());
            } finally {
                if (file != null)
                    try {
                        file.close();
                    } catch (Exception e) {
                    }
            }

        } else {
            throw Context.reportRuntimeError("The function call getLastModifiedTime is not valid.");
        }
    } catch (Exception e) {
        throw Context.reportRuntimeError(e.toString());
    }
}

From source file:com.panet.imeta.job.entries.deletefile.JobEntryDeleteFile.java

public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) {
    LogWriter log = LogWriter.getInstance();
    Result result = previousResult;
    result.setResult(false);/* w w  w . ja v  a  2  s.co m*/

    if (filename != null) {
        String realFilename = getRealFilename();

        FileObject fileObject = null;
        try {
            fileObject = KettleVFS.getFileObject(realFilename);

            if (!fileObject.exists()) {
                if (isFailIfFileNotExists()) {
                    // File doesn't exist and fail flag is on.
                    result.setResult(false);
                    log.logError(toString(), Messages
                            .getString("JobEntryDeleteFile.ERROR_0004_File_Does_Not_Exist", realFilename)); //$NON-NLS-1$
                } else {
                    // File already deleted, no reason to try to delete it
                    result.setResult(true);
                    if (log.isBasic())
                        log.logBasic(toString(),
                                Messages.getString("JobEntryDeleteFile.File_Already_Deleted", realFilename)); //$NON-NLS-1$
                }
            } else {
                // Here gc() is explicitly called if e.g. createfile is used
                // in the same
                // job for the same file. The problem is that after creating
                // the file the
                // file object is not properly garbaged collected and thus
                // the file cannot
                // be deleted anymore. This is a known problem in the JVM.
                System.gc();

                boolean deleted = fileObject.delete();
                if (!deleted) {
                    log.logError(toString(), Messages
                            .getString("JobEntryDeleteFile.ERROR_0005_Could_Not_Delete_File", realFilename)); //$NON-NLS-1$
                    result.setResult(false);
                    result.setNrErrors(1);
                }
                if (log.isBasic())
                    log.logBasic(toString(),
                            Messages.getString("JobEntryDeleteFile.File_Deleted", realFilename)); //$NON-NLS-1$
                result.setResult(true);
            }
        } catch (IOException e) {
            log.logError(toString(), Messages.getString("JobEntryDeleteFile.ERROR_0006_Exception_Deleting_File", //$NON-NLS-1$
                    realFilename, e.getMessage()));
            result.setResult(false);
            result.setNrErrors(1);
        } finally {
            if (fileObject != null) {
                try {
                    fileObject.close();
                } catch (IOException ex) {
                }
                ;
            }
        }
    } else {
        log.logError(toString(), Messages.getString("JobEntryDeleteFile.ERROR_0007_No_Filename_Is_Defined")); //$NON-NLS-1$
    }

    return result;
}

From source file:com.panet.imeta.job.entries.movefiles.JobEntryMoveFiles.java

public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) throws KettleException {
    LogWriter log = LogWriter.getInstance();
    Result result = previousResult;
    List<RowMetaAndData> rows = result.getRows();
    RowMetaAndData resultRow = null;// w w  w . ja  v a 2 s.  co  m
    result.setEntryNr(1);
    result.setResult(false);

    NrErrors = 0;
    NrSuccess = 0;
    successConditionBroken = false;
    successConditionBrokenExit = false;
    limitFiles = Const.toInt(environmentSubstitute(getNrErrorsLessThan()), 10);

    if (simulate) {
        if (log.isDetailed())
            log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.SimulationOn"));
    }
    if (include_subfolders) {
        if (log.isDetailed())
            log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.IncludeSubFoldersOn"));
    }

    String MoveToFolder = environmentSubstitute(destinationFolder);
    // Get source and destination files, also wildcard
    String vsourcefilefolder[] = source_filefolder;
    String vdestinationfilefolder[] = destination_filefolder;
    String vwildcard[] = wildcard;

    if (iffileexists.equals("move_file")) {
        if (Const.isEmpty(MoveToFolder)) {
            log.logError(toString(), Messages.getString("JobMoveFiles.Log.Error.MoveToFolderMissing"));
            return result;
        }
        FileObject folder = null;
        try {
            folder = KettleVFS.getFileObject(MoveToFolder);
            if (!folder.exists()) {
                if (log.isDetailed())
                    log.logDetailed(toString(),
                            Messages.getString("JobMoveFiles.Log.Error.FolderMissing", MoveToFolder));
                if (create_move_to_folder) {
                    folder.createFolder();
                } else {
                    log.logError(toString(),
                            Messages.getString("JobMoveFiles.Log.Error.FolderMissing", MoveToFolder));
                    return result;
                }
            }
            if (!folder.getType().equals(FileType.FOLDER)) {
                log.logError(toString(), Messages.getString("JobMoveFiles.Log.Error.NotFolder", MoveToFolder));
                return result;
            }
        } catch (Exception e) {
            log.logError(toString(), Messages.getString("JobMoveFiles.Log.Error.GettingMoveToFolder",
                    MoveToFolder, e.getMessage()));
            return result;
        } finally {
            if (folder != null) {
                try {
                    folder.close();
                } catch (IOException ex) {
                }
                ;
            }
        }
    }

    if (arg_from_previous) {
        if (log.isDetailed())
            log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.ArgFromPrevious.Found",
                    (rows != null ? rows.size() : 0) + ""));
    }
    if (arg_from_previous && rows != null) {
        for (int iteration = 0; iteration < rows.size() && !parentJob.isStopped(); iteration++) {
            // Success condition broken?
            if (successConditionBroken) {
                if (!successConditionBrokenExit) {
                    log.logError(toString(),
                            Messages.getString("JobMoveFiles.Error.SuccessConditionbroken", "" + NrErrors));
                    successConditionBrokenExit = true;
                }
                result.setNrErrors(NrErrors);
                displayResults(log);
                return result;
            }

            resultRow = rows.get(iteration);

            // Get source and destination file names, also wildcard
            String vsourcefilefolder_previous = resultRow.getString(0, null);
            String vdestinationfilefolder_previous = resultRow.getString(1, null);
            String vwildcard_previous = resultRow.getString(2, null);

            if (!Const.isEmpty(vsourcefilefolder_previous) && !Const.isEmpty(vdestinationfilefolder_previous)) {
                if (log.isDetailed())
                    log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.ProcessingRow",
                            vsourcefilefolder_previous, vdestinationfilefolder_previous, vwildcard_previous));

                if (!ProcessFileFolder(vsourcefilefolder_previous, vdestinationfilefolder_previous,
                        vwildcard_previous, parentJob, result, MoveToFolder, log)) {
                    // The move process fail
                    // Update Errors
                    updateErrors();
                }
            } else {
                if (log.isDetailed())
                    log.logDetailed(toString(),
                            Messages.getString("JobMoveFiles.Log.IgnoringRow", vsourcefilefolder[iteration],
                                    vdestinationfilefolder[iteration], vwildcard[iteration]));
            }
        }
    } else if (vsourcefilefolder != null && vdestinationfilefolder != null) {
        for (int i = 0; i < vsourcefilefolder.length && !parentJob.isStopped(); i++) {
            // Success condition broken?
            if (successConditionBroken) {
                if (!successConditionBrokenExit) {
                    log.logError(toString(),
                            Messages.getString("JobMoveFiles.Error.SuccessConditionbroken", "" + NrErrors));
                    successConditionBrokenExit = true;
                }
                result.setEntryNr(NrErrors);
                displayResults(log);
                return result;
            }

            if (!Const.isEmpty(vsourcefilefolder[i]) && !Const.isEmpty(vdestinationfilefolder[i])) {
                // ok we can process this file/folder
                if (log.isDetailed())
                    log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.ProcessingRow",
                            vsourcefilefolder[i], vdestinationfilefolder[i], vwildcard[i]));

                if (!ProcessFileFolder(vsourcefilefolder[i], vdestinationfilefolder[i], vwildcard[i], parentJob,
                        result, MoveToFolder, log)) {
                    // Update Errors
                    updateErrors();
                }
            } else {
                if (log.isDetailed())
                    log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.IgnoringRow",
                            vsourcefilefolder[i], vdestinationfilefolder[i], vwildcard[i]));
            }
        }
    }

    // Success Condition
    result.setNrErrors(NrErrors);
    result.setNrLinesWritten(NrSuccess);
    if (getSuccessStatus())
        result.setResult(true);

    displayResults(log);

    return result;
}

From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java

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

    try {//from www  .ja  v  a 2  s  .  co 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) {
                    }
                if (fileDestination != null)
                    try {
                        fileDestination.close();
                    } catch (Exception e) {
                    }
            }

        } else {
            throw new RuntimeException("The function call copyFile is not valid.");
        }
    } catch (Exception e) {
        throw new RuntimeException(e.toString());
    }
}

From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java

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

    try {//from www.ja v  a  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 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 {
                    new RuntimeException("file to copy [" + (String) 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) {
                    }
                if (fileDestination != null)
                    try {
                        fileDestination.close();
                    } catch (Exception e) {
                    }
            }

        } else {
            throw new RuntimeException("The function call copyFileis not valid.");
        }
    } catch (Exception e) {
        throw new RuntimeException(e.toString());
    }
}

From source file:com.panet.imeta.job.entries.filecompare.JobEntryFileCompare.java

public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) {
    LogWriter log = LogWriter.getInstance();
    Result result = previousResult;
    result.setResult(false);/*from  w  ww  .ja  va2  s. c  o m*/

    String realFilename1 = getRealFilename1();
    String realFilename2 = getRealFilename2();

    FileObject file1 = null;
    FileObject file2 = null;
    try {
        if (filename1 != null && filename2 != null) {
            file1 = KettleVFS.getFileObject(realFilename1);
            file2 = KettleVFS.getFileObject(realFilename2);

            if (file1.exists() && file2.exists()) {
                if (equalFileContents(file1, file2)) {
                    result.setResult(true);
                } else {
                    result.setResult(false);
                }

                // add filename to result filenames
                if (addFilenameToResult && file1.getType() == FileType.FILE
                        && file2.getType() == FileType.FILE) {
                    ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, file1,
                            parentJob.getJobname(), toString());
                    resultFile.setComment(Messages.getString("JobWaitForFile.FilenameAdded"));
                    result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                    resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, file2, parentJob.getJobname(),
                            toString());
                    resultFile.setComment(Messages.getString("JobWaitForFile.FilenameAdded"));
                    result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                }
            } else {
                if (!file1.exists())
                    log.logError(toString(), Messages
                            .getString("JobEntryFileCompare.ERROR_0004_File1_Does_Not_Exist", realFilename1)); //$NON-NLS-1$
                if (!file2.exists())
                    log.logError(toString(), Messages
                            .getString("JobEntryFileCompare.ERROR_0005_File2_Does_Not_Exist", realFilename2)); //$NON-NLS-1$
                result.setResult(false);
                result.setNrErrors(1);
            }
        } else {
            log.logError(toString(), Messages.getString("JobEntryFileCompare.ERROR_0006_Need_Two_Filenames")); //$NON-NLS-1$
        }
    } catch (Exception e) {
        result.setResult(false);
        result.setNrErrors(1);
        log.logError(toString(), Messages.getString("JobEntryFileCompare.ERROR_0007_Comparing_Files", //$NON-NLS-1$
                realFilename2, realFilename2, e.getMessage()));
    } finally {
        try {
            if (file1 != null)
                file1.close();

            if (file2 != null)
                file2.close();
        } catch (IOException e) {
        }
    }

    return result;
}

From source file:com.panet.imeta.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java

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

    try {/* w w  w . j  av a2  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) {
                    }
                if (fileDestination != null)
                    try {
                        fileDestination.close();
                    } catch (Exception e) {
                    }
            }

        } else {
            throw Context.reportRuntimeError("The function call copyFile is not valid.");
        }
    } catch (Exception e) {
        throw Context.reportRuntimeError(e.toString());
    }
}

From source file:com.panet.imeta.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java

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

    try {//from ww  w .j av  a 2s.  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 copy
                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 copy the file...
                        if ((destinationExists && overwrite) || !destinationExists)
                            FileUtil.copyContent(fileSource, fileDestination);

                    }
                } else {
                    Context.reportRuntimeError(
                            "file to copy [" + Context.toString(ArgList[0]) + "] can not be found!");
                }
            } catch (IOException e) {
                throw Context.reportRuntimeError("The function call copyFile throw an error : " + e.toString());
            } finally {
                if (fileSource != null)
                    try {
                        fileSource.close();
                    } catch (Exception e) {
                    }
                if (fileDestination != null)
                    try {
                        fileDestination.close();
                    } catch (Exception e) {
                    }
            }

        } else {
            throw Context.reportRuntimeError("The function call copyFileis not valid.");
        }
    } catch (Exception e) {
        throw Context.reportRuntimeError(e.toString());
    }
}