Search in sources :

Example 31 with FileVisitResult

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();
}
Also used : Path(java.nio.file.Path) PathMatcher(java.nio.file.PathMatcher) ArrayList(java.util.ArrayList) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 32 with FileVisitResult

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();
}
Also used : Path(java.nio.file.Path) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) ImmutableList(com.google.common.collect.ImmutableList) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 33 with FileVisitResult

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();
    }
}
Also used : Path(java.nio.file.Path) JarCreator(com.google.devtools.build.buildjar.jarhelper.JarCreator) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 34 with FileVisitResult

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;
            }
        });
    }
}
Also used : Path(java.nio.file.Path) Context(com.sun.tools.javac.util.Context) JavacTool(com.sun.tools.javac.api.JavacTool) JarOutputStream(java.util.jar.JarOutputStream) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) BufferedWriter(java.io.BufferedWriter) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) OutputStreamWriter(java.io.OutputStreamWriter) JavacTask(com.sun.source.util.JavacTask) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) PrintWriter(java.io.PrintWriter)

Example 35 with FileVisitResult

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.");
}
Also used : Path(java.nio.file.Path) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

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