Example usage for org.apache.commons.vfs2 FileObject getChildren

List of usage examples for org.apache.commons.vfs2 FileObject getChildren

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileObject getChildren.

Prototype

FileObject[] getChildren() throws FileSystemException;

Source Link

Document

Lists the children of this file.

Usage

From source file:com.flicklib.folderscanner.AdvancedFolderScanner.java

/**
 * add the compressed files to the file group, which are in the specified directory.
 * @param sm/*from ww  w.  j a va 2s . c o  m*/
 * @param fg
 * @param fileList
 * @param folderName
 * @throws FileSystemException 
 */
private void addCompressedFiles(FileGroup fg, FileObject[] fileList, String folderName)
        throws FileSystemException {
    for (FileObject f : fileList) {
        if (f.getType().hasChildren() && folderName.equals(f.getName().getBaseName().toLowerCase())) {
            addCompressedFiles(fg, f.getChildren());
        }
    }
}

From source file:net.sf.jabb.web.action.VfsTreeAction.java

protected List<JsTreeNodeData> getChildNodes(FileObject rootFile, String parentPath)
        throws FileSystemException {
    List<JsTreeNodeData> childNodes = new LinkedList<JsTreeNodeData>();
    FileObject parent = null;
    if (requestData.isRoot()) {
        parent = rootFile;// w  ww . jav  a  2s  . c  o m
    } else {
        parent = rootFile.resolveFile(parentPath, NameScope.DESCENDENT_OR_SELF);
    }
    for (FileObject child : parent.getChildren()) {
        JsTreeNodeData childNode = populateTreeNodeData(rootFile, child);
        childNodes.add(childNode);
    }
    if (sortByName || folderFirst) {
        Collections.sort(childNodes, new Comparator<JsTreeNodeData>() {

            @Override
            public int compare(JsTreeNodeData o1, JsTreeNodeData o2) {
                int result = 0;
                if (folderFirst) {
                    String t1 = o1.getAttr().get("rel").toString();
                    String t2 = o2.getAttr().get("rel").toString();
                    if (t1.equalsIgnoreCase(t2)) {
                        result = 0;
                    } else if ("file".equalsIgnoreCase(t2)) {
                        result = -1;
                    } else if ("file".equalsIgnoreCase(t1)) {
                        result = 1;
                    } else {
                        result = t1.compareToIgnoreCase(t2);
                    }
                }
                if (result == 0 && sortByName) {
                    String n1 = o1.getData().toString();
                    String n2 = o2.getData().toString();
                    result = n1.compareToIgnoreCase(n2);
                }
                return result;
            }

        });
    }
    return childNodes;
}

From source file:com.streamsets.pipeline.stage.origin.remote.RemoteDownloadSource.java

private void queueFiles(FileObject remoteDir) throws FileSystemException {
    remoteDir.refresh();//from w w w . j ava 2  s . c o  m
    for (FileObject remoteFile : remoteDir.getChildren()) {
        if (remoteFile.getType().toString().equals("folder")) {
            queueFiles(remoteFile);
            continue;
        }
        long lastModified = remoteFile.getContent().getLastModifiedTime();
        RemoteFile tempFile = new RemoteFile(remoteFile.getName().getBaseName(), lastModified, remoteFile);

        if (shouldQueue(tempFile)) {
            // If we are done with all files, the files with the final mtime might get re-ingested over and over.
            // So if it is the one of those, don't pull it in.
            fileQueue.add(tempFile);
        }
    }
}

From source file:de.innovationgate.wgpublisher.design.fs.FileSystemDesignProvider.java

