Java Code Examples for java.nio.channels.FileLock#isValid()

The following examples show how to use java.nio.channels.FileLock#isValid() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AtlasFileLock.java    From AtlasForAndroid with MIT License 6 votes vote down vote up
public boolean LockExclusive(File file) {
    if (file == null) {
        return false;
    }
    try {
        FileChannel channel = new RandomAccessFile(file.getAbsolutePath(), "rw").getChannel();
        if (channel == null) {
            return false;
        }
        Log.i(TAG, processName + " attempting to FileLock " + file);
        FileLock lock = channel.lock();
        if (!lock.isValid()) {
            return false;
        }
        RefCntInc(file.getAbsolutePath(), lock);
        Log.i(TAG, processName + " FileLock " + file + " Suc! ");
        return true;
    } catch (Exception e) {
        Log.e(TAG, processName + " FileLock " + file + " FAIL! " + e.getMessage());
        return false;
    }
}
 
Example 2
Source File: OpenAtlasFileLock.java    From ACDD with MIT License 6 votes vote down vote up
/**
 * lock odex
 *
 * @param bundleDexFile optimize dex file
 **/
public boolean LockExclusive(File bundleDexFile) {

    if (bundleDexFile == null) {
        return false;
    }
    try {
        File lockFile = new File(bundleDexFile.getParentFile().getAbsolutePath().concat("/lock"));
        if (!lockFile.exists()) {
            lockFile.createNewFile();
        }
        RandomAccessFile randomAccessFile = new RandomAccessFile(lockFile.getAbsolutePath(), "rw");
        FileChannel channel = randomAccessFile.getChannel();
        FileLock lock = channel.lock();
        if (!lock.isValid()) {
            return false;
        }
        RefCntInc(lockFile.getAbsolutePath(), lock, randomAccessFile, channel);
        return true;
    } catch (Exception e) {
        log.error(processName + " FileLock " + bundleDexFile.getParentFile().getAbsolutePath().concat("/lock") + " Lock FAIL! " + e.getMessage());
        return false;
    }
}
 
Example 3
Source File: DiskStoreImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
void closeLockFile() {
  FileLock myfl = this.fl;
  if (myfl != null) {
    try {
      FileChannel fc = myfl.channel();
      if (myfl.isValid()) {
        myfl.release();
      }
      fc.close();
    } catch (IOException ignore) {
    }
    this.fl = null;
  }
  File f = this.lockFile;
  if (f != null) {
    if (f.delete()) {
      if (logger.fineEnabled()) {
        logger.fine("Deleted lock file " + f);
      }
    } else if (f.exists()) {
      if (logger.fineEnabled()) {
        logger.fine("Could not delete lock file " + f);
      }
    }
  }
  logger.info(LocalizedStrings.DEBUG, "Unlocked disk store " + name); // added
                                                                      // to
                                                                      // help
                                                                      // debug
                                                                      // 41734
}
 
Example 4
Source File: Fetcher.java    From tez with Apache License 2.0 5 votes vote down vote up
private void releaseLock(FileLock lock) throws IOException {
  if (lock != null && lock.isValid()) {
    FileChannel lockChannel = lock.channel();
    lock.release();
    lockChannel.close();
  }
}
 
Example 5
Source File: AtlasFileLock.java    From AtlasForAndroid with MIT License 5 votes vote down vote up
public void unLock(File file) {
    if (file == null || this.mRefCountMap.containsKey(file.getAbsolutePath())) {
        FileLock fileLock = ((FileLockCount) this.mRefCountMap.get(file.getAbsolutePath())).mFileLock;
        if (fileLock != null && fileLock.isValid()) {
            try {
                if (RefCntDec(file.getAbsolutePath()) <= 0) {
                    fileLock.release();
                    Log.i(TAG, processName + " FileLock " + file.getAbsolutePath() + " SUC! ");
                }
            } catch (IOException e) {
            }
        }
    }
}
 
