Search in sources :

Example 66 with FileVisitResult

use of java.nio.file.FileVisitResult in project streamsx.topology by IBMStreams.

the class ToolkitRemoteContext method createJarFile.

/**
     * Create a jar file from a classes directory,
     * creating it directly in the toolkit.
     */
private static String createJarFile(String classes, String name, File toolkitLib) throws IOException {
    assert name.endsWith(".jar");
    final Path classesPath = Paths.get(classes);
    final Path jarPath = new File(toolkitLib, name).toPath();
    try (final JarOutputStream jarOut = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(jarPath.toFile()), 128 * 1024))) {
        Files.walkFileTree(classesPath, new FileVisitor<Path>() {

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                File classFile = file.toFile();
                if (classFile.isFile()) {
                    //  Write the entry followed by the data.
                    Path relativePath = classesPath.relativize(file);
                    JarEntry je = new JarEntry(relativePath.toString());
                    je.setTime(classFile.lastModified());
                    jarOut.putNextEntry(je);
                    final byte[] data = new byte[32 * 1024];
                    try (final BufferedInputStream classIn = new BufferedInputStream(new FileInputStream(classFile), data.length)) {
                        for (; ; ) {
                            int count = classIn.read(data);
                            if (count == -1)
                                break;
                            jarOut.write(data, 0, count);
                        }
                    }
                    jarOut.closeEntry();
                }
                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 {
                dir.toFile().delete();
                return FileVisitResult.CONTINUE;
            }
        });
    }
    return jarPath.getFileName().toString();
}
Also used : Path(java.nio.file.Path) JarOutputStream(java.util.jar.JarOutputStream) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 67 with FileVisitResult

use of java.nio.file.FileVisitResult in project streamsx.topology by IBMStreams.

the class ToolkitRemoteContext method copyDirectory.

/**
     * Copy srcDir's children, recursively, to dstDir.  dstDir is created
     * if necessary.  Any existing children in dstDir are overwritten.
     * @param srcDir
     * @param dstDir
     */
private static void copyDirectory(File srcDir, File dstDir) throws IOException {
    final Path targetPath = dstDir.toPath();
    final Path sourcePath = srcDir.toPath();
    Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException {
            Files.createDirectories(targetPath.resolve(sourcePath.relativize(dir)));
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
            Files.copy(file, targetPath.resolve(sourcePath.relativize(file)), StandardCopyOption.REPLACE_EXISTING);
            return FileVisitResult.CONTINUE;
        }
    });
}
Also used : Path(java.nio.file.Path) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 68 with FileVisitResult

use of java.nio.file.FileVisitResult in project logging-log4j2 by apache.

the class Log4j1ConfigurationConverter method run.

private void run() {
    if (cla.getRecurseIntoPath() != null) {
        final AtomicInteger countOKs = new AtomicInteger();
        final AtomicInteger countFails = new AtomicInteger();
        try {
            Files.walkFileTree(cla.getRecurseIntoPath(), new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
                    if (cla.getPathIn() == null || file.getFileName().equals(cla.getPathIn())) {
                        verbose("Reading %s", file);
                        String newFile = file.getFileName().toString();
                        final int lastIndex = newFile.lastIndexOf(".");
                        newFile = lastIndex < 0 ? newFile + FILE_EXT_XML : newFile.substring(0, lastIndex) + FILE_EXT_XML;
                        final Path resolved = file.resolveSibling(newFile);
                        try (final InputStream input = new InputStreamWrapper(Files.newInputStream(file), file.toString());
                            final OutputStream output = Files.newOutputStream(resolved)) {
                            try {
                                convert(input, output);
                                countOKs.incrementAndGet();
                            } catch (ConfigurationException | IOException e) {
                                countFails.incrementAndGet();
                                if (cla.isFailFast()) {
                                    throw e;
                                }
                                e.printStackTrace();
                            }
                            verbose("Wrote %s", resolved);
                        }
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (final IOException e) {
            throw new ConfigurationException(e);
        } finally {
            verbose("OK = %,d, Failures = %,d, Total = %,d", countOKs.get(), countFails.get(), countOKs.get() + countFails.get());
        }
    } else {
        verbose("Reading %s", cla.getPathIn());
        try (final InputStream input = getInputStream();
            final OutputStream output = getOutputStream()) {
            convert(input, output);
        } catch (final IOException e) {
            throw new ConfigurationException(e);
        }
        verbose("Wrote %s", cla.getPathOut());
    }
}
Also used : Path(java.nio.file.Path) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConfigurationException(org.apache.logging.log4j.core.config.ConfigurationException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 69 with FileVisitResult

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

the class ZkCopier method uploadToZK.

public static void uploadToZK(SolrZkClient zkClient, final Path fromPath, final String zkPath, final Pattern filenameExclusions) throws IOException {
    String path = fromPath.toString();
    if (path.endsWith("*")) {
        path = path.substring(0, path.length() - 1);
    }
    final Path rootPath = Paths.get(path);
    if (!Files.exists(rootPath))
        throw new IOException("Path " + rootPath + " does not exist");
    Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            String filename = file.getFileName().toString();
            if (filenameExclusions != null && filenameExclusions.matcher(filename).matches()) {
                log.info("uploadToZK skipping '{}' due to filenameExclusions '{}'", filename, filenameExclusions);
                return FileVisitResult.CONTINUE;
            }
            String zkNode = createZkNodeName(zkPath, rootPath, file);
            try {
                // if the path exists (and presumably we're uploading data to it) just set its data
                if (file.toFile().getName().equals(ZKNODE_DATA_FILE) && zkClient.exists(zkNode, true)) {
                    zkClient.setData(zkNode, file.toFile(), true);
                } else {
                    zkClient.makePath(zkNode, file.toFile(), false, true);
                }
            } catch (KeeperException | InterruptedException e) {
                throw new IOException("Error uploading file " + file.toString() + " to zookeeper path " + zkNode, SolrZkClient.checkInterrupted(e));
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            if (dir.getFileName().toString().startsWith("."))
                return FileVisitResult.SKIP_SUBTREE;
            return FileVisitResult.CONTINUE;
        }
    });
}
Also used : Path(java.nio.file.Path) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 70 with FileVisitResult

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

the class ClientSideTeleporter method embedClassesDirectory.

/** Embeds every class found in the given directory
     * 
     * @throws IOException */
public void embedClassesDirectory(File classesDirectory) throws IOException, ClassNotFoundException {
    final Path start = classesDirectory.toPath();
    Files.walkFileTree(start, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (file.getFileName().toString().endsWith(".class")) {
                String className = start.relativize(file).toString().replace(file.getFileSystem().getSeparator(), ".");
                // strip off extension
                className = className.substring(0, className.length() - 6);
                try {
                    Class<?> clazz = this.getClass().getClassLoader().loadClass(className);
                    embedClass(clazz);
                } catch (ClassNotFoundException e) {
                    throw new IOException("Could not load class with name '" + className + "'", e);
                }
            }
            return FileVisitResult.CONTINUE;
        }
    });
}
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