Example usage for org.apache.commons.vfs FileSelectInfo getBaseFolder

List of usage examples for org.apache.commons.vfs FileSelectInfo getBaseFolder

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileSelectInfo getBaseFolder.

Prototype

FileObject getBaseFolder();

Source Link

Document

Returns the base folder of the traversal.

Usage

From source file:org.pentaho.di.job.entries.hadooptransjobexecutor.DistributedCacheUtil.java

/**
 * Attempts to find a plugin's installation folder on disk within all known plugin folder locations
 *
 * @param pluginFolderName Name of plugin folder
 * @return Location of the first plugin folder found as a direct descendant of one of the known plugin folder locations
 * @throws KettleFileException Error getting plugin folders
 *///  w w w. j a  v a 2s  . c  o m
public FileObject findPluginFolder(final String pluginFolderName) throws KettleFileException {
    List<PluginFolderInterface> pluginFolders = PluginFolder.populateFolders(null);
    if (pluginFolders != null) {
        for (PluginFolderInterface pluginFolder : pluginFolders) {
            FileObject folder = KettleVFS.getFileObject(pluginFolder.getFolder());

            try {
                if (folder.exists()) {
                    FileObject[] files = folder.findFiles(new FileSelector() {
                        @Override
                        public boolean includeFile(FileSelectInfo fileSelectInfo) throws Exception {
                            if (fileSelectInfo.getFile().equals(fileSelectInfo.getBaseFolder())) {
                                // Do not consider the base folders
                                return false;
                            }
                            // Determine relative name to compare
                            int baseNameLength = fileSelectInfo.getBaseFolder().getName().getPath().length()
                                    + 1;
                            String relativeName = fileSelectInfo.getFile().getName().getPath()
                                    .substring(baseNameLength);
                            // Compare plugin folder name with the relative name
                            return pluginFolderName.equals(relativeName);
                        }

                        @Override
                        public boolean traverseDescendents(FileSelectInfo fileSelectInfo) throws Exception {
                            return true;
                        }
                    });
                    if (files != null && files.length > 0) {
                        return files[0]; // Return the first match
                    }
                }
            } catch (FileSystemException ex) {
                throw new KettleFileException("Error searching for folder '" + pluginFolderName + "'", ex);
            }
        }
    }
    return null;
}