Example usage for org.apache.commons.vfs FileType FOLDER

List of usage examples for org.apache.commons.vfs FileType FOLDER

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileType FOLDER.

Prototype

FileType FOLDER

To view the source code for org.apache.commons.vfs FileType FOLDER.

Click Source Link

Document

A folder.

Usage

From source file:com.vobject.appengine.java.io.File.java

private File[] listChildrenAsFileArray(final FilenameFilter filter, final FileObject dir,
        final boolean recursive) throws FileSystemException {
    if (dir.getType() == FileType.FOLDER) {
        final FileObject[] children = dir.getChildren();
        final List list = new ArrayList();
        for (int i = 0; i < children.length; i++) {
            final FileObject child = children[i];
            final String fileName = child.getName().getBaseName();
            if (filter != null) {
                if (filter.accept(null, fileName)) {
                    list.add(new File(child));
                }/*from  w  w w  . j  a  va2  s  .  c  o  m*/
            } else {
                list.add(new File(child));
            }
            if (child.getType() == FileType.FOLDER) {
                if (recursive) {
                    File[] childs = listChildrenAsFileArray(filter, child, recursive);
                    list.addAll(Arrays.asList(childs));
                }
            }
        }
        return (File[]) list.toArray(new File[0]);
    } else {
        return null;
    }
}

From source file:com.panet.imeta.job.entries.copyfiles.JobEntryCopyFiles.java

private boolean ProcessFileFolder(String sourcefilefoldername, String destinationfilefoldername,
        String wildcard, Job parentJob, Result result) {

    LogWriter log = LogWriter.getInstance();
    boolean entrystatus = false;
    FileObject sourcefilefolder = null;
    FileObject destinationfilefolder = null;

    // Clear list files to remove after copy process
    // This list is also added to result files name
    list_files_remove.clear();//from ww  w.j ava 2s.  c  o  m
    list_add_result.clear();

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

    try {

        // 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 (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)// destinationfilefolder.getType().equals(FileType.FILE))
                {
                    // Source is a folder, destination is a file
                    // WARNING !!! CAN NOT COPY FOLDER TO FILE !!!

                    log.logError(Messages.getString("JobCopyFiles.Log.Forbidden"),
                            Messages.getString("JobCopyFiles.Log.CanNotCopyFolderToFile",
                                    realSourceFilefoldername, realDestinationFilefoldername));

                    NbrFail++;

                } else {

                    if (destinationfilefolder.getType().equals(FileType.FOLDER)
                            && sourcefilefolder.getType().equals(FileType.FILE)) {
                        // Source is a file, destination is a folder
                        // Copy the file to the destination folder

                        destinationfilefolder.copyFrom(sourcefilefolder.getParent(),
                                new TextOneFileSelector(sourcefilefolder.getParent().toString(),
                                        sourcefilefolder.getName().getBaseName(),
                                        destinationfilefolder.toString()));
                        if (log.isDetailed())
                            log.logDetailed(Messages.getString("JobCopyFiles.Log.FileCopiedInfos"),
                                    Messages.getString("JobCopyFiles.Log.FileCopied",
                                            sourcefilefolder.getName().toString(),
                                            destinationfilefolder.getName().toString()));

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

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

                        }
                        destinationfilefolder.copyFrom(sourcefilefolder,
                                new TextFileSelector(sourcefilefolder.toString(),
                                        destinationfilefolder.toString(), realWildcard, parentJob));
                    }

                    // Remove Files if needed
                    if (remove_source_files && !list_files_remove.isEmpty()) {
                        for (Iterator<String> iter = list_files_remove.iterator(); iter.hasNext()
                                && !parentJob.isStopped();) {
                            String fileremoventry = (String) iter.next();
                            // Remove ONLY Files
                            if (KettleVFS.getFileObject(fileremoventry).getType() == FileType.FILE) {
                                boolean deletefile = KettleVFS.getFileObject(fileremoventry).delete();
                                log.logBasic("", " ------ ");
                                if (!deletefile) {
                                    log.logError("      " + Messages.getString("JobCopyFiles.Log.Error"),
                                            Messages.getString(
                                                    "JobCopyFiles.Error.Exception.CanRemoveFileFolder",
                                                    fileremoventry));
                                } else {
                                    if (log.isDetailed())
                                        log.logDetailed(
                                                "      " + Messages
                                                        .getString("JobCopyFiles.Log.FileFolderRemovedInfos"),
                                                Messages.getString("JobCopyFiles.Log.FileFolderRemoved",
                                                        fileremoventry));
                                }
                            }
                        }
                    }

                    // Add files to result files name
                    if (add_result_filesname && !list_add_result.isEmpty()) {
                        for (Iterator<String> iter = list_add_result.iterator(); iter.hasNext();) {
                            String fileaddentry = (String) iter.next();
                            // Add ONLY Files
                            if (KettleVFS.getFileObject(fileaddentry).getType() == FileType.FILE) {
                                ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL,
                                        KettleVFS.getFileObject(fileaddentry), parentJob.getJobname(),
                                        toString());
                                result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                                if (log.isDetailed()) {
                                    log.logDetailed("", " ------ ");
                                    log.logDetailed(
                                            "      " + Messages.getString("JobCopyFiles.Log.ResultFilesName"),
                                            Messages.getString("JobCopyFiles.Log.FileAddedToResultFilesName",
                                                    fileaddentry));
                                }
                            }
                        }
                    }
                }
                entrystatus = true;
            } else {
                // Destination Folder or Parent folder is missing
                log.logError(toString(), Messages.getString("JobCopyFiles.Error.DestinationFolderNotFound",
                        realDestinationFilefoldername));
            }
        } else {
            log.logError(toString(),
                    Messages.getString("JobCopyFiles.Error.SourceFileNotExists", realSourceFilefoldername));

        }
    } catch (IOException e) {
        log.logError("Error", Messages.getString("JobCopyFiles.Error.Exception.CopyProcess",
                realSourceFilefoldername.toString(), destinationfilefolder.toString(), e.getMessage()));
    } finally {
        if (sourcefilefolder != null) {
            try {
                sourcefilefolder.close();

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

            } catch (IOException ex) {
            }
            ;
        }
    }

    return entrystatus;
}

