private void init() throws IOException, GitAPIException { if (!directory.toFile().exists()) { throw new ConfigException(String.format("Directory '%s' does not exist.", directory.toString())); if (!directory.toFile().isDirectory()) { throw new ConfigException(String.format("'%s' is not a directory.", directory.toString())); try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(directory)) { if (dirStream.iterator().hasNext()) { try { recordGit(Git.open(directory.toFile()));
private void doTestBBEDirectory(Path path) throws RuntimeException { try { File resource = path.toFile(); if (resource.exists()) { if (resource.isFile() && resource.getName().endsWith(".bal")) { doTestBBEFile(resource, null); // If the resource is a directory, recursively test the sub directories/files accordingly. } else if (resource.isDirectory() && !resource.getName().contains("tests")) { DirectoryStream<Path> ds = Files.newDirectoryStream(path); for (Path subPath : ds) { doTestBBEDirectory(subPath); } ds.close(); } } } catch (IOException e) { throw new RuntimeException(e); } }
@NotNull private List<String> listParentDirectory(Path path) throws IOException { List<String> result = new ArrayList<>(); Path parent = path.getParent(); Files.newDirectoryStream(parent, p -> { if (p.toString().startsWith(path.toString())) { if (p.toFile().isFile()) { return p.toString().endsWith(".jar"); } return true; } return false; }).forEach(x -> result.add(x.toString())); return result; }
private static List<File> getLibs(Path dir) throws IOException { Filter<Path> filter = path -> { String name = path.getFileName().toString(); return name.endsWith(".jar") || name.endsWith(".zip") || name.endsWith(".aar"); }; List<File> files = new ArrayList<>(); try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, filter)) { stream.forEach(p -> files.add(p.toFile())); } return files; }
if (levels == 0) { try { stream = Files.newDirectoryStream(root, "*.xml"); iterator = stream.iterator(); } catch (IOException e) { ElytronMessages.log.debug("Unable to open directory", e); } else { try { stream = Files.newDirectoryStream(root, entry -> { final String fileName = entry.getFileName().toString(); return fileName.length() == 1 && !fileName.equals(".") && Files.isDirectory(entry); }); iterator = stream.iterator(); } catch (IOException e) { ElytronMessages.log.debug("Unable to open directory", e);
public static Offloaders searchForOffloaders(String connectorsDirectory) throws IOException { Path path = Paths.get(connectorsDirectory).toAbsolutePath(); log.info("Searching for offloaders in {}", path); Offloaders offloaders = new Offloaders(); if (!path.toFile().exists()) { log.warn("Offloaders archive directory not found"); return offloaders; } try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, "*.nar")) { stream.forEach(archive -> { try { OffloaderDefinition definition = getOffloaderDefinition(archive.toString()); log.info("Found offloader {} from {}", definition, archive); if (!StringUtils.isEmpty(definition.getOffloaderFactoryClass())) { // Validate offloader factory class to be present and of the right type Pair<NarClassLoader, LedgerOffloaderFactory> offloaderFactoryPair = getOffloaderFactory(archive.toString()); if (null != offloaderFactoryPair) { offloaders.getOffloaders().add(offloaderFactoryPair); } } } catch (Throwable t) { log.warn("Failed to load offloader from {}", archive, t); } }); } log.info("Found and loaded {} offloaders", offloaders.getOffloaders().size()); return offloaders; }
public void copyFiles(Path sourcePath, Path targetPath) throws IOException { if (!Files.exists(Objects.requireNonNull(targetPath))) { Files.createDirectories(targetPath); } Files.newDirectoryStream(sourcePath).forEach(source -> { String name = source.toFile().getName(); try { if (name.endsWith(".csv")) { Path target = targetPath.resolve(name); Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING); } } catch (IOException e) { throw new RuntimeException("Cant' copy allegro temporary referential " + name, e); } }); }
public WildcardsPdfFileSourceAdapter(String path) { if (getName(path).equalsIgnoreCase("*.pdf")) { Path directory = Paths.get(getFullPath(path)).toAbsolutePath().normalize(); if (!Files.isDirectory(directory)) { throw new ConversionException("Path '" + directory.toString() + "' is not an existing directory"); } try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(directory, "*.pdf")) { dirStream.forEach(p -> sources.add(PdfFileSource.newInstanceNoPassword(p.toFile()))); } catch (IOException e) { throw new ConversionException("Unable to list PDF files in " + path, e); } } else { sources.add(new PdfFileSourceAdapter(path).getPdfFileSource()); } sources.sort(Comparator.comparing(s -> s.getName())); }
private static void deleteAllRecursivelyExceptLockFile(Path dirToDelete) { try (DirectoryStream<Path> stream = list(dirToDelete)) { Iterator<Path> it = stream.iterator(); while (it.hasNext()) { FileUtils.deleteQuietly(it.next().toFile()); } } catch (IOException e) { throw new IllegalStateException("Failed to clean working directory: " + dirToDelete.toString(), e); } }
public static List<AthenzService> findSiaServices(Path root) { String keyFileSuffix = ".key.pem"; Path keysDirectory = root.resolve("keys"); if ( ! Files.exists(keysDirectory)) return emptyList(); try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(keysDirectory)) { return StreamSupport.stream(directoryStream.spliterator(), false) .map(path -> path.getFileName().toString()) .filter(fileName -> fileName.endsWith(keyFileSuffix)) .map(fileName -> fileName.substring(0, fileName.length() - keyFileSuffix.length())) .map(AthenzService::new) .collect(toList()); } catch (IOException e) { throw new UncheckedIOException(e); } }
private Iterator<Path> getUncompressedLogrotatedFileIterator(SingularityExecutorTaskDefinition taskDefinition) { final Path serviceLogOutPath = taskDefinition.getServiceLogOutPath(); final Path parent = serviceLogOutPath.getParent(); if (parent == null) { throw new IllegalStateException("Service log path " + serviceLogOutPath + " has no parent"); } final Path logrotateToPath = parent.resolve(executorConfiguration.getLogrotateToDirectory()); if (!logrotateToPath.toFile().exists() || !logrotateToPath.toFile().isDirectory()) { LOG.warn("Skipping uncompressed logrotated file cleanup for {} -- {} does not exist or is not a directory (task sandbox was probably garbage collected by Mesos)", taskDefinition.getTaskId(), logrotateToPath); return Collections.emptyIterator(); } try { DirectoryStream<Path> dirStream = Files.newDirectoryStream(logrotateToPath, String.format("%s-*", serviceLogOutPath.getFileName())); return dirStream.iterator(); } catch (IOException e) { throw Throwables.propagate(e); } }
/** Return the filenames of all matching files in the configuration directory (absolute paths returned ). */ public static List<String> existingConfigurationFile(String baseFilename) { try { List<String> paths = new ArrayList<>() ; try (DirectoryStream<Path> stream = Files.newDirectoryStream(FusekiWebapp.dirConfiguration, baseFilename+".*") ) { stream.forEach((p)-> paths.add(FusekiWebapp.dirConfiguration.resolve(p).toString() )); } return paths ; } catch (IOException ex) { throw new InternalErrorException("Failed to read configuration directory "+FusekiWebapp.dirConfiguration) ; } } }
public static void clearDirectory(String path) throws IOException { Path directory = Paths.get(path); Files.newDirectoryStream(directory).forEach(file -> { try { if (Files.isDirectory(file)) { clearDirectory(file.toAbsolutePath().toString()); } Files.delete(file); } catch (IOException e) { throw new UncheckedIOException(e); } }); }
@Test @Category( Windows.class ) public void testCaseRemembering() throws IOException { Path file = dirTA().resolve( nameD() ); // create file where last filename is mixed case Files.write( mixCase( file ), CONTENT ); try( DirectoryStream<Path> dstr = Files.newDirectoryStream( childGetParent( file ) ) ) { Path kid = dstr.iterator().next(); assertThat( kid ).isEqualTo( file ); assertThat( kid.toString() ).isNotEqualTo( file.toString() ); } }
@NotNull private List<String> listDirectory(Path path) throws IOException { List<String> result = new ArrayList<>(); Files.newDirectoryStream(path, filter) .forEach(x -> result.add(x.toString())); return result; }
private List<URL> jarsInDirectory(final Path directory) { final DirectoryStream.Filter<Path> pathFilter = entry -> Files.isRegularFile(entry) && entry.toString().endsWith(".jar"); try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory, pathFilter)) { return StreamSupport.stream(stream.spliterator(), false) .filter(Files::isRegularFile) .map(this::toUrlSafe) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toList()); } catch (IOException e) { LOGGER.error("Could not load plugin: {}", e); return Collections.emptyList(); } }
/** * @param annotation The annotation * @param classes The classes * @param file The file */ protected void traverseFile(String annotation, List<Class> classes, File file) { if (file.isDirectory()) { try (DirectoryStream<Path> dirs = Files.newDirectoryStream(file.toPath())) { dirs.forEach(path -> { File f = path.toFile(); if (f.isDirectory()) { traverseFile(annotation, classes, f); } else { scanFile(annotation, f, classes); } }); } catch (IOException e) { if (LOG.isDebugEnabled()) { LOG.debug("Ignoring directory [" + file + "] due to I/O error: " + e.getMessage(), e); } } } else { scanFile(annotation, file, classes); } }
/** * Get a file that matches the glob pattern in the base directory * @param base directory to check * @param glob the glob pattern to match * @return a {@link File} if found, otherwise null */ private File getFileFromGlob(String base, String glob) { try (DirectoryStream<java.nio.file.Path> dirStream = Files.newDirectoryStream(Paths.get(base), glob)) { Iterator<java.nio.file.Path> iter = dirStream.iterator(); if (iter.hasNext()) { java.nio.file.Path path = iter.next(); return path.toFile(); } else { return null; } } catch (IOException e) { return null; } }
/** * Return the last modified time for all log files in a worker's log dir. * Using stream rather than File.listFiles is to avoid large mem usage * when a directory has too many files. */ private long lastModifiedTimeWorkerLogdir(File logDir) { long dirModified = logDir.lastModified(); DirectoryStream<Path> dirStream; try { dirStream = directoryCleaner.getStreamForDirectory(logDir); } catch (IOException e) { return dirModified; } if (dirStream == null) { return dirModified; } try { return StreamSupport.stream(dirStream.spliterator(), false) .reduce(dirModified, (maximum, path) -> { long curr = path.toFile().lastModified(); return curr > maximum ? curr : maximum; }, BinaryOperator.maxBy(Long::compareTo)); } catch (Exception ex) { LOG.error(ex.getMessage(), ex); return dirModified; } finally { IOUtils.closeQuietly(dirStream); } }
static URL[] extensionsClasspath(File appHomeDir) throws IOException { final File extensionsDir = new File(appHomeDir, "cli"); try (DirectoryStream<Path> jars = Files.newDirectoryStream(Paths.get(extensionsDir.getAbsolutePath()), "*.jar")) { return stream(jars.spliterator(), false).map(path -> { try { return path.toFile().toURI().toURL(); } catch (final Exception e) { throw new RuntimeException(e); } }).toArray(URL[]::new); } }