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:org.wso2.carbon.connector.FileSearch.java

/**
 *
 * @param dir sub directory/*from  ww  w .j av  a  2  s .  com*/
 * @param fileList list of file inside directory
 * @param messageContext the message context that is generated for processing the file
 */
private void getAllFiles(FileObject dir, List<FileObject> fileList, MessageContext messageContext) {
    try {
        FileObject[] children = dir.getChildren();
        for (FileObject child : children) {
            fileList.add(child);
        }
    } catch (IOException e) {
        handleException("Unable to list all folders", e, messageContext);
    } finally {
        try {
            if (dir != null) {
                dir.close();
            }
        } catch (IOException e) {
            log.error("Error while closing Directory: " + e.getMessage(), e);
        }
    }
}

From source file:org.wso2.carbon.connector.FileSearchConnector.java

/**
 * List the all files of given pattern./*  ww w .  j  av  a  2s  .  c o m*/
 *
 * @param messageContext The message context that is generated for processing the file.
 */
private void searchFile(MessageContext messageContext) {
    String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.FILE_LOCATION);
    String filePattern = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.FILE_PATTERN);
    boolean enableRecursiveSearch = false;

    String recursiveSearchParameter = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.RECURSIVE_SEARCH);

    if (StringUtils.isNotEmpty(recursiveSearchParameter)) {
        enableRecursiveSearch = Boolean.parseBoolean(recursiveSearchParameter);
    }
    StandardFileSystemManager manager = FileConnectorUtils.getManager();
    if (StringUtils.isEmpty(filePattern)) {
        throw new SynapseException("FilePattern should not be null");
    }
    try {
        FileSystemOptions opt = FileConnectorUtils.init(messageContext);
        FileObject remoteFile = manager.resolveFile(source, opt);
        if (!remoteFile.exists()) {
            throw new SynapseException("File location does not exist");
        }
        FileObject[] children = remoteFile.getChildren();
        OMFactory factory = OMAbstractFactory.getOMFactory();
        OMNamespace ns = factory.createOMNamespace(FileConstants.FILECON, FileConstants.NAMESPACE);
        OMElement result = factory.createOMElement(FileConstants.RESULT, ns);
        ResultPayloadCreator.preparePayload(messageContext, result);
        FilePattenMatcher fpm = new FilePattenMatcher(filePattern);

        for (FileObject child : children) {
            try {
                if (FileType.FILE.equals(child.getType()) && fpm.validate(child.getName().getBaseName())) {
                    String outputResult = child.getName().getPath();
                    OMElement messageElement = factory.createOMElement(FileConstants.FILE, ns);
                    messageElement.setText(outputResult);
                    result.addChild(messageElement);
                } else if (FileType.FOLDER.equals(child.getType()) && enableRecursiveSearch) {
                    searchSubFolders(child, filePattern, factory, result, ns);
                }
            } catch (FileSystemException e) {
                throw new SynapseException("Unable to search a file.", e);
            } finally {
                try {
                    if (child != null) {
                        child.close();
                    }
                } catch (IOException e) {
                    log.error("Error while closing Directory: " + e.getMessage(), e);
                }
            }
        }
        messageContext.getEnvelope().getBody().addChild(result);
    } catch (FileSystemException e) {
        throw new SynapseException("Unable to search a file for a given pattern.", e);
    } finally {
        manager.close();
    }
}

From source file:org.wso2.carbon.connector.FileSearchConnector.java

/**
 * Search the files of given pattern inside the sub directory.
 *
 * @param child          Child folder./*from  w  w w  .  ja va 2s  . c o m*/
 * @param filePattern    Pattern of the file to be searched.
 * @param factory        OMFactory.
 * @param result         OMElement.
 * @param ns             OMNamespace.
 * @throws FileSystemException On error getting file type.
 */
