Search in sources :

Example 21 with FileVisitResult

use of java.nio.file.FileVisitResult in project karaf by apache.

the class Profiles method loadProfiles.

public static Map<String, Profile> loadProfiles(final Path root) throws IOException {
    final Map<String, Profile> profiles = new HashMap<>();
    Files.walkFileTree(root, new SimpleFileVisitor<Path>() {

        ProfileBuilder builder;

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            Path fileName = dir.getFileName();
            if (fileName != null && (fileName.toString().endsWith(PROFILE_FOLDER_SUFFIX) || fileName.toString().endsWith(PROFILE_FOLDER_SUFFIX + "/"))) {
                String profileId = root.relativize(dir).toString();
                if (profileId.endsWith("/")) {
                    profileId = profileId.substring(0, profileId.length() - 1);
                }
                profileId = profileId.replaceAll(root.getFileSystem().getSeparator(), "-");
                profileId = profileId.substring(0, profileId.length() - PROFILE_FOLDER_SUFFIX.length());
                builder = ProfileBuilder.Factory.create(profileId);
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            if (exc != null) {
                throw exc;
            }
            if (builder != null) {
                Profile profile = builder.getProfile();
                profiles.put(profile.getId(), profile);
                builder = null;
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (builder != null) {
                String pid = file.getFileName().toString();
                byte[] data = Files.readAllBytes(file);
                builder.addFileConfiguration(pid, data);
            }
            return FileVisitResult.CONTINUE;
        }
    });
    return profiles;
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) ProfileBuilder(org.apache.karaf.profile.ProfileBuilder) Profile(org.apache.karaf.profile.Profile) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 22 with FileVisitResult

use of java.nio.file.FileVisitResult in project karaf by apache.

the class FilesStream method files.

private static Stream<Path> files(Path cur, String glob) {
    String prefix;
    String rem;
    int idx = glob.lastIndexOf('/');
    if (idx >= 0) {
        prefix = glob.substring(0, idx + 1);
        rem = glob.substring(idx + 1);
    } else {
        prefix = "";
        rem = glob;
    }
    Path dir = cur.resolve(prefix);
    final PathMatcher matcher = dir.getFileSystem().getPathMatcher("glob:" + rem);
    Stream.Builder<Path> stream = Stream.builder();
    try {
        Files.walkFileTree(dir, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new FileVisitor<Path>() {

            @Override
            public FileVisitResult preVisitDirectory(Path file, BasicFileAttributes attrs) throws IOException {
                if (file.equals(dir)) {
                    return FileVisitResult.CONTINUE;
                }
                if (Files.isHidden(file)) {
                    return FileVisitResult.SKIP_SUBTREE;
                }
                Path r = dir.relativize(file);
                if (matcher.matches(r)) {
                    stream.add(file);
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (!Files.isHidden(file)) {
                    Path r = dir.relativize(file);
                    if (matcher.matches(r)) {
                        stream.add(file);
                    }
                }
                return FileVisitResult.CONTINUE;
            }

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

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        LOGGER.warn("Error generating filenames", e);
    }
    return stream.build();
}
Also used : Path(java.nio.file.Path) PathMatcher(java.nio.file.PathMatcher) Stream(java.util.stream.Stream) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 23 with FileVisitResult

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

the class TestIndexWriterOnJRECrash method checkIndexes.

/**
   * Recursively looks for indexes underneath <code>file</code>,
   * and runs checkindex on them. returns true if it found any indexes.
   */
public boolean checkIndexes(Path path) throws IOException {
    final AtomicBoolean found = new AtomicBoolean();
    Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult postVisitDirectory(Path dirPath, IOException exc) throws IOException {
            if (exc != null) {
                throw exc;
            } else {
                try (BaseDirectoryWrapper dir = newFSDirectory(dirPath)) {
                    // don't double-checkindex
                    dir.setCheckIndexOnClose(false);
                    if (DirectoryReader.indexExists(dir)) {
                        if (VERBOSE) {
                            System.err.println("Checking index: " + dirPath);
                        }
                        // since that too risky):
                        if (SegmentInfos.getLastCommitGeneration(dir) > 1) {
                            TestUtil.checkIndex(dir);
                        }
                        found.set(true);
                    }
                }
                return FileVisitResult.CONTINUE;
            }
        }
    });
    return found.get();
}
Also used : Path(java.nio.file.Path) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BaseDirectoryWrapper(org.apache.lucene.store.BaseDirectoryWrapper) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException)

Example 24 with FileVisitResult

use of java.nio.file.FileVisitResult 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 25 with FileVisitResult

use of java.nio.file.FileVisitResult in project wildfly by wildfly.

the class JBeretSubsystemParsingTestCase method testLegacySubsystems.

@Test
public void testLegacySubsystems() throws Exception {
    // Get a list of all the logging_x_x.xml files
    final Pattern pattern = Pattern.compile("(.*-subsystem)_\\d+_\\d+\\.xml");
    // Using the CP as that's the standardSubsystemTest will use to find the config file
    final String cp = WildFlySecurityManager.getPropertyPrivileged("java.class.path", ".");
    final String[] entries = cp.split(Pattern.quote(File.pathSeparator));
    final List<String> configs = new ArrayList<>();
    for (String entry : entries) {
        final Path path = Paths.get(entry);
        if (Files.isDirectory(path)) {
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
                    final String name = file.getFileName().toString();
                    if (pattern.matcher(name).matches()) {
                        configs.add("/" + name);
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        }
    }
    // The paths shouldn't be empty
    Assert.assertFalse("No configs were found", configs.isEmpty());
    for (String configId : configs) {
        // Run the standard subsystem test, but don't compare the XML as it should never match
        standardSubsystemTest(configId, false);
    }
}
Also used : Path(java.nio.file.Path) Pattern(java.util.regex.Pattern) ArrayList(java.util.ArrayList) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Test(org.junit.Test)

Aggregations

FileVisitResult (java.nio.file.FileVisitResult)74 Path (java.nio.file.Path)67 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)66 IOException (java.io.IOException)65 File (java.io.File)10 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)8 InputStream (java.io.InputStream)4 HashSet (java.util.HashSet)4 JarEntry (java.util.jar.JarEntry)4 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 FileOutputStream (java.io.FileOutputStream)3 JarOutputStream (java.util.jar.JarOutputStream)3 PathSourcePath (com.facebook.buck.rules.PathSourcePath)2 SourcePath (com.facebook.buck.rules.SourcePath)2 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)2 FileSystemException (io.vertx.core.file.FileSystemException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileInputStream (java.io.FileInputStream)2