Example 6
Source File: FileChannelImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected void implCloseChannel() throws IOException {
    // Release and invalidate any locks that we still hold
    if (fileLockTable != null) {
        for (FileLock fl: fileLockTable.removeAll()) {
            synchronized (fl) {
                if (fl.isValid()) {
                    nd.release(fd, fl.position(), fl.size());
                    ((FileLockImpl)fl).invalidate();
                }
            }
        }
    }

    // signal any threads blocked on this channel
    threads.signalAndWait();

    if (parent != null) {

        // Close the fd via the parent stream's close method.  The parent
        // will reinvoke our close method, which is defined in the
        // superclass AbstractInterruptibleChannel, but the isOpen logic in
        // that method will prevent this method from being reinvoked.
        //
        ((java.io.Closeable)parent).close();
    } else {
        nd.close(fd);
    }

}
 
Example 7
Source File: FileChannelImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected void implCloseChannel() throws IOException {
    // Release and invalidate any locks that we still hold
    if (fileLockTable != null) {
        for (FileLock fl: fileLockTable.removeAll()) {
            synchronized (fl) {
                if (fl.isValid()) {
                    nd.release(fd, fl.position(), fl.size());
                    ((FileLockImpl)fl).invalidate();
                }
            }
        }
    }

    // signal any threads blocked on this channel
    threads.signalAndWait();

    if (parent != null) {

        // Close the fd via the parent stream's close method.  The parent
        // will reinvoke our close method, which is defined in the
        // superclass AbstractInterruptibleChannel, but the isOpen logic in
        // that method will prevent this method from being reinvoked.
        //
        ((java.io.Closeable)parent).close();
    } else {
        nd.close(fd);
    }

}
 
Example 8
Source File: DiskStoreImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
void closeLockFile() {
  FileLock myfl = this.fl;
  if (myfl != null) {
    try {
      FileChannel fc = myfl.channel();
      if (myfl.isValid()) {
        myfl.release();
      }
      fc.close();
    } catch (IOException ignore) {
    }
    this.fl = null;
  }
  File f = this.lockFile;
  if (f != null) {
    if (f.delete()) {
      if (logger.fineEnabled()) {
        logger.fine("Deleted lock file " + f);
      }
    } else if (f.exists()) {
      if (logger.fineEnabled()) {
        logger.fine("Could not delete lock file " + f);
      }
    }
  }
  logger.info(LocalizedStrings.DEBUG, "Unlocked disk store " + name); // added
                                                                      // to
                                                                      // help
                                                                      // debug
                                                                      // 41734
}
 
Example 9
Source File: OpenAtlasFileLock.java    From ACDD with MIT License 5 votes vote down vote up
/**
 * unlock odex file
 **/
public void unLock(File bundleDexFile) {

    File lockFile = new File(bundleDexFile.getParentFile().getAbsolutePath().concat("/lock"));
    if (!lockFile.exists()) {
        return;
    }
    if (lockFile == null || this.mRefCountMap.containsKey(lockFile.getAbsolutePath())) {
        FileLockCount fileLockCount = this.mRefCountMap.get(lockFile.getAbsolutePath());
        if (fileLockCount != null) {
            FileLock fileLock = fileLockCount.mFileLock;
            RandomAccessFile randomAccessFile = fileLockCount.fOs;
            FileChannel fileChannel = fileLockCount.fChannel;
            try {
                if (RefCntDec(lockFile.getAbsolutePath()) <= 0) {
                    if (fileLock != null && fileLock.isValid()) {
                        fileLock.release();
                    }
                    if (randomAccessFile != null) {
                        randomAccessFile.close();
                    }
                    if (fileChannel != null) {
                        fileChannel.close();
                    }
                }
            } catch (IOException e) {
                log.error(processName + " FileLock " + bundleDexFile.getParentFile().getAbsolutePath().concat("/lock") + " unlock FAIL! " + e.getMessage());
            }
        }
    }
}
 
Example 10
Source File: FileChannelImpl.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
protected void implCloseChannel() throws IOException {
    // Release and invalidate any locks that we still hold
    if (fileLockTable != null) {
        for (FileLock fl: fileLockTable.removeAll()) {
            synchronized (fl) {
                if (fl.isValid()) {
                    nd.release(fd, fl.position(), fl.size());
                    ((FileLockImpl)fl).invalidate();
                }
            }
        }
    }

    // signal any threads blocked on this channel
    threads.signalAndWait();

    if (parent != null) {

        // Close the fd via the parent stream's close method.  The parent
        // will reinvoke our close method, which is defined in the
        // superclass AbstractInterruptibleChannel, but the isOpen logic in
        // that method will prevent this method from being reinvoked.
        //
        ((java.io.Closeable)parent).close();
    } else {
        nd.close(fd);
    }

}
 