From source file:com.panet.imeta.job.entries.unzip.JobEntryUnZip.java

public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) {
    LogWriter log = LogWriter.getInstance();
    Result result = previousResult;
    result.setResult(false);/*from   w  w  w .  j a  va2s.c  om*/
    result.setEntryNr(1);

    List<RowMetaAndData> rows = result.getRows();
    RowMetaAndData resultRow = null;

    String realFilenameSource = environmentSubstitute(zipFilename);
    String realWildcardSource = environmentSubstitute(wildcardSource);
    String realWildcard = environmentSubstitute(wildcard);
    String realWildcardExclude = environmentSubstitute(wildcardexclude);
    String realTargetdirectory = environmentSubstitute(targetdirectory);
    String realMovetodirectory = environmentSubstitute(movetodirectory);

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

    if (isfromprevious) {
        if (log.isDetailed())
            log.logDetailed(toString(), Messages.getString("JobUnZip.Log.ArgFromPrevious.Found",
                    (rows != null ? rows.size() : 0) + ""));

        if (rows.size() == 0)
            return result;
    } else {
        if (Const.isEmpty(zipFilename)) {
            // Zip file/folder is missing
            log.logError(toString(), Messages.getString("JobUnZip.No_ZipFile_Defined.Label"));
            return result;
        }
    }

    FileObject fileObject = null;
    FileObject targetdir = null;
    FileObject movetodir = null;

    try {

        // Let's make some checks here, before running job entry ...

        boolean exitjobentry = false;
        // Target folder
        targetdir = KettleVFS.getFileObject(realTargetdirectory);
        if (!targetdir.exists()) {
            if (createfolder) {
                targetdir.createFolder();
                if (log.isDetailed())
                    log.logDetailed(toString(),
                            Messages.getString("JobUnZip.Log.TargetFolderCreated", realTargetdirectory));

            } else {
                log.logError(Messages.getString("JobUnZip.Error.Label"),
                        Messages.getString("JobUnZip.TargetFolderNotFound.Label"));
                exitjobentry = true;
            }
        } else {
            if (!(targetdir.getType() == FileType.FOLDER)) {
                log.logError(Messages.getString("JobUnZip.Error.Label"),
                        Messages.getString("JobUnZip.TargetFolderNotFolder.Label", realTargetdirectory));
                exitjobentry = true;
            } else {
                if (log.isDetailed())
                    log.logDetailed(toString(),
                            Messages.getString("JobUnZip.TargetFolderExists.Label", realTargetdirectory));
            }
        }

        // If user want to move zip files after process
        // movetodirectory must be provided
        if (afterunzip == 2) {
            if (Const.isEmpty(movetodirectory)) {
                log.logError(Messages.getString("JobUnZip.Error.Label"),
                        Messages.getString("JobUnZip.MoveToDirectoryEmpty.Label"));
                exitjobentry = true;
            } else {
                movetodir = KettleVFS.getFileObject(realMovetodirectory);
                if (!(movetodir.exists()) || !(movetodir.getType() == FileType.FOLDER)) {
                    if (createMoveToDirectory) {
                        movetodir.createFolder();
                        if (log.isDetailed())
                            log.logDetailed(toString(), Messages.getString("JobUnZip.Log.MoveToFolderCreated",
                                    realMovetodirectory));
                    } else {
                        log.logError(Messages.getString("JobUnZip.Error.Label"),
                                Messages.getString("JobUnZip.MoveToDirectoryNotExists.Label"));
                        exitjobentry = true;
                    }
                }
            }
        }

        // We found errors...now exit
        if (exitjobentry)
            return result;

        if (isfromprevious) {
            if (rows != null) // Copy the input row to the (command line)
            // arguments
            {
                for (int iteration = 0; iteration < rows.size() && !parentJob.isStopped(); iteration++) {
                    if (successConditionBroken) {
                        if (!successConditionBrokenExit) {
                            log.logError(toString(),
                                    Messages.getString("JobUnZip.Error.SuccessConditionbroken", "" + NrErrors));
                            successConditionBrokenExit = true;
                        }
                        result.setEntryNr(NrErrors);
                        return result;
                    }

                    resultRow = rows.get(iteration);

                    // Get sourcefile/folder and wildcard
                    realFilenameSource = resultRow.getString(0, null);
                    realWildcardSource = resultRow.getString(1, null);

                    fileObject = KettleVFS.getFileObject(realFilenameSource);
                    if (fileObject.exists()) {
                        processOneFile(log, result, parentJob, fileObject, realTargetdirectory, realWildcard,
                                realWildcardExclude, movetodir, realMovetodirectory, realWildcardSource);
                    } else {
                        updateErrors();
                        log.logError(toString(),
                                Messages.getString("JobUnZip.Error.CanNotFindFile", realFilenameSource));
                    }
                }
            }
        } else {
            fileObject = KettleVFS.getFileObject(realFilenameSource);
            if (!fileObject.exists()) {
                log.logError(Messages.getString("JobUnZip.Error.Label"),
                        Messages.getString("JobUnZip.ZipFile.NotExists.Label", realFilenameSource));
                return result;
            }

            if (log.isDetailed())
                log.logDetailed(toString(),
                        Messages.getString("JobUnZip.Zip_FileExists.Label", realFilenameSource));
            if (Const.isEmpty(targetdirectory)) {
                log.logError(Messages.getString("JobUnZip.Error.Label"),
                        Messages.getString("JobUnZip.TargetFolderNotFound.Label"));
                return result;
            }

            processOneFile(log, result, parentJob, fileObject, realTargetdirectory, realWildcard,
                    realWildcardExclude, movetodir, realMovetodirectory, realWildcardSource);
        }
    } catch (Exception e) {
        log.logError(Messages.getString("JobUnZip.Error.Label"),
                Messages.getString("JobUnZip.ErrorUnzip.Label", realFilenameSource, e.getMessage()));
        updateErrors();
    } finally {
        if (fileObject != null) {
            try {
                fileObject.close();
            } catch (IOException ex) {
            }
            ;
        }
        if (targetdir != null) {
            try {
                targetdir.close();
            } catch (IOException ex) {
            }
            ;
        }
        if (movetodir != null) {
            try {
                movetodir.close();
            } catch (IOException ex) {
            }
            ;
        }
    }

    result.setNrErrors(NrErrors);
    result.setNrLinesWritten(NrSuccess);
    if (getSuccessStatus())
        result.setResult(true);
    displayResults(log);

    return result;
}

