Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache.kahadb.util » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.kahadb.util;
   18   
   19   import java.io.File;
   20   import java.io.IOException;
   21   import java.io.RandomAccessFile;
   22   import java.nio.channels.FileLock;
   23   import java.nio.channels.OverlappingFileLockException;
   24   
   25   /**
   26    * Used to lock a File.
   27    * 
   28    * @author chirino
   29    */
   30   public class LockFile {
   31       
   32       private static final boolean DISABLE_FILE_LOCK = "true".equals(System.getProperty("java.nio.channels.FileLock.broken", "false"));
   33       final private File file;
   34       
   35       private FileLock lock;
   36       private RandomAccessFile readFile;
   37       private int lockCounter;
   38       private final boolean deleteOnUnlock;
   39       
   40       public LockFile(File file, boolean deleteOnUnlock) {
   41           this.file = file;
   42           this.deleteOnUnlock = deleteOnUnlock;
   43       }
   44   
   45       /**
   46        * @throws IOException
   47        */
   48       synchronized public void lock() throws IOException {
   49           if (DISABLE_FILE_LOCK) {
   50               return;
   51           }
   52   
   53           if( lockCounter>0 ) {
   54               return;
   55           }
   56           
   57           IOHelper.mkdirs(file.getParentFile());
   58           
   59           if (lock == null) {
   60               readFile = new RandomAccessFile(file, "rw");
   61               IOException reason = null;
   62               try {
   63                   lock = readFile.getChannel().tryLock();
   64               } catch (OverlappingFileLockException e) {
   65                   reason = IOExceptionSupport.create("File '" + file + "' could not be locked.",e);
   66               }
   67               if (lock != null) {
   68                   lockCounter++;
   69               } else {
   70                   // new read file for next attempt
   71                   closeReadFile();
   72                   if (reason != null) {
   73                       throw reason;
   74                   }
   75                   throw new IOException("File '" + file + "' could not be locked.");
   76               }
   77                 
   78           }
   79       }
   80   
   81       /**
   82        */
   83       public void unlock() {
   84           if (DISABLE_FILE_LOCK) {
   85               return;
   86           }
   87           
   88           lockCounter--;
   89           if( lockCounter!=0 ) {
   90               return;
   91           }
   92           
   93           // release the lock..
   94           if (lock != null) {
   95               try {
   96                   lock.release();
   97               } catch (Throwable ignore) {
   98               }
   99               lock = null;
  100           }
  101           closeReadFile();
  102           
  103           if( deleteOnUnlock ) {
  104               file.delete();
  105           }
  106       }
  107   
  108       private void closeReadFile() {
  109           // close the file.
  110           if (readFile != null) {
  111               try {
  112                   readFile.close();
  113               } catch (Throwable ignore) {
  114               }
  115               readFile = null;
  116           }
  117           
  118       }
  119   
  120   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache.kahadb.util » [javadoc | source]