use of java.nio.file.NotDirectoryException in project elasticsearch by elastic.
the class FileUtils method findJsonSpec.
/**
* Returns the json files found within the directory provided as argument.
* Files are looked up in the classpath, or optionally from {@code fileSystem} if its not null.
*/
public static Set<Path> findJsonSpec(FileSystem fileSystem, String optionalPathPrefix, String path) throws IOException {
Path dir = resolveFile(fileSystem, optionalPathPrefix, path, null);
if (!Files.isDirectory(dir)) {
throw new NotDirectoryException(path);
}
Set<Path> jsonFiles = new HashSet<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path item : stream) {
if (item.toString().endsWith(JSON_SUFFIX)) {
jsonFiles.add(item);
}
}
}
if (jsonFiles.isEmpty()) {
throw new NoSuchFileException(path, null, "no json files found");
}
return jsonFiles;
}
use of java.nio.file.NotDirectoryException in project elasticsearch by elastic.
the class ExceptionSerializationTests method testFileSystemExceptions.
public void testFileSystemExceptions() throws IOException {
for (FileSystemException ex : Arrays.asList(new FileSystemException("a", "b", "c"), new NoSuchFileException("a", "b", "c"), new NotDirectoryException("a"), new DirectoryNotEmptyException("a"), new AtomicMoveNotSupportedException("a", "b", "c"), new FileAlreadyExistsException("a", "b", "c"), new AccessDeniedException("a", "b", "c"), new FileSystemLoopException("a"))) {
FileSystemException serialize = serialize(ex);
assertEquals(serialize.getClass(), ex.getClass());
assertEquals("a", serialize.getFile());
if (serialize.getClass() == NotDirectoryException.class || serialize.getClass() == FileSystemLoopException.class || serialize.getClass() == DirectoryNotEmptyException.class) {
assertNull(serialize.getOtherFile());
assertNull(serialize.getReason());
} else {
assertEquals(serialize.getClass().toString(), "b", serialize.getOtherFile());
assertEquals(serialize.getClass().toString(), "c", serialize.getReason());
}
}
}
use of java.nio.file.NotDirectoryException in project streamline by hortonworks.
the class FileWatcher method register.
public void register() {
try {
watchService = FileSystems.getDefault().newWatchService();
String message = "Could not register %s with file watch service.";
if (fileEventHandlers != null && fileEventHandlers.size() > 0) {
for (FileEventHandler fileEventHandler : fileEventHandlers) {
try {
Path path = Paths.get(fileEventHandler.getDirectoryToWatch());
WatchKey watchKey = path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
watchKeyFileEventHandlerMap.put(watchKey, fileEventHandler);
watchKeyPathMap.put(watchKey, path);
LOG.info("Successfully registered " + fileEventHandler.getDirectoryToWatch() + " with file watch service.");
} catch (NotDirectoryException e) {
LOG.warn(String.format(message, fileEventHandler.getDirectoryToWatch()) + "Reason: Not a directory", e);
} catch (RuntimeException | IOException e) {
LOG.warn(String.format(message, fileEventHandler.getDirectoryToWatch()), e);
}
}
if (watchKeyFileEventHandlerMap.isEmpty()) {
LOG.warn("Could not register any file event handler successfully.");
}
} else {
LOG.info("No file event handlers passed.");
}
} catch (IOException e) {
LOG.warn("Unable to get the default file system watch service. Streamline FileWatcher is not active.", e);
}
}
use of java.nio.file.NotDirectoryException in project embulk by embulk.
the class MavenArtifactFinder method create.
public static MavenArtifactFinder create(final Path localMavenRepositoryPath) throws MavenRepositoryNotFoundException {
final Path absolutePath;
try {
absolutePath = localMavenRepositoryPath.normalize().toAbsolutePath();
} catch (IOError ex) {
throw new MavenRepositoryNotFoundException(localMavenRepositoryPath, ex);
} catch (SecurityException ex) {
throw new MavenRepositoryNotFoundException(localMavenRepositoryPath, ex);
}
if (!Files.exists(absolutePath)) {
throw new MavenRepositoryNotFoundException(localMavenRepositoryPath, absolutePath, new NoSuchFileException(absolutePath.toString()));
}
if (!Files.isDirectory(absolutePath)) {
throw new MavenRepositoryNotFoundException(localMavenRepositoryPath, absolutePath, new NotDirectoryException(absolutePath.toString()));
}
final RepositorySystem repositorySystem = createRepositorySystem();
return new MavenArtifactFinder(localMavenRepositoryPath, absolutePath, repositorySystem, createRepositorySystemSession(repositorySystem, absolutePath));
}
use of java.nio.file.NotDirectoryException in project cryptofs by cryptomator.
the class CryptoFileSystemProvider method initialize.
/**
* Creates a new vault at the given directory path.
*
* @param pathToVault Path to a not yet existing directory
* @param masterkeyFilename Name of the masterkey file
* @param pepper Application-specific pepper used during key derivation
* @param passphrase Passphrase that should be used to unlock the vault
* @throws NotDirectoryException If the given path is not an existing directory.
* @throws IOException If the vault structure could not be initialized due to I/O errors
* @since 1.3.2
*/
public static void initialize(Path pathToVault, String masterkeyFilename, byte[] pepper, CharSequence passphrase) throws NotDirectoryException, IOException {
if (!Files.isDirectory(pathToVault)) {
throw new NotDirectoryException(pathToVault.toString());
}
try (Cryptor cryptor = CRYPTOR_PROVIDER.createNew()) {
// save masterkey file:
Path masterKeyPath = pathToVault.resolve(masterkeyFilename);
byte[] keyFileContents = cryptor.writeKeysToMasterkeyFile(Normalizer.normalize(passphrase, Form.NFC), pepper, Constants.VAULT_VERSION).serialize();
Files.write(masterKeyPath, keyFileContents, CREATE_NEW, WRITE);
// create "d/RO/OTDIRECTORY":
String rootDirHash = cryptor.fileNameCryptor().hashDirectoryId(Constants.ROOT_DIR_ID);
Path rootDirPath = pathToVault.resolve(Constants.DATA_DIR_NAME).resolve(rootDirHash.substring(0, 2)).resolve(rootDirHash.substring(2));
Files.createDirectories(rootDirPath);
// create "m":
Files.createDirectory(pathToVault.resolve(Constants.METADATA_DIR_NAME));
}
assert containsVault(pathToVault, masterkeyFilename);
}
Aggregations