Search in sources :

Example 21 with OverlappingFileLockException

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

the class NearCachePreloaderLockTest method testAcquireLock_whenTryLockThrowsOverlappingFileLockException_thenThrowHazelcastException.

/**
     * The {@link FileChannel#tryLock()} throws an {@link OverlappingFileLockException}:
     * <pre>
     * If a lock that overlaps the requested region is already held by
     * this Java virtual machine, or if another thread is already
     * blocked in this method and is attempting to lock an overlapping
     * region
     * </pre>
     */
@Test
public void testAcquireLock_whenTryLockThrowsOverlappingFileLockException_thenThrowHazelcastException() throws Exception {
    when(channel.tryLock()).thenThrow(new OverlappingFileLockException());
    rule.expect(HazelcastException.class);
    rule.expectMessage("File is already being used by this Hazelcast instance.");
    preloaderLock.acquireLock(lockFile, channel);
}
Also used : OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 22 with OverlappingFileLockException

use of java.nio.channels.OverlappingFileLockException in project neo4j-mobile-android by neo4j-contrib.

the class FileLock method getLockFileBasedFileLock.

private static FileLock getLockFileBasedFileLock(File storeDir) throws IOException {
    File lockFile = new File(storeDir, "lock");
    if (!lockFile.exists()) {
        if (!lockFile.createNewFile()) {
            throw new IOException("Couldn't create lock file " + lockFile.getAbsolutePath());
        }
    }
    FileChannel fileChannel = new RandomAccessFile(lockFile, "rw").getChannel();
    java.nio.channels.FileLock fileChannelLock = null;
    try {
        fileChannelLock = fileChannel.tryLock();
    } catch (OverlappingFileLockException e) {
    // OK, let fileChannelLock continue to be null and we'll deal with it below
    }
    if (fileChannelLock == null) {
        fileChannel.close();
        return null;
    }
    return new WindowsFileLock(lockFile, fileChannel, fileChannelLock);
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Example 23 with OverlappingFileLockException

use of java.nio.channels.OverlappingFileLockException in project hadoop by apache.

the class DataStorage method isPreUpgradableLayout.

@Override
public boolean isPreUpgradableLayout(StorageDirectory sd) throws IOException {
    File oldF = new File(sd.getRoot(), "storage");
    if (!oldF.exists()) {
        return false;
    }
    // Lock and Read old storage file
    try (RandomAccessFile oldFile = new RandomAccessFile(oldF, "rws");
        FileLock oldLock = oldFile.getChannel().tryLock()) {
        if (null == oldLock) {
            LOG.error("Unable to acquire file lock on path " + oldF.toString());
            throw new OverlappingFileLockException();
        }
        oldFile.seek(0);
        int oldVersion = oldFile.readInt();
        if (oldVersion < LAST_PRE_UPGRADE_LAYOUT_VERSION) {
            return false;
        }
    }
    return true;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileLock(java.nio.channels.FileLock) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Example 24 with OverlappingFileLockException

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

the class OverlappingFileLockExceptionTest method test_Constructor.

/**
     * @tests {@link java.nio.channels.OverlappingFileLockException#OverlappingFileLockException()}
     */
public void test_Constructor() {
    OverlappingFileLockException e = new OverlappingFileLockException();
    assertNull(e.getMessage());
    assertNull(e.getLocalizedMessage());
    assertNull(e.getCause());
}
Also used : OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Example 25 with OverlappingFileLockException

use of java.nio.channels.OverlappingFileLockException in project sonatype-aether by sonatype.

the class AsyncRepositoryConnector method lockFile.

/**
     * Create a temporary file used to lock ({@link FileLock}) an associated incomplete file {@link File}. The {@link FileLock}'s name
     * is derived from the original file, appending ".lock" at the end. Usually this method gets executed when a
     * download fail to complete because the JVM goes down. In that case we resume the incomplete download and to prevent
     * multiple process to work on the same file, we use a dedicated {@link FileLock}.
     *
     * @param tmpFile a file on which we want to create a temporary lock file.
     * @return a {@link FileLockCompanion} contains the {@link File} and a {@link FileLock} if it was possible to lock the file.
     */
private FileLockCompanion lockFile(File tmpFile) {
    try {
        // of current resumable file.
        if (activeDownloadFiles.containsKey(tmpFile)) {
            return new FileLockCompanion(tmpFile, null);
        }
        RandomAccessFile tmpLock = new RandomAccessFile(tmpFile.getPath() + ".lock", "rw");
        FileLock lock = tmpLock.getChannel().tryLock(0, 1, false);
        if (lock != null) {
            activeDownloadFiles.put(tmpLock, Boolean.TRUE);
        } else if (lock == null) {
            try {
                tmpLock.close();
            } catch (IOException ex) {
            }
        }
        return new FileLockCompanion(tmpFile, lock, tmpFile.getPath() + ".lock");
    } catch (OverlappingFileLockException ex) {
        return new FileLockCompanion(tmpFile, null);
    } catch (IOException ex) {
        return new FileLockCompanion(tmpFile, null);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileLock(java.nio.channels.FileLock) IOException(java.io.IOException) 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