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:ee.ria.xroad.common.conf.globalconf.ConfigurationDirectoryV1.java

/**
 * Reloads the configuration directory. Only files that are new or have
 * changed, are actually loaded./*from   w w w .ja va2 s.c om*/
 * @throws Exception if an error occurs during reload
 */
public synchronized void reload() throws Exception {
    Map<String, PrivateParametersV1> privateParams = new HashMap<>();
    Map<String, SharedParametersV1> sharedParams = new HashMap<>();

    log.trace("Reloading configuration from {}", path);

    instanceIdentifier = null;

    try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, Files::isDirectory)) {
        for (Path instanceDir : stream) {
            log.trace("Loading parameters from {}", instanceDir);

            loadPrivateParameters(instanceDir, privateParams);
            loadSharedParameters(instanceDir, sharedParams);
        }
    }

    this.privateParameters = privateParams;
    this.sharedParameters = sharedParams;
}

From source file:sadl.run.datagenerators.DataGenerator.java

private void run() throws IOException, InterruptedException, ClassNotFoundException {
    try {//w w w .  jav  a2 s  .  co  m
        if (Files.notExists(outputDir)) {
            Files.createDirectories(outputDir);
        }
        Files.walk(outputDir).filter(p -> !Files.isDirectory(p)).forEach(p -> {
            try {
                logger.info("Deleting file {}", p);
                Files.delete(p);
            } catch (final Exception e) {
                e.printStackTrace();
            }
        });
        // parse timed sequences
        final TimedInput trainingTimedSequences = TimedInput.parseAlt(Paths.get(dataString), 1);
        final TauPtaLearner learner = new TauPtaLearner();
        final TauPTA pta = learner.train(trainingTimedSequences);
        IoUtils.serialize(pta, outputDir.resolve(Paths.get("pta_normal.ser")));
        pta.toGraphvizFile(outputDir.resolve(Paths.get("pta_normal.dot")), false);
        // try (BufferedWriter br = Files.newBufferedWriter(outputDir.resolve(Paths.get("normal_sequences")), StandardCharsets.UTF_8)) {
        // logger.info("sampling normal sequences");
        // for (int i = 0; i < 1000000; i++) {
        // br.write(pta.sampleSequence().toString(true));
        // br.write('\n');
        // }
        // }

        for (final AnomalyInsertionType type : AnomalyInsertionType.values()) {
            if (type != AnomalyInsertionType.NONE && type != AnomalyInsertionType.ALL) {
                final TauPTA anomaly1 = SerializationUtils.clone(pta);
                logger.info("inserting Anomaly Type {}", type);
                anomaly1.makeAbnormal(type);
                try {
                    anomaly1.toGraphvizFile(
                            outputDir.resolve(Paths.get("pta_abnormal_" + type.getTypeIndex() + ".dot")),
                            false);
                    IoUtils.serialize(anomaly1,
                            outputDir.resolve(Paths.get("pta_abnormal_" + type.getTypeIndex() + ".ser")));
                    final TauPTA des = (TauPTA) IoUtils.deserialize(
                            outputDir.resolve(Paths.get("pta_abnormal_" + type.getTypeIndex() + ".ser")));
                    if (!anomaly1.equals(des)) {
                        throw new IllegalStateException();
                    }
                } catch (final IOException e) {
                    logger.error("unexpected exception while printing graphviz file", e);
                }
                // try (BufferedWriter br = Files.newBufferedWriter(outputDir.resolve(Paths.get("abnormal_sequences_type_" + type.getTypeIndex())),
                // StandardCharsets.UTF_8)) {
                // for (int i = 0; i < 100000; i++) {
                // br.write(anomaly1.sampleSequence().toString(true));
                // br.write('\n');
                // }
                // }
            }
        }
    } finally {
        logger.info("Starting to dot PTAs");
        try (DirectoryStream<Path> ds = Files.newDirectoryStream(outputDir.resolve(Paths.get(".")), "*.dot")) {
            for (final Path p : ds) {
                if (System.getProperty("os.name").toLowerCase().contains("linux")) {
                    logger.info("dotting {}...", p);
                    Runtime.getRuntime().exec("dot -Tpdf -O " + p.toString());
                    Runtime.getRuntime().exec("dot -Tpng -O " + p.toString());
                    logger.info("dotted {}.", p);
                }
            }
        }
    }

}

