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

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

Introduction

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

Prototype

public boolean exists() throws FileSystemException;

Source Link

Document

Determines if this file exists.

Usage

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

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

    try {/*from ww  w  .j a  va2s. 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(Context.toString(ArgList[0]));
                if (fileObject.exists()) {
                    if (fileObject.getType() == FileType.FILE) {
                        if (!fileObject.delete())
                            Context.reportRuntimeError(
                                    "We can not delete file [" + Context.toString(ArgList[0]) + "]!");
                    }

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

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

From source file:com.panet.imeta.core.fileinput.FileInputList.java

public static FileInputList createFolderList(VariableSpace space, String[] folderName,
        String[] folderRequired) {
    FileInputList fileInputList = new FileInputList();

    // Replace possible environment variables...
    final String realfolder[] = space.environmentSubstitute(folderName);

    for (int i = 0; i < realfolder.length; i++) {
        final String onefile = realfolder[i];
        final boolean onerequired = YES.equalsIgnoreCase(folderRequired[i]);
        final boolean subdirs = true;
        final FileTypeFilter filter = FileTypeFilter.ONLY_FOLDERS;

        if (Const.isEmpty(onefile))
            continue;
        FileObject directoryFileObject = null;

        try {//from   w w w .  j  a v  a 2s. c  om
            // Find all folder names  in this directory
            //
            directoryFileObject = KettleVFS.getFileObject(onefile);
            if (directoryFileObject != null && directoryFileObject.getType() == FileType.FOLDER) // it's a directory
            {

                FileObject[] fileObjects = directoryFileObject.findFiles(new AllFileSelector() {
                    public boolean traverseDescendents(FileSelectInfo info) {
                        return info.getDepth() == 0 || subdirs;
                    }

                    public boolean includeFile(FileSelectInfo info) {
                        // Never return the parent directory of a file list.
                        if (info.getDepth() == 0) {
                            return false;
                        }

                        FileObject fileObject = info.getFile();
                        try {
                            if (fileObject != null && filter.isFileTypeAllowed(fileObject.getType())) {
                                return true;
                            }
                            return false;
                        } catch (FileSystemException ex) {
                            // Upon error don't process the file.
                            return false;
                        }
                    }
                });
                if (fileObjects != null) {
                    for (int j = 0; j < fileObjects.length; j++) {
                        if (fileObjects[j].exists())
                            fileInputList.addFile(fileObjects[j]);
                    }
                }
                if (Const.isEmpty(fileObjects)) {
                    if (onerequired)
                        fileInputList.addNonAccessibleFile(directoryFileObject);
                }

                // Sort the list: quicksort, only for regular files
                fileInputList.sortFiles();
            } else {
                if (onerequired && !directoryFileObject.exists())
                    fileInputList.addNonExistantFile(directoryFileObject);
            }
        } catch (Exception e) {
            LogWriter.getInstance().logError("FileInputList", Const.getStackTracker(e));
        } finally {
            try {
                if (directoryFileObject != null)
                    directoryFileObject.close();
                directoryFileObject = null;
            } catch (Exception e) {
            }
            ;
        }
    }

    return fileInputList;
}

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;/*from w w w .  jav a  2  s.c om*/
    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 double getFileSize(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    try {//from ww  w .  ja va2s.c  om
        if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) {
            if (ArgList[0].equals(null))
                return 0;
            FileObject file = null;

            try {
                // Source file
                file = KettleVFS.getFileObject((String) ArgList[0]);
                long filesize = 0;
                if (file.exists()) {
                    if (file.getType().equals(FileType.FILE))
                        filesize = file.getContent().getSize();
                    else
                        new RuntimeException("[" + (String) ArgList[0] + "] is not a file!");
                } else {
                    new RuntimeException("file [" + (String) ArgList[0] + "] can not be found!");
                }
                return filesize;
            } catch (IOException e) {
                throw new RuntimeException("The function call getFileSize throw an error : " + e.toString());
            } finally {
                if (file != null)
                    try {
                        file.close();
                    } catch (Exception e) {
                    }
            }

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

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

public static String getShortFilename(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    try {// w  w w . j  ava 2s.  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((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.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java

public static boolean isFile(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    try {//from  ww w .j  a va  2s  .  c  o m
        if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) {
            if (ArgList[0].equals(null))
                return false;
            FileObject file = null;

            try {
                // Source file
                file = KettleVFS.getFileObject((String) ArgList[0]);
                boolean isafile = false;
                if (file.exists()) {
                    if (file.getType().equals(FileType.FILE))
                        isafile = true;
                    else
                        new RuntimeException("[" + (String) ArgList[0] + "] is not a file!");
                } else {
                    new RuntimeException("file [" + (String) ArgList[0] + "] can not be found!");
                }
                return isafile;
            } catch (IOException e) {
                throw new RuntimeException("The function call is File throw an error : " + e.toString());
            } finally {
                if (file != null)
                    try {
                        file.close();
                    } catch (Exception e) {
                    }
            }

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

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 2 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((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 {//from w  w  w  .  j  a  v a2  s  .  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 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.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java

public static boolean isFolder(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    try {/*from  w  w  w . j  ava2  s.  c o m*/
        if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) {
            if (ArgList[0].equals(null))
                return false;
            FileObject file = null;

            try {
                // Source file
                file = KettleVFS.getFileObject((String) ArgList[0]);
                boolean isafolder = false;
                if (file.exists()) {
                    if (file.getType().equals(FileType.FOLDER))
                        isafolder = true;
                    else
                        new RuntimeException("[" + (String) ArgList[0] + "] is not a folder!");
                } else {
                    new RuntimeException("folder [" + (String) ArgList[0] + "] can not be found!");
                }
                return isafolder;
            } catch (IOException e) {
                throw new RuntimeException("The function call isFolder throw an error : " + e.toString());
            } finally {
                if (file != null)
                    try {
                        file.close();
                    } catch (Exception e) {
                    }
            }

        } else {
            throw new RuntimeException("The function call isFolder 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 double getFileSize(Context actualContext, Scriptable actualObject, Object[] ArgList,
        Function FunctionContext) {
    try {//from w  w w.ja  v a 2s  .  c  o m
        if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) {
            if (ArgList[0].equals(null))
                return 0;
            FileObject file = null;

            try {
                // Source file
                file = KettleVFS.getFileObject(Context.toString(ArgList[0]));
                long filesize = 0;
                if (file.exists()) {
                    if (file.getType().equals(FileType.FILE))
                        filesize = file.getContent().getSize();
                    else
                        Context.reportRuntimeError("[" + Context.toString(ArgList[0]) + "] is not a file!");
                } else {
                    Context.reportRuntimeError("file [" + Context.toString(ArgList[0]) + "] can not be found!");
                }
                return filesize;
            } catch (IOException e) {
                throw Context
                        .reportRuntimeError("The function call getFileSize throw an error : " + e.toString());
            } finally {
                if (file != null)
                    try {
                        file.close();
                    } catch (Exception e) {
                    }
            }

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