From source file:com.panet.imeta.job.entries.xmlwellformed.JobEntryXMLWellFormed.java

private boolean ProcessFileFolder(String sourcefilefoldername, String wildcard, Job parentJob, Result result) {
    LogWriter log = LogWriter.getInstance();
    boolean entrystatus = false;
    FileObject sourcefilefolder = null;
    FileObject CurrentFile = null;

    // Get real source file and wilcard
    String realSourceFilefoldername = environmentSubstitute(sourcefilefoldername);
    if (Const.isEmpty(realSourceFilefoldername)) {
        log.logError(toString(),//  w  w  w . ja  v a2s .  c  o  m
                Messages.getString("JobXMLWellFormed.log.FileFolderEmpty", sourcefilefoldername));
        // Update Errors
        updateErrors();

        return entrystatus;
    }
    String realWildcard = environmentSubstitute(wildcard);

    try {
        sourcefilefolder = KettleVFS.getFileObject(realSourceFilefoldername);

        if (sourcefilefolder.exists()) {
            if (log.isDetailed())
                log.logDetailed(toString(),
                        Messages.getString("JobXMLWellFormed.Log.FileExists", sourcefilefolder.toString()));
            if (sourcefilefolder.getType() == FileType.FILE) {
                entrystatus = checkOneFile(sourcefilefolder, log, result, parentJob);

            } else if (sourcefilefolder.getType() == FileType.FOLDER) {
                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;
                            if (fileObject.getType() != FileType.FILE)
                                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++) {
                        if (successConditionBroken) {
                            if (!successConditionBrokenExit) {
                                log.logError(toString(), Messages.getString(
                                        "JobXMLWellFormed.Error.SuccessConditionbroken", "" + NrAllErrors));
                                successConditionBrokenExit = true;
                            }
                            return false;
                        }
                        // Fetch files in list one after one ...
                        CurrentFile = fileObjects[j];

                        if (!CurrentFile.getParent().toString().equals(sourcefilefolder.toString())) {
                            // Not in the Base Folder..Only if include sub folders  
                            if (include_subfolders) {
                                if (GetFileWildcard(CurrentFile.toString(), realWildcard)) {
                                    checkOneFile(CurrentFile, log, result, parentJob);
                                }
                            }

                        } else {
                            // In the base folder
                            if (GetFileWildcard(CurrentFile.toString(), realWildcard)) {
                                checkOneFile(CurrentFile, log, result, parentJob);
                            }
                        }
                    }
                }
            } else {
                log.logError(toString(), Messages.getString("JobXMLWellFormed.Error.UnknowFileFormat",
                        sourcefilefolder.toString()));
                // Update Errors
                updateErrors();
            }
        } else {
            log.logError(toString(),
                    Messages.getString("JobXMLWellFormed.Error.SourceFileNotExists", realSourceFilefoldername));
            // Update Errors
            updateErrors();
        }
    } // end try

    catch (IOException e) {
        log.logError(toString(), Messages.getString("JobXMLWellFormed.Error.Exception.Processing",
                realSourceFilefoldername.toString(), e.getMessage()));
        // Update Errors
        updateErrors();
    } finally {
        if (sourcefilefolder != null) {
            try {
                sourcefilefolder.close();
            } catch (IOException ex) {
            }
            ;

        }
        if (CurrentFile != null) {
            try {
                CurrentFile.close();
            } catch (IOException ex) {
            }
            ;
        }
    }
    return entrystatus;
}

