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, DirectoryStream.Filter<? super Path> filter)
        throws IOException 

Source Link

Document

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

Usage

From source file:codes.thischwa.c5c.impl.JarFilemanagerMessageResolver.java

@Override
public void setServletContext(ServletContext servletContext) {
    ObjectMapper mapper = new ObjectMapper();
    try {/*  w  w  w. j  av a 2  s .  c  o m*/
        if (JarPathResolver.insideJar(getMessagesFolderPath())) {
            CodeSource src = JarFilemanagerMessageResolver.class.getProtectionDomain().getCodeSource();
            URI uri = src.getLocation().toURI();
            logger.info("Message folder is inside jar: {}", uri);

            try (FileSystem jarFS = FileSystems.newFileSystem(uri, new HashMap<String, String>())) {
                if (jarFS.getRootDirectories().iterator().hasNext()) {
                    Path rootDirectory = jarFS.getRootDirectories().iterator().next();

                    Path langFolder = rootDirectory.resolve(getMessagesFolderPath());
                    if (langFolder != null) {
                        try (DirectoryStream<Path> langFolderStream = Files.newDirectoryStream(langFolder,
                                JS_FILE_MASK)) {
                            for (Path langFile : langFolderStream) {
                                String lang = langFile.getFileName().toString();
                                InputStream is = Files.newInputStream(langFile);
                                Map<String, String> langData = mapper.readValue(is,
                                        new TypeReference<HashMap<String, String>>() {
                                        });
                                collectLangData(lang, langData);
                            }
                        }
                    } else {
                        throw new RuntimeException("Folder in jar " + langFolder + " does not exists.");
                    }
                }
            }
        } else {
            File messageFolder = JarPathResolver.getFolder(getMessagesFolderPath());
            logger.info("Message folder resolved to: {}", messageFolder);

            if (messageFolder == null || !messageFolder.exists()) {
                throw new RuntimeException("Folder " + getMessagesFolderPath() + " does not exist");
            }

            for (File file : messageFolder.listFiles(jsFilter)) {
                String lang = FilenameUtils.getBaseName(file.getName());
                Map<String, String> langData = mapper.readValue(file,
                        new TypeReference<HashMap<String, String>>() {
                        });
                collectLangData(lang, langData);
            }
        }
    } catch (URISyntaxException | IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:io.github.swagger2markup.extensions.DynamicContentExtension.java

/**
 * Builds extension sections//from   ww  w  .  jav  a 2 s. c  o  m
 *
 * @param extensionMarkupLanguage the MarkupLanguage of the snippets content
 * @param contentPath the path where the content files reside
 * @param prefix      extension file prefix
 * @param levelOffset import markup level offset
 */
public void extensionsSection(MarkupLanguage extensionMarkupLanguage, Path contentPath, final String prefix,
        int levelOffset) {
    final Collection<String> filenameExtensions = globalContext.getConfig().getMarkupLanguage()
            .getFileNameExtensions().stream().map(fileExtension -> StringUtils.stripStart(fileExtension, "."))
            .collect(Collectors.toList());

    DirectoryStream.Filter<Path> filter = entry -> {
        String fileName = entry.getFileName().toString();
        return fileName.startsWith(prefix) && FilenameUtils.isExtension(fileName, filenameExtensions);
    };

    try (DirectoryStream<Path> extensionFiles = Files.newDirectoryStream(contentPath, filter)) {

        if (extensionFiles != null) {
            List<Path> extensions = Lists.newArrayList(extensionFiles);
            Collections.sort(extensions, Ordering.natural());

            for (Path extension : extensions) {
                importContent(extension, (reader) -> contentContext.getMarkupDocBuilder().importMarkup(reader,
                        extensionMarkupLanguage, levelOffset));
            }
        }
    } catch (IOException e) {
        if (logger.isDebugEnabled())
            logger.debug("Failed to read extension files from directory {}", contentPath);
    }
}

From source file:com.marklogic.entityservices.e2e.CSVLoader.java

public void go() throws InterruptedException {

    logger.info("job started.");

    File dir = new File(projectDir + "/data/superstore-csv");

    WriteHostBatcher batcher = moveMgr.newWriteHostBatcher().withBatchSize(100).withThreadCount(10)
            .onBatchSuccess((client, batch) -> logger.info(getSummaryReport(batch)))
            .onBatchFailure((client, batch, throwable) -> {
                logger.warn("FAILURE on batch:" + batch.toString() + "\n", throwable);
                throwable.printStackTrace();
            });//from w  w w . j a v  a  2s  . c om

    ticket = moveMgr.startJob(batcher);

    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir.toPath(), "*.csv")) {
        for (Path entry : stream) {
            logger.debug("Adding " + entry.getFileName().toString());

            MappingIterator<ObjectNode> it = csvMapper.readerFor(ObjectNode.class).with(bootstrapSchema)
                    .readValues(entry.toFile());
            long i = 0;
            while (it.hasNext()) {
                ObjectNode jsonNode = it.next();
                String jsonString = mapper.writeValueAsString(jsonNode);

                String uri = entry.toUri().toString() + "-" + Long.toString(i++) + ".json";
                DocumentMetadataHandle metadata = new DocumentMetadataHandle() //
                        .withCollections("raw", "csv") //
                        .withPermission("nwind-reader", Capability.READ) //
                        .withPermission("nwind-writer", Capability.INSERT, Capability.UPDATE);
                batcher.add(uri, metadata, new StringHandle(jsonString));
                if (i % 1000 == 0)
                    logger.debug("Inserting JSON document " + uri);
            }
            it.close();
        }
    }

    catch (IOException e)

    {
        e.printStackTrace();
    }

    batcher.flush();
}

