Example usage for java.nio.file Files newDirectoryStream

List of usage examples for java.nio.file Files newDirectoryStream

Introduction

In this page you can find the example usage for java.nio.file Files newDirectoryStream.

Prototype

public static DirectoryStream<Path> newDirectoryStream(Path dir) throws IOException 

Source Link

Document

Opens a directory, returning a DirectoryStream to iterate over all entries in the directory.

Usage

From source file:com.epam.catgenome.manager.FileManager.java

private int countChildrenFiles(Path child) throws IOException {
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(child)) {
        Iterator<Path> iter = stream.iterator();
        if (!iter.hasNext()) {
            return 0;
        }//from   ww  w.  j a  va2s .c  om

        int count = 0;
        while (iter.hasNext()) {
            File file = iter.next().toFile();
            BiologicalDataItemFormat format = NgbFileUtils.getFormatByExtension(file.getName());
            if (file.isDirectory() || (format != null && NgbFileUtils.isFileBrowsingAllowed(format))) {
                count++;
            }
        }

        return count;
    }
}

From source file:org.elasticsearch.test.ElasticsearchIntegrationTest.java

/**
 * Return settings that could be used to start a node that has the given zipped home directory.
 *///from  ww  w . java2 s. com
protected Settings prepareBackwardsDataDir(Path backwardsIndex, Object... settings) throws IOException {
    Path indexDir = createTempDir();
    Path dataDir = indexDir.resolve("data");
    try (InputStream stream = Files.newInputStream(backwardsIndex)) {
        TestUtil.unzip(stream, indexDir);
    }
    assertTrue(Files.exists(dataDir));

    // list clusters in the datapath, ignoring anything from extrasfs
    final Path[] list;
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dataDir)) {
        List<Path> dirs = new ArrayList<>();
        for (Path p : stream) {
            if (!p.getFileName().toString().startsWith("extra")) {
                dirs.add(p);
            }
        }
        list = dirs.toArray(new Path[0]);
    }

    if (list.length != 1) {
        throw new IllegalStateException(
                "Backwards index must contain exactly one cluster\n" + StringUtils.join(list, "\n"));
    }
    Path src = list[0];
    Path dest = dataDir.resolve(internalCluster().getClusterName());
    assertTrue(Files.exists(src));
    Files.move(src, dest);
    assertFalse(Files.exists(src));
    assertTrue(Files.exists(dest));
    Settings.Builder builder = Settings.builder().put(settings).put("path.data", dataDir.toAbsolutePath());

    Path configDir = indexDir.resolve("config");
    if (Files.exists(configDir)) {
        builder.put("path.conf", configDir.toAbsolutePath());
    }
    return builder.build();
}

From source file:org.ow2.authzforce.pap.dao.flatfile.FlatFileBasedDomainsDAO.java

/**
 * Creates instance//ww w. ja v a  2 s.c o m
 * 
 * @param domainsRoot
 *            root directory of the configuration data of security domains,
 *            one subdirectory per domain
 * @param domainTmpl
 *            domain template directory; directories of new domains are
 *            created from this template
 * @param domainsSyncIntervalSec
 *            how often (in seconds) the synchronization of managed domains
 *            (in memory) with the domain subdirectories in the
 *            <code>domainsRoot</code> directory (on disk) is done. If
 *            <code>domainSyncInterval</code> > 0, every
 *            <code>domainSyncInterval</code>, the managed domains (loaded
 *            in memory) are updated if any change has been detected in the
 *            <code>domainsRoot</code> directory in this interval (since
 *            last sync). To be more specific, <i>any change</i> here means
 *            any creation/deletion/modification of a domain folder
 *            (modification means: any file changed within the folder). If
 *            <code>domainSyncInterval</code> &lt;= 0, synchronization is
 *            disabled.
 * @param pdpModelHandler
 *            PDP configuration model handler
 * @param useRandomAddressBasedUUID
 *            true iff a random multicast address must be used as node field
 *            of generated UUIDs (Version 1), else the MAC address of one of
 *            the network interfaces is used. Setting this to 'true' is NOT
 *            recommended unless the host is disconnected from the network.
 *            These generated UUIDs are used for domain IDs.
 * @param domainDAOClientFactory
 *            domain DAO client factory
 * @throws IOException
 *             I/O error occurred scanning existing domain folders in
 *             {@code domainsRoot} for loading.
 */
