List of usage examples for java.nio.file FileVisitResult SKIP_SUBTREE
FileVisitResult SKIP_SUBTREE
To view the source code for java.nio.file FileVisitResult SKIP_SUBTREE.
Click Source Link
From source file:io.fabric8.docker.client.impl.BuildImage.java
@Override public OutputHandle fromFolder(String path) { try {/*from ww w . ja v a 2 s.co m*/ final Path root = Paths.get(path); final Path dockerIgnore = root.resolve(DOCKER_IGNORE); final List<String> ignorePatterns = new ArrayList<>(); if (dockerIgnore.toFile().exists()) { for (String p : Files.readAllLines(dockerIgnore, UTF_8)) { ignorePatterns.add(path.endsWith(File.separator) ? path + p : path + File.separator + p); } } final DockerIgnorePathMatcher dockerIgnorePathMatcher = new DockerIgnorePathMatcher(ignorePatterns); File tempFile = Files.createTempFile(Paths.get(DEFAULT_TEMP_DIR), DOCKER_PREFIX, BZIP2_SUFFIX).toFile(); try (FileOutputStream fout = new FileOutputStream(tempFile); BufferedOutputStream bout = new BufferedOutputStream(fout); BZip2CompressorOutputStream bzout = new BZip2CompressorOutputStream(bout); final TarArchiveOutputStream tout = new TarArchiveOutputStream(bzout)) { Files.walkFileTree(root, new SimpleFileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { if (dockerIgnorePathMatcher.matches(dir)) { return FileVisitResult.SKIP_SUBTREE; } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (dockerIgnorePathMatcher.matches(file)) { return FileVisitResult.SKIP_SUBTREE; } final Path relativePath = root.relativize(file); final TarArchiveEntry entry = new TarArchiveEntry(file.toFile()); entry.setName(relativePath.toString()); entry.setMode(TarArchiveEntry.DEFAULT_FILE_MODE); entry.setSize(attrs.size()); tout.putArchiveEntry(entry); Files.copy(file, tout); tout.closeArchiveEntry(); return FileVisitResult.CONTINUE; } }); fout.flush(); } return fromTar(tempFile.getAbsolutePath()); } catch (IOException e) { throw DockerClientException.launderThrowable(e); } }
From source file:org.eclipse.winery.repository.importing.CSARImporter.java
/** * Import an extracted CSAR from a directory * /*from w w w .j av a2s . c om*/ * @param path the root path of an extracted CSAR file * @param overwrite if true: contents of the repo are overwritten * @param asyncWPDParsing true if WPD should be parsed asynchronously to speed up the import. * Required, because JUnit terminates the used ExecutorService * @throws InvalidCSARException * @throws IOException */ void importFromDir(final Path path, final List<String> errors, final boolean overwrite, final boolean asyncWPDParsing) throws IOException { Path toscaMetaPath = path.resolve("xml/TOSCA-Metadata/TOSCA.meta"); if (!Files.exists(toscaMetaPath)) { toscaMetaPath = path.resolve("TOSCA-Metadata/TOSCA.meta"); } if (!Files.exists(toscaMetaPath)) { errors.add("TOSCA.meta does not exist"); return; } final TOSCAMetaFileParser tmfp = new TOSCAMetaFileParser(); final TOSCAMetaFile tmf = tmfp.parse(toscaMetaPath); // we do NOT do any sanity checks, of TOSAC.meta // and just start parsing if (tmf.getEntryDefinitions() != null) { // we obey the entry definitions and "just" import that // imported definitions are added recursively Path defsPath = path.resolve(tmf.getEntryDefinitions()); this.importDefinitions(tmf, defsPath, errors, overwrite, asyncWPDParsing); this.importSelfServiceMetaData(tmf, path, defsPath, errors); } else { // no explicit entry definitions found // we import all available definitions // The specification says (cos01, Section 16.1, line 2935) that all definitions are contained // in the "Definitions" directory // The alternative is to go through all entries in the TOSCA Meta File, but there is no // guarantee that this list is complete Path definitionsDir = path.resolve("Definitions"); if (!Files.exists(definitionsDir)) { errors.add("No entry definitions defined and Definitions directory does not exist."); return; } final List<IOException> exceptions = new ArrayList<IOException>(); Files.walkFileTree(definitionsDir, new SimpleFileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { if (dir.endsWith("Definitions")) { return FileVisitResult.CONTINUE; } else { return FileVisitResult.SKIP_SUBTREE; } } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { try { CSARImporter.this.importDefinitions(tmf, file, errors, overwrite, asyncWPDParsing); } catch (IOException e) { exceptions.add(e); return FileVisitResult.TERMINATE; } return FileVisitResult.CONTINUE; } }); if (!exceptions.isEmpty()) { // something went wrong during parsing // we rethrow the exception throw exceptions.get(0); } } this.importNamespacePrefixes(path); }
From source file:com.cloudbees.clickstack.util.Files2.java
public static void unzip(@Nonnull Path zipFile, @Nonnull final Path destDir) throws RuntimeIOException { try {//from w ww. ja va2 s . c o m //if the destination doesn't exist, create it if (Files.notExists(destDir)) { logger.trace("Create dir: {}", destDir); Files.createDirectories(destDir); } try (FileSystem zipFileSystem = createZipFileSystem(zipFile, false)) { final Path root = zipFileSystem.getPath("/"); //walk the zip file tree and copy files to the destination Files.walkFileTree(root, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { try { final Path destFile = Paths.get(destDir.toString(), file.toString()); logger.trace("Extract file {} to {}", file, destDir); Files.copy(file, destFile, StandardCopyOption.REPLACE_EXISTING); } catch (IOException | RuntimeException e) { logger.warn("Exception copying file '" + file + "' to '" + destDir + "', ignore file", e); } return FileVisitResult.CONTINUE; } @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { final Path dirToCreate = Paths.get(destDir.toString(), dir.toString()); if (Files.notExists(dirToCreate)) { logger.trace("Create dir {}", dirToCreate); try { Files.createDirectory(dirToCreate); } catch (IOException e) { logger.warn("Exception creating directory '" + dirToCreate + "' for '" + dir + "', ignore dir subtree", e); return FileVisitResult.SKIP_SUBTREE; } } return FileVisitResult.CONTINUE; } }); } } catch (IOException e) { throw new RuntimeIOException("Exception expanding " + zipFile + " to " + destDir, e); } }
From source file:org.schedulesdirect.grabber.Grabber.java
private void loadSeriesInfoIds(Path root) throws IOException { Files.walkFileTree(root, new FileVisitor<Path>() { @Override/*ww w. java2 s. co m*/ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { return FileVisitResult.SKIP_SUBTREE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { String name = file.getFileName().toString(); name = name.substring(0, name.lastIndexOf('.')); cachedSeriesIds.add(name); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { throw exc; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { if (exc != null) throw exc; return FileVisitResult.CONTINUE; } }); }
From source file:org.apache.nifi.controller.repository.FileSystemRepository.java
private synchronized void initializeRepository() throws IOException { final Map<String, Path> realPathMap = new HashMap<>(); final ExecutorService executor = Executors.newFixedThreadPool(containers.size()); final List<Future<Long>> futures = new ArrayList<>(); // Run through each of the containers. For each container, create the sections if necessary. // Then, we need to scan through the archived data so that we can determine what the oldest // archived data is, so that we know when we have to start aging data off. for (final Map.Entry<String, Path> container : containers.entrySet()) { final String containerName = container.getKey(); final ContainerState containerState = containerStateMap.get(containerName); final Path containerPath = container.getValue(); final boolean pathExists = Files.exists(containerPath); final Path realPath; if (pathExists) { realPath = containerPath.toRealPath(); } else {/* w w w . j a v a2s.com*/ realPath = Files.createDirectories(containerPath).toRealPath(); } for (int i = 0; i < SECTIONS_PER_CONTAINER; i++) { Files.createDirectories(realPath.resolve(String.valueOf(i))); } realPathMap.put(containerName, realPath); // We need to scan the archive directories to find out the oldest timestamp so that know whether or not we // will have to delete archived data based on time threshold. Scanning all of the directories can be very // expensive because of all of the disk accesses. So we do this in multiple threads. Since containers are // often unique to a disk, we just map 1 thread to each container. final Callable<Long> scanContainer = new Callable<Long>() { @Override public Long call() throws IOException { final AtomicLong oldestDateHolder = new AtomicLong(0L); // the path already exists, so scan the path to find any files and update maxIndex to the max of // all filenames seen. Files.walkFileTree(realPath, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { LOG.warn("Content repository contains un-readable file or directory '" + file.getFileName() + "'. Skipping. ", exc); return FileVisitResult.SKIP_SUBTREE; } @Override public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { if (attrs.isDirectory()) { return FileVisitResult.CONTINUE; } // Check if this is an 'archive' directory final Path relativePath = realPath.relativize(file); if (relativePath.getNameCount() > 3 && ARCHIVE_DIR_NAME.equals(relativePath.subpath(1, 2).toString())) { final long lastModifiedTime = getLastModTime(file); if (lastModifiedTime < oldestDateHolder.get()) { oldestDateHolder.set(lastModifiedTime); } containerState.incrementArchiveCount(); } return FileVisitResult.CONTINUE; } }); return oldestDateHolder.get(); } }; // If the path didn't exist to begin with, there's no archive directory, so don't bother scanning. if (pathExists) { futures.add(executor.submit(scanContainer)); } } executor.shutdown(); for (final Future<Long> future : futures) { try { final Long oldestDate = future.get(); if (oldestDate < oldestArchiveDate.get()) { oldestArchiveDate.set(oldestDate); } } catch (final ExecutionException | InterruptedException e) { if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); } else { throw new RuntimeException(e); } } } containers.clear(); containers.putAll(realPathMap); }
From source file:org.eclipse.winery.repository.backend.filebased.FilebasedRepository.java
@Override public void doDump(OutputStream out) throws IOException { final ZipOutputStream zout = new ZipOutputStream(out); final int cutLength = this.repositoryRoot.toString().length() + 1; Files.walkFileTree(this.repositoryRoot, new SimpleFileVisitor<Path>() { @Override//from ww w . j a v a 2 s . c o m public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { if (dir.endsWith(".git")) { return FileVisitResult.SKIP_SUBTREE; } else { return FileVisitResult.CONTINUE; } } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { String name = file.toString().substring(cutLength); ZipEntry ze = new ZipEntry(name); try { ze.setTime(Files.getLastModifiedTime(file).toMillis()); ze.setSize(Files.size(file)); zout.putNextEntry(ze); Files.copy(file, zout); zout.closeEntry(); } catch (IOException e) { FilebasedRepository.logger.debug(e.getMessage()); } return FileVisitResult.CONTINUE; } }); zout.close(); }
From source file:com.bytelightning.opensource.pokerface.PokerFace.java
/** * If requested by the user, this method walks the script directory discovering, loading, compiling, and initialing an .js javascript files it finds in the specified directory or it's children. * @param baseScriptDirectory The contents of this directory should be structured in the same layout as the url's we wish to interfere with. * @param watchScriptDirectory If true, a watch will be placed on <code>baseScriptDirectory</code> and any javascript file modifications (cud) will be dynamically rebuilt and reflected in the running server. * @return True if all scripts were successfully loaded. *//* ww w . j ava 2s. c o m*/ protected boolean configureScripts(final List<Path> jsLibs, final HierarchicalConfiguration scriptConfig, final Path baseScriptDirectory, boolean watchScriptDirectory) { // Our unit test has verified that CompiledScripts can produce objects (endpoints) that can be executed from ANY thread (and even concurrently execute immutable methods). // However we have not validated that Nashorn can compile *and* recompile scripts from multiple threads. //TODO: Write unit test to see if we can use all available processors to compile discovered javascript files. ScriptCompilationExecutor = Executors.newSingleThreadScheduledExecutor(); // This is done to make sure the engine is allocated in the same thread that will be doing the compiling. Callable<Boolean> compileScriptsTask = new Callable<Boolean>() { @Override public Boolean call() { Nashorn = new ScriptEngineManager().getEngineByName("nashorn"); if (jsLibs != null) for (Path lib : jsLibs) if (!loadScriptLibrary(lib)) return false; // Recursively discover javascript files, compile, load, and setup any that are found. EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS); try { Files.walkFileTree(baseScriptDirectory, opts, Integer.MAX_VALUE, new SimpleFileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { if (Files.isDirectory(dir) && dir.getFileName().toString().startsWith("#")) return FileVisitResult.SKIP_SUBTREE; return super.preVisitDirectory(dir, attrs); } @Override public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { if (Files.isRegularFile(path)) { if (path.toString().toLowerCase().endsWith(".js")) { MakeJavaScriptEndPointDescriptor(baseScriptDirectory, path, scriptConfig, new NewEndpointSetupCallback()); } } return FileVisitResult.CONTINUE; } }); } catch (IOException e) { Logger.error("Unable recursively load scripts", e); return false; } return true; } }; // Walk the root directory recursively compiling all discovered javascript files (does not return until all endpoint files have been setup). try { if (!ScriptCompilationExecutor.submit(compileScriptsTask).get()) return false; } catch (Throwable e) { Logger.error("Unable to compile scripts", e); return false; } if (watchScriptDirectory) { try { // Establish a watch on the root ScriptDirectoryWatcher.establishWatch(baseScriptDirectory, new DirectoryWatchEventListener() { // Internal Callable task to load, compile, and initialize a javascript file endpoint. final class CreateEndpointTask implements Callable<Void> { public CreateEndpointTask(Path file, EndpointSetupCompleteCallback callback) { this.file = file; this.callback = callback; } private final Path file; private final EndpointSetupCompleteCallback callback; @Override public Void call() { MakeJavaScriptEndPointDescriptor(baseScriptDirectory, file, scriptConfig, callback); return null; } } // Internal Callable task that gives us the ability to schedule a delayed unload of a deleted or obsoleted endpoint. // By delaying for a period of time longer than twice the socket timeout, we can safely call the endpoint's teardown method. final class DecommisionEndpointTask implements Callable<Void> { private DecommisionEndpointTask(ScriptObjectMirror endpoint) { this.endpoint = endpoint; } private final ScriptObjectMirror endpoint; @Override public Void call() { if (endpoint.hasMember("teardown")) endpoint.callMember("teardown"); return null; } } /** * Called by the WatchService when the contents of the script directory have changed. */ @Override public void onWatchEvent(Path watchDir, final Path oldFile, final Path newFile, FileChangeType change) { if (change == FileChangeType.eRenamed) { // If it was changed to something that does *not* end .js then it should no longer be considered an endpoint. if (oldFile.toString().toLowerCase().endsWith(".js")) if (!newFile.toString().toLowerCase().endsWith(".js")) change = FileChangeType.eDeleted; } if (change == FileChangeType.eModified || change == FileChangeType.eRenamed) { // Decommission the obsolete and load the update. try { assert newFile.toString().toLowerCase().endsWith(".js"); // Will be true because of the 'rename' check at the top of this method. ScriptCompilationExecutor .submit(new CreateEndpointTask(newFile, new NewEndpointSetupCallback() { @Override public ScriptObjectMirror setupComplete(JavaScriptEndPoint endpoint) { ScriptObjectMirror old = super.setupComplete(endpoint); assert old != null; // Yeah, it's hincky, but it won't be in use this long after we remove it from the Map. ScriptCompilationExecutor.schedule(new DecommisionEndpointTask(old), 6, TimeUnit.MINUTES); return null; } })); } catch (Throwable e) { Logger.error("Unable to compile modified script found at " + newFile.toAbsolutePath().toString(), e); } } else if (change == FileChangeType.eCreated) { // This is the easy one. If a javascript file was created, load it. if (newFile.toString().toLowerCase().endsWith(".js")) { try { ScriptCompilationExecutor.submit( new CreateEndpointTask(newFile, new NewEndpointSetupCallback())); } catch (Throwable e) { Logger.error("Unable to compile new script found at " + newFile.toAbsolutePath().toString(), e); } } } else if (change == FileChangeType.eDeleted) { // Endpoint should be decommisioned. if (oldFile.toString().toLowerCase().endsWith(".js")) { String uriKey = FileToUriKey(baseScriptDirectory, oldFile); ScriptObjectMirror desc = scripts.remove(uriKey); if (desc != null) { // Yeah, it's hincky, but it won't be in use this long after we remove it from the Map. ScriptCompilationExecutor.schedule(new DecommisionEndpointTask(desc), 6, TimeUnit.MINUTES); } } } } }); } catch (IOException e) { Logger.error("Unable to establish a real time watch on the script directory.", e); } } else // Not watching for changes, so we are done with the Executor. ScriptCompilationExecutor.shutdown(); return true; }
From source file:org.schedulesdirect.grabber.Grabber.java
private void removeExpiredSchedules(FileSystem vfs) throws IOException, JSONException { final int[] i = new int[] { 0 }; final Path root = vfs.getPath("schedules"); Files.walkFileTree(root, new FileVisitor<Path>() { @Override/*w w w. j a v a 2 s. c o m*/ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { return !Files.isSameFile(dir, root) ? FileVisitResult.SKIP_SUBTREE : FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { try (InputStream ins = Files.newInputStream(file)) { JSONArray sched = Config.get().getObjectMapper() .readValue(IOUtils.toString(ins, ZipEpgClient.ZIP_CHARSET.toString()), JSONObject.class) .getJSONArray("programs"); if (isScheduleExpired(sched)) { Files.delete(file); ++i[0]; } } 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 { return FileVisitResult.CONTINUE; } }); LOG.info(String.format("Removed %d expired schedule(s).", i[0])); }
From source file:org.schedulesdirect.grabber.Grabber.java
private void removeUnusedPrograms(FileSystem vfs) throws IOException { final int[] i = new int[] { 0 }; final Path root = vfs.getPath("programs"); Files.walkFileTree(root, new FileVisitor<Path>() { @Override// w ww . jav a2 s . c o m public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { return !Files.isSameFile(root, dir) ? FileVisitResult.SKIP_SUBTREE : FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { String id = file.getName(file.getNameCount() - 1).toString(); id = id.substring(0, id.indexOf('.')); if (!activeProgIds.contains(id)) { if (LOG.isDebugEnabled()) LOG.debug(String.format("CacheCleaner: Unused '%s'", id)); Files.delete(file); ++i[0]; } 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 { return FileVisitResult.CONTINUE; } }); LOG.info(String.format("Removed %d unused program(s).", i[0])); }
From source file:org.schedulesdirect.grabber.Grabber.java
private void removeIgnoredStations(FileSystem vfs) throws IOException { final int[] i = new int[] { 0 }; final Path root = vfs.getPath("schedules"); Files.walkFileTree(root, new FileVisitor<Path>() { @Override/*from ww w . ja v a 2s . c o m*/ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { return !Files.isSameFile(root, dir) ? FileVisitResult.SKIP_SUBTREE : FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { String id = file.getName(file.getNameCount() - 1).toString(); id = id.substring(0, id.indexOf('.')); if (stationList != null && !stationList.contains(id)) { if (LOG.isDebugEnabled()) LOG.debug(String.format("CacheCleaner: Remove '%s'", id)); Files.delete(file); ++i[0]; } 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 { return FileVisitResult.CONTINUE; } }); LOG.info(String.format("Removed %d ignored station(s).", i[0])); }