Search in sources :

Example 16 with NotDirectoryException

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;
}
Also used : Path(java.nio.file.Path) NotDirectoryException(java.nio.file.NotDirectoryException) NoSuchFileException(java.nio.file.NoSuchFileException) HashSet(java.util.HashSet)

Example 17 with NotDirectoryException

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());
        }
    }
}
Also used : FileSystemException(java.nio.file.FileSystemException) NotDirectoryException(java.nio.file.NotDirectoryException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) AccessDeniedException(java.nio.file.AccessDeniedException) NoSuchFileException(java.nio.file.NoSuchFileException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) FileSystemLoopException(java.nio.file.FileSystemLoopException)

Example 18 with NotDirectoryException

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);
    }
}
Also used : Path(java.nio.file.Path) NotDirectoryException(java.nio.file.NotDirectoryException) WatchKey(java.nio.file.WatchKey) IOException(java.io.IOException)

Example 19 with NotDirectoryException

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));
}
Also used : Path(java.nio.file.Path) RepositorySystem(org.eclipse.aether.RepositorySystem) NotDirectoryException(java.nio.file.NotDirectoryException) IOError(java.io.IOError) NoSuchFileException(java.nio.file.NoSuchFileException)

Example 20 with NotDirectoryException

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);
}
Also used : Path(java.nio.file.Path) NotDirectoryException(java.nio.file.NotDirectoryException) Cryptor(org.cryptomator.cryptolib.api.Cryptor)

Aggregations

NotDirectoryException (java.nio.file.NotDirectoryException)31 Path (java.nio.file.Path)17 NoSuchFileException (java.nio.file.NoSuchFileException)16 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)7 Test (org.junit.Test)7 IOException (java.io.IOException)6 AccessDeniedException (java.nio.file.AccessDeniedException)6 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)6 FileNotFoundException (java.io.FileNotFoundException)5 FileSystemException (java.nio.file.FileSystemException)5 MCRFilesystemNode (org.mycore.datamodel.ifs.MCRFilesystemNode)5 AtomicMoveNotSupportedException (java.nio.file.AtomicMoveNotSupportedException)4 FileSystemLoopException (java.nio.file.FileSystemLoopException)4 MCRDirectory (org.mycore.datamodel.ifs.MCRDirectory)4 MCRPath (org.mycore.datamodel.niofs.MCRPath)4 EOFException (java.io.EOFException)3 HashSet (java.util.HashSet)3 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)3 IndexFormatTooNewException (org.apache.lucene.index.IndexFormatTooNewException)3 IndexFormatTooOldException (org.apache.lucene.index.IndexFormatTooOldException)3