@ConstructorProperties({ "domainsRoot", "domainTmpl", "domainsSyncIntervalSec", "pdpModelHandler",
        "useRandomAddressBasedUUID", "domainDAOClientFactory" })
public FlatFileBasedDomainsDAO(final Resource domainsRoot, final Resource domainTmpl,
        final int domainsSyncIntervalSec, final PdpModelHandler pdpModelHandler,
        final boolean useRandomAddressBasedUUID,
        final DomainDAOClient.Factory<VERSION_DAO_CLIENT, POLICY_DAO_CLIENT, FlatFileBasedDomainDAO<VERSION_DAO_CLIENT, POLICY_DAO_CLIENT>, DOMAIN_DAO_CLIENT> domainDAOClientFactory)
        throws IOException {
    if (domainsRoot == null || domainTmpl == null || pdpModelHandler == null
            || domainDAOClientFactory == null) {
        throw ILLEGAL_CONSTRUCTOR_ARGS_EXCEPTION;
    }

    this.domainDAOClientFactory = domainDAOClientFactory;
    this.policyDAOClientFactory = domainDAOClientFactory.getPolicyDAOClientFactory();
    this.policyVersionDAOClientFactory = policyDAOClientFactory.getVersionDAOClientFactory();

    this.uuidGen = initUUIDGenerator(useRandomAddressBasedUUID);
    this.pdpModelHandler = pdpModelHandler;

    // Validate domainsRoot arg
    if (!domainsRoot.exists()) {
        throw new IllegalArgumentException(
                "'domainsRoot' resource does not exist: " + domainsRoot.getDescription());
    }

    final String ioExMsg = "Cannot resolve 'domainsRoot' resource '" + domainsRoot.getDescription()
            + "' as a file on the file system";
    File domainsRootFile = null;
    try {
        domainsRootFile = domainsRoot.getFile();
    } catch (final IOException e) {
        throw new IllegalArgumentException(ioExMsg, e);
    }

    this.domainsRootDir = domainsRootFile.toPath();
    FlatFileDAOUtils.checkFile("File defined by SecurityDomainManager parameter 'domainsRoot'", domainsRootDir,
            true, true);

    // Validate domainTmpl directory arg
    if (!domainTmpl.exists()) {
        throw new IllegalArgumentException(
                "'domainTmpl' resource does not exist: " + domainTmpl.getDescription());
    }

    final String ioExMsg2 = "Cannot resolve 'domainTmpl' resource '" + domainTmpl.getDescription()
            + "' as a file on the file system";
    File domainTmplFile = null;
    try {
        domainTmplFile = domainTmpl.getFile();
    } catch (final IOException e) {
        throw new IllegalArgumentException(ioExMsg2, e);
    }

    this.domainTmplDirPath = domainTmplFile.toPath();
    FlatFileDAOUtils.checkFile("File defined by SecurityDomainManager parameter 'domainTmpl'",
            domainTmplDirPath, true, false);

    LOGGER.debug("Looking for domain sub-directories in directory {}", domainsRootDir);
    try (final DirectoryStream<Path> dirStream = Files.newDirectoryStream(domainsRootDir)) {
        for (final Path domainPath : dirStream) {
            LOGGER.debug("Checking domain in file {}", domainPath);
            if (!Files.isDirectory(domainPath)) {
                LOGGER.warn("Ignoring invalid domain file {} (not a directory)", domainPath);
                continue;
            }

            // domain folder name is the domain ID
            final Path lastPathSegment = domainPath.getFileName();
            if (lastPathSegment == null) {
                throw new RuntimeException("Invalid Domain folder path '" + domainPath + "': no filename");
            }

            final String domainId = lastPathSegment.toString();
            FlatFileBasedDomainDAO<VERSION_DAO_CLIENT, POLICY_DAO_CLIENT> domainDAO = null;
            try {
                domainDAO = new FileBasedDomainDAOImpl(domainPath, null);
            } catch (final IllegalArgumentException e) {
                throw new RuntimeException("Invalid domain data for domain '" + domainId + "'", e);
            }

            final DOMAIN_DAO_CLIENT domain = domainDAOClientFactory.getInstance(domainId, domainDAO);
            domainMap.put(domainId, domain);
        }
    } catch (final IOException e) {
        throw new IOException("Failed to scan files in the domains root directory '" + domainsRootDir
                + "' looking for domain directories", e);
    }

    this.domainDirToMemSyncIntervalSec = Integer.valueOf(domainsSyncIntervalSec).longValue();
}