private static void determineChangedResources(int resourceType, FileObject categoryFolder, FileObject source,
        FileObject target, FileObject baseFolder, String sourceEncoding, String targetEncoding,
        OverlayStatus status, Logger log, DesignFileValidator validator)
        throws WGDesignSyncException, NoSuchAlgorithmException, IOException {

    for (FileObject sourceFile : source.getChildren()) {
        if (!isValidDesignFile(sourceFile, validator)) {
            continue;
        }//  w  w  w  .j  a  v a  2  s. co  m

        FileObject targetFile = target.resolveFile(sourceFile.getName().getBaseName());
        String resourcePath = baseFolder.getName().getRelativeName(targetFile.getName());

        if (sourceFile.getType().equals(FileType.FOLDER)) {
            if (!targetFile.exists()) {
                status.getNewFolders().add(targetFile.getName().getPath());
            } else if (targetFile.getType().equals(FileType.FILE)) {
                throw new WGDesignSyncException("Unable to apply overlay. Folder '"
                        + baseFolder.getName().getRelativeName(targetFile.getName())
                        + " already exists as file. Delete it to enable overlay management again");
            }
            determineChangedResources(resourceType, categoryFolder, sourceFile, targetFile, baseFolder,
                    sourceEncoding, targetEncoding, status, log, validator);
        } else if (sourceFile.getType().equals(FileType.FILE)) {

            // File does not exist.
            if (!targetFile.exists()) {

                // Was it once deployed?
                ResourceData originalHash = status.getOverlayData().getOverlayResources().get(resourcePath);
                if (originalHash == null) { // No, so it must be new in base
                    status.addChangedResource(resourceType, sourceFile, targetFile, categoryFolder,
                            OverlayStatus.ChangeType.NEW, resourcePath, null);
                } else { // Yes, Check if the base file changed since deployment
                    String newHash = MD5HashingInputStream
                            .getStreamHash(sourceFile.getContent().getInputStream());
                    if (newHash.equals(originalHash.getMd5Hash())) { // Nope. So this is no change. The overlay just chose not to use the file
                        continue;
                    } else { // Yes, so it is indeed a conflict
                        status.addChangedResource(resourceType, sourceFile, targetFile, categoryFolder,
                                OverlayStatus.ChangeType.CONFLICT, resourcePath, null);
                    }
                }

            }

            // File does exist: Determine if is updated in base since the overlay file was deployed
            else {

                ResourceData originalHash = status.getOverlayData().getOverlayResources().get(resourcePath);
                if (originalHash == null) {
                    if (!status.isUpdatedBaseDesign()) {
                        log.info("There is no information about the original deployment state of  resource "
                                + resourcePath
                                + ". Setting original deployment state now to the current base version.");
                        OverlayData.ResourceData resource = new OverlayData.ResourceData();
                        resource.setMd5Hash(
                                MD5HashingInputStream.getStreamHash(sourceFile.getContent().getInputStream()));
                        status.getOverlayData().setOverlayResource(resourcePath, resource);
                    } else {
                        log.info("Cannot update overlay resource " + resourcePath
                                + " as there is no information of its original deployment state.");
                    }
                    continue;
                }

                // First determine if the resource really changed from what was distributed
                String newHash = MD5HashingInputStream.getStreamHash(sourceFile.getContent().getInputStream());
                if (newHash.equals(originalHash.getMd5Hash())) {
                    continue;
                }

                // Determine if the target file is the same as was distributed. If not then it was user modified, so it is a conflict
                String currentHash = MD5HashingInputStream
                        .getStreamHash(targetFile.getContent().getInputStream());
                if (!currentHash.equals(originalHash.getMd5Hash())) {
                    status.addChangedResource(resourceType, sourceFile, targetFile, categoryFolder,
                            OverlayStatus.ChangeType.CONFLICT, resourcePath, null);
                }

                // It is a normal change
                else {
                    status.addChangedResource(resourceType, sourceFile, targetFile, categoryFolder,
                            OverlayStatus.ChangeType.CHANGED, resourcePath, null);
                }
            }
        }
    }

}

From source file:de.innovationgate.wgpublisher.design.fs.FileSystemDesignProvider.java

