Package java.nio.channels

Examples of java.nio.channels.FileLock


 
  /** @deprecated */ @Deprecated
    public void release(Path p) throws IOException {
    File f = pathToFile(p);
   
    FileLock lockObj;
    FileInputStream sharedLockData;
    FileOutputStream nonsharedLockData;
    synchronized (this) {
      lockObj = lockObjSet.remove(f);
      sharedLockData = sharedLockDataSet.remove(f);
      nonsharedLockData = nonsharedLockDataSet.remove(f);
    }
   
    if (lockObj == null) {
      throw new IOException("Given target not held as lock");
    }
    if (sharedLockData == null && nonsharedLockData == null) {
      throw new IOException("Given target not held as lock");
    }
   
    lockObj.release();
   
    if (sharedLockData != null) {
      sharedLockData.close();
    } else {
      nonsharedLockData.close();


  public synchronized boolean isLocked() throws IOException {
    if (fileLock != null)
      return true;
    try {
      RandomAccessFile temp = new RandomAccessFile(lockFile, "rw"); //$NON-NLS-1$
      FileLock tempLock = null;
      try {
        /*
         * fix for bug http://bugs.sun.com/view_bug.do?bug_id=6628575 and
         * https://bugs.eclipse.org/bugs/show_bug.cgi?id=44735#c17
         */
        try {
          tempLock = temp.getChannel().tryLock(0, 1, false);
        } catch (IOException ioe) {
          if (BasicLocation.DEBUG)
            System.out.println(NLS.bind(EclipseAdaptorMsg.location_cannotLock, lockFile));
          // produce a more specific message for clients
          String specificMessage = NLS.bind(EclipseAdaptorMsg.location_cannotLockNIO, new Object[] {lockFile, ioe.getMessage(), "\"-D" + BasicLocation.PROP_OSGI_LOCKING + "=none\""}); //$NON-NLS-1$ //$NON-NLS-2$
          throw new IOException(specificMessage);
        }
        if (tempLock != null) {
          tempLock.release(); // allow IOException to propagate because that would mean it is still locked
          return false;
        }
        return true;
      } catch (OverlappingFileLockException e) {
        return true;

    if (!oldF.exists())
      return false;
    // check the layout version inside the storage file
    // Lock and Read old storage file
    RandomAccessFile oldFile = new RandomAccessFile(oldF, "rws");
    FileLock oldLock = oldFile.getChannel().tryLock();
    try {
      oldFile.seek(0);
      int oldVersion = oldFile.readInt();
      if (oldVersion < LAST_PRE_UPGRADE_LAYOUT_VERSION)
        return false;
    } finally {
      oldLock.release();
      oldFile.close();
    }
    return true;
  }

      return getState() == FAILINGBACK;
   }

   public boolean isBackupLive() throws Exception
   {
      FileLock liveAttemptLock;
      liveAttemptLock = channel.tryLock(LIVE_LOCK_POS, LOCK_LENGTH, false);
      if(liveAttemptLock == null)
      {
         return true;
      }
      else
      {
         liveAttemptLock.release();
         return false;
      }
   }

         return channel.lock(liveLockPos, i, false);
      }
      catch (IOException e)
      {
         //todo this is here because sometimes channel.lock throws a resource deadlock exception but trylock works, need to investigate further and review
         FileLock lock;
         do
         {
            lock = channel.tryLock(liveLockPos, i, false);
            if (lock == null)
            {

     * @tests java.nio.channels.FileChannel#lock(long, long, boolean)
     */
    public void test_lockJJZ_NotOverlapping() throws Exception {
        final long POSITION = 100;
        final long SIZE = 200;
        FileLock fileLock1 = writeOnlyFileChannel.lock(POSITION, SIZE, false);
        assertTrue(fileLock1.isValid());
        FileLock fileLock2 = writeOnlyFileChannel.lock(POSITION + SIZE, SIZE,
                false);
        assertTrue(fileLock2.isValid());
    }

     * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean)
     */
    public void test_tryLockJJZ_NotOverlapping() throws Exception {
        final long POSITION = 100;
        final long SIZE = 200;
        FileLock fileLock1 = writeOnlyFileChannel
                .tryLock(POSITION, SIZE, false);
        assertTrue(fileLock1.isValid());

        FileLock fileLock2 = writeOnlyFileChannel.tryLock(POSITION + SIZE,
                SIZE, false);
        assertTrue(fileLock2.isValid());
    }

            if (LOG.isDebugEnabled()) {
                LOG.debug("Locking the file: " + file + " using the lock file name: " + lockFileName);
            }

            FileChannel channel = new RandomAccessFile(lockFileName, "rw").getChannel();
            FileLock lock = channel.lock();
            if (lock != null) {
                exchange.setProperty("org.apache.camel.file.lock", lock);
                exchange.setProperty("org.apache.camel.file.lock.name", lockFileName);
                return true;
            } else {

        this.lockFileRenamer = lockFileRenamer;
    }

    protected void unlockFile(FileEndpoint endpoint, FileExchange exchange, File file) throws Exception {
        if (isLockFile()) {
            FileLock lock = ExchangeHelper.getMandatoryProperty(exchange, "org.apache.camel.file.lock", FileLock.class);
            String lockFileName = ExchangeHelper.getMandatoryProperty(exchange, "org.apache.camel.file.lock.name", String.class);
            Channel channel = lock.channel();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Unlocking file: " + file);
            }
            try {
                lock.release();
            } finally {
                // must close channel
                ObjectHelper.close(channel, "Closing channel", LOG);

                if (LOG.isTraceEnabled()) {

    private void createSlowFile() throws Exception {
        LOG.info("Creating a slow file ...");
        File file = new File("./target/exclusiveread/slowfile/hello.txt");
        FileOutputStream fos = new FileOutputStream(file);
        FileLock lock = fos.getChannel().lock();
        fos.write("Hello World".getBytes());
        for (int i = 0; i < 3; i++) {
            Thread.sleep(1000);
            fos.write(("Line #" + i).getBytes());
            LOG.info("Appending to slowfile");
        }
        fos.write("Bye World".getBytes());
        lock.release();
        fos.close();
        LOG.info("... done creating slowfile");
    }

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.