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.mirth.connect.util.MessageImporter.java

public MessageImportResult importMessages(String path, Boolean recursive, MessageWriter messageWriter,
        String baseDir) throws InterruptedException, MessageImportException, MessageImportInvalidPathException {
    int[] result = new int[] { 0, 0 };

    if (baseDir == null) {
        baseDir = System.getProperty("user.dir");
    }//from w ww.ja va2s.c om

    try {
        path = FilenameUtils.getAbsolutePath(new File(baseDir), path);

        if (!new File(path).canRead()) {
            throw new MessageImportInvalidPathException(
                    "The file/folder was not found or is not readable: " + path);
        }

        FileObject file = VfsUtils.getManager().resolveFile(VfsUtils.pathToUri(path));

        switch (file.getType()) {
        case FOLDER:
            for (FileObject child : file.getChildren()) {
                if (recursive) {
                    importVfsFileRecursive(child, messageWriter, result);
                } else if (child.getType() == FileType.FILE) {
                    importVfsFile(child, messageWriter, result);
                }
            }

            break;

        case FILE:
            importVfsFile(file, messageWriter, result);
            break;
        }

        messageWriter.finishWrite();
    } catch (Exception e) {
        throw new MessageImportException(e);
    } finally {
        try {
            messageWriter.close();
        } catch (Exception e) {
            logger.error("Failed to close message writer", e);
        }
    }

    return new MessageImportResult(result[0], result[1]);
}

From source file:de.innovationgate.wgpublisher.design.sync.FileContainerDeployment.java

public void resetUpdateInformation()
        throws InstantiationException, IllegalAccessException, IOException, WGDesignSyncException {

    FileObject folder = getCodeFile();
    Map files = new HashMap();
    FileObject[] folderFiles = folder.getChildren();
    FileObject folderFile;/*from www  .ja  va  2  s  . c om*/
    for (int i = 0; i < folderFiles.length; i++) {

        folderFile = folderFiles[i];
        if (isExcludedFileContainerFile(folderFile)) {
            continue;
        }

        ContainerFile containerFile = new ContainerFile(folderFile);
        files.put(containerFile.getName(), containerFile);
    }
    _files = files;

    FileObject metadataFile = getMetadataFile();
    if (metadataFile.exists()) {
        _timestampOfMetadataFile = metadataFile.getContent().getLastModifiedTime();
    }

}

From source file:hadoopInstaller.installation.Installer.java

private void getBundleInstallDirectory(String resource, FileObject file) throws InstallationFatalError {
    getLog().trace("HadoopInstaller.InstallationDirectory.Start", resource); //$NON-NLS-1$
    try {/*from  ww w  .  j  a v  a 2  s. c  o  m*/
        String uri = URI.create(file.getName().getURI().replaceFirst("file:", "tgz:")) //$NON-NLS-1$ //$NON-NLS-2$
                .toString();
        FileObject tgzFile;
        tgzFile = VFS.getManager().resolveFile(uri);
        String name = tgzFile.getChildren()[0].getName().getBaseName();
        getDirectories().put(resource, name);
        getLog().debug("HadoopInstaller.InstallationDirectory.Success", //$NON-NLS-1$
                resource, name);
    } catch (FileSystemException e) {
        throw new InstallationFatalError(e, "HadoopInstaller.InstallationDirectory.Error", //$NON-NLS-1$
                file.getName().getBaseName());
    }
}

From source file:com.sludev.commons.vfs2.provider.azure.AzFileProviderTest.java

/**
 * By default FileObject.getChildren() will use doListChildrenResolved() if available
 * //from   w w  w  .  j  a  v a 2 s.  c  o  m
 * @throws Exception 
 */
