Search in sources :

Example 16 with FileVisitOption

use of java.nio.file.FileVisitOption in project bw-calendar-engine by Bedework.

the class Restorer method restoreContacts.

protected void restoreContacts() throws CalFacadeException {
    try {
        final Path p = openDir(Defs.contactsDirName);
        if (p == null) {
            return;
        }
        final DirRestore<BwContact> dirRestore = new DirRestore<>(p, restoreCtct);
        final EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
        Files.walkFileTree(p, opts, Integer.MAX_VALUE, dirRestore);
    } catch (final IOException ie) {
        throw new CalFacadeException(ie);
    } finally {
        popPath();
    }
}
Also used : Path(java.nio.file.Path) FileVisitOption(java.nio.file.FileVisitOption) BwContact(org.bedework.calfacade.BwContact) IOException(java.io.IOException) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 17 with FileVisitOption

use of java.nio.file.FileVisitOption in project bw-calendar-engine by Bedework.

the class Restorer method restoreCategories.

protected void restoreCategories() throws CalFacadeException {
    try {
        final Path p = openDir(Defs.categoriesDirName);
        if (p == null) {
            return;
        }
        final DirRestore<BwCategory> dirRestore = new DirRestore<>(p, restoreCat);
        final EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
        Files.walkFileTree(p, opts, Integer.MAX_VALUE, dirRestore);
    } catch (final IOException ie) {
        throw new CalFacadeException(ie);
    } finally {
        popPath();
    }
}
Also used : Path(java.nio.file.Path) FileVisitOption(java.nio.file.FileVisitOption) BwCategory(org.bedework.calfacade.BwCategory) IOException(java.io.IOException) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 18 with FileVisitOption

use of java.nio.file.FileVisitOption in project ceylon by eclipse.

the class JavacPathFileManager method list.

private void list(Path path, String packageName, final Set<Kind> kinds, boolean recurse, final ListBuffer<JavaFileObject> results) throws IOException {
    if (!Files.exists(path))
        return;
    final Path pathDir;
    if (isDirectory(path))
        pathDir = path;
    else {
        FileSystem fs = getFileSystem(path);
        if (fs == null)
            return;
        pathDir = fs.getRootDirectories().iterator().next();
    }
    String sep = path.getFileSystem().getSeparator();
    Path packageDir = packageName.isEmpty() ? pathDir : pathDir.resolve(packageName.replace(".", sep));
    if (!Files.exists(packageDir))
        return;
    /* Alternate impl of list, superceded by use of Files.walkFileTree */
    // Deque<Path> queue = new LinkedList<Path>();
    // queue.add(packageDir);
    // 
    // Path dir;
    // while ((dir = queue.poll()) != null) {
    // DirectoryStream<Path> ds = dir.newDirectoryStream();
    // try {
    // for (Path p: ds) {
    // String name = p.getFileName().toString();
    // if (isDirectory(p)) {
    // if (recurse && SourceVersion.isIdentifier(name)) {
    // queue.add(p);
    // }
    // } else {
    // if (kinds.contains(getKind(name))) {
    // JavaFileObject fe =
    // PathFileObject.createDirectoryPathFileObject(this, p, pathDir);
    // results.append(fe);
    // }
    // }
    // }
    // } finally {
    // ds.close();
    // }
    // }
    int maxDepth = (recurse ? Integer.MAX_VALUE : 1);
    Set<FileVisitOption> opts = EnumSet.of(FOLLOW_LINKS);
    Files.walkFileTree(packageDir, opts, maxDepth, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
            Path name = dir.getFileName();
            if (// JSR 292?
            name == null || SourceVersion.isIdentifier(name.toString()))
                return FileVisitResult.CONTINUE;
            else
                return FileVisitResult.SKIP_SUBTREE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
            if (attrs.isRegularFile() && kinds.contains(getKind(file.getFileName().toString()))) {
                JavaFileObject fe = PathFileObject.createDirectoryPathFileObject(JavacPathFileManager.this, file, pathDir);
                results.append(fe);
            }
            return FileVisitResult.CONTINUE;
        }
    });
}
Also used : Path(java.nio.file.Path) JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) FileVisitOption(java.nio.file.FileVisitOption) FileSystem(java.nio.file.FileSystem) FileVisitResult(java.nio.file.FileVisitResult) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 19 with FileVisitOption

