Search in sources :

Example 1 with FileVisitOption

use of java.nio.file.FileVisitOption in project lucene-solr by apache.

the class CorePropertiesLocator method discover.

@Override
public List<CoreDescriptor> discover(final CoreContainer cc) {
    logger.debug("Looking for core definitions underneath {}", rootDirectory);
    final List<CoreDescriptor> cds = Lists.newArrayList();
    try {
        Set<FileVisitOption> options = new HashSet<>();
        options.add(FileVisitOption.FOLLOW_LINKS);
        final int maxDepth = 256;
        Files.walkFileTree(this.rootDirectory, options, maxDepth, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (file.getFileName().toString().equals(PROPERTIES_FILENAME)) {
                    CoreDescriptor cd = buildCoreDescriptor(file, cc);
                    if (cd != null) {
                        logger.debug("Found core {} in {}", cd.getName(), cd.getInstanceDir());
                        cds.add(cd);
                    }
                    return FileVisitResult.SKIP_SIBLINGS;
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                // otherwise, log a warning and continue to try and load other cores
                if (file.equals(rootDirectory)) {
                    logger.error("Error reading core root directory {}: {}", file, exc);
                    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error reading core root directory");
                }
                logger.warn("Error visiting {}: {}", file, exc);
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Couldn't walk file tree under " + this.rootDirectory, e);
    }
    logger.info("Found {} core definitions underneath {}", cds.size(), rootDirectory);
    if (cds.size() > 0) {
        logger.info("Cores are: {}", cds.stream().map(CoreDescriptor::getName).collect(Collectors.toList()));
    }
    return cds;
}
Also used : Path(java.nio.file.Path) FileVisitOption(java.nio.file.FileVisitOption) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) SolrException(org.apache.solr.common.SolrException) HashSet(java.util.HashSet)

Example 2 with FileVisitOption

use of java.nio.file.FileVisitOption in project raml-module-builder by folio-org.

the class RamlDirCopier method copy.

/**
 * Copy a RAML directory tree to a target directory tree and
 * dereference all $ref references of all .json and .schema files.
 * It does not delete any existing files or directories at target,
 * but a file may get overwritten.
 *
 * @param sourceDir  directory tree to copy
 * @param targetDir  target directory
 * @throws IOException  on read or write error
 */
public static void copy(Path sourceDir, Path targetDir) throws IOException {
    File targetFile = targetDir.toFile();
    if (!targetFile.isDirectory()) {
        targetFile.mkdirs();
    }
    Set<FileVisitOption> options = Collections.emptySet();
    RamlDirCopier ramlDirCopier = new RamlDirCopier(sourceDir, targetDir);
    Files.walkFileTree(sourceDir, options, Integer.MAX_VALUE, ramlDirCopier.fileVisitor);
}
Also used : FileVisitOption(java.nio.file.FileVisitOption) File(java.io.File)

Example 3 with FileVisitOption

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

the class Restorer method restoreLocations.

protected void restoreLocations() throws CalFacadeException {
    try {
        final Path p = openDir(Defs.locationsDirName);
        if (p == null) {
            return;
        }
        final DirRestore<BwLocation> dirRestore = new DirRestore<>(p, restoreLoc);
        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) BwLocation(org.bedework.calfacade.BwLocation) FileVisitOption(java.nio.file.FileVisitOption) IOException(java.io.IOException) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 4 with FileVisitOption

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

the class Restorer method restoreCollections.

protected void restoreCollections(final Path p) throws CalFacadeException {
    try {
        final DirRestore<BwCalendar> colRestore = new DirRestore<>(p, restoreCol);
        final EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
        Files.walkFileTree(p, opts, Integer.MAX_VALUE, colRestore);
    } catch (final IOException ie) {
        throw new CalFacadeException(ie);
    } finally {
        popPath();
    }
}
Also used : FileVisitOption(java.nio.file.FileVisitOption) BwCalendar(org.bedework.calfacade.BwCalendar) IOException(java.io.IOException) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 5 with FileVisitOption

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

the class LocalFileInputPlugin method listFiles.

public List<String> listFiles(PluginTask task) {
    Path pathPrefix = Paths.get(task.getPathPrefix()).normalize();
    final Path directory;
    final String fileNamePrefix;
    if (Files.isDirectory(pathPrefix)) {
        directory = pathPrefix;
        fileNamePrefix = "";
    } else {
        fileNamePrefix = pathPrefix.getFileName().toString();
        Path d = pathPrefix.getParent();
        directory = (d == null ? CURRENT_DIR : d);
    }
    final ImmutableList.Builder<String> builder = ImmutableList.builder();
    final String lastPath = task.getLastPath().orNull();
    try {
        log.info("Listing local files at directory '{}' filtering filename by prefix '{}'", directory.equals(CURRENT_DIR) ? "." : directory.toString(), fileNamePrefix);
        int maxDepth = Integer.MAX_VALUE;
        Set<FileVisitOption> opts;
        if (task.getFollowSymlinks()) {
            opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
        } else {
            opts = EnumSet.noneOf(FileVisitOption.class);
            log.info("\"follow_symlinks\" is set false. Note that symbolic links to directories are skipped.");
        }
        Files.walkFileTree(directory, opts, maxDepth, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes attrs) {
                if (path.equals(directory)) {
                    return FileVisitResult.CONTINUE;
                } else if (lastPath != null && path.toString().compareTo(lastPath) <= 0) {
                    return FileVisitResult.SKIP_SUBTREE;
                } else {
                    Path parent = path.getParent();
                    if (parent == null) {
                        parent = CURRENT_DIR;
                    }
                    if (parent.equals(directory)) {
                        if (path.getFileName().toString().startsWith(fileNamePrefix)) {
                            return FileVisitResult.CONTINUE;
                        } else {
                            return FileVisitResult.SKIP_SUBTREE;
                        }
                    } else {
                        return FileVisitResult.CONTINUE;
                    }
                }
            }

            @Override
            public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) {
                try {
                    // Symbolic links to directories are explicitly skipped here by checking with |Path#toReadlPath|.
                    if (Files.isDirectory(path.toRealPath())) {
                        return FileVisitResult.CONTINUE;
                    }
                } catch (IOException ex) {
                    throw new RuntimeException("Can't resolve symbolic link", ex);
                }
                if (lastPath != null && path.toString().compareTo(lastPath) <= 0) {
                    return FileVisitResult.CONTINUE;
                } else {
                    Path parent = path.getParent();
                    if (parent == null) {
                        parent = CURRENT_DIR;
                    }
                    if (parent.equals(directory)) {
                        if (path.getFileName().toString().startsWith(fileNamePrefix)) {
                            builder.add(path.toString());
                            return FileVisitResult.CONTINUE;
                        }
                    } else {
                        builder.add(path.toString());
                    }
                    return FileVisitResult.CONTINUE;
                }
            }
        });
    } catch (IOException ex) {
        throw new RuntimeException(String.format("Failed get a list of local files at '%s'", directory), ex);
    }
    return builder.build();
}
Also used : Path(java.nio.file.Path) ImmutableList(com.google.common.collect.ImmutableList) FileVisitOption(java.nio.file.FileVisitOption) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

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