Package java.nio.channels

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;


                    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) {
                }

            }

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

   * 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);
  }

  @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;

   * 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;
  }

                                                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;
    }

   * 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);
  }

  @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;

   * 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;
  }

TOP

Related Classes of java.nio.channels.FileLock

Copyright © 2018 www.massapicom. 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.