List of usage examples for java.nio.file SimpleFileVisitor SimpleFileVisitor
protected SimpleFileVisitor()
From source file:com.netflix.nicobar.core.module.ScriptModuleLoader.java
/** * Add or update the existing {@link ScriptModule}s with the given script archives. * This method will convert the archives to modules and then compile + link them in to the * dependency graph. It will then recursively re-link any modules depending on the new modules. * If this loader already contains an old version of the module, it will be unloaded on * successful compile of the new module. * * @param candidateArchives archives to load or update *//* w w w. ja v a 2 s . c om*/ public synchronized void updateScriptArchives(Set<? extends ScriptArchive> candidateArchives) { Objects.requireNonNull(candidateArchives); long updateNumber = System.currentTimeMillis(); // map script module id to archive to be compiled Map<ModuleId, ScriptArchive> archivesToCompile = new HashMap<ModuleId, ScriptArchive>( candidateArchives.size() * 2); // create an updated mapping of the scriptModuleId to latest revisionId including the yet-to-be-compiled archives Map<ModuleId, ModuleIdentifier> oldRevisionIdMap = jbossModuleLoader.getLatestRevisionIds(); Map<ModuleId, ModuleIdentifier> updatedRevisionIdMap = new HashMap<ModuleId, ModuleIdentifier>( (oldRevisionIdMap.size() + candidateArchives.size()) * 2); updatedRevisionIdMap.putAll(oldRevisionIdMap); // Map of the scriptModuleId to it's updated set of dependencies Map<ModuleId, Set<ModuleId>> archiveDependencies = new HashMap<ModuleId, Set<ModuleId>>(); for (ScriptArchive scriptArchive : candidateArchives) { ModuleId scriptModuleId = scriptArchive.getModuleSpec().getModuleId(); // filter out archives that have a newer module already loaded long createTime = scriptArchive.getCreateTime(); ScriptModule scriptModule = loadedScriptModules.get(scriptModuleId); long latestCreateTime = scriptModule != null ? scriptModule.getCreateTime() : 0; if (createTime < latestCreateTime) { notifyArchiveRejected(scriptArchive, ArchiveRejectedReason.HIGHER_REVISION_AVAILABLE, null); continue; } // create the new revisionIds that should be used for the linkages when the new modules // are defined. ModuleIdentifier newRevisionId = JBossModuleUtils.createRevisionId(scriptModuleId, updateNumber); updatedRevisionIdMap.put(scriptModuleId, newRevisionId); archivesToCompile.put(scriptModuleId, scriptArchive); // create a dependency map of the incoming archives so that we can later build a candidate graph archiveDependencies.put(scriptModuleId, scriptArchive.getModuleSpec().getModuleDependencies()); } // create a dependency graph with the candidates swapped in in order to figure out the // order in which the candidates should be loaded DirectedGraph<ModuleId, DefaultEdge> candidateGraph = jbossModuleLoader.getModuleNameGraph(); GraphUtils.swapVertices(candidateGraph, archiveDependencies); // iterate over the graph in reverse dependency order Set<ModuleId> leaves = GraphUtils.getLeafVertices(candidateGraph); while (!leaves.isEmpty()) { for (ModuleId scriptModuleId : leaves) { ScriptArchive scriptArchive = archivesToCompile.get(scriptModuleId); if (scriptArchive == null) { continue; } ModuleSpec moduleSpec; ModuleIdentifier candidateRevisionId = updatedRevisionIdMap.get(scriptModuleId); Path modulePath = createModulePath(candidateRevisionId); final Path moduleCompilationRoot = compilationRootDir.resolve(modulePath); FileUtils.deleteQuietly(moduleCompilationRoot.toFile()); try { Files.createDirectories(moduleCompilationRoot); } catch (IOException ioe) { notifyArchiveRejected(scriptArchive, ArchiveRejectedReason.ARCHIVE_IO_EXCEPTION, ioe); } try { moduleSpec = createModuleSpec(scriptArchive, candidateRevisionId, updatedRevisionIdMap, moduleCompilationRoot); } catch (ModuleLoadException e) { logger.error("Exception loading archive " + scriptArchive.getModuleSpec().getModuleId(), e); notifyArchiveRejected(scriptArchive, ArchiveRejectedReason.ARCHIVE_IO_EXCEPTION, e); continue; } // load and compile the module jbossModuleLoader.addModuleSpec(moduleSpec); Module jbossModule = null; try { jbossModule = jbossModuleLoader.loadModule(candidateRevisionId); compileModule(jbossModule, moduleCompilationRoot); // Now refresh the resource loaders for this module, and load the set of // compiled classes and populate into the module's local class cache. jbossModuleLoader.rescanModule(jbossModule); final Set<String> classesToLoad = new LinkedHashSet<String>(); Files.walkFileTree(moduleCompilationRoot, new SimpleFileVisitor<Path>() { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { String relativePath = moduleCompilationRoot.relativize(file).toString(); if (relativePath.endsWith(".class")) { String className = relativePath.replaceAll("\\.class$", "").replace("\\", ".") .replace("/", "."); classesToLoad.add(className); } return FileVisitResult.CONTINUE; }; }); for (String loadClass : classesToLoad) { Class<?> loadedClass = jbossModule.getClassLoader().loadClassLocal(loadClass, true); if (loadedClass == null) throw new ScriptCompilationException("Unable to load compiled class: " + loadClass); } } catch (Exception e) { // rollback logger.error("Exception loading module " + candidateRevisionId, e); if (candidateArchives.contains(scriptArchive)) { // this spec came from a candidate archive. Send reject notification notifyArchiveRejected(scriptArchive, ArchiveRejectedReason.COMPILE_FAILURE, e); } if (jbossModule != null) { jbossModuleLoader.unloadModule(jbossModule); } continue; } // commit the change by removing the old module ModuleIdentifier oldRevisionId = oldRevisionIdMap.get(scriptModuleId); if (oldRevisionId != null) { jbossModuleLoader.unloadModule(oldRevisionId); } JBossScriptModule scriptModule = new JBossScriptModule(scriptModuleId, jbossModule, scriptArchive); ScriptModule oldModule = loadedScriptModules.put(scriptModuleId, scriptModule); notifyModuleUpdate(scriptModule, oldModule); // find dependents and add them to the to be compiled set Set<ModuleId> dependents = GraphUtils.getIncomingVertices(candidateGraph, scriptModuleId); for (ModuleId dependentScriptModuleId : dependents) { if (!archivesToCompile.containsKey(dependentScriptModuleId)) { ScriptModule dependentScriptModule = loadedScriptModules.get(dependentScriptModuleId); if (dependentScriptModule != null) { archivesToCompile.put(dependentScriptModuleId, dependentScriptModule.getSourceArchive()); ModuleIdentifier dependentRevisionId = JBossModuleUtils .createRevisionId(dependentScriptModuleId, updateNumber); updatedRevisionIdMap.put(dependentScriptModuleId, dependentRevisionId); } } } } GraphUtils.removeVertices(candidateGraph, leaves); leaves = GraphUtils.getLeafVertices(candidateGraph); } }
From source file:com.facebook.buck.io.ProjectFilesystemTest.java
@Test public void testWalkFileTreeWhenProjectRootIsWorkingDir() throws IOException { ProjectFilesystem projectFilesystem = new ProjectFilesystem(Paths.get(".").toAbsolutePath()); final ImmutableList.Builder<String> fileNames = ImmutableList.builder(); Path pathRelativeToProjectRoot = Paths .get("test/com/facebook/buck/io/testdata/directory_traversal_ignore_paths"); projectFilesystem.walkRelativeFileTree(pathRelativeToProjectRoot, new SimpleFileVisitor<Path>() { @Override/*w w w . j av a 2 s . c om*/ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { fileNames.add(file.getFileName().toString()); return FileVisitResult.CONTINUE; } }); assertThat(fileNames.build(), containsInAnyOrder("file", "a_file", "b_file", "b_c_file", "b_d_file")); }
From source file:org.hawkular.inventory.impl.tinkerpop.test.BasicTest.java
private static void deleteGraph() throws Exception { Path path = Paths.get("./", "__tinker.graph"); if (!path.toFile().exists()) { return;/*w w w. ja va2 s . c o m*/ } Files.walkFileTree(path, new SimpleFileVisitor<Path>() { @Override 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:com.searchcode.app.jobs.IndexSvnRepoJob.java
/** * Indexes all the documents in the path provided. Will also remove anything from the index if not on disk * Generally this is a slow update used only for the inital clone of a repository * NB this can be used for updates but it will be much slower as it needs to to walk the contents of the disk *//*from ww w. j a va2 s . c o m*/ public void indexDocsByPath(Path path, String repoName, String repoLocations, String repoRemoteLocation, boolean existingRepo) { SearchcodeLib scl = Singleton.getSearchCodeLib(); // Should have data object by this point List<String> fileLocations = new ArrayList<>(); Queue<CodeIndexDocument> codeIndexDocumentQueue = Singleton.getCodeIndexQueue(); // Convert once outside the main loop String fileRepoLocations = FilenameUtils.separatorsToUnix(repoLocations); boolean lowMemory = this.LOWMEMORY; try { Files.walkFileTree(path, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { while (CodeIndexer.shouldPauseAdding()) { Singleton.getLogger().info("Pausing parser."); try { Thread.sleep(SLEEPTIME); } catch (InterruptedException ex) { } } // Convert Path file to unix style that way everything is easier to reason about String fileParent = FilenameUtils.separatorsToUnix(file.getParent().toString()); String fileToString = FilenameUtils.separatorsToUnix(file.toString()); String fileName = file.getFileName().toString(); String md5Hash = Values.EMPTYSTRING; if (fileParent.endsWith("/.svn") || fileParent.contains("/.svn/")) { return FileVisitResult.CONTINUE; } List<String> codeLines; try { codeLines = Helpers.readFileLines(fileToString, MAXFILELINEDEPTH); } catch (IOException ex) { return FileVisitResult.CONTINUE; } try { FileInputStream fis = new FileInputStream(new File(fileToString)); md5Hash = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis); fis.close(); } catch (IOException ex) { Singleton.getLogger().warning("Unable to generate MD5 for " + fileToString); } // is the file minified? if (scl.isMinified(codeLines)) { Singleton.getLogger().info("Appears to be minified will not index " + fileToString); return FileVisitResult.CONTINUE; } String languageName = scl.languageGuesser(fileName, codeLines); String fileLocation = fileToString.replace(fileRepoLocations, Values.EMPTYSTRING) .replace(fileName, Values.EMPTYSTRING); String fileLocationFilename = fileToString.replace(fileRepoLocations, Values.EMPTYSTRING); String repoLocationRepoNameLocationFilename = fileToString; String newString = getBlameFilePath(fileLocationFilename); String codeOwner = getInfoExternal(codeLines.size(), repoName, fileRepoLocations, newString) .getName(); // If low memory don't add to the queue, just index it directly if (lowMemory) { CodeIndexer.indexDocument(new CodeIndexDocument(repoLocationRepoNameLocationFilename, repoName, fileName, fileLocation, fileLocationFilename, md5Hash, languageName, codeLines.size(), StringUtils.join(codeLines, " "), repoRemoteLocation, codeOwner)); } else { Singleton.incrementCodeIndexLinesCount(codeLines.size()); codeIndexDocumentQueue.add(new CodeIndexDocument(repoLocationRepoNameLocationFilename, repoName, fileName, fileLocation, fileLocationFilename, md5Hash, languageName, codeLines.size(), StringUtils.join(codeLines, " "), repoRemoteLocation, codeOwner)); } fileLocations.add(fileLocationFilename); return FileVisitResult.CONTINUE; } }); } catch (IOException ex) { Singleton.getLogger().warning("ERROR - caught a " + ex.getClass() + " in " + this.getClass() + "\n with message: " + ex.getMessage()); } if (existingRepo) { CodeSearcher cs = new CodeSearcher(); List<String> indexLocations = cs.getRepoDocuments(repoName); for (String file : indexLocations) { if (!fileLocations.contains(file)) { Singleton.getLogger().info("Missing from disk, removing from index " + file); try { CodeIndexer.deleteByFileLocationFilename(file); } catch (IOException ex) { Singleton.getLogger().warning("ERROR - caught a " + ex.getClass() + " in " + this.getClass() + "\n with message: " + ex.getMessage()); } } } } }
From source file:com.facebook.buck.util.ProjectFilesystemTest.java
@Test public void testWalkFileTreeFollowsSymlinks() throws IOException { tmp.newFolder("dir"); tmp.newFile("dir/file.txt"); java.nio.file.Files.createSymbolicLink(tmp.getRoot().toPath().resolve("linkdir"), tmp.getRoot().toPath().resolve("dir")); final ImmutableList.Builder<Path> filePaths = ImmutableList.builder(); filesystem.walkRelativeFileTree(Paths.get(""), new SimpleFileVisitor<Path>() { @Override/*from w w w .ja v a 2 s . c o m*/ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { filePaths.add(file); return FileVisitResult.CONTINUE; } }); assertThat(filePaths.build(), containsInAnyOrder(Paths.get("dir/file.txt"), Paths.get("linkdir/file.txt"))); }
From source file:org.apache.openaz.xacml.pdp.test.TestBase.java
/** * Removes all the Response* files from the results directory. *//*w ww . j av a2s . c o m*/ public void removeResults() { try { // // Determine where the results are supposed to be written to // Path resultsPath; if (this.output != null) { resultsPath = this.output; } else { resultsPath = Paths.get(this.directory.toString(), "results"); } // // Walk the files // Files.walkFileTree(resultsPath, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (file.getFileName().toString().startsWith("Response")) { Files.delete(file); } return super.visitFile(file, attrs); } }); } catch (IOException e) { logger.error("Failed to removeRequests from " + this.directory + " " + e); } }
From source file:com.facebook.buck.io.ProjectFilesystemTest.java
@Test public void testWalkFileTreeFollowsSymlinks() throws IOException { tmp.newFolder("dir"); tmp.newFile("dir/file.txt"); Files.createSymbolicLink(tmp.getRoot().resolve("linkdir"), tmp.getRoot().resolve("dir")); final ImmutableList.Builder<Path> filePaths = ImmutableList.builder(); filesystem.walkRelativeFileTree(Paths.get(""), new SimpleFileVisitor<Path>() { @Override//from ww w. j a va2 s .c o m public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { filePaths.add(file); return FileVisitResult.CONTINUE; } }); assertThat(filePaths.build(), containsInAnyOrder(Paths.get("dir/file.txt"), Paths.get("linkdir/file.txt"))); }
From source file:com.searchcode.app.jobs.IndexGitRepoJob.java
/** * Indexes all the documents in the path provided. Will also remove anything from the index if not on disk * Generally this is a slow update used only for the inital clone of a repository * NB this can be used for updates but it will be much slower as it needs to to walk the contents of the disk *//*from w ww . j a v a 2s . c om*/ public void indexDocsByPath(Path path, String repoName, String repoLocations, String repoRemoteLocation, boolean existingRepo) { SearchcodeLib scl = Singleton.getSearchCodeLib(); // Should have data object by this point List<String> fileLocations = new ArrayList<>(); Queue<CodeIndexDocument> codeIndexDocumentQueue = Singleton.getCodeIndexQueue(); // Convert once outside the main loop String fileRepoLocations = FilenameUtils.separatorsToUnix(repoLocations); boolean lowMemory = this.LOWMEMORY; boolean useSystemGit = this.USESYSTEMGIT; try { Files.walkFileTree(path, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { while (CodeIndexer.shouldPauseAdding()) { Singleton.getLogger().info("Pausing parser."); try { Thread.sleep(SLEEPTIME); } catch (InterruptedException ex) { } } // Convert Path file to unix style that way everything is easier to reason about String fileParent = FilenameUtils.separatorsToUnix(file.getParent().toString()); String fileToString = FilenameUtils.separatorsToUnix(file.toString()); String fileName = file.getFileName().toString(); String md5Hash = Values.EMPTYSTRING; if (fileParent.endsWith("/.git") || fileParent.contains("/.git/")) { return FileVisitResult.CONTINUE; } List<String> codeLines; try { codeLines = Helpers.readFileLines(fileToString, MAXFILELINEDEPTH); } catch (IOException ex) { return FileVisitResult.CONTINUE; } try { FileInputStream fis = new FileInputStream(new File(fileToString)); md5Hash = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis); fis.close(); } catch (IOException ex) { Singleton.getLogger().warning("Unable to generate MD5 for " + fileToString); } // is the file minified? if (scl.isMinified(codeLines)) { Singleton.getLogger().info("Appears to be minified will not index " + fileToString); return FileVisitResult.CONTINUE; } String languageName = scl.languageGuesser(fileName, codeLines); String fileLocation = fileToString.replace(fileRepoLocations, Values.EMPTYSTRING) .replace(fileName, Values.EMPTYSTRING); String fileLocationFilename = fileToString.replace(fileRepoLocations, Values.EMPTYSTRING); String repoLocationRepoNameLocationFilename = fileToString; String newString = getBlameFilePath(fileLocationFilename); List<CodeOwner> owners; if (useSystemGit) { owners = getBlameInfoExternal(codeLines.size(), repoName, fileRepoLocations, newString); } else { owners = getBlameInfo(codeLines.size(), repoName, fileRepoLocations, newString); } String codeOwner = scl.codeOwner(owners); // If low memory don't add to the queue, just index it directly if (lowMemory) { CodeIndexer.indexDocument(new CodeIndexDocument(repoLocationRepoNameLocationFilename, repoName, fileName, fileLocation, fileLocationFilename, md5Hash, languageName, codeLines.size(), StringUtils.join(codeLines, " "), repoRemoteLocation, codeOwner)); } else { Singleton.incrementCodeIndexLinesCount(codeLines.size()); codeIndexDocumentQueue.add(new CodeIndexDocument(repoLocationRepoNameLocationFilename, repoName, fileName, fileLocation, fileLocationFilename, md5Hash, languageName, codeLines.size(), StringUtils.join(codeLines, " "), repoRemoteLocation, codeOwner)); } fileLocations.add(fileLocationFilename); return FileVisitResult.CONTINUE; } }); } catch (IOException ex) { Singleton.getLogger().warning("ERROR - caught a " + ex.getClass() + " in " + this.getClass() + "\n with message: " + ex.getMessage()); } if (existingRepo) { CodeSearcher cs = new CodeSearcher(); List<String> indexLocations = cs.getRepoDocuments(repoName); for (String file : indexLocations) { if (!fileLocations.contains(file)) { Singleton.getLogger().info("Missing from disk, removing from index " + file); try { CodeIndexer.deleteByFileLocationFilename(file); } catch (IOException ex) { Singleton.getLogger().warning("ERROR - caught a " + ex.getClass() + " in " + this.getClass() + "\n with message: " + ex.getMessage()); } } } } }
From source file:com.facebook.buck.io.filesystem.impl.DefaultProjectFilesystemTest.java
@Test public void testWalkFileTreeWhenProjectRootIsNotWorkingDir() throws IOException { tmp.newFolder("dir"); tmp.newFile("dir/file.txt"); tmp.newFolder("dir/dir2"); tmp.newFile("dir/dir2/file2.txt"); ImmutableList.Builder<String> fileNames = ImmutableList.builder(); filesystem.walkRelativeFileTree(Paths.get("dir"), new SimpleFileVisitor<Path>() { @Override//from www .j a v a 2 s. c o m public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { fileNames.add(file.getFileName().toString()); return FileVisitResult.CONTINUE; } }); assertThat(fileNames.build(), containsInAnyOrder("file.txt", "file2.txt")); }
From source file:org.roda.core.common.RodaUtils.java
/** * @deprecated 20160907 hsilva: not seeing any method using it, so it will be * removed soon/*from w ww . j a va2 s.com*/ */ @Deprecated public static long getPathSize(Path startPath) throws IOException { final AtomicLong size = new AtomicLong(0); Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { size.addAndGet(attrs.size()); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { // Skip folders that can't be traversed return FileVisitResult.CONTINUE; } }); return size.get(); }