KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > nio > channels > FileLock


1 /*
2  * @(#)FileLock.java 1.8 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.nio.channels;
9
10 import java.io.IOException JavaDoc;
11
12
13 /**
14  * A token representing a lock on a region of a file.
15  *
16  * <p> A file-lock object is created each time a lock is acquired on a file via
17  * one of the {@link FileChannel#lock(long,long,boolean) lock} or {@link
18  * FileChannel#tryLock(long,long,boolean) tryLock} methods of the {@link
19  * FileChannel} class.
20  *
21  * <p> A file-lock object is initially valid. It remains valid until the lock
22  * is released by invoking the {@link #release release} method, by closing the
23  * channel that was used to acquire it, or by the termination of the Java
24  * virtual machine, whichever comes first. The validity of a lock may be
25  * tested by invoking its {@link #isValid isValid} method.
26  *
27  * <p> A file lock is either <i>exclusive</i> or <i>shared</i>. A shared lock
28  * prevents other concurrently-running programs from acquiring an overlapping
29  * exclusive lock, but does allow them to acquire overlapping shared locks. An
30  * exclusive lock prevents other programs from acquiring an overlapping lock of
31  * either type. Once it is released, a lock has no further effect on the locks
32  * that may be acquired by other programs.
33  *
34  * <p> Whether a lock is exclusive or shared may be determined by invoking its
35  * {@link #isShared isShared} method. Some platforms do not support shared
36  * locks, in which case a request for a shared lock is automatically converted
37  * into a request for an exclusive lock.
38  *
39  * <p> The locks held on a particular file by a single Java virtual machine do
40  * not overlap. The {@link #overlaps overlaps} method may be used to test
41  * whether a candidate lock range overlaps an existing lock.
42  *
43  * <p> A file-lock object records the file channel upon whose file the lock is
44  * held, the type and validity of the lock, and the position and size of the
45  * locked region. Only the validity of a lock is subject to change over time;
46  * all other aspects of a lock's state are immutable.
47  *
48  * <p> File locks are held on behalf of the entire Java virtual machine.
49  * They are not suitable for controlling access to a file by multiple
50  * threads within the same virtual machine.
51  *
52  * <p> File-lock objects are safe for use by multiple concurrent threads.
53  *
54  *
55  * <a name="pdep">
56  * <h4> Platform dependencies </h4>
57  *
58  * <p> This file-locking API is intended to map directly to the native locking
59  * facility of the underlying operating system. Thus the locks held on a file
60  * should be visible to all programs that have access to the file, regardless
61  * of the language in which those programs are written.
62  *
63  * <p> Whether or not a lock actually prevents another program from accessing
64  * the content of the locked region is system-dependent and therefore
65  * unspecified. The native file-locking facilities of some systems are merely
66  * <i>advisory</i>, meaning that programs must cooperatively observe a known
67  * locking protocol in order to guarantee data integrity. On other systems
68  * native file locks are <i>mandatory</i>, meaning that if one program locks a
69  * region of a file then other programs are actually prevented from accessing
70  * that region in a way that would violate the lock. On yet other systems,
71  * whether native file locks are advisory or mandatory is configurable on a
72  * per-file basis. To ensure consistent and correct behavior across platforms,
73  * it is strongly recommended that the locks provided by this API be used as if
74  * they were advisory locks.
75  *
76  * <p> On some systems, acquiring a mandatory lock on a region of a file
77  * prevents that region from being {@link java.nio.channels.FileChannel#map
78  * </code>mapped into memory<code>}, and vice versa. Programs that combine
79  * locking and mapping should be prepared for this combination to fail.
80  *
81  * <p> On some systems, closing a channel releases all locks held by the Java
82  * virtual machine on the underlying file regardless of whether the locks were
83  * acquired via that channel or via another channel open on the same file. It
84  * is strongly recommended that, within a program, a unique channel be used to
85  * acquire all locks on any given file.
86  *
87  * <p> Some network filesystems permit file locking to be used with
88  * memory-mapped files only when the locked regions are page-aligned and a
89  * whole multiple of the underlying hardware's page size. Some network
90  * filesystems do not implement file locks on regions that extend past a
91  * certain position, often 2<sup>30</sup> or 2<sup>31</sup>. In general, great
92  * care should be taken when locking files that reside on network filesystems.
93  *
94  *
95  * @author Mark Reinhold
96  * @author JSR-51 Expert Group
97  * @version 1.8, 03/12/19
98  * @since 1.4
99  */

