Search in sources :

Example 6 with AccessDeniedException

use of java.nio.file.AccessDeniedException in project graylog2-server by Graylog2.

the class CmdLineTool method annotateInjectorExceptions.

protected void annotateInjectorExceptions(Collection<Message> messages) {
    for (Message message : messages) {
        //noinspection ThrowableResultOfMethodCallIgnored
        final Throwable rootCause = ExceptionUtils.getRootCause(message.getCause());
        if (rootCause instanceof NodeIdPersistenceException) {
            LOG.error(UI.wallString("Unable to read or persist your NodeId file. This means your node id file (" + configuration.getNodeIdFile() + ") is not readable or writable by the current user. The following exception might give more information: " + message));
            System.exit(-1);
        } else if (rootCause instanceof AccessDeniedException) {
            LOG.error(UI.wallString("Unable to access file " + rootCause.getMessage()));
            System.exit(-2);
        } else {
            // other guice error, still print the raw messages
            // TODO this could potentially print duplicate messages depending on what a subclass does...
            LOG.error("Guice error (more detail on log level debug): {}", message.getMessage());
            if (rootCause != null) {
                LOG.debug("Stacktrace:", rootCause);
            }
        }
    }
}
Also used : AccessDeniedException(java.nio.file.AccessDeniedException) Message(com.google.inject.spi.Message) NodeIdPersistenceException(org.graylog2.plugin.system.NodeIdPersistenceException)

Example 7 with AccessDeniedException

use of java.nio.file.AccessDeniedException in project lucene-solr by apache.

the class SimpleFSLockFactory method obtainFSLock.

@Override
protected Lock obtainFSLock(FSDirectory dir, String lockName) throws IOException {
    Path lockDir = dir.getDirectory();
    // Ensure that lockDir exists and is a directory.
    // note: this will fail if lockDir is a symlink
    Files.createDirectories(lockDir);
    Path lockFile = lockDir.resolve(lockName);
    // create the file: this will fail if it already exists
    try {
        Files.createFile(lockFile);
    } catch (FileAlreadyExistsException | AccessDeniedException e) {
        // convert optional specific exception to our optional specific exception
        throw new LockObtainFailedException("Lock held elsewhere: " + lockFile, e);
    }
    // used as a best-effort check, to see if the underlying file has changed
    final FileTime creationTime = Files.readAttributes(lockFile, BasicFileAttributes.class).creationTime();
    return new SimpleFSLock(lockFile, creationTime);
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) AccessDeniedException(java.nio.file.AccessDeniedException) FileTime(java.nio.file.attribute.FileTime) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 8 with AccessDeniedException

use of java.nio.file.AccessDeniedException in project flink by apache.

the class FileUtilsTest method testDeleteDirectory.

@Test
public void testDeleteDirectory() throws Exception {
    // deleting a non-existent file should not cause an error
    File doesNotExist = new File(tmp.newFolder(), "abc");
    FileUtils.deleteDirectory(doesNotExist);
    // deleting a write protected file should throw an error
    File cannotDeleteParent = tmp.newFolder();
    File cannotDeleteChild = new File(cannotDeleteParent, "child");
    try {
        assumeTrue(cannotDeleteChild.createNewFile());
        assumeTrue(cannotDeleteParent.setWritable(false));
        assumeTrue(cannotDeleteChild.setWritable(false));
        FileUtils.deleteDirectory(cannotDeleteParent);
        fail("this should fail with an exception");
    } catch (AccessDeniedException ignored) {
    // this is expected
    } finally {
        //noinspection ResultOfMethodCallIgnored
        cannotDeleteParent.setWritable(true);
        //noinspection ResultOfMethodCallIgnored
        cannotDeleteChild.setWritable(true);
    }
}
Also used : AccessDeniedException(java.nio.file.AccessDeniedException) File(java.io.File) Test(org.junit.Test)

Example 9 with AccessDeniedException

use of java.nio.file.AccessDeniedException in project elasticsearch by elastic.

the class StreamOutput method writeException.

