List of usage examples for java.nio.file Files walkFileTree
public static Path walkFileTree(Path start, FileVisitor<? super Path> visitor) throws IOException
From source file:org.talend.dataprep.cache.file.FileSystemContentCacheTest.java
@Test public void testJanitorEvictionPeriod() throws Exception { // given some cache entries List<ContentCacheKey> keys = new ArrayList<>(); for (int i = 0; i < 10; i++) { keys.add(new DummyCacheKey("janitor me " + i + 1)); }/*from w w w . ja v a2s . c o m*/ for (ContentCacheKey key : keys) { addCacheEntry(key, "janitor content", ContentCache.TimeToLive.IMMEDIATE); Assert.assertThat(cache.has(key), is(true)); } // when eviction is performed and the janitor is called janitor.janitor(); // then, none of the cache entries should be removed for (ContentCacheKey key : keys) { Assert.assertThat(cache.has(key), is(true)); } Thread.sleep(ContentCache.TimeToLive.IMMEDIATE.getTime() + 500); // then, none of the cache entries should be removed for (ContentCacheKey key : keys) { Assert.assertThat(cache.has(key), is(false)); } // when eviction is performed and the janitor is called janitor.janitor(); Files.walkFileTree(Paths.get(TEST_DIRECTORY), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (!StringUtils.contains(file.toFile().getName(), ".nfs")) { Assert.fail("file " + file + " was not cleaned by the janitor"); } return super.visitFile(file, attrs); } }); }
From source file:org.apache.beam.sdk.io.TextIOTest.java
License:asdf
@AfterClass public static void teardownClass() throws IOException { Files.walkFileTree(tempFolder, new SimpleFileVisitor<Path>() { @Override//from w ww. j a va 2s . c om public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; } }); }
From source file:org.elasticsearch.bootstrap.JarHell.java
/** * Checks the set of URLs for duplicate classes * @throws IllegalStateException if jar hell was found *///w w w. j a v a 2s.co m @SuppressForbidden(reason = "needs JarFile for speed, just reading entries") public static void checkJarHell(URL urls[]) throws Exception { ESLogger logger = Loggers.getLogger(JarHell.class); // we don't try to be sneaky and use deprecated/internal/not portable stuff // like sun.boot.class.path, and with jigsaw we don't yet have a way to get // a "list" at all. So just exclude any elements underneath the java home String javaHome = System.getProperty("java.home"); logger.debug("java.home: {}", javaHome); final Map<String, Path> clazzes = new HashMap<>(32768); Set<Path> seenJars = new HashSet<>(); for (final URL url : urls) { final Path path = PathUtils.get(url.toURI()); // exclude system resources if (path.startsWith(javaHome)) { logger.debug("excluding system resource: {}", path); continue; } if (path.toString().endsWith(".jar")) { if (!seenJars.add(path)) { logger.debug("excluding duplicate classpath element: {}", path); continue; // we can't fail because of sheistiness with joda-time } logger.debug("examining jar: {}", path); try (JarFile file = new JarFile(path.toString())) { Manifest manifest = file.getManifest(); if (manifest != null) { checkManifest(manifest, path); } // inspect entries Enumeration<JarEntry> elements = file.entries(); while (elements.hasMoreElements()) { String entry = elements.nextElement().getName(); if (entry.endsWith(".class")) { // for jar format, the separator is defined as / entry = entry.replace('/', '.').substring(0, entry.length() - 6); checkClass(clazzes, entry, path); } } } } else { logger.debug("examining directory: {}", path); // case for tests: where we have class files in the classpath final Path root = PathUtils.get(url.toURI()); final String sep = root.getFileSystem().getSeparator(); Files.walkFileTree(root, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { String entry = root.relativize(file).toString(); if (entry.endsWith(".class")) { // normalize with the os separator entry = entry.replace(sep, ".").substring(0, entry.length() - 6); checkClass(clazzes, entry, path); } return super.visitFile(file, attrs); } }); } } }
From source file:org.batfish.common.plugin.PluginConsumer.java
protected final void loadPlugins() { for (Path pluginDir : _pluginDirs) { if (Files.exists(pluginDir)) { try { Files.walkFileTree(pluginDir, new SimpleFileVisitor<Path>() { @Override/* ww w . java 2s . com*/ public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { loadPluginJar(path); return FileVisitResult.CONTINUE; } }); } catch (IOException e) { throw new BatfishException("Error walking through plugin dir: '" + pluginDir.toString() + "'", e); } } } }
From source file:org.cryptomator.ui.InitializeController.java
private void encryptExistingContents() { try {// w w w. java 2 s . co m final FileVisitor<Path> visitor = new EncryptingFileVisitor(directory.getPath(), directory.getCryptor(), this::shouldEncryptExistingFile); Files.walkFileTree(directory.getPath(), visitor); } catch (IOException ex) { LOG.error("I/O Exception", ex); } }
From source file:com.ejisto.util.IOUtils.java
public static Collection<String> findAllClassesInJarFile(File jarFile) throws IOException { final List<String> ret = new ArrayList<>(); Map<String, String> env = new HashMap<>(); env.put("create", "false"); try (FileSystem targetFs = FileSystems.newFileSystem(URI.create("jar:file:" + jarFile.getAbsolutePath()), env)) {//from ww w . j a v a 2s . co m final Path root = targetFs.getPath("/"); Files.walkFileTree(root, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { String ext = ".class"; if (attrs.isRegularFile() && file.getFileName().toString().endsWith(ext)) { String path = root.relativize(file).toString(); ret.add(translatePath(path.substring(0, path.length() - ext.length()), "/")); } return FileVisitResult.CONTINUE; } }); } return ret; }
From source file:com.sastix.cms.server.services.content.HashedDirectoryServiceTest.java
private int listRecursive(Path path) throws IOException { final int[] counter = { 0 }; Files.walkFileTree(path, new SimpleFileVisitor<Path>() { @Override//from w ww . j a v a 2 s . com public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { counter[0]++; LOGGER.trace("Listing: [" + counter[0] + "]\t" + file.getParent() + "/" + file.getFileName()); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { return FileVisitResult.CONTINUE; } }); return counter[0]; }
From source file:org.structr.web.maintenance.DirectFileImportCommand.java
@Override public void execute(final Map<String, Object> attributes) throws FrameworkException { indexer = StructrApp.getInstance(securityContext).getFulltextIndexer(); final String sourcePath = getParameterValueAsString(attributes, "source", null); final String modeString = getParameterValueAsString(attributes, "mode", Mode.COPY.name()).toUpperCase(); final String existingString = getParameterValueAsString(attributes, "existing", Existing.SKIP.name()) .toUpperCase();/*from ww w. java 2s. c o m*/ final boolean doIndex = Boolean .parseBoolean(getParameterValueAsString(attributes, "index", Boolean.TRUE.toString())); if (StringUtils.isBlank(sourcePath)) { throw new FrameworkException(422, "Please provide 'source' attribute for deployment source directory path."); } if (!EnumUtils.isValidEnum(Mode.class, modeString)) { throw new FrameworkException(422, "Unknown value for 'mode' attribute. Valid values are: copy, move"); } if (!EnumUtils.isValidEnum(Existing.class, existingString)) { throw new FrameworkException(422, "Unknown value for 'existing' attribute. Valid values are: skip, overwrite, rename"); } // use actual enums final Existing existing = Existing.valueOf(existingString); final Mode mode = Mode.valueOf(modeString); final List<Path> paths = new ArrayList<>(); if (sourcePath.contains(PathHelper.PATH_SEP)) { final String folderPart = PathHelper.getFolderPath(sourcePath); final String namePart = PathHelper.getName(sourcePath); if (StringUtils.isNotBlank(folderPart)) { final Path source = Paths.get(folderPart); if (!Files.exists(source)) { throw new FrameworkException(422, "Source path " + sourcePath + " does not exist."); } if (!Files.isDirectory(source)) { throw new FrameworkException(422, "Source path " + sourcePath + " is not a directory."); } try { try (final DirectoryStream<Path> stream = Files.newDirectoryStream(source, namePart)) { for (final Path entry : stream) { paths.add(entry); } } catch (final DirectoryIteratorException ex) { throw ex.getCause(); } } catch (final IOException ioex) { throw new FrameworkException(422, "Unable to parse source path " + sourcePath + "."); } } } else { // Relative path final Path source = Paths.get(Settings.BasePath.getValue()).resolve(sourcePath); if (!Files.exists(source)) { throw new FrameworkException(422, "Source path " + sourcePath + " does not exist."); } paths.add(source); } final SecurityContext ctx = SecurityContext.getSuperUserInstance(); final App app = StructrApp.getInstance(ctx); String targetPath = getParameterValueAsString(attributes, "target", "/"); Folder targetFolder = null; ctx.setDoTransactionNotifications(false); if (StringUtils.isNotBlank(targetPath) && !("/".equals(targetPath))) { try (final Tx tx = app.tx()) { targetFolder = app.nodeQuery(Folder.class).and(StructrApp.key(Folder.class, "path"), targetPath) .getFirst(); if (targetFolder == null) { throw new FrameworkException(422, "Target path " + targetPath + " does not exist."); } tx.success(); } } String msg = "Starting direct file import from source directory " + sourcePath + " into target path " + targetPath; logger.info(msg); publishProgressMessage(msg); paths.forEach((path) -> { try { final String newTargetPath; // If path is a directory, create it and use it as the new target folder if (Files.isDirectory(path)) { Path parentPath = path.getParent(); if (parentPath == null) { parentPath = path; } createFileOrFolder(ctx, app, parentPath, path, Files.readAttributes(path, BasicFileAttributes.class), sourcePath, targetPath, mode, existing, doIndex); newTargetPath = targetPath + PathHelper.PATH_SEP + PathHelper.clean(path.getFileName().toString()); } else { newTargetPath = targetPath; } Files.walkFileTree(path, new FileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException { return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { return createFileOrFolder(ctx, app, path, file, attrs, sourcePath, newTargetPath, mode, existing, doIndex); } @Override public FileVisitResult visitFileFailed(final Path file, final IOException exc) throws IOException { return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException { return FileVisitResult.CONTINUE; } }); } catch (final IOException ex) { logger.debug("Mode: " + modeString + ", path: " + sourcePath, ex); } }); msg = "Finished direct file import from source directory " + sourcePath + ". Imported " + folderCount + " folders and " + fileCount + " files."; logger.info(msg); publishProgressMessage(msg); }
From source file:org.talend.dataprep.cache.file.FileSystemContentCache.java
@Override @Timed/*w w w .ja v a2 s . c o m*/ public void evict(ContentCacheKey key) { final Path path = computeEntryPath(key, null); final Path parent = path.getParent(); // defensive programming if (!parent.toFile().exists()) { return; } try { final String keyStr = key.getKey(); final BiConsumer<Path, String> evictKey = getEvictionConsumer( (entryKey) -> StringUtils.startsWith(entryKey, keyStr)); final boolean skipPermanentEntries = false; Files.walkFileTree(Paths.get(location), new FileSystemVisitor(evictKey, skipPermanentEntries)); } catch (IOException e) { LOGGER.error("Unable to evict.", e); } LOGGER.debug("[{}] Evict.", key); }