From source file:edu.cornell.mannlib.vitro.webapp.servlet.setup.FileGraphSetup.java

private Set<Path> getFilegraphPaths(ServletContext ctx, String... strings) {
    StartupStatus ss = StartupStatus.getBean(ctx);

    String homeDirProperty = ApplicationUtils.instance().getHomeDirectory().getPath().toString();
    Path filegraphDir = Paths.get(homeDirProperty, strings);

    Set<Path> paths = new TreeSet<>();
    if (Files.isDirectory(filegraphDir)) {
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(filegraphDir, REJECT_HIDDEN_FILES)) {
            for (Path p : stream) {
                paths.add(p);//from w w  w  .ja va  2  s  .  c  o m
            }
            ss.info(this, "Read " + paths.size() + " RDF files from '" + filegraphDir + "'");
        } catch (IOException e) {
            ss.fatal(this, "Failed to read filegraph RDF from '" + filegraphDir + "' directory.", e);
        }
    } else {
        ss.info(this, "Filegraph directory '" + filegraphDir + "' doesn't exist.");
    }
    log.debug("Paths from '" + filegraphDir + "': " + paths);
    return paths;
}

From source file:org.apache.hadoop.hive.ql.log.TestSlidingFilenameRolloverStrategy.java

private void deleteLogFiles(Path parent, String fileName) throws IOException {
    DirectoryStream<Path> stream = Files.newDirectoryStream(parent, fileName + ".*");
    for (Path path : stream) {
        Files.delete(path);//from   w w w . ja  v a  2s. c  om
    }
}

From source file:com.sonar.it.scanner.msbuild.TestUtils.java

public static Path getCustomRoslynPlugin() {
    Path customPluginDir = Paths.get("").resolve("analyzers");

    DirectoryStream.Filter<Path> jarFilter = new DirectoryStream.Filter<Path>() {
        public boolean accept(Path file) throws IOException {
            return Files.isRegularFile(file) && file.toString().endsWith(".jar");
        }/*from w w  w. jav a 2 s  .  c o m*/
    };
    List<Path> jars = new ArrayList<>();
    try {
        Files.newDirectoryStream(customPluginDir, jarFilter).forEach(p -> jars.add(p));
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
    if (jars.isEmpty()) {
        throw new IllegalStateException("No jars found in " + customPluginDir.toString());
    } else if (jars.size() > 1) {
        throw new IllegalStateException("Several jars found in " + customPluginDir.toString());
    }

    return jars.get(0);
}

From source file:gov.gtas.job.scheduler.LoaderScheduler.java

private void processInputAndOutputDirectories(Path incomingDir, Path outgoingDir, LoaderStatistics stats) {
    // No hidden files.
    DirectoryStream.Filter<Path> filter = entry -> {
        File f = entry.toFile();/* w w w .j  a  v a2  s .c o  m*/
        return !f.isHidden() && f.isFile();
    };
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(incomingDir, filter)) {

        final Iterator<Path> iterator = stream.iterator();
        List<File> files = new ArrayList<>();
        for (int i = 0; iterator.hasNext() && i < maxNumofFiles; i++) {
            files.add(iterator.next().toFile());
        }
        Collections.sort(files, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR);
        files.stream().forEach(f -> {
            processSingleFile(f, stats);
            f.renameTo(new File(outgoingDir.toFile() + File.separator + f.getName()));
        });
        stream.close();
    } catch (IOException ex) {
        logger.error("IOException:" + ex.getMessage(), ex);
        ErrorDetailInfo errInfo = ErrorHandlerFactory.createErrorDetails(ex);
        errorPersistenceService.create(errInfo);
    }
}

From source file:org.fao.geonet.api.records.attachments.FilesystemStore.java

