KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > FileLock


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.filesystems;
20
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.util.logging.Level JavaDoc;
23 import java.util.logging.Logger JavaDoc;
24
25
26 /** Represents an acquired lock on a <code>FileObject</code>.
27 * Typical usage includes locking the file in the editor on first
28 * modification, and then using this object to ensure exclusive access when
29 * overwriting the file (saving) by using {@link FileObject#getOutputStream}.
30 * Also used for renames, deletes, &amp;c.
31 * <p>Note that such locks are only used to protect against concurrent write accesses,
32 * and are not used for read operations (i.e. they are <em>not</em> write-one-read-many locks).
33 * Normally this is sufficient protection. If you really need an atomic read, you may
34 * simply lock the file, perform the read, and unlock it when done. The file will still
35 * be protected against writes, although the read operation did not request a lock.
36 *
37 * @see FileObject
38 *
39 * @author Petr Hamernik, Jaroslav Tulach, Ian Formanek
40 * @version 0.16, Jun 5, 1997
41 *
42 */

43 public class FileLock extends Object JavaDoc {
44     // ========================= NONE file lock =====================================
45

46     /** Constant that can be used in filesystems that do not support locking.
47      * Represents a lock which is never valid.
48     */

49     public static final FileLock NONE = new FileLock() {
50             /** @return false always. */
51             public boolean isValid() {
52                 return false;
53             }
54         };
55
56     /** Determines if lock is locked or if it was released. */
57     private boolean locked = true;
58     private Throwable JavaDoc lockedBy;
59
60     public FileLock() {
61         assert (lockedBy = new Throwable JavaDoc()) != null;
62     }
63
64     // ===============================================================================
65
// This part of code could be used for monitoring of closing file streams.
66

67     /* public static java.util.HashMap locks = new java.util.HashMap();
68       public FileLock() {
69         locks.put(this, new Exception()); int size = locks.size();
70         System.out.println ("locks:"+(size-1)+" => "+size);
71       }
72       public void releaseLock() {
73         locked = false; locks.remove(this); int size = locks.size();
74         System.out.println ("locks:"+(size+1)+" => "+size);
75       } */

76
77     // End of the debug part
78
// ============================================================================
79
// Begin of the original part
80

81     /** Release this lock.
82     * In typical usage this method will be called in a <code>finally</code> clause.
83     */

84     public void releaseLock() {
85         locked = false;
86     }
87
88     // End of the original part
89
// ============================================================================
90

91     /** Test whether this lock is still active, or released.
92     * @return <code>true</code> if lock is still active
93     */

94     public boolean isValid() {
95         return locked;
96     }
97
98     /** Finalize this object. Calls {@link #releaseLock} to release the lock if the program
99     * for some reason failed to.
100     */

101     public void finalize() {
102         assert (!isValid()) : assertMessageForInvalidLocks();
103         releaseLock();
104     }
105
106     private String JavaDoc assertMessageForInvalidLocks() {
107         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
108
109         if (lockedBy != null) {
110             Logger.getLogger(FileLock.class.getName()).log(Level.WARNING, null,
111                               new Exception JavaDoc("Not released lock for file: " +
112                                             toString() +
113                                             " (traped in finalizer)").initCause(lockedBy));//NOI18N
114
}
115
116         releaseLock();
117         return bos.toString();
118     }
119 }
120
Popular Tags