Examples of FileLock


Examples of java.nio.channels.FileLock

   * Unlock directory.
   *
   * @throws IOException
   */
  private void unlock(File dir) throws IOException {
    FileLock lock = locks.remove(dir.getAbsolutePath());
    if(lock == null) {
      return;
    }
    lock.release();
    lock.channel().close();
    lock = null;
  }

Examples of java.nio.channels.FileLock

   * @throws IOException
   * @see StorageDirectory#lock()
   */
  public boolean isLockSupported(int idx) throws IOException {
    StorageDirectory sd = storageDirs.get(idx);
    FileLock firstLock = null;
    FileLock secondLock = null;
    try {
      firstLock = sd.lock;
      if(firstLock == null) {
        firstLock = sd.tryLock();
        if(firstLock == null)
          return true;
      }
      secondLock = sd.tryLock();
      if(secondLock == null)
        return true;
    } finally {
      if(firstLock != null && firstLock != sd.lock) {
        firstLock.release();
        firstLock.channel().close();
      }
      if(secondLock != null) {
        secondLock.release();
        secondLock.channel().close();
      }
    }
    return false;
  }

Examples of java.nio.channels.FileLock

     */
    FileLock tryLock() throws IOException {
      File lockF = new File(root, STORAGE_FILE_LOCK);
      lockF.deleteOnExit();
      RandomAccessFile file = new RandomAccessFile(lockF, "rws");
      FileLock res = null;
      try {
        res = file.getChannel().tryLock();
      } catch(OverlappingFileLockException oe) {
        file.close();
        return null;

Examples of java.nio.channels.FileLock

        // Since the sge_ca infrastructure can not be shared
        // between processes we have to create a file lock
        File lockFile = null;
        RandomAccessFile raf = null;
        FileLock lock = null;
        if(setLock) {
            lockFile = new File(config.getCaLocalTop(), "lock");
           
            try {
                raf = new RandomAccessFile(lockFile, "rw");
            } catch (IOException ex) {
                throw RB.newGridCAException(ex, "gridCAImpl.error.lockFileNotFound",
                        new Object [] { lockFile, ex.getLocalizedMessage() });
            }


           
            try {
                lock = raf.getChannel().lock();
            } catch (IOException ex) {
                throw RB.newGridCAException(ex, "gridCAImpl.error.lock",
                          new Object [] { lockFile, ex.getLocalizedMessage() });
            }
        }       
        try {
            ErrorHandler eh = new ErrorHandler();
            pb.add(eh);
            int res;
            try {
                res = pb.exec(60 * 1000);
            } catch (IOException ex) {
                throw RB.newGridCAException(ex, "gridCAImpl.sge_ca.ioError",
                                            ex.getLocalizedMessage());
            } catch (InterruptedException ex) {
                throw RB.newGridCAException("gridCAImpl.sge_ca.interrupt");
            }
            if(res != 0) {
                String error = eh.getError();
                if(error == null || error.length() == 0) {
                    throw RB.newGridCAException("gridCAImpl.sge_ca.unknownError",
                                                new Integer(res));
                } else {
                    throw RB.newGridCAException("gridCAImpl.sge_ca.error",
                                                new Object [] { new Integer(res), error });
                }
            }
        } finally {
            if (setLock) {
                try {
                    lock.release();
                } catch (IOException ex) {
                    // Ingore
                }
                try {
                    raf.close();

Examples of java.nio.channels.FileLock

        assert id != null;
        boolean completed = false;
        boolean delete = false;
        File path = idToFile(id);
        RandomAccessFile file = null;
        FileLock lock = null;
        try {
            LOG.debug("Opening session file: {}", path);
            file = new RandomAccessFile(path, "rw");
            lock = acquireLock(id, path, file);
            State state = getSessionState(id, path, file);
            switch (state) {
            case INIT:
                if (create == false) {
                    delete = true;
                    throw new SessionException(id, Reason.NOT_EXIST);
                } else {
                    createSession(path, file);
                }
                break;
            case CREATED:
                if (create) {
                    throw new SessionException(id, Reason.ALREADY_EXIST);
                }
                break;
            case INVALID:
                if (force == false) {
                    WGLOG.error("W01001",
                            id,
                            path);
                    throw new SessionException(id, Reason.BROKEN);
                }
                break;
            default:
                throw new AssertionError(MessageFormat.format(
                        "Invalid state: {2} (id={0}, path={1})",
                        id,
                        path,
                        state));
            }
            completed = true;
            return new FileSessionMirror(id, path, file, lock);
        } catch (SessionException e) {
            throw e;
        } catch (IOException e) {
            WGLOG.error(e, "E01001",
                    id,
                    path);
            throw e;
        } finally {
            if (completed == false) {
                if (delete) {
                    try {
                        invalidate(path, file);
                    } catch (IOException e) {
                        WGLOG.warn(e, "W02002",
                                id,
                                path);
                    }
                }
                if (lock != null) {
                    try {
                        lock.release();
                    } catch (IOException e) {
                        WGLOG.warn(e, "W02001",
                                id,
                                path);
                    }

Examples of java.nio.channels.FileLock

        assert id != null;
        assert path != null;
        assert file != null;
        LOG.debug("Acquiring lock: {}", id);
        try {
            FileLock lock = file.getChannel().tryLock();
            if (lock != null) {
                return lock;
            }
            LOG.debug("Failed to acquire lock: {}", id);
        } catch (OverlappingFileLockException e) {

Examples of java.nio.channels.FileLock

        }

        private FileLock acquireFileLock() throws IOException {
            assert file != null;
            LOG.debug("Acquiring lock: {}", label);
            FileLock flock;
            try {
                flock = file.getChannel().tryLock();
                if (flock != null) {
                    return flock;
                }

Examples of java.nio.channels.FileLock

                LOG.debug(MessageFormat.format(
                        "Start to acquire lock for \"{0}\" ({1})",
                        target,
                        lockFile));
            }
            FileLock lockEntity = getLock(target, lockFile, fd);
            if (lockEntity == null) {
                return null;
            } else {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(MessageFormat.format(
                            "Finished to acquire lock for \"{0}\" ({1})",
                            target,
                            lockFile));
                }
                try {
                    fd.seek(0L);
                    fd.setLength(0L);
                    fd.write(String.valueOf(target).getBytes(ENCODING));
                    success = true;
                    return new LocalFileLockObject<T>(target, lockFile, fd, lockEntity);
                } finally {
                    if (success == false) {
                        lockEntity.release();
                    }
                }
            }
        } finally {
            if (success == false) {

Examples of java.nio.channels.FileLock

        }
    }

    private FileLock getLock(T target, File lockFile, RandomAccessFile fd) throws IOException {
        try {
            FileLock result = fd.getChannel().tryLock();
            if (result == null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(MessageFormat.format(
                            "Failed to acquire lock for \"{0}\" ({1})",
                            target,

Examples of java.nio.channels.FileLock

      File infoFile = new File(lockDir, INFO_FILE_NAME);
      File lockedFile = new File(lockDir, LOCK_FILE_NAME);

      RandomAccessFile raf = new RandomAccessFile(lockedFile, "rw");
      try {
        FileLock fileLock = raf.getChannel().lock();
        lock = createLock(raf, fileLock);
        sign(infoFile);
      }
      catch (IOException e) {
        if (lock != null) {
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.