Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors 
   * as indicated by the @author tags. 
   * See the copyright.txt in the distribution for a
   * full listing of individual contributors. 
   * This copyrighted material is made available to anyone wishing to use,
   * modify, copy, or redistribute it subject to the terms and conditions
   * of the GNU Lesser General Public License, v. 2.1.
  * This program is distributed in the hope that it will be useful, but WITHOUT A 
  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
  * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
  * You should have received a copy of the GNU Lesser General Public License,
  * v.2.1 along with this distribution; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
  * MA  02110-1301, USA.
  * 
  * (C) 2005-2006,
  * @author JBoss Inc.
  */
 /*
  * Copyright (C) 1998, 1999, 2000, 2001,
  *
  * Arjuna Solutions Limited,
  * Newcastle upon Tyne,
  * Tyne and Wear,
  * UK.
  *
  * $Id: FileLock.java 2342 2006-03-30 13:06:17Z  $
  */
 
 package com.arjuna.ats.arjuna.utils;
 
 import java.io.File;
 
Sometimes it is necessary to lock a file at the disk level. Since there is no native Java way of locking a file, we have to implement our own. Unfortunately, it appears as though we can only assume that rename is atomic. We base the locking on this then: rename the lock file and update it with the lock owners. How it works: for every file we want to lock we create an _lock file. This file contains information about who is locking the file, and in what mode. (Single writer, multiple readers.) To guarantee atomicity of update, we move (rename) the actual file each time we want to lock it and update the lock file. When this is done, we move (rename) it back. Almost like a two-phase commit protocol! Currently we don't support re-entrant locking.

Author(s):
Mark Little (mark@arjuna.com)
Version:
$Id: FileLock.java 2342 2006-03-30 13:06:17Z $
Since:
JTS 1.0.
 
 
 public class FileLock
 {
 
     public static final int F_RDLCK = 0;
 
     public static final int F_WRLCK = 1;
 
     public static final int defaultTimeout = 10; // milliseconds
 
     public static final int defaultRetry = 10;
 
     public FileLock (String name)
     {
         this(new File(name));
         
         .deleteOnExit();
     }
     
     public FileLock(File name)
     {
         this(name..);
     }
 
     public FileLock(File namelong timeoutlong retry)
     {
         if (..isTraceEnabled()) {
             ..trace("FileLock ( " + name
                     + ", " + timeout + ", " + retry + " )");
         }
 
          = name;
          = new File(name.toString() + "_lock");
          = new File(name.toString() + "_lock.lock");
          = timeout;
          = retry;
         
         .deleteOnExit();
         .deleteOnExit();
    }

    

Since:
JTS 2.1.1.
    public boolean lock (int lmode)
    {
        return lock(lmodefalse);
    }
    public synchronized boolean lock (int lmodeboolean create)
    {
        if (..isTraceEnabled()) {
            ..trace("FileLock.lock called for "+);
        }
        boolean created = false;
        
        if (create && !.exists())
        {
            createFile();
            
            created = true;
        }
        /*
         * If the lock file exists, and the mode is exclusive, then we can
         * immediately return false. Currently we do not implement re-entrant
         * locking, which requires some owner id.
         */
        if (!created && .exists() && (lmode == .))
            return false;
        int number = 0;
        if (lockFile()) // have we moved the file (if it exists)?
        {
            try
            {
                DataInputStream ifile = new DataInputStream(
                        new FileInputStream());
                int value = ifile.readInt();
                /*
                 * Already exclusively locked.
                 */
                if (value == .)
                {
                    ifile.close();
                    unlockFile();
                    return false;
                }
                else
                    number = ifile.readInt();
                ifile.close();
            }
            catch (FileNotFoundException e)
            {
            }
            catch (IOException e)
            {
                /*
                 * Something went wrong. Abandon.
                 */
                unlockFile();
                return false;
            }
            try
            {
                DataOutputStream ofile = new DataOutputStream(
                        new FileOutputStream());
                number++;
                ofile.writeInt(lmode);
                ofile.writeInt(number);
                ofile.close();
                unlockFile();
                return true;
            }
            catch (IOException e)
            {
                /*
                 * Something went wrong. Abandon. Lock file is ok since we
                 * haven't touched it.
                 */
                unlockFile();
                return false;
            }
        }
        return false;
    }
    public synchronized boolean unlock ()
    {
        if (..isTraceEnabled()) {
            ..trace("FileLock.unlock called "+);
        }
        if (!.exists())
            return false;
        if (lockFile())
        {
            int number = 0, mode = 0;
            try
            {
                DataInputStream ifile = new DataInputStream(
                        new FileInputStream());
                mode = ifile.readInt();
                number = ifile.readInt();
                ifile.close();
                number--;
                if (number == 0)
                {
                    .delete();
                    unlockFile();
                    return true;
                }
            }
            catch (FileNotFoundException e)
            {
                unlockFile();
                return false;
            }
            catch (IOException e)
            {
                unlockFile();
                return false;
            }
            try
            {
                DataOutputStream ofile = new DataOutputStream(
                        new FileOutputStream());
                ofile.writeInt(mode);
                ofile.writeInt(number);
                ofile.close();
                unlockFile();
                return true;
            }
            catch (IOException e)
            {
                unlockFile();
                return false;
            }
        }
        return false;
    }
    public static String modeString (int mode)
    {
        switch (mode)
        {
        case .:
            return "FileLock.F_RDLCK";
        case .:
            return "FileLock.F_WRLCK";
        default:
            return "Unknown";
        }
    }
    private final boolean createFile ()
    {
        if (..isTraceEnabled()) {
            ..trace("FileLock.createFile "+);
        }
        byte b[] = new byte[1];
        try
        {
            if (!.exists())
            {
                .createNewFile();
                return true;
            }
            else
                return false;
        }
        catch (IOException e) {
            return false;
        }
    }
    private final boolean lockFile ()
    {
        if (..isTraceEnabled()) {
            ..trace("FileLock.lockFile called "+);
        }
        
        for (int i = 0; i < i++)
        {
            try
            {
                if (.createNewFile())
                {
                    return true;
                }
                else
                {
                    try
                    {
                        Thread.sleep();
                    }
                    catch (InterruptedException e)
                    {
                    }
                }
            }
            catch (IOException ex)
            {
                // already created, so locked!
            }
        }
        return false;
    }
    private final boolean unlockFile ()
    {
        if (..isTraceEnabled()) {
            ..trace("FileLock.unlockFile called for "+);
        }
        return .delete();
    }
    private File _theFile;
    private File _lockFile;
    private File _lockFileLock;
    private long _timeout;
    private long _retry;
New to GrepCode? Check out our FAQ X