use of java.nio.file.FileVisitResult 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();
}
use of java.nio.file.FileVisitResult in project bazel by bazelbuild.
the class VanillaJavaBuilder method getSources.
/** Returns the sources to compile, including any source jar entries. */
private ImmutableList<JavaFileObject> getSources(OptionsParser optionsParser, StandardJavaFileManager fileManager) throws IOException {
final ImmutableList.Builder<JavaFileObject> sources = ImmutableList.builder();
sources.addAll(fileManager.getJavaFileObjectsFromStrings(optionsParser.getSourceFiles()));
for (String sourceJar : optionsParser.getSourceJars()) {
for (final Path root : getJarFileSystem(Paths.get(sourceJar)).getRootDirectories()) {
Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
if (path.getFileName().toString().endsWith(".java")) {
sources.add(new SourceJarFileObject(root, path));
}
return FileVisitResult.CONTINUE;
}
});
}
}
return sources.build();
}
use of java.nio.file.FileVisitResult in project bazel by bazelbuild.
the class SimpleJavaLibraryBuilder method buildJar.
public void buildJar(JavaLibraryBuildRequest build) throws IOException {
JarCreator jar = new JarCreator(build.getOutputJar());
try {
jar.setNormalize(true);
jar.setCompression(build.compressJar());
for (String resourceJar : build.getResourceJars()) {
for (Path root : getJarFileSystem(Paths.get(resourceJar)).getRootDirectories()) {
Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
// TODO(b/28452451): omit directories entries from jar files
if (dir.getNameCount() > 0) {
jar.addEntry(root.relativize(dir).toString(), dir);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
jar.addEntry(root.relativize(path).toString(), path);
return FileVisitResult.CONTINUE;
}
});
}
}
jar.addDirectory(build.getClassDir());
jar.addRootEntries(build.getRootResourceFiles());
addResourceEntries(jar, build.getResourceFiles());
addMessageEntries(jar, build.getMessageFiles());
} finally {
jar.execute();
}
}
use of java.nio.file.FileVisitResult in project bazel by bazelbuild.
the class JavacTurbineTest method compileLib.
private void compileLib(Path jar, Collection<Path> classpath, Iterable<? extends JavaFileObject> units) throws IOException {
final Path outdir = temp.newFolder().toPath();
JavacFileManager fm = new JavacFileManager(new Context(), false, UTF_8);
fm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT, Collections.singleton(outdir));
fm.setLocationFromPaths(StandardLocation.CLASS_PATH, classpath);
List<String> options = Arrays.asList("-d", outdir.toString());
JavacTool tool = JavacTool.create();
JavacTask task = tool.getTask(new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true), fm, null, options, null, units);
assertThat(task.call()).isTrue();
try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(jar))) {
Files.walkFileTree(outdir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
JarEntry je = new JarEntry(outdir.relativize(path).toString());
jos.putNextEntry(je);
Files.copy(path, jos);
return FileVisitResult.CONTINUE;
}
});
}
}
use of java.nio.file.FileVisitResult in project cryptomator by cryptomator.
the class UpgradeVersion4to5 method upgrade.
@Override
protected void upgrade(Vault vault, Cryptor cryptor) throws UpgradeFailedException {
Path dataDir = vault.getPath().resolve("d");
if (!Files.isDirectory(dataDir)) {
// empty vault. no migration needed.
return;
}
try {
Files.walkFileTree(dataDir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (BASE32_PATTERN.matcher(file.getFileName().toString()).find() && attrs.size() > cryptor.fileHeaderCryptor().headerSize()) {
migrate(file, attrs, cryptor);
} else {
LOG.info("Skipping irrelevant file {}.", file);
}
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
LOG.error("Migration failed.", e);
throw new UpgradeFailedException(localization.getString("upgrade.version4to5.err.io"));
}
LOG.info("Migration finished.");
}
Aggregations