From source file:com.marklogic.entityservices.examples.CSVLoader.java

public void go() throws InterruptedException {

    logger.info("job started.");

    File dir = new File(projectDir + "/data/third-party/csv");

    WriteHostBatcher batcher = moveMgr.newWriteHostBatcher().withBatchSize(100).withThreadCount(10)
            .onBatchSuccess((client, batch) -> logger.info(getSummaryReport(batch)))
            .onBatchFailure((client, batch, throwable) -> {
                logger.warn("FAILURE on batch:" + batch.toString() + "\n", throwable);
                throwable.printStackTrace();
            });/* w  w w  .j  a v a2s .c  o  m*/

    ticket = moveMgr.startJob(batcher);

    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir.toPath(), "*.csv")) {
        for (Path entry : stream) {
            logger.debug("Adding " + entry.getFileName().toString());

            MappingIterator<ObjectNode> it = csvMapper.readerFor(ObjectNode.class).with(bootstrapSchema)
                    .readValues(entry.toFile());
            long i = 0;
            while (it.hasNext()) {
                ObjectNode jsonNode = it.next();
                String jsonString = mapper.writeValueAsString(jsonNode);

                String uri = entry.toUri().toString() + "-" + Long.toString(i++) + ".json";
                DocumentMetadataHandle metadata = new DocumentMetadataHandle() //
                        .withCollections("raw", "csv") //
                        .withPermission("race-reader", Capability.READ) //
                        .withPermission("race-writer", Capability.INSERT, Capability.UPDATE);
                batcher.add(uri, metadata, new StringHandle(jsonString));
                if (i % 1000 == 0)
                    logger.debug("Inserting JSON document " + uri);
            }
            it.close();
        }
    }

    catch (IOException e)

    {
        e.printStackTrace();
    }

    batcher.flush();
}

From source file:uk.co.inetria.gce.GceUsageParser.java

public GceUsageParser(String folder) throws Exception {

    super();//from  w w  w  .  ja va2  s  .co m

    // local file
    DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
        public boolean accept(Path file) throws IOException {

            String filename = file.toString();
            return filename.contains(USAGE_PREFIX);
        }
    };

    Path dir = Paths.get(folder);
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, filter)) {
        for (Path path : stream) {
            this.files.add(path.toString());
        }
    }

}

From source file:org.spongepowered.common.world.WorldMigrator.java

/**
 * Performs the migration of worlds from {@link WorldMigrator#getOldWorldContainer()} to the provided worldContainer.
 *
 * @param worldContainer The container to move worlds to
 *///from   ww  w. ja va2 s .c o m