From source file:com.adito.networkplaces.actions.FileSystemViewDispatchAction.java

void buildModel(VFSResource res, FileSystemForm fileSystemForm, HttpServletRequest request) throws Exception {
    List<FileSystemItem> allFileSystemItems = new ArrayList<FileSystemItem>();
    ActionMessages warnings = new ActionMessages();
    if (res != null) {
        Iterator itr = res.getChildren();

        for (int i = 0; itr != null && itr.hasNext(); i++) {
            // We overide to string so filtering works
            Calendar gc = new GregorianCalendar() {
                public String toString() {
                    return SimpleDateFormat.getInstance().format(this.getTime());
                }/*ww  w. j  av  a2  s .  c  o m*/
            };
            gc.setTimeInMillis(0);
            FileObjectVFSResource element = (FileObjectVFSResource) itr.next();
            // this is an extra defense against imaginary files.
            FileType ft = null;
            try {
                ft = element.getFile().getType();
            } catch (FileSystemException e) {
            } catch (IOException e) {
            }
            FileSystemItem item = null;
            if (ft != null && element.getFile().getType().equals(FileType.FOLDER)
                    && fileSystemForm.getNetworkPlace().isAllowRecursive()) {
                // if it is a folder
                if (element.getLastModified() != null)
                    gc.setTime(element.getLastModified());
                item = new FolderItem(fileSystemForm.getLaunchSession(), element.getDisplayName(),
                        res.getMount().getStore().getName(), fileSystemForm.getPath(), gc,
                        element.getFile().getType().getName(), false, i);
            } else if (ft != null && element.getFile().getType().equals(FileType.FILE)) {
                // if it is a file
                if (element.getLastModified() != null)
                    gc.setTime(element.getLastModified());
                item = new FileItem(fileSystemForm.getLaunchSession(), element.getDisplayName(),
                        element.getContentLength().longValue(), gc, element.getFile().getType().getName(),
                        false, i);
            } else {
                if (log.isInfoEnabled())
                    log.info("Unable to display file " + element.getDisplayName()
                            + " as it is an imaginary file.");
                warnings.add(Constants.REQ_ATTR_WARNINGS,
                        new BundleActionMessage(NetworkPlacePlugin.MESSAGE_RESOURCES_KEY, "vfs.imaginary.file",
                                element.getDisplayName()));

                // decrement the counter as there is no file added.
                i--;
            }
            if (item != null) {
                allFileSystemItems.add(item);
                if (request.getParameter("select" + Util.urlEncode(item.getFileName())) != null) {
                    item.setChecked(true);
                }
            }
        }
        if (fileSystemForm.getPaths() == null || fileSystemForm.getPaths().isEmpty()) {
            fileSystemForm.setHome(fileSystemForm.getPath());
        } else {
            fileSystemForm.addPath(fileSystemForm.getPath());
        }
    }
    if (warnings.size() > 0) {
        addWarnings(request, warnings);
    }
    fileSystemForm.initialize(request, allFileSystemItems, this.getSessionInfo(request));
}

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  ww.ja  va2 s . c o 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.panet.imeta.job.entries.exportrepository.JobEntryExportRepository.java