Example 11
Source File: FileChannelImpl.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
protected void implCloseChannel() throws IOException {
    // Release and invalidate any locks that we still hold
    if (fileLockTable != null) {
        for (FileLock fl: fileLockTable.removeAll()) {
            synchronized (fl) {
                if (fl.isValid()) {
                    nd.release(fd, fl.position(), fl.size());
                    ((FileLockImpl)fl).invalidate();
                }
            }
        }
    }

    // signal any threads blocked on this channel
    threads.signalAndWait();

    if (parent != null) {

        // Close the fd via the parent stream's close method.  The parent
        // will reinvoke our close method, which is defined in the
        // superclass AbstractInterruptibleChannel, but the isOpen logic in
        // that method will prevent this method from being reinvoked.
        //
        ((java.io.Closeable)parent).close();
    } else {
        nd.close(fd);
    }

}
 
Example 12
Source File: FileChannelImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
protected void implCloseChannel() throws IOException {
    // Release and invalidate any locks that we still hold
    if (fileLockTable != null) {
        for (FileLock fl: fileLockTable.removeAll()) {
            synchronized (fl) {
                if (fl.isValid()) {
                    nd.release(fd, fl.position(), fl.size());
                    ((FileLockImpl)fl).invalidate();
                }
            }
        }
    }

    // signal any threads blocked on this channel
    threads.signalAndWait();

    if (parent != null) {

        // Close the fd via the parent stream's close method.  The parent
        // will reinvoke our close method, which is defined in the
        // superclass AbstractInterruptibleChannel, but the isOpen logic in
        // that method will prevent this method from being reinvoked.
        //
        ((java.io.Closeable)parent).close();
    } else {
        nd.close(fd);
    }

}
 
Example 13
Source File: FileChannelImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected void implCloseChannel() throws IOException {
    // Release and invalidate any locks that we still hold
    if (fileLockTable != null) {
        for (FileLock fl: fileLockTable.removeAll()) {
            synchronized (fl) {
                if (fl.isValid()) {
                    nd.release(fd, fl.position(), fl.size());
                    ((FileLockImpl)fl).invalidate();
                }
            }
        }
    }

    // signal any threads blocked on this channel
    threads.signalAndWait();

    if (parent != null) {

        // Close the fd via the parent stream's close method.  The parent
        // will reinvoke our close method, which is defined in the
        // superclass AbstractInterruptibleChannel, but the isOpen logic in
        // that method will prevent this method from being reinvoked.
        //
        ((java.io.Closeable)parent).close();
    } else {
        nd.close(fd);
    }

}
 
Example 14
Source File: FileChannelImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
protected void implCloseChannel() throws IOException {
    if (!fd.valid())
        return; // nothing to do

    // Release and invalidate any locks that we still hold
    if (fileLockTable != null) {
        for (FileLock fl: fileLockTable.removeAll()) {
            synchronized (fl) {
                if (fl.isValid()) {
                    nd.release(fd, fl.position(), fl.size());
                    ((FileLockImpl)fl).invalidate();
                }
            }
        }
    }

    // signal any threads blocked on this channel
    threads.signalAndWait();

    if (parent != null) {

        // Close the fd via the parent stream's close method.  The parent
        // will reinvoke our close method, which is defined in the
        // superclass AbstractInterruptibleChannel, but the isOpen logic in
        // that method will prevent this method from being reinvoked.
        //
        ((java.io.Closeable)parent).close();
    } else {
        nd.close(fd);
    }

}
 
