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.createfolder.JobEntryCreateFolder.java

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

    if (foldername != null) {
        String realFoldername = getRealFoldername();
        FileObject FolderObject = null;
        try {
            FolderObject = KettleVFS.getFileObject(realFoldername);

            if (FolderObject.exists()) {
                boolean isFolder = false;

                //Check if it's a folder
                if (FolderObject.getType() == FileType.FOLDER)
                    isFolder = true;

                if (isFailOfFolderExists()) {
                    // Folder exists and fail flag is on.
                    result.setResult(false);
                    if (isFolder)
                        log.logError(toString(), "Folder [" + realFoldername + "] exists, failing.");
                    else
                        log.logError(toString(), "File [" + realFoldername + "] exists, failing.");
                } else {
                    // Folder already exists, no reason to try to create it
                    result.setResult(true);
                    if (log.isDetailed())
                        log.logDetailed(toString(),
                                "Folder [" + realFoldername + "] already exists, not recreating.");
                }

            } else {
                //  No Folder yet, create an empty Folder.
                FolderObject.createFolder();
                if (log.isDetailed())
                    log.logDetailed(toString(), "Folder [" + realFoldername + "] created!");
                result.setResult(true);
            }
        } catch (IOException e) {
            log.logError(toString(),
                    "Could not create Folder [" + realFoldername + "], exception: " + e.getMessage());
            result.setResult(false);
            result.setNrErrors(1);
        } finally {
            if (FolderObject != null) {
                try {
                    FolderObject.close();
                } catch (IOException ex) {
                }
                ;
            }
        }
    } else {
        log.logError(toString(), "No Foldername is defined.");
    }

    return result;
}

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

