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);
}
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);
}
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;
}
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());
}
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);
}
}
Aggregations