public static OverlayStatus determineOverlayStatus(FileSystemDesignProvider sourceDesignProvider,
        PluginID baseId, FileObject targetDirectory, String targetEncoding, Logger log,
        DesignFileValidator validator) throws Exception {

    OverlayStatus status = new OverlayStatus();

    // Copy an overlay flag file to the system file container
    FileObject targetFCFolder = targetDirectory.resolveFile(DesignDirectory.FOLDERNAME_FILES);
    FileObject systemFC = targetFCFolder.resolveFile("system");
    if (!systemFC.exists()) {
        systemFC.createFolder();//from   w ww .  ja v a 2 s.c o m
    }

    // Import overlay data, if available
    FileObject overlayDataFile = systemFC.resolveFile(OverlayDesignProvider.OVERLAY_DATA_FILE);
    if (overlayDataFile.exists()) {
        try {
            InputStream in = new BufferedInputStream(overlayDataFile.getContent().getInputStream());
            status.setOverlayData(OverlayData.read(in));
            in.close();
        } catch (Exception e) {
            log.error("Exception reading overlay status. Creating new status file", e);
        }

        if (status.getOverlayData() != null
                && !status.getOverlayData().getBasepluginName().equals(baseId.getUniqueName())) {
            throw new WGDesignSyncException("The overlay folder '" + targetDirectory.getName().getPath()
                    + "' is used with plugin '" + status.getOverlayData().getBasepluginName() + "' not '"
                    + baseId.getUniqueName() + "'. Overlay status determination was canceled.");
        }
    }

    Version providerVersion = baseId.getVersion();
    if (status.getOverlayData() == null) {
        OverlayData overlayData = new OverlayData();
        overlayData.setBasepluginName(baseId.getUniqueName());
        status.setOverlayData(overlayData);
        status.setNewOverlay(true);
        overlayData.setInitialBasepluginVersion(providerVersion.toString());
    }

    // Test for version compatibility between base design and overlay

    status.setCurrentBaseVersion(providerVersion);
    if (status.getOverlayData().getBasepluginVersion() != null) {
        Version baseVersion = new Version(status.getOverlayData().getBasepluginVersion());

        // Base design version is different than the compliance version of the overlay. Look if it higher (=upgrade) or lower (=error)
        if (!providerVersion.equals(baseVersion)
                || providerVersion.getBuildVersion() != baseVersion.getBuildVersion()) {
            if (providerVersion.compareTo(baseVersion) >= 0 || (providerVersion.equals(baseVersion)
                    && providerVersion.getBuildVersion() > baseVersion.getBuildVersion())) {
                status.setUpdatedBaseDesign(true);
            } else if (providerVersion.compareTo(baseVersion) < 0) {
                throw new WGDesignSyncException("The used base design version (" + providerVersion.toString()
                        + ") is lower than the compliant version for the overlay ("
                        + status.getOverlayData().getBasepluginVersion() + ").");
            }
        }

    }

    if (status.isUpdatedBaseDesign()) {
        log.info("Used version of base design is " + providerVersion.toString()
                + ". Overlay currently complies with base design version "
                + status.getOverlayData().getBasepluginVersion() + ". The overlay can be upgraded.");
    }

    // Gather changed resources in base design, so we can priorize them against the overlay resources
    FileObject sourceTmlFolder = sourceDesignProvider.getTmlFolder();
    FileObject targetTmlFolder = targetDirectory.resolveFile(DesignDirectory.FOLDERNAME_TML);
    if (sourceTmlFolder.exists() && sourceTmlFolder.getType().equals(FileType.FOLDER)) {
        for (FileObject mediaKeyFolder : sourceTmlFolder.getChildren()) {
            FileObject overlayFolder = mediaKeyFolder.resolveFile(OverlayDesignProvider.OVERLAY_FOLDER);
            if (overlayFolder.exists()) {
                FileObject targetMediaKeyFolder = targetTmlFolder
                        .resolveFile(mediaKeyFolder.getName().getBaseName());
                determineChangedResources(WGDocument.TYPE_TML, targetMediaKeyFolder, overlayFolder,
                        targetMediaKeyFolder, targetDirectory, sourceDesignProvider.getFileEncoding(),
                        targetEncoding, status, log, validator);
            }
        }
    }

    FileObject targetScriptFolder = targetDirectory.resolveFile(DesignDirectory.FOLDERNAME_SCRIPT);
    FileObject sourceScriptFolder = sourceDesignProvider.getScriptFolder();
    if (sourceScriptFolder.exists() && sourceScriptFolder.getType().equals(FileType.FOLDER)) {
        for (FileObject scriptTypeFolder : sourceScriptFolder.getChildren()) {
            FileObject overlayFolder = scriptTypeFolder.resolveFile(OverlayDesignProvider.OVERLAY_FOLDER);
            if (overlayFolder.exists()) {
                FileObject targetScriptTypeFolder = targetScriptFolder
                        .resolveFile(scriptTypeFolder.getName().getBaseName());
                determineChangedResources(WGDocument.TYPE_CSSJS, targetScriptTypeFolder, overlayFolder,
                        targetScriptTypeFolder, targetDirectory, sourceDesignProvider.getFileEncoding(),
                        targetEncoding, status, log, validator);
            }
        }
    }

    FileObject overlayFolder = sourceDesignProvider.getFilesFolder()
            .resolveFile(OverlayDesignProvider.OVERLAY_FOLDER);
    if (overlayFolder.exists()) {
        determineChangedFileContainerResources(targetFCFolder, overlayFolder, targetFCFolder, targetDirectory,
                null, null, status, log, validator);
    }

    return status;

}