public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) {
    LogWriter log = LogWriter.getInstance();
    Result result = previousResult;
    result.setNrErrors(1);/*from ww  w.  ja  v  a 2 s  .  c om*/
    result.setResult(false);

    String realrepName = environmentSubstitute(repositoryname);
    String realoutfilename = environmentSubstitute(targetfilename);
    String realusername = environmentSubstitute(username);
    String realpassword = environmentSubstitute(password);
    String realfoldername = environmentSubstitute(directoryPath);

    NrErrors = 0;
    successConditionBroken = false;
    limitErr = Const.toInt(environmentSubstitute(getNrLimit()), 10);

    try {
        file = KettleVFS.getFileObject(realoutfilename);
        if (file.exists()) {
            if (export_type.equals(Export_All) || export_type.equals(Export_Jobs)
                    || export_type.equals(Export_Trans) || export_type.equals(Export_One_Folder)) {
                if (iffileexists.equals(If_FileExists_Fail)) {
                    log.logError(toString(),
                            Messages.getString("JobExportRepository.Log.Failing", realoutfilename));
                    return result;
                } else if (iffileexists.equals(If_FileExists_Skip)) {
                    if (log.isDetailed())
                        log.logDetailed(toString(),
                                Messages.getString("JobExportRepository.Log.Exit", realoutfilename));
                    result.setResult(true);
                    result.setNrErrors(0);
                    return result;
                } else if (iffileexists.equals(If_FileExists_Uniquename)) {
                    String parentFolder = KettleVFS.getFilename(file.getParent());
                    String shortFilename = file.getName().getBaseName();
                    shortFilename = buildUniqueFilename(shortFilename);
                    file = KettleVFS.getFileObject(parentFolder + Const.FILE_SEPARATOR + shortFilename);
                    if (log.isDetailed())
                        log.logDetailed(toString(),
                                Messages.getString("JobExportRepository.Log.NewFilename", file.toString()));
                }
            } else if (export_type.equals(Export_By_Folder)) {
                if (file.getType() != FileType.FOLDER) {
                    log.logError(toString(),
                            Messages.getString("JobExportRepository.Log.NotFolder", "" + file.getName()));
                    return result;
                }
            }
        } else {
            if (export_type.equals(Export_By_Folder)) {
                // create folder?
                if (log.isDetailed())
                    log.logDetailed(toString(),
                            Messages.getString("JobExportRepository.Log.FolderNotExists", "" + file.getName()));
                if (!createfolder) {
                    return result;
                }
                file.createFolder();
                if (log.isDetailed())
                    log.logDetailed(toString(),
                            Messages.getString("JobExportRepository.Log.FolderCreated", file.toString()));
            } else if (export_type.equals(Export_All) || export_type.equals(Export_Jobs)
                    || export_type.equals(Export_Trans) || export_type.equals(Export_One_Folder)) {
                // create parent folder?
                if (!file.getParent().exists()) {
                    if (log.isDetailed())
                        log.logDetailed(toString(), Messages.getString(
                                "JobExportRepository.Log.FolderNotExists", "" + file.getParent().toString()));
                    if (createfolder) {
                        file.getParent().createFolder();
                        if (log.isDetailed())
                            log.logDetailed(toString(), Messages.getString(
                                    "JobExportRepository.Log.FolderCreated", file.getParent().toString()));
                    } else {
                        return result;
                    }
                }
            }
        }

        realoutfilename = KettleVFS.getFilename(this.file);

        // connect to repository
        connectRep(log, realrepName, realusername, realpassword);

        if (export_type.equals(Export_All)) {
            if (log.isDetailed())
                log.logDetailed(toString(),
                        Messages.getString("JobExportRepository.Log.StartingExportAllRep", realoutfilename));
            this.repo.exportAllObjects(null, realoutfilename, null, "all");
            if (log.isDetailed())
                log.logDetailed(toString(),
                        Messages.getString("JobExportRepository.Log.EndExportAllRep", realoutfilename));

            if (add_result_filesname)
                addFileToResultFilenames(realoutfilename, log, result, parentJob);
        } else if (export_type.equals(Export_Jobs)) {
            if (log.isDetailed())
                log.logDetailed(toString(),
                        Messages.getString("JobExportRepository.Log.StartingExportJobsRep", realoutfilename));
            this.repo.exportAllObjects(null, realoutfilename, null, "jobs");
            if (log.isDetailed())
                log.logDetailed(toString(),
                        Messages.getString("JobExportRepository.Log.EndExportJobsRep", realoutfilename));

            if (add_result_filesname)
                addFileToResultFilenames(realoutfilename, log, result, parentJob);
        } else if (export_type.equals(Export_Trans)) {
            if (log.isDetailed())
                log.logDetailed(toString(),
                        Messages.getString("JobExportRepository.Log.StartingExportTransRep", realoutfilename));
            this.repo.exportAllObjects(null, realoutfilename, null, "trans");
            if (log.isDetailed())
                log.logDetailed(toString(),
                        Messages.getString("JobExportRepository.Log.EndExportTransRep", realoutfilename));

            if (add_result_filesname)
                addFileToResultFilenames(realoutfilename, log, result, parentJob);
        } else if (export_type.equals(Export_One_Folder)) {
            RepositoryDirectory directory = new RepositoryDirectory();
            directory = repo.getDirectoryTree().findDirectory(realfoldername);
            if (directory != null) {
                if (log.isDetailed())
                    log.logDetailed(toString(), Messages.getString("JobExportRepository.Log.ExpAllFolderRep",
                            directoryPath, realoutfilename));
                this.repo.exportAllObjects(null, realoutfilename, directory, "all");
                if (log.isDetailed())
                    log.logDetailed(toString(), Messages.getString("JobExportRepository.Log.EndExpAllFolderRep",
                            directoryPath, realoutfilename));

                if (add_result_filesname)
                    addFileToResultFilenames(realoutfilename, log, result, parentJob);
            } else {
                log.logError(toString(), Messages.getString("JobExportRepository.Error.CanNotFindFolderInRep",
                        realfoldername, realrepName));
                return result;
            }
        } else if (export_type.equals(Export_By_Folder)) {
            // User must give a destination folder..

            RepositoryDirectory directory = new RepositoryDirectory();
            directory = this.repo.getDirectoryTree().findRoot();
            // Loop over all the directory id's
            long dirids[] = directory.getDirectoryIDs();
            if (log.isDetailed())
                log.logDetailed(toString(),
                        Messages.getString("JobExportRepository.Log.TotalFolders", "" + dirids.length));
            for (int d = 0; d < dirids.length && !parentJob.isStopped(); d++) {
                // Success condition broken?
                if (successConditionBroken) {
                    log.logError(toString(), Messages
                            .getString("JobExportRepository.Error.SuccessConditionbroken", "" + NrErrors));
                    throw new Exception(Messages.getString("JobExportRepository.Error.SuccessConditionbroken",
                            "" + NrErrors));
                }

                RepositoryDirectory repdir = directory.findDirectory(dirids[d]);
                if (!processOneFolder(parentJob, result, log, repdir, realoutfilename, d, dirids.length)) {
                    // updateErrors
                    updateErrors();
                }
            } // end for
        }

    } catch (Exception e) {
        updateErrors();
        log.logError(toString(), Messages.getString("JobExportRepository.UnExpectedError", e.toString()));
        log.logError(toString(), "Stack trace: " + Const.CR + Const.getStackTracker(e));
    } finally {
        if (this.repo != null)
            this.repo.disconnect();
        if (this.repinfo != null)
            this.repinfo = null;
        if (this.userinfo != null)
            this.userinfo = null;
        if (this.repsinfo != null) {
            this.repsinfo.clear();
            this.repsinfo = null;
        }
        if (this.file != null)
            try {
                this.file.close();
            } catch (Exception e) {
            }
    }

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

    return result;
}