100
101 public abstract class FileLock {
102
103     private final FileChannel JavaDoc channel;
104     private final long position;
105     private final long size;
106     private final boolean shared;
107
108     /**
109      * Initializes a new instance of this class. </p>
110      *
111      * @param channel
112      * The file channel upon whose file this lock is held
113      *
114      * @param position
115      * The position within the file at which the locked region starts;
116      * must be non-negative
117      *
118      * @param size
119      * The size of the locked region; must be non-negative, and the sum
120      * <tt>position</tt>&nbsp;+&nbsp;<tt>size</tt> must be non-negative
121      *
122      * @param shared
123      * <tt>true</tt> if this lock is shared,
124      * <tt>false</tt> if it is exclusive
125      *
126      * @throws IllegalArgumentException
127      * If the preconditions on the parameters do not hold
128      */

129     protected FileLock(FileChannel JavaDoc channel,
130                long position, long size, boolean shared)
131     {
132     if (position < 0)
133         throw new IllegalArgumentException JavaDoc("Negative position");
134     if (size < 0)
135         throw new IllegalArgumentException JavaDoc("Negative size");
136     if (position + size < 0)
137         throw new IllegalArgumentException JavaDoc("Negative position + size");
138     this.channel = channel;
139     this.position = position;
140     this.size = size;
141     this.shared = shared;
142     }
143
144     /**
145      * Returns the file channel upon whose file this lock is held. </p>
146      *
147      * @return The file channel
148      */

149     public final FileChannel JavaDoc channel() {
150     return channel;
151     }
152
153     /**
154      * Returns the position within the file of the first byte of the locked
155      * region.
156      *
157      * <p> A locked region need not be contained within, or even overlap, the
158      * actual underlying file, so the value returned by this method may exceed
159      * the file's current size. </p>
160      *
161      * @return The position
162      */

163     public final long position() {
164     return position;
165     }
166
167     /**
168      * Returns the size of the locked region in bytes.
169      *
170      * <p> A locked region need not be contained within, or even overlap, the
171      * actual underlying file, so the value returned by this method may exceed
172      * the file's current size. </p>
173      *
174      * @return The size of the locked region
175      */

176     public final long size() {
177     return size;
178     }
179
180     /**
181      * Tells whether this lock is shared. </p>
182      *
183      * @return <tt>true</tt> if lock is shared,
184      * <tt>false</tt> if it is exclusive
185      */

186     public final boolean isShared() {
187     return shared;
188     }
189
190     /**
191      * Tells whether or not this lock overlaps the given lock range. </p>
192      *
193      * @return <tt>true</tt> if, and only if, this lock and the given lock
194      * range overlap by at least one byte
195      */

196     public final boolean overlaps(long position, long size) {
197     if (position + size <= this.position)
198         return false; // That is below this
199
if (this.position + this.size <= position)
200         return false; // This is below that
201
return true;
202     }
203
204     /**
205      * Tells whether or not this lock is valid.
206      *
207      * <p> A lock object remains valid until it is released or the associated
208      * file channel is closed, whichever comes first. </p>
209      *
210      * @return <tt>true</tt> if, and only if, this lock is valid
211      */

212     public abstract boolean isValid();
213
214     /**
215      * Releases this lock.
216      *
217      * <p> If this lock object is valid then invoking this method releases the
218      * lock and renders the object invalid. If this lock object is invalid
219      * then invoking this method has no effect. </p>
220      *
221      * @throws ClosedChannelException
222      * If the channel that was used to acquire this lock
223      * is no longer open
224      *
225      * @throws IOException
226      * If an I/O error occurs
227      */

228     public abstract void release() throws IOException JavaDoc;
229
230     /**
231      * Returns a string describing the range, type, and validity of this lock.
232      *
233      * @return A descriptive string
234      */

235     public final String JavaDoc toString() {
236     return (this.getClass().getName()
237         + "[" + position
238         + ":" + size
239         + " " + (shared ? "shared" : "exclusive")
240         + " " + (isValid() ? "valid" : "invalid")
241         + "]");
242     }
243
244 }
245
Popular Tags