@Test
public void A005_listChildren() throws Exception {
    String currAccountStr = testProperties.getProperty("azure.account.name");
    String currKey = testProperties.getProperty("azure.account.key");
    String currContainerStr = testProperties.getProperty("azure.test0001.container.name");
    String currHost = testProperties.getProperty("azure.host"); // <account>.blob.core.windows.net

    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(AzConstants.AZSBSCHEME, new AzFileProvider());
    currMan.init();

    StaticUserAuthenticator auth = new StaticUserAuthenticator("", currAccountStr, currKey);
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    String currFileNameStr = "uploadFile02";
    String currUriStr = String.format("%s://%s/%s/%s", AzConstants.AZSBSCHEME, currHost, currContainerStr,
            currFileNameStr);
    FileObject currFile = currMan.resolveFile(currUriStr, opts);

    FileObject[] currObjs = currFile.getChildren();
    for (FileObject obj : currObjs) {
        FileName currName = obj.getName();
        Boolean res = obj.exists();
        FileType ft = obj.getType();

        log.info(String.format("\nNAME.PATH : '%s'\nEXISTS : %b\nTYPE : %s\n\n", currName.getPath(), res, ft));
    }
}

From source file:cz.lbenda.dataman.db.ExtConfFactory.java

/** Load extend configuration to given database configuration */
public void load() {
    if (exConf != null && StringUtils.isBlank(exConf.getSrc())) {
        loadExConfType(exConf);/*from ww  w  .  ja v  a2  s . c o  m*/
    } else if (exConf != null) {
        if (exConf.getSrc().startsWith("db://")) {
            String path = exConf.getSrc().substring(5, exConf.getSrc().length());
            dbConfig.getConnectionProvider()
                    .onPreparedStatement(String.format(
                            "select usr, exConf from %s where (usr = ? or usr is null or usr = '')", path),
                            tuple2 -> {
                                PreparedStatement ps = tuple2.get1();
                                String extendConfiguration = null;
                                try {
                                    ps.setString(1, dbConfig.getConnectionProvider().getUser().getUsername());
                                    try (ResultSet rs = ps.executeQuery()) {
                                        while (rs.next()) {
                                            if (rs.getString(1) == null && extendConfiguration == null) { // The null user is used only if no specific user configuration is read
                                                extendConfiguration = rs.getString(2);
                                            } else if (rs.getString(1) != null) {
                                                extendConfiguration = rs.getString(2);
                                            }
                                        }
                                    }
                                } catch (SQLException e) {
                                    LOG.error("Problem with read extend config from table: " + exConf.getSrc(),
                                            e);
                                    ExceptionMessageFrmController.showException(
                                            "Problem with read extend config from table: " + exConf.getSrc(),
                                            e);
                                }
                                if (!StringUtils.isBlank(extendConfiguration)) {
                                    loadExConfType(new StringReader(extendConfiguration));
                                } else {
                                    StringUtils.isBlank(null);
                                }
                            });
        } else {
            try {
                FileSystemManager fsManager = VFS.getManager();
                FileObject file = fsManager.resolveFile(exConf.getSrc());
                if (!file.exists()) {
                    ExceptionMessageFrmController.showException("File not exist: " + exConf.getSrc());
                } else if (file.getChildren() == null || file.getChildren().length == 0) {
                    new Thread(() -> {
                        try {
                            FileContent content = file.getContent();
                            loadExConfType(new InputStreamReader(content.getInputStream()));
                            content.close();
                        } catch (FileSystemException e) {
                            LOG.error("Problem with read extend config from file: " + exConf.getSrc(), e);
                            ExceptionMessageFrmController.showException(
                                    "Problem with read extend config from file: " + exConf.getSrc(), e);
                        }
                    }).start();
                } else {
                    ExceptionMessageFrmController
                            .showException("The file type isn't supported: " + exConf.getSrc());
                }
            } catch (FileSystemException e) {
                LOG.error("Problem with read extend config from file: " + exConf.getSrc(), e);
                ExceptionMessageFrmController
                        .showException("Problem with read extend config from file: " + exConf.getSrc(), e);
            }
        }
    }
}

From source file:com.mirth.connect.util.MessageImporter.java

