001: /*
002: * This file or a portion of this file is licensed under the terms of
003: * the Globus Toolkit Public License, found in file ../GTPL, or at
004: * http://www.globus.org/toolkit/download/license.html. This notice must
005: * appear in redistributions of this file, with or without modification.
006: *
007: * Redistributions of this Software, with or without modification, must
008: * reproduce the GTPL in: (1) the Software, or (2) the Documentation or
009: * some other similar material which is provided with the Software (if
010: * any).
011: *
012: * Copyright 1999-2004 University of Chicago and The University of
013: * Southern California. All rights reserved.
014: */
015: package org.griphyn.vdl.util;
016:
017: import org.griphyn.vdl.util.*;
018: import java.util.*;
019: import java.io.*;
020: import java.nio.channels.*;
021:
022: /**
023: * This class encapsulates the instance variables necessary to lock
024: * a file. The NIO new locking methods are required.
025: *
026: * @author Jens-S. Vöckler
027: * @author Yong Zhao
028: * @version $Revision: 50 $
029: *
030: * @see java.io.RandomAccessFile
031: * @see java.nio.channels.FileChannel
032: * @see java.nio.channels.FileLock
033: */
034: public class FcntlLock {
035: /**
036: * For logging purposes, which file is it...
037: */
038: private File m_file;
039:
040: /**
041: * The random access file that remains open during lock operations.
042: */
043: private RandomAccessFile m_channel;
044:
045: /**
046: * One lock, covering the whole file, during the operation.
047: */
048: private FileLock m_lock;
049:
050: /**
051: * Create a file lock on the given file.
052: *
053: * @param f is the file name to lock.
054: * @param rw is true for read/write mode, which will create missing
055: * files. If false, read-only mode is used.
056: * @param shared is true for shared locks, false for exclusive locks.
057: * @throws FileNotFoundException if the file was not found
058: * @throws IOException if the lock could not be acquired
059: */
060: public FcntlLock(File f, boolean rw, boolean shared)
061: throws FileNotFoundException, IOException {
062: Logging.instance().log(
063: "lock",
064: 2,
065: "requesting " + (shared ? "shared" : "exclusive")
066: + " lock on " + f.getPath());
067:
068: m_file = f;
069: m_channel = new RandomAccessFile(f, rw ? "rw" : "r");
070: m_lock = m_channel.getChannel()
071: .lock(0L, Long.MAX_VALUE, shared);
072: }
073:
074: /**
075: * Release the file lock acquired with this instance.
076: */
077: public void done() {
078: if (m_lock != null) {
079: Logging.instance().log("lock", 2,
080: "releasing lock on " + m_file.getPath());
081: try {
082: m_lock.release();
083: } catch (IOException ioe) {
084: }
085: m_lock = null;
086: }
087:
088: if (m_channel != null) {
089: try {
090: m_channel.close();
091: } catch (IOException ioe) {
092: }
093: m_channel = null;
094: }
095: }
096:
097: /**
098: * Releases all resource, if they were still active.
099: */
100: protected void finalize() throws Throwable {
101: done();
102: }
103: }
|