From source file:de.unioninvestment.portal.explorer.view.vfs.TableView.java

private void fillTableData(String selectedDir, Table table, FileSystemManager fsManager, FileSystemOptions opts,
        String filterVal) throws IOException {
    table.removeAllItems();//from ww  w  .  j a  va2  s . c  o m

    FileObject fileObject = fsManager.resolveFile(selectedDir, opts);
    FileObject[] files = fileObject.getChildren();
    for (FileObject file : files) {

        if (filterVal == null) {
            addTableItem(table, file);
        } else {
            String regex = filterVal.replace("?", ".?").replace("*", ".*?");
            String name = getDisplayPath(file.getName().toString());
            if (name.matches(regex))
                addTableItem(table, file);
        }

    }
}

From source file:com.app.server.JarDeployer.java

/**
 * This method obtains the class name for each of the executor services
 * @param jarFile//from   w  w w  .j a  v  a2 s.  c o m
 * @param classList
 * @throws FileSystemException
 */
public void getChildren(FileObject jarFile, CopyOnWriteArrayList classList) throws FileSystemException {
    if (jarFile.getType() == FileType.FILE)
        return;
    FileObject[] children = jarFile.getChildren();
    //log.info( "Children of " + jarFile.getName().getURI() );
    if (children == null)
        return;
    for (int i = 0; i < children.length; i++) {
        //log.info(children[i].+" "+ children[i].getName().getBaseName() );
        if (children[i].getType() == FileType.FILE && children[i].getName().getBaseName().endsWith(".class"))
            classList.add(children[i].toString().substring(children[i].toString().indexOf('!') + 2));
        getChildren(children[i], classList);
        children[i].close();
        //fsManager.closeFileSystem(children[i].getFileSystem());
    }
}

From source file:com.web.server.JarDeployer.java

/**
 * This method obtains the class name for each of the executor services
 * @param jarFile/*from ww w  .ja v a  2  s  . c o  m*/
 * @param classList
 * @throws FileSystemException
 */
