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;
}
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();
}
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();
}
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;
}
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);
}
}
Aggregations