use of java.nio.file.PathMatcher in project beam by apache.
the class LocalFileSystem method matchOne.
private MatchResult matchOne(String spec) throws IOException {
File file = Paths.get(spec).toFile();
if (file.exists()) {
return MatchResult.create(Status.OK, ImmutableList.of(toMetadata(file)));
}
File parent = file.getAbsoluteFile().getParentFile();
if (!parent.exists()) {
return MatchResult.create(Status.NOT_FOUND, Collections.<Metadata>emptyList());
}
// Method getAbsolutePath() on Windows platform may return something like
// "c:\temp\file.txt". FileSystem.getPathMatcher() call below will treat
// '\' (backslash) as an escape character, instead of a directory
// separator. Replacing backslash with double-backslash solves the problem.
// We perform the replacement on all platforms, even those that allow
// backslash as a part of the filename, because Globs.toRegexPattern will
// eat one backslash.
String pathToMatch = file.getAbsolutePath().replaceAll(Matcher.quoteReplacement("\\"), Matcher.quoteReplacement("\\\\"));
final PathMatcher matcher = java.nio.file.FileSystems.getDefault().getPathMatcher("glob:" + pathToMatch);
// TODO: Avoid iterating all files: https://issues.apache.org/jira/browse/BEAM-1309
Iterable<File> files = com.google.common.io.Files.fileTreeTraverser().preOrderTraversal(parent);
Iterable<File> matchedFiles = Iterables.filter(files, Predicates.and(com.google.common.io.Files.isFile(), new Predicate<File>() {
@Override
public boolean apply(File input) {
return matcher.matches(input.toPath());
}
}));
List<Metadata> result = Lists.newLinkedList();
for (File match : matchedFiles) {
result.add(toMetadata(match));
}
if (result.isEmpty()) {
// TODO: consider to return Status.OK for globs.
return MatchResult.create(Status.NOT_FOUND, new FileNotFoundException(String.format("No files found for spec: %s.", spec)));
} else {
return MatchResult.create(Status.OK, result);
}
}
use of java.nio.file.PathMatcher 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.PathMatcher in project heron by twitter.
the class TopologyUtils method lookUpTopologyDefnFile.
public static String lookUpTopologyDefnFile(String dir, String filename) {
String pattern = String.format("glob:%s/%s.defn", dir, filename);
PathMatcher matcher = FileSystems.getDefault().getPathMatcher(pattern);
for (File file : new File(dir).listFiles()) {
if (matcher.matches(file.toPath())) {
// We would return the first one matched
return file.getPath();
}
}
throw new IllegalStateException("Failed to find topology defn file");
}
use of java.nio.file.PathMatcher in project google-cloud-java by GoogleCloudPlatform.
the class CloudStorageFileSystemTest method testMatcher.
@Test
public void testMatcher() throws IOException {
try (FileSystem fs = CloudStorageFileSystem.forBucket("bucket")) {
String pattern1 = "glob:*.java";
PathMatcher javaFileMatcher = fs.getPathMatcher(pattern1);
assertMatches(fs, javaFileMatcher, "a.java", true);
assertMatches(fs, javaFileMatcher, "a.text", false);
assertMatches(fs, javaFileMatcher, "folder/c.java", true);
assertMatches(fs, javaFileMatcher, "d", false);
String pattern2 = "glob:*.{java,text}";
PathMatcher javaAndTextFileMatcher = fs.getPathMatcher(pattern2);
assertMatches(fs, javaAndTextFileMatcher, "a.java", true);
assertMatches(fs, javaAndTextFileMatcher, "a.text", true);
assertMatches(fs, javaAndTextFileMatcher, "folder/c.java", true);
assertMatches(fs, javaAndTextFileMatcher, "d", false);
}
}
use of java.nio.file.PathMatcher in project cdap by caskdata.
the class SparkPackageUtils method getSpark1AssemblyJar.
/**
* Locates the spark-assembly jar from the local file system for Spark1.
*
* @param sparkLibrary file path to the spark-assembly jar in the local file system
* @param sparkHome file path to the spark home.
*
* @return the spark-assembly jar location
* @throws IllegalStateException if cannot locate the spark assembly jar
*/
private static File getSpark1AssemblyJar(@Nullable String sparkLibrary, String sparkHome) {
if (sparkLibrary != null) {
return new File(sparkLibrary);
}
// Look for spark-assembly.jar symlink
Path assemblyJar = Paths.get(sparkHome, "lib", "spark-assembly.jar");
if (Files.isSymbolicLink(assemblyJar)) {
return assemblyJar.toFile();
}
// No symbolic link exists. Search for spark-assembly*.jar in the lib directory
final List<Path> jar = new ArrayList<>(1);
Path sparkLib = Paths.get(sparkHome, "lib");
final PathMatcher pathMatcher = sparkLib.getFileSystem().getPathMatcher("glob:spark-assembly*.jar");
try {
Files.walkFileTree(sparkLib, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
// Take the first file match
if (attrs.isRegularFile() && pathMatcher.matches(file.getFileName())) {
jar.add(file);
return FileVisitResult.TERMINATE;
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
// Ignore error
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
// Just log, don't throw.
// If we already located the Spark Assembly jar during visiting, we can still use the jar.
LOG.warn("Exception raised while inspecting {}", sparkLib, e);
}
Preconditions.checkState(!jar.isEmpty(), "Failed to locate Spark library from %s", sparkHome);
assemblyJar = jar.get(0);
LOG.debug("Located Spark Assembly JAR in {}", assemblyJar);
return assemblyJar.toFile();
}
Aggregations