Example 15
Source File: FileChannelImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
protected void implCloseChannel() throws IOException {
    if (!fd.valid())
        return; // nothing to do

    // Release and invalidate any locks that we still hold
    if (fileLockTable != null) {
        for (FileLock fl: fileLockTable.removeAll()) {
            synchronized (fl) {
                if (fl.isValid()) {
                    nd.release(fd, fl.position(), fl.size());
                    ((FileLockImpl)fl).invalidate();
                }
            }
        }
    }

    // signal any threads blocked on this channel
    threads.signalAndWait();

    if (parent != null) {

        // Close the fd via the parent stream's close method.  The parent
        // will reinvoke our close method, which is defined in the
        // superclass AbstractInterruptibleChannel, but the isOpen logic in
        // that method will prevent this method from being reinvoked.
        //
        ((java.io.Closeable)parent).close();
    } else if (closer != null) {
        // Perform the cleaning action so it is not redone when
        // this channel becomes phantom reachable.
        try {
            closer.clean();
        } catch (UncheckedIOException uioe) {
            throw uioe.getCause();
        }
    } else {
        fdAccess.close(fd);
    }

}
 
Example 16
Source File: FileChannelImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
protected void implCloseChannel() throws IOException {
    // Release and invalidate any locks that we still hold
    if (fileLockTable != null) {
        for (FileLock fl: fileLockTable.removeAll()) {
            synchronized (fl) {
                if (fl.isValid()) {
                    nd.release(fd, fl.position(), fl.size());
                    ((FileLockImpl)fl).invalidate();
                }
            }
        }
    }

    // signal any threads blocked on this channel
    threads.signalAndWait();

    if (parent != null) {

        // Close the fd via the parent stream's close method.  The parent
        // will reinvoke our close method, which is defined in the
        // superclass AbstractInterruptibleChannel, but the isOpen logic in
        // that method will prevent this method from being reinvoked.
        //
        ((java.io.Closeable)parent).close();
    } else {
        nd.close(fd);
    }

}
 
Example 17
Source File: FileChannelImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
protected void implCloseChannel() throws IOException {
    // Release and invalidate any locks that we still hold
    if (fileLockTable != null) {
        for (FileLock fl: fileLockTable.removeAll()) {
            synchronized (fl) {
                if (fl.isValid()) {
                    nd.release(fd, fl.position(), fl.size());
                    ((FileLockImpl)fl).invalidate();
                }
            }
        }
    }

    // signal any threads blocked on this channel
    threads.signalAndWait();

    if (parent != null) {

        // Close the fd via the parent stream's close method.  The parent
        // will reinvoke our close method, which is defined in the
        // superclass AbstractInterruptibleChannel, but the isOpen logic in
        // that method will prevent this method from being reinvoked.
        //
        ((java.io.Closeable)parent).close();
    } else {
        nd.close(fd);
    }

}
 
Example 18
Source File: FileChannelImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
protected void implCloseChannel() throws IOException {
    // Release and invalidate any locks that we still hold
    if (fileLockTable != null) {
        for (FileLock fl: fileLockTable.removeAll()) {
            synchronized (fl) {
                if (fl.isValid()) {
                    nd.release(fd, fl.position(), fl.size());
                    ((FileLockImpl)fl).invalidate();
                }
            }
        }
    }

    // signal any threads blocked on this channel
    threads.signalAndWait();

    if (parent != null) {

        // Close the fd via the parent stream's close method.  The parent
        // will reinvoke our close method, which is defined in the
        // superclass AbstractInterruptibleChannel, but the isOpen logic in
        // that method will prevent this method from being reinvoked.
        //
        ((java.io.Closeable)parent).close();
    } else {
        nd.close(fd);
    }

}
 
Example 19
Source File: FileChannelImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
protected void implCloseChannel() throws IOException {
    // Release and invalidate any locks that we still hold
    if (fileLockTable != null) {
        for (FileLock fl: fileLockTable.removeAll()) {
            synchronized (fl) {
                if (fl.isValid()) {
                    nd.release(fd, fl.position(), fl.size());
                    ((FileLockImpl)fl).invalidate();
                }
            }
        }
    }

    // signal any threads blocked on this channel
    threads.signalAndWait();

    if (parent != null) {

        // Close the fd via the parent stream's close method.  The parent
        // will reinvoke our close method, which is defined in the
        // superclass AbstractInterruptibleChannel, but the isOpen logic in
        // that method will prevent this method from being reinvoked.
        //
        ((java.io.Closeable)parent).close();
    } else {
        nd.close(fd);
    }

}