private void searchSubFolders(FileObject child, String filePattern, OMFactory factory, OMElement result,
        OMNamespace ns) throws FileSystemException {
    List<FileObject> fileList = new ArrayList<>();
    Collections.addAll(fileList, child.getChildren());
    FilePattenMatcher fpm = new FilePattenMatcher(filePattern);
    String outputResult;
    try {
        for (FileObject file : fileList) {
            if (FileType.FILE.equals(file.getType())) {
                if (fpm.validate(file.getName().getBaseName().toLowerCase())) {
                    outputResult = file.getName().getPath();
                    OMElement messageElement = factory.createOMElement(FileConstants.FILE, ns);
                    messageElement.setText(outputResult);
                    result.addChild(messageElement);
                }
            } else if (FileType.FOLDER.equals(file.getType())) {
                searchSubFolders(file, filePattern, factory, result, ns);
            }
        }
    } catch (FileSystemException e) {
        throw new SynapseException("Unable to search files in sub folder", e);
    } finally {
        try {
            child.close();
        } catch (IOException e) {
            log.error("Error while closing Directory: " + e.getMessage(), e);
        }
    }
}

From source file:org.wso2.carbon.inbound.endpoint.protocol.file.FilePollingConsumer.java

/**
 * /*ww w. j ava2s.  co m*/
 * Do the file processing operation for the given set of properties. Do the
 * checks and pass the control to processFile method
 * 
 * */