public void writeException(Throwable throwable) throws IOException {
    if (throwable == null) {
        writeBoolean(false);
    } else {
        writeBoolean(true);
        boolean writeCause = true;
        boolean writeMessage = true;
        if (throwable instanceof CorruptIndexException) {
            writeVInt(1);
            writeOptionalString(((CorruptIndexException) throwable).getOriginalMessage());
            writeOptionalString(((CorruptIndexException) throwable).getResourceDescription());
            writeMessage = false;
        } else if (throwable instanceof IndexFormatTooNewException) {
            writeVInt(2);
            writeOptionalString(((IndexFormatTooNewException) throwable).getResourceDescription());
            writeInt(((IndexFormatTooNewException) throwable).getVersion());
            writeInt(((IndexFormatTooNewException) throwable).getMinVersion());
            writeInt(((IndexFormatTooNewException) throwable).getMaxVersion());
            writeMessage = false;
            writeCause = false;
        } else if (throwable instanceof IndexFormatTooOldException) {
            writeVInt(3);
            IndexFormatTooOldException t = (IndexFormatTooOldException) throwable;
            writeOptionalString(t.getResourceDescription());
            if (t.getVersion() == null) {
                writeBoolean(false);
                writeOptionalString(t.getReason());
            } else {
                writeBoolean(true);
                writeInt(t.getVersion());
                writeInt(t.getMinVersion());
                writeInt(t.getMaxVersion());
            }
            writeMessage = false;
            writeCause = false;
        } else if (throwable instanceof NullPointerException) {
            writeVInt(4);
            writeCause = false;
        } else if (throwable instanceof NumberFormatException) {
            writeVInt(5);
            writeCause = false;
        } else if (throwable instanceof IllegalArgumentException) {
            writeVInt(6);
        } else if (throwable instanceof AlreadyClosedException) {
            writeVInt(7);
        } else if (throwable instanceof EOFException) {
            writeVInt(8);
            writeCause = false;
        } else if (throwable instanceof SecurityException) {
            writeVInt(9);
        } else if (throwable instanceof StringIndexOutOfBoundsException) {
            writeVInt(10);
            writeCause = false;
        } else if (throwable instanceof ArrayIndexOutOfBoundsException) {
            writeVInt(11);
            writeCause = false;
        } else if (throwable instanceof FileNotFoundException) {
            writeVInt(12);
            writeCause = false;
        } else if (throwable instanceof FileSystemException) {
            writeVInt(13);
            if (throwable instanceof NoSuchFileException) {
                writeVInt(0);
            } else if (throwable instanceof NotDirectoryException) {
                writeVInt(1);
            } else if (throwable instanceof DirectoryNotEmptyException) {
                writeVInt(2);
            } else if (throwable instanceof AtomicMoveNotSupportedException) {
                writeVInt(3);
            } else if (throwable instanceof FileAlreadyExistsException) {
                writeVInt(4);
            } else if (throwable instanceof AccessDeniedException) {
                writeVInt(5);
            } else if (throwable instanceof FileSystemLoopException) {
                writeVInt(6);
            } else {
                writeVInt(7);
            }
            writeOptionalString(((FileSystemException) throwable).getFile());
            writeOptionalString(((FileSystemException) throwable).getOtherFile());
            writeOptionalString(((FileSystemException) throwable).getReason());
            writeCause = false;
        } else if (throwable instanceof IllegalStateException) {
            writeVInt(14);
        } else if (throwable instanceof LockObtainFailedException) {
            writeVInt(15);
        } else if (throwable instanceof InterruptedException) {
            writeVInt(16);
            writeCause = false;
        } else if (throwable instanceof IOException) {
            writeVInt(17);
        } else {
            ElasticsearchException ex;
            if (throwable instanceof ElasticsearchException && ElasticsearchException.isRegistered(throwable.getClass(), version)) {
                ex = (ElasticsearchException) throwable;
            } else {
                ex = new NotSerializableExceptionWrapper(throwable);
            }
            writeVInt(0);
            writeVInt(ElasticsearchException.getId(ex.getClass()));
            ex.writeTo(this);
            return;
        }
        if (writeMessage) {
            writeOptionalString(throwable.getMessage());
        }
        if (writeCause) {
            writeException(throwable.getCause());
        }
        ElasticsearchException.writeStackTraces(throwable, this);
    }
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) AccessDeniedException(java.nio.file.AccessDeniedException) FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ElasticsearchException(org.elasticsearch.ElasticsearchException) FileSystemException(java.nio.file.FileSystemException) NotDirectoryException(java.nio.file.NotDirectoryException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) EOFException(java.io.EOFException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) IOException(java.io.IOException) IndexFormatTooNewException(org.apache.lucene.index.IndexFormatTooNewException) FileSystemLoopException(java.nio.file.FileSystemLoopException)

Example 10 with AccessDeniedException

use of java.nio.file.AccessDeniedException 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)

Aggregations

AccessDeniedException (java.nio.file.AccessDeniedException)16 Test (org.junit.Test)6 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)5 IOException (java.io.IOException)4 NoSuchFileException (java.nio.file.NoSuchFileException)4 Path (java.nio.file.Path)4 EOFException (java.io.EOFException)3 FileNotFoundException (java.io.FileNotFoundException)3 AtomicMoveNotSupportedException (java.nio.file.AtomicMoveNotSupportedException)3 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)3 FileSystemException (java.nio.file.FileSystemException)3 FileSystemLoopException (java.nio.file.FileSystemLoopException)3 NotDirectoryException (java.nio.file.NotDirectoryException)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Closeable (java.io.Closeable)2 Configuration (org.apache.hadoop.conf.Configuration)2 Path (org.apache.hadoop.fs.Path)2 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)2 IndexFormatTooNewException (org.apache.lucene.index.IndexFormatTooNewException)2 IndexFormatTooOldException (org.apache.lucene.index.IndexFormatTooOldException)2