use of java.nio.file.FileVisitOption in project structr by structr.

the class DirectoryWatchService method scanDirectoryTree.

private void scanDirectoryTree(final boolean registerWatchKey, final Path root, final Path path) throws IOException {
    final Set<FileVisitOption> options = new LinkedHashSet<>();
    if (registerWatchKey) {
        final WatchKey key = path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
        if (key != null) {
            watchKeyMap.put(key, root);
        }
    }
    // follow symlinks?
    if (Settings.FollowSymlinks.getValue()) {
        options.add(FileVisitOption.FOLLOW_LINKS);
    }
    final SecurityContext securityContext = SecurityContext.getSuperUserInstance();
    final List<File> directories = new LinkedList<>();
    int count = 0;
    // configure security context for maximum performance
    securityContext.disableEnsureCardinality();
    securityContext.disableModificationOfAccessTime();
    securityContext.ignoreResultCount(true);
    final List<File> files = Arrays.asList(path.toFile().listFiles());
    final Iterator<File> it = files.iterator();
    while (it.hasNext()) {
        int batchCount = 0;
        try (final Tx tx = StructrApp.getInstance(securityContext).tx(true, false, false)) {
            while (it.hasNext() && batchCount++ < 1000) {
                final File file = it.next();
                final Path filePath = file.toPath();
                if (file.isDirectory()) {
                    directories.add(file);
                }
                // notify listener of directory discovery
                listener.onDiscover(root, path, filePath);
                count++;
            }
            // commit batch after 1000 files
            tx.success();
        } catch (FrameworkException fex) {
            fex.printStackTrace();
        }
    }
    logger.info("{}: {} files", path.toString(), count);
    // recurse
    for (final File directory : directories) {
        // recurse (but not in a new thread)
        new ScanWorker(registerWatchKey, root, directory.getAbsolutePath(), false).run();
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Path(java.nio.file.Path) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) FileVisitOption(java.nio.file.FileVisitOption) WatchKey(java.nio.file.WatchKey) LinkedList(java.util.LinkedList) SecurityContext(org.structr.common.SecurityContext) File(java.io.File)

Example 20 with FileVisitOption

use of java.nio.file.FileVisitOption in project crate by crate.

the class LogConfigurator method configure.

private static void configure(final Settings settings, final Path configsPath, final Path logsPath) throws IOException, UserException {
    Objects.requireNonNull(settings);
    Objects.requireNonNull(configsPath);
    Objects.requireNonNull(logsPath);
    loadLog4jPlugins();
    setLogConfigurationSystemProperty(logsPath, settings);
    // we initialize the status logger immediately otherwise Log4j will complain when we try to get the context
    configureStatusLogger();
    final LoggerContext context = (LoggerContext) LogManager.getContext(false);
    final Set<String> locationsWithDeprecatedPatterns = Collections.synchronizedSet(new HashSet<>());
    final List<AbstractConfiguration> configurations = new ArrayList<>();
    /*
         * Subclass the properties configurator to hack the new pattern in
         * place so users don't have to change log4j2.properties in
         * a minor release. In 7.0 we'll remove this and force users to
         * change log4j2.properties. If they don't customize log4j2.properties
         * then they won't have to do anything anyway.
         *
         * Everything in this subclass that isn't marked as a hack is copied
         * from log4j2's source.
         */
    final PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory() {

        @Override
        public PropertiesConfiguration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
            final Properties properties = new Properties();
            try (InputStream configStream = source.getInputStream()) {
                properties.load(configStream);
            } catch (final IOException ioe) {
                throw new ConfigurationException("Unable to load " + source.toString(), ioe);
            }
            // Hack the new pattern into place
            for (String name : properties.stringPropertyNames()) {
                if (false == name.endsWith(".pattern"))
                    continue;
                // Null is weird here but we can't do anything with it so ignore it
                String value = properties.getProperty(name);
                if (value == null)
                    continue;
                // Tests don't need to be changed
                if (value.contains("%test_thread_info"))
                    continue;
                /*
                     * Patterns without a marker are sufficiently customized
                     * that we don't have an opinion about them.
                     */
                if (false == value.contains("%marker"))
                    continue;
                if (false == value.contains("%node_name")) {
                    locationsWithDeprecatedPatterns.add(source.getLocation());
                    properties.setProperty(name, value.replace("%marker", "[%node_name]%marker "));
                }
            }
            // end hack
            return new PropertiesConfigurationBuilder().setConfigurationSource(source).setRootProperties(properties).setLoggerContext(loggerContext).build();
        }
    };
    final Set<FileVisitOption> options = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
    Files.walkFileTree(configsPath, options, Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
            if (file.getFileName().toString().equals("log4j2.properties")) {
                configurations.add((PropertiesConfiguration) factory.getConfiguration(context, file.toString(), file.toUri()));
            }
            return FileVisitResult.CONTINUE;
        }
    });
    if (configurations.isEmpty()) {
        throw new UserException(ExitCodes.CONFIG, "no log4j2.properties found; tried [" + configsPath + "] and its subdirectories");
    }
    context.start(new CompositeConfiguration(configurations));
    configureLoggerLevels(settings);
    final String deprecatedLocationsString = String.join("\n  ", locationsWithDeprecatedPatterns);
    if (deprecatedLocationsString.length() > 0) {
        LogManager.getLogger(LogConfigurator.class).warn("Some logging configurations have %marker but don't have %node_name. " + "We will automatically add %node_name to the pattern to ease the migration for users who customize " + "log4j2.properties but will stop this behavior in 7.0. You should manually replace `%node_name` with " + "`[%node_name]%marker ` in these locations:\n  {}", deprecatedLocationsString);
    }
}
Also used : FileVisitOption(java.nio.file.FileVisitOption) ArrayList(java.util.ArrayList) Properties(java.util.Properties) PropertiesConfiguration(org.apache.logging.log4j.core.config.properties.PropertiesConfiguration) AbstractConfiguration(org.apache.logging.log4j.core.config.AbstractConfiguration) PropertiesConfigurationFactory(org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory) ConfigurationException(org.apache.logging.log4j.core.config.ConfigurationException) UserException(org.elasticsearch.cli.UserException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Path(java.nio.file.Path) ConfigurationSource(org.apache.logging.log4j.core.config.ConfigurationSource) InputStream(java.io.InputStream) CompositeConfiguration(org.apache.logging.log4j.core.config.composite.CompositeConfiguration) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) LoggerContext(org.apache.logging.log4j.core.LoggerContext) PropertiesConfigurationBuilder(org.apache.logging.log4j.core.config.properties.PropertiesConfigurationBuilder)

Aggregations

FileVisitOption (java.nio.file.FileVisitOption)21 Path (java.nio.file.Path)18 IOException (java.io.IOException)12 FileVisitResult (java.nio.file.FileVisitResult)6 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)6 File (java.io.File)5 HashSet (java.util.HashSet)5 HashMap (java.util.HashMap)4 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)4 Test (org.junit.Test)4 FileWriter (java.io.FileWriter)2 PrintWriter (java.io.PrintWriter)2 FileSystem (java.nio.file.FileSystem)2 PathMatcher (java.nio.file.PathMatcher)2 ArrayList (java.util.ArrayList)2 LoggerContext (org.apache.logging.log4j.core.LoggerContext)2 AbstractConfiguration (org.apache.logging.log4j.core.config.AbstractConfiguration)2 CompositeConfiguration (org.apache.logging.log4j.core.config.composite.CompositeConfiguration)2 PropertiesConfiguration (org.apache.logging.log4j.core.config.properties.PropertiesConfiguration)2 PropertiesConfigurationFactory (org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory)2