public FileObject poll() {
    if (fileURI == null || fileURI.trim().equals("")) {
        log.error("Invalid file url. Check the inbound endpoint configuration. Endpoint Name : " + name
                + ", File URL : " + VFSUtils.maskURLPassword(fileURI));
        return null;
    }

    if (log.isDebugEnabled()) {
        log.debug("Start : Scanning directory or file : " + VFSUtils.maskURLPassword(fileURI));
    }

    if (!initFileCheck()) {
        // Unable to read from the source location provided.
        return null;
    }

    // If file/folder found proceed to the processing stage
    try {
        lastCycle = 0;
        if (fileObject.exists() && fileObject.isReadable()) {
            FileObject[] children = null;
            try {
                children = fileObject.getChildren();
            } catch (FileNotFolderException ignored) {
                if (log.isDebugEnabled()) {
                    log.debug("No Folder found. Only file found on : " + VFSUtils.maskURLPassword(fileURI));
                }
            } catch (FileSystemException ex) {
                log.error(ex.getMessage(), ex);
            }

            // if this is a file that would translate to a single message
            if (children == null || children.length == 0) {
                // Fail record is a one that is processed but was not moved
                // or deleted due to an error.
                boolean isFailedRecord = VFSUtils.isFailRecord(fsManager, fileObject);
                if (!isFailedRecord) {
                    fileHandler();
                    if (injectHandler == null) {
                        return fileObject;
                    }
                } else {
                    try {
                        lastCycle = 2;
                        moveOrDeleteAfterProcessing(fileObject);
                    } catch (SynapseException synapseException) {
                        log.error("File object '" + VFSUtils.maskURLPassword(fileObject.getURL().toString())
                                + "' " + "cloud not be moved after first attempt", synapseException);
                    }
                    if (fileLock) {
                        // TODO: passing null to avoid build break. Fix properly
                        VFSUtils.releaseLock(fsManager, fileObject, fso);
                    }
                    if (log.isDebugEnabled()) {
                        log.debug("File '" + VFSUtils.maskURLPassword(fileObject.getURL().toString())
                                + "' has been marked as a failed" + " record, it will not process");
                    }
                }
            } else {
                FileObject fileObject = directoryHandler(children);
                if (fileObject != null) {
                    return fileObject;
                }
            }
        } else {
            log.warn("Unable to access or read file or directory : " + VFSUtils.maskURLPassword(fileURI) + "."
                    + " Reason: "
                    + (fileObject.exists()
                            ? (fileObject.isReadable() ? "Unknown reason" : "The file can not be read!")
                            : "The file does not exists!"));
            return null;
        }
    } catch (FileSystemException e) {
        log.error("Error checking for existence and readability : " + VFSUtils.maskURLPassword(fileURI), e);
        return null;
    } catch (Exception e) {
        log.error("Error while processing the file/folder in URL : " + VFSUtils.maskURLPassword(fileURI), e);
        return null;
    } finally {
        try {
            if (fsManager != null) {
                fsManager.closeFileSystem(fileObject.getParent().getFileSystem());
            }
            fileObject.close();
        } catch (Exception e) {
            log.error("Unable to close the file system. " + e.getMessage());
            log.error(e);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("End : Scanning directory or file : " + VFSUtils.maskURLPassword(fileURI));
    }
    return null;
}

From source file:org.wso2.carbon.transport.filesystem.connector.server.FileSystemConsumer.java

/**
 * Handle directory with child elements.
 *
 * @param children The array containing child elements of a folder
 *///  w  w w  .  j a  va  2 s  .c o m
private void directoryHandler(FileObject[] children) throws FileSystemServerConnectorException {
    // Sort the files according to given properties
    String strSortParam = fileProperties.get(Constants.FILE_SORT_PARAM);

    // TODO: rethink the way the string constants are handled
    if (strSortParam != null && !"NONE".equals(strSortParam)) {
        if (log.isDebugEnabled()) {
            log.debug("Starting to sort the files in folder: "
                    + FileTransportUtils.maskURLPassword(listeningDirURI));
        }

        String strSortOrder = fileProperties.get(Constants.FILE_SORT_ORDER);
        boolean bSortOrderAscending = true;

        if (strSortOrder != null) {
            bSortOrderAscending = Boolean.parseBoolean(strSortOrder);
        }
        if (log.isDebugEnabled()) {
            log.debug("Sorting the files by : " + strSortOrder + ". (" + bSortOrderAscending + ")");
        }
        switch (strSortParam) {
        case Constants.FILE_SORT_VALUE_NAME:
            if (bSortOrderAscending) {
                Arrays.sort(children, new FileNameAscComparator());
            } else {
                Arrays.sort(children, new FileNameDesComparator());
            }
            break;
        case Constants.FILE_SORT_VALUE_SIZE:
            if (bSortOrderAscending) {
                Arrays.sort(children, new FileSizeAscComparator());
            } else {
                Arrays.sort(children, new FileSizeDesComparator());
            }
            break;
        case Constants.FILE_SORT_VALUE_LASTMODIFIEDTIMESTAMP:
            if (bSortOrderAscending) {
                Arrays.sort(children, new FileLastmodifiedtimestampAscComparator());
            } else {
                Arrays.sort(children, new FileLastmodifiedtimestampDesComparator());
            }
            break;
        default:
            log.warn("Invalid value given for " + Constants.FILE_SORT_PARAM + " parameter. "
                    + " Expected one of the values: " + Constants.FILE_SORT_VALUE_NAME + ", "
                    + Constants.FILE_SORT_VALUE_SIZE + " or " + Constants.FILE_SORT_VALUE_LASTMODIFIEDTIMESTAMP
                    + ". Found: " + strSortParam);
            break;
        }
        if (log.isDebugEnabled()) {
            log.debug("End sorting the files.");
        }
    }
    for (FileObject child : children) {
        if (fileProcessCount != 0 && processCount > fileProcessCount) {
            return;
        }
        if (child.getName().getBaseName().endsWith(".lock")
                || child.getName().getBaseName().endsWith(".fail")) {
            continue;
        }
        if (!(fileNamePattern == null || child.getName().getBaseName().matches(fileNamePattern))) {
            if (log.isDebugEnabled()) {
                log.debug("File " + FileTransportUtils.maskURLPassword(listeningDir.getName().getBaseName())
                        + " is not processed because it did not match the specified pattern.");
            }
        } else {
            FileType childType = getFileType(child);
            if (childType == FileType.FOLDER) {
                FileObject[] c = null;
                try {
                    c = child.getChildren();
                } catch (FileSystemException ignored) {
                    if (log.isDebugEnabled()) {
                        log.debug("The file does not exist, or is not a folder, or an error "
                                + "has occurred when trying to list the children. File URI : "
                                + FileTransportUtils.maskURLPassword(listeningDirURI), ignored);
                    }
                }

                // if this is a file that would translate to a single message
                if (c == null || c.length == 0) {
                    if (log.isDebugEnabled()) {
                        log.debug("Folder at " + FileTransportUtils.maskURLPassword(child.getName().getURI())
                                + " is empty.");
                    }
                } else {
                    directoryHandler(c);
                }
                postProcess(child, false);
            } else {
                fileHandler(child);
            }
        }
    }
}

From source file:org.wso2.carbon.transport.remotefilesystem.server.RemoteFileSystemConsumer.java

/**
 * Handle directory with child elements.
 *
 * @param children The array containing child elements of a folder
 *//*from  w  ww  .j  a v  a2  s .com*/
private void directoryHandler(FileObject[] children) throws RemoteFileSystemConnectorException {
    // Sort the files according to given properties
    String strSortParam = fileProperties.get(Constants.FILE_SORT_PARAM);

    // TODO: rethink the way the string constants are handled
    if (strSortParam != null && !Constants.ACTION_NONE.equals(strSortParam)) {
        if (log.isDebugEnabled()) {
            log.debug("Starting to sort the files in folder: "
                    + FileTransportUtils.maskURLPassword(listeningDirURI));
        }
        String strSortOrder = fileProperties.get(Constants.FILE_SORT_ORDER);
        boolean bSortOrderAscending = true;
        if (strSortOrder != null) {
            bSortOrderAscending = Boolean.parseBoolean(strSortOrder);
        }
        if (log.isDebugEnabled()) {
            log.debug("Sorting the files by : " + strSortOrder + ". (" + bSortOrderAscending + ")");
        }
        switch (strSortParam) {
        case Constants.FILE_SORT_VALUE_NAME:
            if (bSortOrderAscending) {
                Arrays.sort(children, new FileNameAscComparator());
            } else {
                Arrays.sort(children, new FileNameDesComparator());
            }
            break;
        case Constants.FILE_SORT_VALUE_SIZE:
            if (bSortOrderAscending) {
                Arrays.sort(children, new FileSizeAscComparator());
            } else {
                Arrays.sort(children, new FileSizeDesComparator());
            }
            break;
        case Constants.FILE_SORT_VALUE_LASTMODIFIEDTIMESTAMP:
            if (bSortOrderAscending) {
                Arrays.sort(children, new FileLastModifiedTimestampAscComparator());
            } else {
                Arrays.sort(children, new FileLastModifiedTimestampDesComparator());
            }
            break;
        default:
            log.warn("[" + serviceName + "] Invalid value given for " + Constants.FILE_SORT_PARAM
                    + " parameter. " + " Expected one of the values: " + Constants.FILE_SORT_VALUE_NAME + ", "
                    + Constants.FILE_SORT_VALUE_SIZE + " or " + Constants.FILE_SORT_VALUE_LASTMODIFIEDTIMESTAMP
                    + ". Found: " + strSortParam);
            break;
        }
        if (log.isDebugEnabled()) {
            log.debug("End sorting the files.");
        }
    }
    for (FileObject child : children) {
        if (fileProcessCount != 0 && processCount > fileProcessCount) {
            return;
        }
        if (child.getName().getBaseName().endsWith(".lock")
                || child.getName().getBaseName().endsWith(".fail")) {
            continue;
        }
        if (!(fileNamePattern == null || child.getName().getBaseName().matches(fileNamePattern))) {
            if (log.isDebugEnabled()) {
                log.debug("File " + FileTransportUtils.maskURLPassword(listeningDir.getName().getBaseName())
                        + " is not processed because it did not match the specified pattern.");
            }
        } else {
            FileType childType = getFileType(child);
            if (childType == FileType.FOLDER) {
                FileObject[] c = null;
                try {
                    c = child.getChildren();
                } catch (FileSystemException ignored) {
                    if (log.isDebugEnabled()) {
                        log.debug("The file does not exist, or is not a folder, or an error "
                                + "has occurred when trying to list the children. File URI : "
                                + FileTransportUtils.maskURLPassword(listeningDirURI), ignored);
                    }
                }

                // if this is a file that would translate to a single message
                if (c == null || c.length == 0) {
                    if (log.isDebugEnabled()) {
                        log.debug("Folder at " + FileTransportUtils.maskURLPassword(child.getName().getURI())
                                + " is empty.");
                    }
                } else {
                    directoryHandler(c);
                }
                postProcess(child, true);
            } else {
                fileHandler(child);
            }
        }
    }
}

From source file:pl.otros.vfs.browser.util.VFSUtils.java

public static FileObject[] getChildren(FileObject fileObject) throws FileSystemException {
    FileObject[] result;//from   w w w  .  j  a va2s . c  o  m
    if (isHttpProtocol(fileObject)) {
        result = extractHttpFileObjectChildren(fileObject);
    } else if (isLocalFileSystem(fileObject) && isArchive(fileObject)) {
        String extension = fileObject.getName().getExtension();
        result = VFSUtils.resolveFileObject(extension + ":" + fileObject.getURL().toString() + "!/")
                .getChildren();
    } else {
        result = fileObject.getChildren();
    }
    return result;
}

From source file:se.kth.hopsworks.zeppelin.notebook.repo.FSNotebookRepo.java

private LinkedList<FileObject> getNotes(FileObject root, FileObject proj) throws IOException {
    FileObject[] rootchildren = root.getChildren();
    FileObject[] projChildren;//from   w  w  w  .  j ava  2s. c  o m
    LinkedList<FileObject> children = new LinkedList<>();
    if (isDirectory(proj)) {
        projChildren = proj.getChildren();
        if (projChildren != null) {
            children = new LinkedList<>(Arrays.asList(projChildren));
        }
    }
    // add notes in the root dir that are not project specific ex. tutorials
    for (FileObject f : rootchildren) {
        if (isDirectory(f)) {
            FileObject noteJson = f.resolveFile("note.json", NameScope.CHILD);
            if (noteJson.exists() && !listContainsNote(children, f)) {
                children.add(f);
            }
        }
    }

    return children;
}

From source file:tain.kr.test.vfs.v01.Shell.java

/**
 * Lists the children of a folder./*from   w  w w. jav  a  2 s .c  om*/
 */
private void listChildren(final FileObject dir, final boolean recursive, final String prefix)
        throws FileSystemException {

    final FileObject[] children = dir.getChildren();
    for (final FileObject child : children) {
        System.out.print(prefix);
        System.out.print(child.getName().getBaseName());

        if (child.getType() == FileType.FOLDER) {

            System.out.println("/");
            if (recursive) {
                listChildren(child, recursive, prefix + "    ");
            }
        } else {
            System.out.println();
        }
    }
}

From source file:tain.kr.test.vfs.v01.ShowProperties.java

private static void test01(String[] args) throws Exception {

    if (flag)/*from  w  ww.j a va2s .  c  o  m*/
        new ShowProperties();

    if (flag) {

        if (args.length == 0) {
            System.err.println("Please pass the name of a file as parameter.");
            System.err.println("e.g. java org.apache.commons.vfs2.example.ShowProperties LICENSE.txt");
            return;
        }

        for (final String arg : args) {
            try {
                final FileSystemManager mgr = VFS.getManager();
                System.out.println();
                System.out.println("Parsing   : " + arg);
                final FileObject file = mgr.resolveFile(arg);
                System.out.println("URL       : " + file.getURL());
                System.out.println("getName() : " + file.getName());
                System.out.println("BaseName  : " + file.getName().getBaseName());
                System.out.println("Extension : " + file.getName().getExtension());
                System.out.println("Path      : " + file.getName().getPath());
                System.out.println("Scheme    : " + file.getName().getScheme());
                System.out.println("URI       : " + file.getName().getURI());
                System.out.println("Root URI  : " + file.getName().getRootURI());
                System.out.println("Parent    : " + file.getName().getParent());
                System.out.println("Type      : " + file.getType());
                System.out.println("Exists    : " + file.exists());
                System.out.println("Readable  : " + file.isReadable());
                System.out.println("Writeable : " + file.isWriteable());
                System.out.println("Root path : " + file.getFileSystem().getRoot().getName().getPath());

                if (file.exists()) {
                    if (file.getType().equals(FileType.FILE)) {
                        System.out.println("Size: " + file.getContent().getSize() + " bytes");

                    } else if (file.getType().equals(FileType.FOLDER) && file.isReadable()) {

                        final FileObject[] children = file.getChildren();
                        System.out.println("Directory with " + children.length + " files");

                        for (int iterChildren = 0; iterChildren < children.length; iterChildren++) {
                            System.out.println("#" + iterChildren + ": " + children[iterChildren].getName());
                            if (iterChildren > SHOW_MAX) {
                                break;
                            }
                        }
                    }

                    System.out.println("Last modified: " + DateFormat.getInstance()
                            .format(new Date(file.getContent().getLastModifiedTime())));
                } else {
                    System.out.println("The file does not exist");
                }

                file.close();
            } catch (final FileSystemException ex) {
                ex.printStackTrace();
            }
        }
    }
}