Package java.nio.channels

Examples of java.nio.channels.FileLock


    // check the layout version inside the storage file
    // Lock and Read old storage file
    RandomAccessFile oldFile = new RandomAccessFile(oldF, "rws");
    if (oldFile == null)
      throw new IOException("Cannot read file: " + oldF);
    FileLock oldLock = oldFile.getChannel().tryLock();
    try {
      oldFile.seek(0);
      int odlVersion = oldFile.readInt();
      if (odlVersion < LAST_PRE_UPGRADE_LAYOUT_VERSION)
        return false;
    } finally {
      oldLock.release();
      oldFile.close();
    }
    // check consistency of the old storage
    File oldDataDir = new File(sd.root, "data");
    if (!oldDataDir.exists())


   
    // Lock and Read old storage file
    RandomAccessFile oldFile = new RandomAccessFile(oldF, "rws");
    if (oldFile == null)
      throw new IOException("Cannot read file: " + oldF);
    FileLock oldLock = oldFile.getChannel().tryLock();
    if (oldLock == null)
      throw new IOException("Cannot lock file: " + oldF);
    String odlStorageID = "";
    try {
      oldFile.seek(0);
      int odlVersion = oldFile.readInt();
      if (odlVersion < LAST_PRE_UPGRADE_LAYOUT_VERSION)
        throw new IncorrectVersionException(odlVersion, "file " + oldF,
                                            LAST_PRE_UPGRADE_LAYOUT_VERSION);
      odlStorageID = org.apache.hadoop.io.UTF8.readString(oldFile);
 
      // check new storage
      File newDataDir = sd.getCurrentDir();
      File versionF = sd.getVersionFile();
      if (versionF.exists())
        throw new IOException("Version file already exists: " + versionF);
      if (newDataDir.exists()) // somebody created current dir manually
        deleteDir(newDataDir);
      // move "data" to "current"
      rename(oldDataDir, newDataDir);
      // close and unlock old file
    } finally {
      oldLock.release();
      oldFile.close();
    }
    // move old storage file into current dir
    rename(oldF, new File(sd.getCurrentDir(), "storage"));

    File f = pathToFile(p);
    f.createNewFile();
   
    if (shared) {
      FileInputStream lockData = new FileInputStream(f);
      FileLock lockObj =
        lockData.getChannel().lock(0L, Long.MAX_VALUE, shared);
      synchronized (this) {
        sharedLockDataSet.put(f, lockData);
        lockObjSet.put(f, lockObj);
      }
    } else {
      FileOutputStream lockData = new FileOutputStream(f);
      FileLock lockObj = lockData.getChannel().lock(0L, Long.MAX_VALUE, shared);
      synchronized (this) {
        nonsharedLockDataSet.put(f, lockData);
        lockObjSet.put(f, lockObj);
      }
    }

 
  /** @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();

    private synchronized void writeToFile(String action) {
        Date date = new Date();
        try {
            FileOutputStream out = new FileOutputStream(logFile, true);
            FileChannel channel = out.getChannel();
            FileLock lock = channel.lock(0, Long.MAX_VALUE, false);
            PrintWriter writer = new PrintWriter(out, false);
            writer.println(DATE_FORMAT.format(date)+" - "+action+" - "+username);
            writer.flush();
            writer.close();
            if(lock.isValid()) {
                lock.release();
            }
        } catch (IOException e) {
            throw new RuntimeException("Unable to write to authentication log file", e);
        }
    }

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

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

        }

        // try to acquire rw lock on the file before we can consume it
        FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
        try {
            FileLock lock = channel.lock();
            if (LOG.isTraceEnabled()) {
                LOG.trace("Acquired exclusive read lock: " + lock + " to file: " + file);
            }
            // just release it now we dont want to hold it during the rest of the processing
            lock.release();
        } finally {
            // must close channel
            ObjectHelper.close(channel, "FileConsumer during acquiring of exclusive read lock", LOG);
        }
    }

    File f = pathToFile(p);
    f.createNewFile();
   
    if (shared) {
      FileInputStream lockData = new FileInputStream(f);
      FileLock lockObj =
        lockData.getChannel().lock(0L, Long.MAX_VALUE, shared);
      synchronized (this) {
        sharedLockDataSet.put(f, lockData);
        lockObjSet.put(f, lockObj);
      }
    } else {
      FileOutputStream lockData = new FileOutputStream(f);
      FileLock lockObj = lockData.getChannel().lock(0L, Long.MAX_VALUE, shared);
      synchronized (this) {
        nonsharedLockDataSet.put(f, lockData);
        lockObjSet.put(f, lockObj);
      }
    }

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.