Examples of FileLock


Examples of java.nio.channels.FileLock

                if (lockType == SqlJetLockType.SHARED
                        || (lockType == SqlJetLockType.EXCLUSIVE && this.lockType.compareTo(SqlJetLockType.PENDING) < 0)) {

                    if (lockType != SqlJetLockType.SHARED) {
            if (lockInfo.sharedLockCount <= 1) {
                          final FileLock sharedLock = locks.remove(SqlJetLockType.SHARED);
                          if(null != sharedLock) {
                            sharedLock.release();
                            lockInfo.sharedLock = null;
                          }
            }
                    }

                    if (!locks.containsKey(SqlJetLockType.PENDING)) {
                        final FileLock pendingLock = fileLockManager.tryLock(PENDING_BYTE, 1,
                                lockType == SqlJetLockType.SHARED);
                        if (null == pendingLock)
                            return false;
                        locks.put(SqlJetLockType.PENDING, pendingLock);
                    }
                }

                /*
                 * If control gets to this point, then actually go ahead and
                 * make operating system calls for the specified lock.
                 */
                if (lockType == SqlJetLockType.SHARED) {

                    /* Now get the read-lock */
                    final FileLock sharedLock = fileLockManager.tryLock(SHARED_FIRST, SHARED_SIZE, true);
                    locks.put(SqlJetLockType.SHARED, sharedLock);

                    /* Drop the temporary PENDING lock */
                    final FileLock pendingLock = locks.get(SqlJetLockType.PENDING);
                    if (null != pendingLock) {
                        pendingLock.release();
                        locks.remove(SqlJetLockType.PENDING);
                    }

                    if (null == sharedLock)
                        return false;

                    this.lockType = SqlJetLockType.SHARED;
                    openCount.numLock++;
                    lockInfo.sharedLockCount = 1;
                    lockInfo.sharedLock = sharedLock;

                } else if (lockType == SqlJetLockType.EXCLUSIVE && lockInfo.sharedLockCount > 1) {
                    /*
                     * We are trying for an exclusive lock but another thread in
                     * this same process is still holding a shared lock.
                     */
                    return false;

                } else {
                    /*
                     * The request was for a RESERVED or EXCLUSIVE lock. It is
                     * assumed that there is a SHARED or greater lock on the
                     * file already.
                     */
                    assert (SqlJetLockType.NONE != this.lockType);

                    switch (lockType) {
                    case RESERVED:
                        final FileLock reservedLock = fileLockManager.tryLock(RESERVED_BYTE, 1, false);
                        if (null == reservedLock)
                            return false;
                        locks.put(SqlJetLockType.RESERVED, reservedLock);
                        break;
                    case EXCLUSIVE:
                        final FileLock exclusiveLock = fileLockManager.tryLock(SHARED_FIRST, SHARED_SIZE, false);
                        if (null == exclusiveLock) {
                            this.lockType = SqlJetLockType.PENDING;
                            lockInfo.lockType = SqlJetLockType.PENDING;
                            return false;
                        }

Examples of java.nio.channels.FileLock

                if (SqlJetLockType.SHARED.compareTo(this.lockType) < 0) {

                    if (SqlJetLockType.SHARED == lockType) {

                        final FileLock exclusiveLock = locks.get(SqlJetLockType.EXCLUSIVE);
                        if (null != exclusiveLock) {
                            if (exclusiveLock.isValid())
                                exclusiveLock.release();
                            locks.remove(SqlJetLockType.EXCLUSIVE);
                        }

                        if (null == locks.get(SqlJetLockType.SHARED)) {
                            final FileLock sharedLock = fileLockManager.lock(SHARED_FIRST, SHARED_SIZE, true);
                            if (null == sharedLock)
                                return false;
                            locks.put(SqlJetLockType.SHARED, sharedLock);
                            lockInfo.sharedLock = sharedLock;
                        }

                    }

                    final FileLock reservedLock = locks.get(SqlJetLockType.RESERVED);
                    if (null != reservedLock) {
                        if (reservedLock.isValid())
                            reservedLock.release();
                        locks.remove(SqlJetLockType.RESERVED);
                    }

                    final FileLock pendingLock = locks.get(SqlJetLockType.PENDING);
                    if (null != pendingLock) {
                        if (pendingLock.isValid())
                            pendingLock.release();
                        locks.remove(SqlJetLockType.PENDING);
                    }

                    lockInfo.lockType = SqlJetLockType.SHARED;

Examples of java.nio.channels.FileLock

                    return true;

                /* Otherwise see if some other process holds it. */
                try {

                    final FileLock reservedLock = fileLockManager.tryLock(RESERVED_BYTE, 1, false);

                    if (null == reservedLock) {
                        reserved = true;
                        return true;
                    }

                    reservedLock.release();

                } catch (IOException e) {
                }

            }

Examples of java.nio.channels.FileLock

        RandomAccessFile randomAccessFile = new RandomAccessFile(testFile,"rw");
        byte[] result = new byte[(int) randomAccessFile.length()];
        ByteBuffer buffer = ByteBuffer.wrap(result);
        try {
            randomAccessFile.getChannel().read(buffer, 0);
            FileLock fileLock = randomAccessFile.getChannel().tryLock(0,1,false);//read(buffer, 0);
            if(fileLock!=null){
                throw new IOException();
            }
        } catch (Exception e) {
            sessionContainer.close();

Examples of java.nio.channels.FileLock

   * storage directory. Otherwise, no guarantee is given.
   *
   * @throws IOException if locking fails
   */
  private void lock(File dir) throws IOException {
    FileLock lock = tryLock(dir);
    if (lock == null) {
      String msg = "Cannot lock " + dir
          + ". The directory is already locked. "
          + channelNameDescriptor;
      LOGGER.info(msg);
      throw new IOException(msg);
    }
    FileLock secondLock = tryLock(dir);
    if(secondLock != null) {
      LOGGER.warn("Directory "+dir+" does not support locking");
      secondLock.release();
      secondLock.channel().close();
    }
    locks.put(dir.getAbsolutePath(), lock);
  }

Examples of java.nio.channels.FileLock

  @SuppressWarnings("resource")
  private FileLock tryLock(File dir) throws IOException {
    File lockF = new File(dir, 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

   * 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

                                                FileOutputStream fos)
        throws IOException {
       
        // See if a file channel and lock can be obtained on the FileOutputStream
        FileChannel channel = null;
        FileLock lock = null;
        ByteBuffer bb = null;
        try {
            channel = fos.getChannel();
            if (channel != null) {
                lock = channel.tryLock();
            }
            bb = getTempByteBuffer();
        } catch (Throwable t) {
        }
        if (lock == null || bb == null || !bb.hasArray()) {
            releaseTempByteBuffer(bb);
            return false// lock could not be set or bb does not have direct array access
        }
       
        try {

            // Read directly into the ByteBuffer array
            int bytesRead = is.read(bb.array());
            while (bytesRead != -1) {
                int written = 0;
               
               
                if (bytesRead < BUFFER_LEN) {
                    // If the ByteBuffer is not full, allocate a new one
                    ByteBuffer temp = ByteBuffer.allocate(bytesRead);
                    temp.put(bb.array(), 0, bytesRead);
                    temp.position(0);
                    written = channel.write(temp);
                } else {
                    // Write to channel
                    bb.position(0);
                    written = channel.write(bb);
                    bb.clear();
                }
               
                // REVIEW: Do we need to ensure that bytesWritten is
                // the same as the number of bytes sent ?
               
                bytesRead = is.read(bb.array());
            }
        } finally {
            // Release the lock
           lock.release();
           releaseTempByteBuffer(bb);
        }
        return true;
    }

Examples of java.nio.channels.FileLock

   * storage directory. Otherwise, no guarantee is given.
   *
   * @throws IOException if locking fails
   */
  private void lock(File dir) throws IOException {
    FileLock lock = tryLock(dir);
    if (lock == null) {
      String msg = "Cannot lock " + dir
          + ". The directory is already locked. "
          + channelNameDescriptor;
      LOGGER.info(msg);
      throw new IOException(msg);
    }
    FileLock secondLock = tryLock(dir);
    if(secondLock != null) {
      LOGGER.warn("Directory "+dir+" does not support locking");
      secondLock.release();
      secondLock.channel().close();
    }
    locks.put(dir.getAbsolutePath(), lock);
  }

Examples of java.nio.channels.FileLock

  @SuppressWarnings("resource")
  private FileLock tryLock(File dir) throws IOException {
    File lockF = new File(dir, 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;
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.