public static String getShortFilename(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    try {//from   w  w w . j av  a 2s  .c o  m
        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().toString();

                } else {
                    new RuntimeException("file [" + (String) 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) {
                    }
            }

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

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

private boolean ProcessFileFolder(String sourcefilefoldername, String destinationfilefoldername,
        String wildcard, Job parentJob, Result result, String MoveToFolder, LogWriter log) {
    boolean entrystatus = false;
    FileObject sourcefilefolder = null;
    FileObject destinationfilefolder = null;
    FileObject movetofolderfolder = null;
    FileObject Currentfile = null;

    // Get real source, destination file and wildcard
    String realSourceFilefoldername = environmentSubstitute(sourcefilefoldername);
    String realDestinationFilefoldername = environmentSubstitute(destinationfilefoldername);
    String realWildcard = environmentSubstitute(wildcard);

    try {//  ww  w  .  j  ava  2 s .com

        // 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();

        sourcefilefolder = KettleVFS.getFileObject(realSourceFilefoldername);
        destinationfilefolder = KettleVFS.getFileObject(realDestinationFilefoldername);
        if (!Const.isEmpty(MoveToFolder))
            movetofolderfolder = KettleVFS.getFileObject(MoveToFolder);

        if (sourcefilefolder.exists()) {

            // Check if destination folder/parent folder exists !
            // If user wanted and if destination folder does not exist
            // PDI will create it
            if (CreateDestinationFolder(destinationfilefolder)) {

                // Basic Tests
                if (sourcefilefolder.getType().equals(FileType.FOLDER) && destination_is_a_file) {
                    // Source is a folder, destination is a file
                    // WARNING !!! CAN NOT MOVE FOLDER TO FILE !!!

                    log.logError(Messages.getString("JobMoveFiles.Log.Forbidden"),
                            Messages.getString("JobMoveFiles.Log.CanNotMoveFolderToFile",
                                    realSourceFilefoldername, realDestinationFilefoldername));

                    // Update Errors
                    updateErrors();
                } else {
                    if (destinationfilefolder.getType().equals(FileType.FOLDER)
                            && sourcefilefolder.getType().equals(FileType.FILE)) {
                        // Source is a file, destination is a folder
                        // return destination short filename
                        String shortfilename = sourcefilefolder.getName().getBaseName();

                        try {
                            shortfilename = getDestinationFilename(sourcefilefolder.getName().getBaseName());
                        } catch (Exception e) {
                            log.logError(toString(),
                                    Messages.getString(Messages.getString("JobMoveFiles.Error.GettingFilename",
                                            sourcefilefolder.getName().getBaseName(), e.toString())));
                            return entrystatus;
                        }
                        // Move the file to the destination folder

                        String destinationfilenamefull = destinationfilefolder.toString() + Const.FILE_SEPARATOR
                                + shortfilename;
                        FileObject destinationfile = KettleVFS.getFileObject(destinationfilenamefull);

                        entrystatus = MoveFile(shortfilename, sourcefilefolder, destinationfile,
                                movetofolderfolder, log, parentJob, result);

                    } else if (sourcefilefolder.getType().equals(FileType.FILE) && destination_is_a_file) {
                        // Source is a file, destination is a file

                        FileObject destinationfile = KettleVFS.getFileObject(realDestinationFilefoldername);

                        // return destination short filename
                        String shortfilename = destinationfile.getName().getBaseName();
                        try {
                            shortfilename = getDestinationFilename(destinationfile.getName().getBaseName());
                        } catch (Exception e) {
                            log.logError(toString(),
                                    Messages.getString(Messages.getString("JobMoveFiles.Error.GettingFilename",
                                            sourcefilefolder.getName().getBaseName(), e.toString())));
                            return entrystatus;
                        }

                        String destinationfilenamefull = destinationfilefolder.getParent().toString()
                                + Const.FILE_SEPARATOR + shortfilename;
                        destinationfile = KettleVFS.getFileObject(destinationfilenamefull);

                        entrystatus = MoveFile(shortfilename, sourcefilefolder, destinationfile,
                                movetofolderfolder, log, parentJob, result);

                    } else {
                        // Both source and destination are folders
                        if (log.isDetailed()) {
                            log.logDetailed(toString(), "  ");
                            log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FetchFolder",
                                    sourcefilefolder.toString()));
                        }

                        FileObject[] fileObjects = sourcefilefolder.findFiles(new AllFileSelector() {
                            public boolean traverseDescendents(FileSelectInfo info) {
                                return true;
                            }

                            public boolean includeFile(FileSelectInfo info) {
                                FileObject fileObject = info.getFile();
                                try {
                                    if (fileObject == null)
                                        return false;
                                } catch (Exception ex) {
                                    // Upon error don't process the
                                    // file.
                                    return false;
                                }

                                finally {
                                    if (fileObject != null) {
                                        try {
                                            fileObject.close();
                                        } catch (IOException ex) {
                                        }
                                        ;
                                    }

                                }
                                return true;
                            }
                        });

                        if (fileObjects != null) {
                            for (int j = 0; j < fileObjects.length && !parentJob.isStopped(); j++) {
                                // Success condition broken?
                                if (successConditionBroken) {
                                    if (!successConditionBrokenExit) {
                                        log.logError(toString(), Messages.getString(
                                                "JobMoveFiles.Error.SuccessConditionbroken", "" + NrErrors));
                                        successConditionBrokenExit = true;
                                    }
                                    return false;
                                }
                                // Fetch files in list one after one ...
                                Currentfile = fileObjects[j];

                                if (!MoveOneFile(Currentfile, sourcefilefolder, realDestinationFilefoldername,
                                        realWildcard, log, parentJob, result, movetofolderfolder)) {
                                    // Update Errors
                                    updateErrors();
                                }

                            }
                        }
                    }

                }
                entrystatus = true;
            } // end if
            else {
                // Destination Folder or Parent folder is missing
                log.logError(toString(), Messages.getString("JobMoveFiles.Error.DestinationFolderNotFound",
                        realDestinationFilefoldername));
            }
        } // end if
        else {
            log.logError(toString(),
                    Messages.getString("JobMoveFiles.Error.SourceFileNotExists", realSourceFilefoldername));
        }
    } // end try
    catch (Exception e) {
        log.logError(toString(), Messages.getString("JobMoveFiles.Error.Exception.MoveProcess",
                realSourceFilefoldername.toString(), destinationfilefolder.toString(), e.getMessage()));
        // Update Errors
        updateErrors();
    } finally {
        if (sourcefilefolder != null) {
            try {
                sourcefilefolder.close();
            } catch (IOException ex) {
            }
            ;
        }
        if (destinationfilefolder != null) {
            try {
                destinationfilefolder.close();
            } catch (IOException ex) {
            }
            ;
        }
        if (Currentfile != null) {
            try {
                Currentfile.close();
            } catch (IOException ex) {
            }
            ;
        }
        if (movetofolderfolder != null) {
            try {
                movetofolderfolder.close();
            } catch (IOException ex) {
            }
            ;
        }
    }
    return entrystatus;
}

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

