Example #1
0
  private static void acquireReadLock(Map<File, RandomAccessFile> lockFileMap, File indexDir)
      throws IOException, LockAquisitionException {
    final File writeLock = new File(indexDir, "delete.lock");
    writeLock.createNewFile();
    while (true) {
      synchronized (lockFileMap) {
        RandomAccessFile raf = lockFileMap.get(indexDir);
        if (raf == null) {
          raf = new RandomAccessFile(writeLock, "r");
          lockFileMap.put(indexDir, raf);
        } else {
          throw new AlreadyOpenException();
        }

        final FileChannel channel = raf.getChannel();
        try {
          channel.lock(0, Long.MAX_VALUE, true);
          if (indexDir.exists()) {
            return;
          }
          lockFileMap.remove(indexDir);
          Closeables2.closeQuietly(raf, log);
          throw new ShardDeletedException();
        } catch (OverlappingFileLockException e) {
          lockFileMap.remove(indexDir);
          Closeables2.closeQuietly(raf, log);
          throw Throwables.propagate(e);
        } catch (FileLockInterruptionException e) {
          lockFileMap.remove(indexDir);
          Closeables2.closeQuietly(raf, log);
        }
      }
    }
  }
Example #2
0
 @Override
 public void close() throws IOException {
   synchronized (lockFileMap) {
     final RandomAccessFile randomAccessFile = lockFileMap.get(indexDir);
     Closeables2.closeQuietly(randomAccessFile, log);
     lockFileMap.remove(indexDir);
   }
 }