From source file:org.ow2.authzforce.pap.dao.flatfile.FlatFileBasedDomainsDAO.java

@Override
public Set<String> getDomainIDs(final String externalId) throws IOException {
    synchronized (domainsRootDir) {
        if (externalId != null) {
            // externalId not null
            final String domainId = domainIDsByExternalId.get(externalId);
            if (domainId == null) {
                return Collections.<String>emptySet();
            }//from ww w.j av a 2s.c om

            // domainId not null, check if domain is still there in the
            // repository
            final Path domainDirPath = this.domainsRootDir.resolve(domainId);
            if (Files.exists(domainDirPath, LinkOption.NOFOLLOW_LINKS)) {
                return Collections.<String>singleton(domainId);
            }

            // domain directory no longer exists, remove from map and so on
            removeDomainFromCache(domainId);
            return Collections.<String>emptySet();
        }

        // externalId == null
        /*
         * All changes to domainMap are synchronized by 'domainsRootDir'. So
         * we can iterate and change if necessary for synchronizing the
         * domains root directory with the domainMap (Using a domainMap is
         * necessary for quick access to domains' PDPs.)
         */
        final Set<String> oldDomainIDs = new HashSet<>(domainMap.keySet());
        final Set<String> newDomainIDs = new HashSet<>();
        try (final DirectoryStream<Path> dirStream = Files.newDirectoryStream(domainsRootDir)) {
            for (final Path domainDirPath : dirStream) {
                LOGGER.debug("Checking domain in file {}", domainDirPath);
                if (!Files.isDirectory(domainDirPath)) {
                    LOGGER.warn("Ignoring invalid domain file {} (not a directory)", domainDirPath);
                    continue;
                }

                // domain folder name is the domain ID
                final Path lastPathSegment = domainDirPath.getFileName();
                if (lastPathSegment == null) {
                    throw new RuntimeException(
                            "Invalid Domain folder path '" + domainDirPath + "': no filename");
                }

                final String domainId = lastPathSegment.toString();
                newDomainIDs.add(domainId);
                if (oldDomainIDs.remove(domainId)) {
                    // not new domain, but directory may have changed ->
                    // sync
                    final DOMAIN_DAO_CLIENT domain = domainMap.get(domainId);
                    if (domain != null) {
                        domain.getDAO().sync();
                    }
                } else {
                    // new domain directory
                    addDomainToCacheAfterDirectoryCreated(domainId, domainDirPath, null);
                }
            }
        } catch (final IOException e) {
            throw new IOException("Failed to scan files in the domains root directory '" + domainsRootDir
                    + "' looking for domain directories", e);
        }

        if (!oldDomainIDs.isEmpty()) {
            // old domains remaining in cache that don't match directories
            // -> removed
            // -> remove from cache
            for (final String domainId : oldDomainIDs) {
                removeDomainFromCache(domainId);
            }
        }

        return newDomainIDs;
    }
}