public void getChildren(FileObject jarFile, CopyOnWriteArrayList classList) throws FileSystemException {
    if (jarFile.getType() == FileType.FILE)
        return;
    FileObject[] children = jarFile.getChildren();
    //System.out.println( "Children of " + jarFile.getName().getURI() );
    if (children == null)
        return;
    for (int i = 0; i < children.length; i++) {
        //System.out.println(children[i].+" "+ children[i].getName().getBaseName() );
        if (children[i].getType() == FileType.FILE && children[i].getName().getBaseName().endsWith(".class"))
            classList.add(children[i].toString().substring(children[i].toString().indexOf('!') + 2));
        getChildren(children[i], classList);
        children[i].close();
        //fsManager.closeFileSystem(children[i].getFileSystem());
    }
}

From source file:com.flicklib.folderscanner.AdvancedFolderScanner.java

/**
 * /*from w  w  w . ja  v a2  s . c om*/
 * @param folder
 * @param monitor 
 * @return true, if it contained movie file
 * @throws InterruptedException 
 * @throws FileSystemException 
 */
private boolean browse(FileObject folder, AsyncMonitor monitor)
        throws InterruptedException, FileSystemException {
    URL url = folder.getURL();
    LOGGER.trace("entering " + url);
    FileObject[] files = folder.getChildren();
    if (monitor != null) {
        if (monitor.isCanceled()) {
            throw new InterruptedException("at " + url);
        }
        monitor.step("scanning " + url);
    }

    Set<String> plainFileNames = new HashSet<String>();
    int subDirectories = 0;
    int compressedFiles = 0;
    Set<String> directoryNames = new HashSet<String>();
    for (FileObject f : files) {
        if (isDirectory(f)) {
            subDirectories++;
            directoryNames.add(f.getName().getBaseName().toLowerCase());
        } else {
            String ext = getExtension(f);
            if (ext == null) {
                LOGGER.trace("Ignoring file without extension: " + f.getURL());
            } else {
                if (MovieFileType.getTypeByExtension(ext) == MovieFileType.COMPRESSED) {
                    compressedFiles++;
                }
                if (ext != null && MovieFileFilter.VIDEO_EXTENSIONS.contains(ext)) {
                    plainFileNames.add(getNameWithoutExt(f));
                }
            }
        }
    }
    // check for multiple compressed files, the case of:
    // Title_of_the_film/abc.rar
    // Title_of_the_film/abc.r01
    // Title_of_the_film/abc.r02
    if (compressedFiles > 0) {
        FileGroup fg = initStorableMovie(folder);
        fg.getLocations().add(new FileLocation(currentLabel, folder.getURL()));
        addCompressedFiles(fg, files);
        add(fg);
        return true;
    }
    if (subDirectories >= 2 && subDirectories <= 5) {
        // the case of :
        // Title_of_the_film/cd1/...
        // Title_of_the_film/cd2/...
        // with an optional sample/subs directory
        // Title_of_the_film/sample/
        // Title_of_the_film/subs/
        // Title_of_the_film/subtitles/
        // or
        // Title_of_the_film/bla1.avi
        // Title_of_the_film/bla2.avi
        // Title_of_the_film/sample/
        // Title_of_the_film/subs/
        if (isMovieFolder(directoryNames)) {
            FileGroup fg = initStorableMovie(folder);
            fg.getLocations().add(new FileLocation(currentLabel, folder.getURL()));
            for (String cdFolder : getCdFolders(directoryNames)) {
                addCompressedFiles(fg, files, cdFolder);
            }
            for (FileObject file : folder.getChildren()) {
                if (!isDirectory(file)) {
                    String ext = getExtension(file);
                    if (MovieFileFilter.VIDEO_EXT_EXTENSIONS.contains(ext)) {
                        fg.getFiles().add(createFileMeta(file, MovieFileType.getTypeByExtension(ext)));
                    }
                }
            }
            add(fg);
            return true;
        }
    }
    boolean subFolderContainMovie = false;
    for (FileObject f : files) {
        final String baseName = f.getName().getBaseName();
        if (isDirectory(f) && !baseName.equalsIgnoreCase("sample") && !baseName.startsWith(".")) {
            subFolderContainMovie |= browse(f, monitor);
        }
    }

    // We want to handle the following cases:
    // 1,
    // Title_of_the_film/abc.avi
    // Title_of_the_film/abc.srt
    // --> no subdirectory, one film -> the title should be name of the
    // directory
    //  
    // 2,
    // Title_of_the_film/abc-cd1.avi
    // Title_of_the_film/abc-cd1.srt
    // Title_of_the_film/abc-cd2.srt
    // Title_of_the_film/abc-cd2.srt
    //

    if (subDirectories > 0 && subFolderContainMovie) {
        return genericMovieFindProcess(files) || subFolderContainMovie;
    } else {

        int foundFiles = plainFileNames.size();
        switch (foundFiles) {
        case 0:
            return subFolderContainMovie;
        case 1: {
            FileGroup fg = initStorableMovie(folder);
            fg.getLocations().add(new FileLocation(currentLabel, folder.getURL()));
            addFiles(fg, files, plainFileNames.iterator().next());
            add(fg);
            return true;
        }
        case 2: {
            Iterator<String> it = plainFileNames.iterator();
            String name1 = it.next();
            String name2 = it.next();
            if (LevenshteinDistance.distance(name1, name2) < 3) {
                // the difference is -cd1 / -cd2
                FileGroup fg = initStorableMovie(folder);

                fg.getLocations().add(new FileLocation(currentLabel, folder.getURL()));
                addFiles(fg, files, name1);
                add(fg);
                return true;
            }
            // the difference is significant, we use the generic
            // solution
        }
        default: {
            return genericMovieFindProcess(files);
        }
        }
    }
}

