Search in sources :

Example 6 with OverlappingFileLockException

use of java.nio.channels.OverlappingFileLockException in project robovm by robovm.

the class FileChannelLockingTest method test_illegalLockParameters.

public void test_illegalLockParameters() throws IOException {
    // Cannot lock negative positions
    try {
        readOnlyChannel.lock(-1, 10, true);
        fail("Passing illegal args to lock should fail.");
    } catch (IllegalArgumentException ex) {
    // expected
    }
    try {
        writeOnlyChannel.lock(-1, 10, false);
        fail("Passing illegal args to lock should fail.");
    } catch (IllegalArgumentException ex) {
    // expected
    }
    try {
        readWriteChannel.lock(-1, 10, false);
        fail("Passing illegal args to lock should fail.");
    } catch (IllegalArgumentException ex) {
    // expected
    }
    // Lock a range at the front, shared.
    FileLock flock1 = readWriteChannel.lock(22, 110, true);
    // Try to acquire an overlapping lock.
    try {
        readWriteChannel.lock(75, 210, true);
    } catch (OverlappingFileLockException exception) {
        // expected
        flock1.release();
    }
}
Also used : FileLock(java.nio.channels.FileLock) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Example 7 with OverlappingFileLockException

use of java.nio.channels.OverlappingFileLockException in project robovm by robovm.

the class FileChannelLockingTest method test_tryLockLLZ.

public void test_tryLockLLZ() throws IOException {
    // It is illegal to request an exclusive lock on a read-only channel
    try {
        readOnlyChannel.tryLock(0, 99, false);
        fail("Acquiring exclusive lock on read-only channel should fail");
    } catch (NonWritableChannelException ex) {
    // Expected
    }
    // It is invalid to request a lock starting before the file start
    try {
        readOnlyChannel.tryLock(-99, 0, true);
        fail("Acquiring an illegal lock value should fail.");
    } catch (IllegalArgumentException ex) {
    // expected
    }
    // Acquire a valid lock
    FileLock tmpLock = readOnlyChannel.tryLock(0, 10, true);
    assertTrue(tmpLock.isValid());
    tmpLock.release();
    // Acquire another valid lock -- and don't release it yet
    FileLock lock = readOnlyChannel.tryLock(10, 788, true);
    assertTrue(lock.isValid());
    // Overlapping locks are illegal
    try {
        readOnlyChannel.tryLock(1, 23, true);
        fail("Acquiring an overlapping lock should fail.");
    } catch (OverlappingFileLockException ex) {
    // Expected
    }
    // Adjacent locks are legal
    FileLock adjacentLock = readOnlyChannel.tryLock(1, 3, true);
    assertTrue(adjacentLock.isValid());
    adjacentLock.release();
    // Release longer lived lock
    lock.release();
}
Also used : FileLock(java.nio.channels.FileLock) NonWritableChannelException(java.nio.channels.NonWritableChannelException) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Example 8 with OverlappingFileLockException

use of java.nio.channels.OverlappingFileLockException in project robovm by robovm.

the class FileChannelImpl method addLock.

/**
     * Add a new pending lock to the manager. Throws an exception if the lock
     * would overlap an existing lock. Once the lock is acquired it remains in
     * this set as an acquired lock.
     */
private synchronized void addLock(FileLock lock) throws OverlappingFileLockException {
    long lockEnd = lock.position() + lock.size();
    for (FileLock existingLock : locks) {
        if (existingLock.position() > lockEnd) {
            // cannot overlap).
            break;
        }
        if (existingLock.overlaps(lock.position(), lock.size())) {
            throw new OverlappingFileLockException();
        }
    }
    locks.add(lock);
}
Also used : FileLock(java.nio.channels.FileLock) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Example 9 with OverlappingFileLockException

use of java.nio.channels.OverlappingFileLockException in project helios by spotify.

the class JobPrefixFile method tryFromExistingFile.

/**
   * Attempts to lock the given file, and create a JobPrefixFile for it. A new JobPrefixFile
   * instance will be returned if a lock can be obtained for the file. Null will be returned if the
   * lock is already held by either this process or another. For all other cases, an exception will
   * be thrown.
   * @param file the path to the file
   * @return a new JobPrefixFile if a file lock can be obtained. Null if a lock for the file is
   *         already held by either this process or another.
   */
public static JobPrefixFile tryFromExistingFile(final Path file) throws IOException {
    Preconditions.checkNotNull(file);
    final FileChannel channel = FileChannel.open(file, WRITE);
    final FileLock lock;
    try {
        // We want to return JobPrefixFile if we can obtain the lock, null if someone already holds
        // the lock, and throw an exception in all other cases. tryLock makes this a little tricky.
        // It's behavior this:
        //   - returns a FileLock if one can be obtained
        //   - returns null if another process holds the lock
        //   - throws OverlappingFileLockException if the lock is already held by this process
        //   - throws a various exceptions for other errors
        lock = channel.tryLock();
    } catch (OverlappingFileLockException e) {
        // If the lock is already held by this process, close the channel and return null.
        close(channel);
        return null;
    } catch (Exception e) {
        // If an unexpected error occurred, close the channel and rethrow.
        close(channel);
        throw e;
    }
    // If another process hold the lock, close the channel and return null.
    if (lock == null) {
        close(channel);
        return null;
    }
    // If we've obtained the lock, return a new JobPrefixFile
    return new JobPrefixFile(file, channel, lock);
}
Also used : FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) IOException(java.io.IOException)

Example 10 with OverlappingFileLockException

use of java.nio.channels.OverlappingFileLockException in project XobotOS by xamarin.

the class FileChannelImpl method addLock.

/**
     * Add a new pending lock to the manager. Throws an exception if the lock
     * would overlap an existing lock. Once the lock is acquired it remains in
     * this set as an acquired lock.
     */
private synchronized void addLock(FileLock lock) throws OverlappingFileLockException {
    long lockEnd = lock.position() + lock.size();
    for (FileLock existingLock : locks) {
        if (existingLock.position() > lockEnd) {
            // cannot overlap).
            break;
        }
        if (existingLock.overlaps(lock.position(), lock.size())) {
            throw new OverlappingFileLockException();
        }
    }
    locks.add(lock);
}
Also used : FileLock(java.nio.channels.FileLock) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Aggregations

OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)28 IOException (java.io.IOException)15 FileLock (java.nio.channels.FileLock)15 File (java.io.File)11 FileChannel (java.nio.channels.FileChannel)10 RandomAccessFile (java.io.RandomAccessFile)7 FileOutputStream (java.io.FileOutputStream)3 TaskId (org.apache.kafka.streams.processor.TaskId)3 Test (org.junit.Test)3 Path (java.nio.file.Path)2 IllegalAlphabetException (org.biojava.bio.symbol.IllegalAlphabetException)2 IllegalSymbolException (org.biojava.bio.symbol.IllegalSymbolException)2 FileSystemOperationException (com.axway.ats.common.filesystem.FileSystemOperationException)1 AttributeNotSupportedException (com.axway.ats.core.filesystem.exceptions.AttributeNotSupportedException)1 FileDoesNotExistException (com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException)1 ParallelTest (com.hazelcast.test.annotation.ParallelTest)1 QuickTest (com.hazelcast.test.annotation.QuickTest)1 LogException (io.mycat.backend.mysql.xa.recovery.LogException)1 EOFException (java.io.EOFException)1 FileNotFoundException (java.io.FileNotFoundException)1