@Override
public List<MetadataResource> getResources(ServiceContext context, String metadataUuid,
        MetadataResourceVisibility visibility, String filter) throws Exception {
    ApplicationContext _appContext = ApplicationContextHolder.get();
    String metadataId = getAndCheckMetadataId(metadataUuid);
    GeonetworkDataDirectory dataDirectory = _appContext.getBean(GeonetworkDataDirectory.class);
    SettingManager settingManager = _appContext.getBean(SettingManager.class);
    AccessManager accessManager = _appContext.getBean(AccessManager.class);

    boolean canEdit = accessManager.canEdit(context, metadataId);
    if (visibility == MetadataResourceVisibility.PRIVATE && !canEdit) {
        throw new SecurityException(String.format(
                "User does not have privileges to get the list of '%s' resources for metadata '%s'.",
                visibility, metadataUuid));
    }/*from  w w w  . j  a  v  a  2  s . c  o m*/

    Path metadataDir = Lib.resource.getMetadataDir(dataDirectory, metadataId);
    Path resourceTypeDir = metadataDir.resolve(visibility.toString());

    List<MetadataResource> resourceList = new ArrayList<>();
    if (filter == null) {
        filter = FilesystemStore.DEFAULT_FILTER;
    }
    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(resourceTypeDir, filter)) {
        for (Path path : directoryStream) {
            MetadataResource resource = new FilesystemStoreResource(
                    UrlEscapers.urlFragmentEscaper().escape(metadataUuid) + "/attachments/"
                            + UrlEscapers.urlFragmentEscaper().escape(path.getFileName().toString()),
                    settingManager.getNodeURL() + "api/records/", visibility, Files.size(path));
            resourceList.add(resource);
        }
    } catch (IOException ignored) {
    }

    Collections.sort(resourceList, MetadataResourceVisibility.sortByFileName);

    return resourceList;
}

From source file:org.wso2.carbon.uuf.internal.io.ArtifactComponentReference.java

@Override
public Map<String, Properties> getI18nFiles() {
    Path lang = componentDirectory.resolve(DIR_NAME_LANGUAGE);
    Map<String, Properties> i18n = new HashMap<>();
    DirectoryStream<Path> stream = null;
    if (!Files.exists(lang)) {
        return i18n;
    }/*w w  w.ja  v a2s . co  m*/

    try {
        stream = Files.newDirectoryStream(lang, "*.{properties}");
        for (Path entry : stream) {
            if (Files.isRegularFile(entry)) {
                Properties props = new Properties();
                InputStreamReader is = null;
                String file = entry.toString();
                try {
                    is = new InputStreamReader(new FileInputStream(file), CHAR_ENCODING);
                    props.load(is);
                } finally {
                    IOUtils.closeQuietly(is);
                }

                Path path = entry.getFileName();
                if (path != null) {
                    String fileName = path.toString();
                    i18n.put(fileName.substring(0, fileName.indexOf('.')), props);
                }
            }
        }
    } catch (IOException e) {
        throw new FileOperationException("An error occurred while reading locale files in '" + lang + "'.", e);
    } finally {
        IOUtils.closeQuietly(stream);
    }
    return i18n;
}

From source file:org.structr.web.maintenance.DirectFileImportCommand.java