public static String getFileExtension(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    try {/*w w w  .  j a v a 2s . com*/
        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().toString();

                } else {
                    new RuntimeException("file [" + (String) 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) {
                    }
            }

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

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

public static String getParentFoldername(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    try {/* w  w w  .  j a v a  2 s  .  c  o m*/
        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 foldername = null;
                if (file.exists()) {
                    foldername = KettleVFS.getFilename(file.getParent());

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

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

        } else {
            throw new RuntimeException("The function call getParentFoldername 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 getShortFilename(Context actualContext, Scriptable actualObject, Object[] ArgList,
        Function FunctionContext) {
    try {//from   w ww  .  ja  v  a2s  .  co  m
        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(Context.toString(ArgList[0]));
                String Filename = null;
                if (file.exists()) {
                    Filename = file.getName().getBaseName().toString();

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

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

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

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

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

    try {/*from ww  w. jav  a  2 s.c o m*/
        if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) {
            //Object act = actualObject.get("_step_", actualObject);
            //ScriptValuesMod act = (ScriptValuesMod)Context.toType(scm_delete, ScriptValuesMod.class);

            FileObject fileObject = null;

            try {
                fileObject = KettleVFS.getFileObject((String) ArgList[0]);
                if (fileObject.exists()) {
                    if (fileObject.getType() == FileType.FILE) {
                        if (!fileObject.delete())
                            new RuntimeException("We can not delete file [" + (String) ArgList[0] + "]!");
                    }

                } else {
                    new RuntimeException("file [" + (String) ArgList[0] + "] can not be found!");
                }
            } catch (IOException e) {
                throw new RuntimeException("The function call deleteFile is not valid.");
            } finally {
                if (fileObject != null)
                    try {
                        fileObject.close();
                    } catch (Exception e) {
                    }
            }

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

From source file:com.panet.imeta.job.entries.ftp.JobEntryFTP.java

private void addFilenameToResultFilenames(LogWriter log, Result result, Job parentJob, String filename)
        throws KettleException {
    if (isaddresult) {
        FileObject targetFile = null;
        try {//ww w  .j av  a2  s. c  om
            targetFile = KettleVFS.getFileObject(filename);

            // Add to the result files...
            ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, targetFile,
                    parentJob.getJobname(), toString());
            resultFile.setComment(Messages.getString("JobEntryFTP.Downloaded", serverName)); //$NON-NLS-1$
            result.getResultFiles().put(resultFile.getFile().toString(), resultFile);

            if (log.isDetailed())
                log.logDetailed(toString(), Messages.getString("JobEntryFTP.FileAddedToResult", filename)); //$NON-NLS-1$
        } catch (Exception e) {
            throw new KettleException(e);
        } finally {
            try {
                targetFile.close();
                targetFile = null;
            } catch (Exception e) {
            }
        }
    }
}

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

public static String getFileExtension(Context actualContext, Scriptable actualObject, Object[] ArgList,
        Function FunctionContext) {
    try {/*from   w w w.  j ava2  s  .  c om*/
        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(Context.toString(ArgList[0]));
                String Extension = null;
                if (file.exists()) {
                    Extension = file.getName().getExtension().toString();

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

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

        } else {
            throw Context.reportRuntimeError("The function call getFileExtension 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 String getParentFoldername(Context actualContext, Scriptable actualObject, Object[] ArgList,
        Function FunctionContext) {
    try {/*from   w  w  w.java2  s.c  o  m*/
        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(Context.toString(ArgList[0]));
                String foldername = null;
                if (file.exists()) {
                    foldername = KettleVFS.getFilename(file.getParent());

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

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

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