List of usage examples for java.nio.file Files newDirectoryStream
public static DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter) throws IOException
From source file:org.codice.ddf.configuration.admin.ConfigurationAdminMigrationTest.java
@Test public void testGetFailedConfigurationFilesTwoFilesInFailedDirectory() throws Exception { when(Files.newDirectoryStream(any(Path.class), anyString())).thenReturn(failedDirectoryStream); setUpTwoConfigFileIterator(failedDirectoryStream); ConfigurationAdminMigration configurationAdminMigration = new ConfigurationAdminMigration( configurationDirectoryStream, PROCESSED_DIRECTORY_PATH, FAILED_DIRECTORY_PATH, configurationFileFactory, configurationFilePoller, configurationAdmin, CONFIGURATION_FILE_EXTENSION); Collection<MigrationWarning> configurationStatusMessages = configurationAdminMigration .getFailedConfigurationFiles(); Collection<String> actualPaths = new ArrayList<>(); for (MigrationWarning configStatus : configurationStatusMessages) { actualPaths.add(configStatus.getMessage()); }/*from w ww. j a v a 2s.c o m*/ verifyStatic(); Files.newDirectoryStream(FAILED_DIRECTORY_PATH, FILE_FILTER); assertThat( "Incorrect number for files returned from configurationAdminMigration.getFailedConfigurationFiles()", configurationStatusMessages.size(), is(2)); }
From source file:org.codice.ddf.configuration.admin.ConfigurationAdminMigrationTest.java
@Test public void testGetFailedConfigurationFilesNoFilesInFailedDirectory() throws Exception { when(failedDirectoryStreamIterator.hasNext()).thenReturn(false); when(failedDirectoryStream.iterator()).thenReturn(failedDirectoryStreamIterator); when(Files.newDirectoryStream(any(Path.class), any(String.class))).thenReturn(failedDirectoryStream); ConfigurationAdminMigration configurationAdminMigration = new ConfigurationAdminMigration( configurationDirectoryStream, PROCESSED_DIRECTORY_PATH, FAILED_DIRECTORY_PATH, configurationFileFactory, configurationFilePoller, configurationAdmin, CONFIGURATION_FILE_EXTENSION); Collection<MigrationWarning> configurationStatusMessages = configurationAdminMigration .getFailedConfigurationFiles(); assertThat("The failed directory does not contain the correct number of files", configurationStatusMessages, is(empty()));/*ww w.j a v a 2 s. c om*/ }
From source file:org.codice.ddf.configuration.admin.ConfigurationAdminMigrationTest.java
@Test(expected = IOException.class) public void testGetFailedConfigurationFilesDirectoryStreamThrowsException() throws Exception { PowerMockito.doThrow(new IOException()).when(Files.class); Files.newDirectoryStream(any(Path.class), any(String.class)); ConfigurationAdminMigration configurationAdminMigration = new ConfigurationAdminMigration( configurationDirectoryStream, PROCESSED_DIRECTORY_PATH, FAILED_DIRECTORY_PATH, configurationFileFactory, configurationFilePoller, configurationAdmin, CONFIGURATION_FILE_EXTENSION); configurationAdminMigration.getFailedConfigurationFiles(); verifyStatic();//ww w. j a va 2 s .c o m Files.newDirectoryStream(any(Path.class), any(String.class)); }
From source file:org.roda.core.storage.fs.FileStorageService.java
private void deleteAllBinaryVersionsUnder(StoragePath storagePath) { Path resourcePath = FSUtils.getEntityPath(basePath, storagePath); Path relativePath = basePath.relativize(resourcePath); Path resourceHistoryDataPath = historyDataPath.resolve(relativePath); if (FSUtils.isDirectory(resourceHistoryDataPath)) { try {//w w w . j ava 2s . com Path resourceHistoryMetadataPath = historyMetadataPath .resolve(historyDataPath.relativize(resourceHistoryDataPath)); trash(resourceHistoryDataPath); trash(resourceHistoryMetadataPath); FSUtils.deleteEmptyAncestorsQuietly(resourceHistoryDataPath, historyDataPath); FSUtils.deleteEmptyAncestorsQuietly(resourceHistoryMetadataPath, historyMetadataPath); } catch (GenericException | NotFoundException e) { LOGGER.warn("Could not delete history under " + resourceHistoryDataPath, e); } } else { Path parent = resourceHistoryDataPath.getParent(); final String baseName = resourceHistoryDataPath.getFileName().toString(); if (FSUtils.exists(parent)) { DirectoryStream<Path> directoryStream = null; try { directoryStream = Files.newDirectoryStream(parent, new DirectoryStream.Filter<Path>() { @Override public boolean accept(Path entry) throws IOException { return entry.getFileName().toString().startsWith(baseName); } }); for (Path p : directoryStream) { trash(p); Path pMetadata = FSUtils.getBinaryHistoryMetadataPath(historyDataPath, historyMetadataPath, p); trash(pMetadata); FSUtils.deleteEmptyAncestorsQuietly(p, historyDataPath); FSUtils.deleteEmptyAncestorsQuietly(pMetadata, historyMetadataPath); } } catch (IOException | GenericException | NotFoundException e) { LOGGER.warn("Could not delete history under " + resourceHistoryDataPath, e); } finally { IOUtils.closeQuietly(directoryStream); } } } }
From source file:com.cloudbees.clickstack.util.Files2.java
/** * Find a file matching {@code $artifactId*$type} in the given {@code srcDir}. * * @param srcDir// ww w . ja va 2s .c o m * @param artifactId * @param type * @return * @throws IllegalStateException More or less than 1 matching artifact found * @throws RuntimeIOException */ @Nonnull public static Path findArtifact(@Nonnull Path srcDir, @Nonnull final String artifactId, @Nonnull final String type) throws RuntimeIOException, IllegalStateException { Preconditions.checkArgument(Files.isDirectory(srcDir), "Source %s is not a directory", srcDir.toAbsolutePath()); DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() { @Override public boolean accept(Path entry) throws IOException { String fileName = entry.getFileName().toString(); if (fileName.startsWith(artifactId) && fileName.endsWith("." + type)) { return true; } else { return false; } } }; try (DirectoryStream<Path> paths = Files.newDirectoryStream(srcDir, filter)) { try { return Iterables.getOnlyElement(paths); } catch (NoSuchElementException e) { throw new IllegalStateException("Artifact '" + artifactId + ":" + type + "' not found in path: " + srcDir + ", absolutePath: " + srcDir.toAbsolutePath()); } catch (IllegalArgumentException e) { throw new IllegalStateException( "More than 1 version of artifact '" + artifactId + ":" + type + "' found in path: " + srcDir + ", absolutePath: " + srcDir.toAbsolutePath() + " -> " + paths); } } catch (IOException e) { throw new RuntimeIOException("Exception finding artifact " + artifactId + "@" + type + " in " + srcDir); } }
From source file:org.roda.core.storage.fs.FSUtils.java
public static CloseableIterable<BinaryVersion> listBinaryVersions(final Path historyDataPath, final Path historyMetadataPath, final StoragePath storagePath) throws GenericException, RequestNotValidException, NotFoundException, AuthorizationDeniedException { Path fauxPath = getEntityPath(historyDataPath, storagePath); final Path parent = fauxPath.getParent(); final String baseName = fauxPath.getFileName().toString(); CloseableIterable<BinaryVersion> iterable; try {//w w w . jav a 2s . com final DirectoryStream<Path> directoryStream = Files.newDirectoryStream(parent, new DirectoryStream.Filter<Path>() { @Override public boolean accept(Path entry) throws IOException { String fileName = entry.getFileName().toString(); int lastIndexOfDot = fileName.lastIndexOf(VERSION_SEP); return lastIndexOfDot > 0 ? fileName.substring(0, lastIndexOfDot).equals(baseName) : false; } }); final Iterator<Path> pathIterator = directoryStream.iterator(); iterable = new CloseableIterable<BinaryVersion>() { @Override public Iterator<BinaryVersion> iterator() { return new Iterator<BinaryVersion>() { @Override public boolean hasNext() { return pathIterator.hasNext(); } @Override public BinaryVersion next() { Path next = pathIterator.next(); BinaryVersion ret; try { ret = convertPathToBinaryVersion(historyDataPath, historyMetadataPath, next); } catch (GenericException | NotFoundException | RequestNotValidException e) { LOGGER.error("Error while list path " + parent + " while parsing resource " + next, e); ret = null; } return ret; } }; } @Override public void close() throws IOException { directoryStream.close(); } }; } catch (NoSuchFileException e) { throw new NotFoundException("Could not find versions of " + storagePath, e); } catch (IOException e) { throw new GenericException("Error finding version of " + storagePath, e); } return iterable; }
From source file:org.apache.nifi.processors.standard.TailFile.java
/** * Returns a list of all Files that match the following criteria: * * <ul>// w ww.ja v a 2s. com * <li>Filename matches the Rolling Filename Pattern</li> * <li>Filename does not match the actual file being tailed</li> * <li>The Last Modified Time on the file is equal to or later than the * given minimum timestamp</li> * </ul> * * <p> * The List that is returned will be ordered by file timestamp, providing * the oldest file first. * </p> * * @param context the ProcessContext to use in order to determine Processor * configuration * @param minTimestamp any file with a Last Modified Time before this * timestamp will not be returned * @return a list of all Files that have rolled over * @throws IOException if unable to perform the listing of files */ private List<File> getRolledOffFiles(final ProcessContext context, final long minTimestamp, final String tailFilePath) throws IOException { final File tailFile = new File(tailFilePath); File directory = tailFile.getParentFile(); if (directory == null) { directory = new File("."); } String rollingPattern = context.getProperty(ROLLING_FILENAME_PATTERN).getValue(); if (rollingPattern == null) { return Collections.emptyList(); } else { rollingPattern = rollingPattern.replace("${filename}", StringUtils.substringBeforeLast(tailFile.getName(), ".")); } final List<File> rolledOffFiles = new ArrayList<>(); try (final DirectoryStream<Path> dirStream = Files.newDirectoryStream(directory.toPath(), rollingPattern)) { for (final Path path : dirStream) { final File file = path.toFile(); final long lastMod = file.lastModified(); if (file.lastModified() < minTimestamp) { getLogger().debug( "Found rolled off file {} but its last modified timestamp is before the cutoff (Last Mod = {}, Cutoff = {}) so will not consume it", new Object[] { file, lastMod, minTimestamp }); continue; } else if (file.equals(tailFile)) { continue; } rolledOffFiles.add(file); } } // Sort files based on last modified timestamp. If same timestamp, use filename as a secondary sort, as often // files that are rolled over are given a naming scheme that is lexicographically sort in the same order as the // timestamp, such as yyyy-MM-dd-HH-mm-ss Collections.sort(rolledOffFiles, new Comparator<File>() { @Override public int compare(final File o1, final File o2) { final int lastModifiedComp = Long.compare(o1.lastModified(), o2.lastModified()); if (lastModifiedComp != 0) { return lastModifiedComp; } return o1.getName().compareTo(o2.getName()); } }); return rolledOffFiles; }
From source file:org.opencb.opencga.server.rest.FileWSServer.java
private ObjectMap getResumeFileJSON(java.nio.file.Path folderPath) throws IOException { ObjectMap objectMap = new ObjectMap(); if (Files.exists(folderPath)) { DirectoryStream<java.nio.file.Path> folderStream = Files.newDirectoryStream(folderPath, "*_partial"); for (java.nio.file.Path partPath : folderStream) { String[] nameSplit = partPath.getFileName().toString().split("_"); ObjectMap chunkInfo = new ObjectMap(); chunkInfo.put("size", Integer.parseInt(nameSplit[1])); objectMap.put(nameSplit[0], chunkInfo); }//ww w.j a va 2 s. c o m } return objectMap; }
From source file:org.opencb.opencga.server.rest.FileWSServer.java
private List<java.nio.file.Path> getSortedChunkList(java.nio.file.Path folderPath) throws IOException { List<java.nio.file.Path> files = new ArrayList<>(); DirectoryStream<java.nio.file.Path> stream = Files.newDirectoryStream(folderPath, "*_partial"); for (java.nio.file.Path p : stream) { logger.info("adding to ArrayList: " + p.getFileName()); files.add(p);/*from ww w . j a v a2 s . c o m*/ } logger.info("----ordered files length: " + files.size()); Collections.sort(files, new Comparator<java.nio.file.Path>() { public int compare(java.nio.file.Path o1, java.nio.file.Path o2) { int id_o1 = Integer.parseInt(o1.getFileName().toString().split("_")[0]); int id_o2 = Integer.parseInt(o2.getFileName().toString().split("_")[0]); return id_o1 - id_o2; } }); return files; }
From source file:de.dal33t.powerfolder.Controller.java
/** * Answers the path, where to load/store miscellanouse files created by * PowerFolder. e.g. .nodes files/*from w w w.j av a 2s . c o m*/ * * @return the file base, a directory */ public static Path getMiscFilesLocation() { Path base; Path unixConfigDir = Paths.get(System.getProperty("user.home"), "." + Constants.MISC_DIR_NAME) .toAbsolutePath(); if (OSUtil.isWindowsSystem() && Feature.WINDOWS_MISC_DIR_USE_APP_DATA.isEnabled()) { String appData; if (Feature.CONFIGURATION_ALL_USERS.isEnabled()) { appData = WinUtils.getAppDataAllUsers(); } else { appData = WinUtils.getAppDataCurrentUser(); } if (StringUtils.isBlank(appData)) { // Appdata not found. Fallback. return unixConfigDir; } Path windowsConfigDir = Paths.get(appData, Constants.MISC_DIR_NAME).toAbsolutePath(); base = windowsConfigDir; // Check if migration is necessary if (Files.exists(unixConfigDir)) { boolean migrateConfig; if (Files.exists(windowsConfigDir)) { // APPDATA/PowerFolder does not yet contain a config file OR Filter<Path> filter = new Filter<Path>() { @Override public boolean accept(Path entry) { return entry.getFileName().toString().endsWith("config"); } }; try (DirectoryStream<Path> stream = Files.newDirectoryStream(windowsConfigDir, filter)) { migrateConfig = !stream.iterator().hasNext(); } catch (IOException ioe) { log.info(ioe.getMessage()); migrateConfig = true; } } else { // Migrate if APPDATA/PowerFolder not existing yet. migrateConfig = true; } if (migrateConfig) { boolean migrationOk = migrateWindowsMiscLocation(unixConfigDir, windowsConfigDir); if (!migrationOk) { // Fallback, migration failed. base = unixConfigDir; } } } } else { base = unixConfigDir; } if (Files.notExists(base)) { try { Files.createDirectories(base); } catch (IOException ioe) { log.severe("Failed to create " + base.toAbsolutePath().toString() + ". " + ioe); } } return base; }