From source file:brainflow.app.toplevel.BrainFlow.java

public void mountDirectory(FileObject dir) {
    try {//from w ww.  j  a v  a2  s  . c o  m
        if (dir.getType() != FileType.FOLDER) {
            throw new IllegalArgumentException("argument " + dir + " is not a directory");
        }
        DirectoryManager.getInstance().mountFileSystem(dir);

    } catch (FileSystemException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.panet.imeta.job.entries.unzip.JobEntryUnZip.java

private boolean processOneFile(LogWriter log, Result result, Job parentJob, FileObject fileObject,
        String realTargetdirectory, String realWildcard, String realWildcardExclude, FileObject movetodir,
        String realMovetodirectory, String realWildcardSource) {
    boolean retval = false;

    try {/*from  ww w.j  av a2s.c om*/
        if (fileObject.getType().equals(FileType.FILE)) {
            // We have to unzip one zip file
            if (!unzipFile(log, fileObject, realTargetdirectory, realWildcard, realWildcardExclude, result,
                    parentJob, fileObject, movetodir, realMovetodirectory))
                updateErrors();
            else
                updateSuccess();
        } else {
            // Folder..let's see wildcard
            FileObject[] children = fileObject.getChildren();

            for (int i = 0; i < children.length && !parentJob.isStopped(); i++) {
                if (successConditionBroken) {
                    if (!successConditionBrokenExit) {
                        log.logError(toString(),
                                Messages.getString("JobUnZip.Error.SuccessConditionbroken", "" + NrErrors));
                        successConditionBrokenExit = true;
                    }
                    return false;
                }
                // Get only file!
                if (!children[i].getType().equals(FileType.FOLDER)) {
                    boolean unzip = true;

                    String filename = children[i].getName().getPath();

                    Pattern patternSource = null;

                    if (!Const.isEmpty(realWildcardSource))
                        patternSource = Pattern.compile(realWildcardSource);

                    // First see if the file matches the regular expression!
                    if (patternSource != null) {
                        Matcher matcher = patternSource.matcher(filename);
                        unzip = matcher.matches();
                    }
                    if (unzip) {
                        if (!unzipFile(log, children[i], realTargetdirectory, realWildcard, realWildcardExclude,
                                result, parentJob, fileObject, movetodir, realMovetodirectory))
                            updateErrors();
                        else
                            updateSuccess();
                    }
                }
            }
        }
    } catch (Exception e) {
        updateErrors();
        log.logError(toString(), Messages.getString("JobUnZip.Error.Label", e.getMessage()));
    } finally {
        if (fileObject != null) {
            try {
                fileObject.close();
            } catch (IOException ex) {
            }
            ;
        }
    }
    return retval;
}

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 {/*  w  ww.jav  a2s .c  o m*/

        // 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;
}