From source file:de.innovationgate.wgpublisher.design.fs.FileSystemDesignSource.java

public List<String> getDesignNames() throws WGADesignRetrievalException {

    try {/*w w w.  j a v a2  s.c o  m*/
        List<String> designs = new ArrayList<String>();

        // Add child design directories - All with a syncinfo/design.xml or those that are completely empty and can be initialized
        _dir.refresh();
        FileObject[] children = _dir.getChildren();
        for (int i = 0; i < children.length; i++) {
            FileObject child = children[i];
            if (child.getType().equals(FileType.FOLDER)) {
                FileObject resolvedChild = WGUtils.resolveDirLink(child);
                if (resolvedChild.getType().equals(FileType.FOLDER)) {
                    FileObject syncInfo = DesignDirectory.getDesignDefinitionFile(resolvedChild);
                    if (syncInfo != null || child.getChildren().length == 0) {
                        designs.add(child.getName().getBaseName());
                    }
                }
            } else if (child.getType().equals(FileType.FILE)
                    && child.getName().getExtension().equalsIgnoreCase(ARCHIVED_DESIGN_EXTENSION)) {
                designs.add(DESIGNNAMEPREFIX_ARCHIVE + child.getName().getBaseName().substring(0,
                        child.getName().getBaseName().lastIndexOf(".")));
            }
        }

        // Add additional directories
        Iterator<Map.Entry<String, String>> dirs = _additionalDirs.entrySet().iterator();
        while (dirs.hasNext()) {
            Map.Entry<String, String> entry = dirs.next();
            FileObject dir = VFS.getManager().resolveFile((String) entry.getValue());
            if (dir.exists() && dir.getType().equals(FileType.FOLDER)) {
                FileObject syncInfo = DesignDirectory.getDesignDefinitionFile(dir);
                if (syncInfo != null || dir.getChildren().length == 0) {
                    designs.add(DESIGNNAMEPREFIX_ADDITIONALDIR + entry.getKey());
                }
            }
        }

        return designs;
    } catch (FileSystemException e) {
        throw new WGADesignRetrievalException("Exception retrieving file system designs", e);
    }

}