private void importVfsFileRecursive(FileObject file, MessageWriter messageWriter, int[] result)
        throws InterruptedException, MessageImportException {
    try {/*from ww w. java 2 s. c o  m*/
        switch (file.getType()) {
        case FOLDER:
            logger.debug("Reading folder: " + file.getName().getURI());

            for (FileObject child : file.getChildren()) {
                ThreadUtils.checkInterruptedStatus();
                importVfsFileRecursive(child, messageWriter, result);
            }

            break;

        case FILE:
            importVfsFile(file, messageWriter, result);
            break;
        }
    } catch (FileSystemException e) {
        logger.error("An error occurred when accessing: " + file.getName().getURI(), e);
    }
}

From source file:de.blizzy.backup.vfs.RemoteFileOrFolder.java

@Override
public Set<IFileSystemEntry> list() throws IOException {
    final FileObject fileObject = getFileObject();
    IAction<Set<IFileSystemEntry>> action = new IAction<Set<IFileSystemEntry>>() {
        @Override/*from   www.  j  ava  2 s .  c  om*/
        public Set<IFileSystemEntry> run() throws IOException {
            Set<IFileSystemEntry> result = new HashSet<>();
            for (FileObject child : fileObject.getChildren()) {
                result.add(getFileOrFolder(file + "/" + child.getName().getBaseName())); //$NON-NLS-1$
            }
            return result;
        }

        @Override
        public boolean canRetry(IOException e) {
            return location.canRetryAction(e);
        }
    };
    return new ActionRunner<>(action, RemoteLocation.MAX_TRIES, location).run();
}

From source file:com.sludev.commons.vfs2.provider.s3.SS3FileProviderTest.java

/**
 * By default FileObject.getChildren() will use doListChildrenResolved() if available
 * /*from  w  w w .ja  v a 2s .c om*/
 * @throws Exception 
 */
@Test
public void A007_listChildren() throws Exception {
    String currAccountStr = testProperties.getProperty("s3.access.id");
    String currKey = testProperties.getProperty("s3.access.secret");
    String currContainerStr = testProperties.getProperty("s3.test0001.bucket.name");
    String currHost = testProperties.getProperty("s3.host");
    String currRegion = testProperties.getProperty("s3.region");

    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    SS3FileProvider currSS3 = new SS3FileProvider();

    // Optional set endpoint
    //currSS3.setEndpoint(currHost);

    // Optional set region
    //currSS3.setRegion(currRegion);

    currMan.addProvider(SS3Constants.S3SCHEME, currSS3);
    currMan.init();

    StaticUserAuthenticator auth = new StaticUserAuthenticator("", currAccountStr, currKey);
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    String currFileNameStr = "uploadFile02";
    String currUriStr = String.format("%s://%s/%s/%s", SS3Constants.S3SCHEME, currHost, currContainerStr,
            currFileNameStr);
    FileObject currFile = currMan.resolveFile(currUriStr, opts);

    FileObject[] currObjs = currFile.getChildren();
    for (FileObject obj : currObjs) {
        FileName currName = obj.getName();
        Boolean res = obj.exists();
        FileType ft = obj.getType();

        log.info(String.format("\nNAME.PATH : '%s'\nEXISTS : %b\nTYPE : %s\n\n", currName.getPath(), res, ft));
    }
}

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

/**
 * Populate a node./*from w w  w  .j  a  v  a 2s. c o  m*/
 * @param root      Relative root directory.
 * @param file      The file object.
 * @return   The node data structure which presents the file.
 * @throws FileSystemException
 */
protected JsTreeNodeData populateTreeNodeData(FileObject root, FileObject file) throws FileSystemException {
    boolean noChild = true;
    FileType type = file.getType();
    if (type.equals(FileType.FOLDER) || type.equals(FileType.FILE_OR_FOLDER)) {
        noChild = file.getChildren().length == 0;
    }
    String relativePath = root.getName().getRelativeName(file.getName());
    return populateTreeNodeData(file, noChild, relativePath);
}

From source file:com.sludev.commons.vfs.simpleshell.SimpleShell.java

/**
 * Lists the children of a folder.//www  .j  a v  a2s  .c  om
 */
public void listChildren(final FileObject dir, final boolean recursive, final String prefix)
        throws FileSystemException {
    final FileObject[] children = dir.getChildren();
    for (int i = 0; i < children.length; i++) {
        final FileObject child = children[i];
        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();
        }
    }
}