@Override
public void execute(final Map<String, Object> attributes) throws FrameworkException {

    indexer = StructrApp.getInstance(securityContext).getFulltextIndexer();

    final String sourcePath = getParameterValueAsString(attributes, "source", null);
    final String modeString = getParameterValueAsString(attributes, "mode", Mode.COPY.name()).toUpperCase();
    final String existingString = getParameterValueAsString(attributes, "existing", Existing.SKIP.name())
            .toUpperCase();/*  w  w  w .  ja  v a2  s.  c  om*/
    final boolean doIndex = Boolean
            .parseBoolean(getParameterValueAsString(attributes, "index", Boolean.TRUE.toString()));

    if (StringUtils.isBlank(sourcePath)) {
        throw new FrameworkException(422,
                "Please provide 'source' attribute for deployment source directory path.");
    }

    if (!EnumUtils.isValidEnum(Mode.class, modeString)) {
        throw new FrameworkException(422, "Unknown value for 'mode' attribute. Valid values are: copy, move");
    }

    if (!EnumUtils.isValidEnum(Existing.class, existingString)) {
        throw new FrameworkException(422,
                "Unknown value for 'existing' attribute. Valid values are: skip, overwrite, rename");
    }

    // use actual enums
    final Existing existing = Existing.valueOf(existingString);
    final Mode mode = Mode.valueOf(modeString);

    final List<Path> paths = new ArrayList<>();

    if (sourcePath.contains(PathHelper.PATH_SEP)) {

        final String folderPart = PathHelper.getFolderPath(sourcePath);
        final String namePart = PathHelper.getName(sourcePath);

        if (StringUtils.isNotBlank(folderPart)) {

            final Path source = Paths.get(folderPart);
            if (!Files.exists(source)) {

                throw new FrameworkException(422, "Source path " + sourcePath + " does not exist.");
            }

            if (!Files.isDirectory(source)) {

                throw new FrameworkException(422, "Source path " + sourcePath + " is not a directory.");
            }

            try {

                try (final DirectoryStream<Path> stream = Files.newDirectoryStream(source, namePart)) {

                    for (final Path entry : stream) {
                        paths.add(entry);
                    }

                } catch (final DirectoryIteratorException ex) {
                    throw ex.getCause();
                }

            } catch (final IOException ioex) {
                throw new FrameworkException(422, "Unable to parse source path " + sourcePath + ".");
            }
        }

    } else {

        // Relative path
        final Path source = Paths.get(Settings.BasePath.getValue()).resolve(sourcePath);
        if (!Files.exists(source)) {

            throw new FrameworkException(422, "Source path " + sourcePath + " does not exist.");
        }

        paths.add(source);

    }

    final SecurityContext ctx = SecurityContext.getSuperUserInstance();
    final App app = StructrApp.getInstance(ctx);
    String targetPath = getParameterValueAsString(attributes, "target", "/");
    Folder targetFolder = null;

    ctx.setDoTransactionNotifications(false);

    if (StringUtils.isNotBlank(targetPath) && !("/".equals(targetPath))) {

        try (final Tx tx = app.tx()) {

            targetFolder = app.nodeQuery(Folder.class).and(StructrApp.key(Folder.class, "path"), targetPath)
                    .getFirst();
            if (targetFolder == null) {

                throw new FrameworkException(422, "Target path " + targetPath + " does not exist.");
            }

            tx.success();
        }
    }

    String msg = "Starting direct file import from source directory " + sourcePath + " into target path "
            + targetPath;
    logger.info(msg);
    publishProgressMessage(msg);

    paths.forEach((path) -> {

        try {

            final String newTargetPath;

            // If path is a directory, create it and use it as the new target folder
            if (Files.isDirectory(path)) {

                Path parentPath = path.getParent();
                if (parentPath == null) {
                    parentPath = path;
                }

                createFileOrFolder(ctx, app, parentPath, path,
                        Files.readAttributes(path, BasicFileAttributes.class), sourcePath, targetPath, mode,
                        existing, doIndex);

                newTargetPath = targetPath + PathHelper.PATH_SEP
                        + PathHelper.clean(path.getFileName().toString());

            } else {

                newTargetPath = targetPath;
            }

            Files.walkFileTree(path, new FileVisitor<Path>() {

                @Override
                public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs)
                        throws IOException {
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs)
                        throws IOException {
                    return createFileOrFolder(ctx, app, path, file, attrs, sourcePath, newTargetPath, mode,
                            existing, doIndex);
                }

                @Override
                public FileVisitResult visitFileFailed(final Path file, final IOException exc)
                        throws IOException {
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory(final Path dir, final IOException exc)
                        throws IOException {
                    return FileVisitResult.CONTINUE;
                }
            });

        } catch (final IOException ex) {
            logger.debug("Mode: " + modeString + ", path: " + sourcePath, ex);
        }
    });

    msg = "Finished direct file import from source directory " + sourcePath + ". Imported " + folderCount
            + " folders and " + fileCount + " files.";
    logger.info(msg);

    publishProgressMessage(msg);
}

From source file:at.ac.tuwien.infosys.repository.LocalComponentRepository.java

protected List<Path> list(Path path) {
    List<Path> results = new ArrayList<Path>();

    DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
        @Override//w  w w  .  ja v  a  2s .co m
        public boolean accept(Path entry) throws IOException {
            return (!Files.isHidden(entry) && Files.isRegularFile(entry));
        }
    };

    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path, filter)) {
        for (Path p : directoryStream) {
            results.add(p);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return results;
}