public static void migrateWorldsTo(Path worldContainer) {
    SpongeImpl.getLogger().info("Checking for worlds that need to be migrated...");

    final Path oldWorldContainer = getOldWorldContainer();
    final List<Path> migrated = new ArrayList<>();

    if (Files.notExists(oldWorldContainer)) {
        return;
    }

    try (DirectoryStream<Path> stream = Files.newDirectoryStream(oldWorldContainer,
            entry -> !entry.getFileName().equals(worldContainer.getFileName())
                    && Files.exists(entry.resolve("level.dat"))
                    && !Files.exists(entry.resolve("level_sponge.dat")))) {
        for (Path oldWorldPath : stream) {
            Path worldPath = worldContainer.resolve(oldWorldPath.getFileName());

            // Only copy over the old world files if we don't have it already.
            if (Files.notExists(worldPath)) {
                SpongeImpl.getLogger().info("Migrating [{}] from [{}].", oldWorldPath.getFileName(),
                        oldWorldContainer);
                try {
                    worldPath = renameToVanillaNetherOrEnd(worldContainer, oldWorldPath, worldPath);
                    FileUtils.copyDirectory(oldWorldPath.toFile(), worldPath.toFile());
                    fixInnerNetherOrEndRegionData(worldPath);
                    removeInnerNameFolder(worldPath);
                    migrated.add(worldPath);
                } catch (IOException ioe) {
                    SpongeImpl.getLogger().warn("Failed to migrate [{}] from [{}] to [{}]",
                            oldWorldPath.getFileName(), oldWorldContainer, worldPath, ioe);
                }

                SpongeImpl.getLogger().info("Migrated world [{}] from [{}] to [{}]", oldWorldPath.getFileName(),
                        oldWorldContainer, worldPath);
            }
        }
    } catch (Exception ignore) {
    }

    if (migrated.size() > 0) {
        SpongeImpl.getLogger().info("[{}] worlds have been migrated back to Vanilla's format.",
                migrated.size());
    } else {
        SpongeImpl.getLogger().info("No worlds were found in need of migration.");
    }
}

From source file:codes.thischwa.c5c.requestcycle.impl.FilemanagerIconResolver.java

protected void collectIcons(String iconPath, java.nio.file.Path iconFolder, PathBuilder urlPath) {
    Map<String, String> iconsPerType = new HashMap<>();
    try {//w  w w  . j av a 2  s  .  c  om
        for (java.nio.file.Path icon : Files.newDirectoryStream(iconFolder,
                new DirectoryStream.Filter<java.nio.file.Path>() {
                    @Override
                    public boolean accept(java.nio.file.Path entry) throws IOException {
                        if (Files.isDirectory(entry))
                            return false;
                        String name = entry.getFileName().toString();
                        return !name.contains("_") && name.endsWith("png");
                    }
                })) {
            String name = icon.getFileName().toString();
            String knownExtension = FilenameUtils.getBaseName(name);
            iconsPerType.put(knownExtension, urlPath.addFile(name));
        }
    } catch (IOException e) {
        throw new RuntimeException("Couldn't read the icon files!", e);
    }

    iconsPerType.put(IconResolver.key_directory, urlPath.addFile(directoryIcon));
    iconsPerType.put(IconResolver.key_default, urlPath.addFile(defaultIcon));
    iconsPerType.put(IconResolver.key_directory_lock, urlPath.addFile("locked_" + directoryIcon));
    iconsPerType.put(IconResolver.key_default_lock, urlPath.addFile("locked_" + defaultIcon));

    iconCache.put(iconPath, new IconRequestResolver(iconsPerType));
}

From source file:io.logspace.it.InfrastructureRule.java

private void initializeHQ() throws Exception {
    File solrDataDirectory = getCanonicalFile(new File("./target/solr"));
    FileUtils.deleteDirectory(solrDataDirectory);

    Path capabilitiesDirectory = Paths.get("./capabilities");
    Files.createDirectories(capabilitiesDirectory);
    Files.newDirectoryStream(capabilitiesDirectory, "*.json").forEach(InfrastructureRule::deleteFile);

    System.setProperty("port", String.valueOf(TEST_PORT));
    System.setProperty("admin.separate-service", "false");
    System.setProperty("log-path", "./target");
    System.setProperty("spring.profiles.active", "prod,test");

    this.logspaceHq = new LogspaceHq();
    this.logspaceHq.invoke();

    this.injectDependencies();

    this.prepareConfig();

    this.initialized = true;
}

From source file:org.sonar.batch.bootstrap.GlobalTempFolderProvider.java

private static void cleanTempFolders(Path path) throws IOException {
    if (Files.exists(path)) {
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, new CleanFilter())) {
            for (Path p : stream) {
                deleteQuietly(p.toFile());
            }/*from ww  w .  j  a va 2 s. co  m*/
        }
    }
}

From source file:org.sonar.scanner.bootstrap.GlobalTempFolderProvider.java

private static void cleanTempFolders(Path path) throws IOException {
    if (path.toFile().exists()) {
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, new CleanFilter())) {
            for (Path p : stream) {
                deleteQuietly(p.toFile());
            }/*w  w  w  . ja  v  a 2  s.c o  m*/
        }
    }
}