KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > persist > NIOLockFile


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.persist;
33
34 import java.nio.channels.FileChannel JavaDoc;
35 import java.nio.channels.FileLock JavaDoc;
36
37 /**
38  * A LockFile variant that capitalizes upon the
39  * availability of {@link java.nio.channels.FileLock FileLock}.
40  *
41  * @author boucherb@users
42  * @version 1.7.2
43  * @since 1.7.2
44  *
45  */

46 final class NIOLockFile extends LockFile {
47
48     // From the java.nio.channels.FileLock API docs:
49
//
50
// Some network filesystems do not implement file locks on regions
51
// that extend past a certain position, often 2**30 or 2**31.
52
// In general, great care should be taken when locking files that
53
// reside on network filesystems.
54
static final long MAX_NFS_LOCK_REGION = (1L << 30);
55     static final long MIN_LOCK_REGION = MAGIC.length + 8;
56
57     /**
58      * A <code>FileChannel</code> object obtained from the super
59      * <code>raf</code> attribute. <p>
60      *
61      * The <code>fc</code> attribute is used to obtain this object's
62      * {@link #fl FileLock} attribute.
63      */

64     private FileChannel JavaDoc fc;
65
66     /**
67      * The <code>FileLock</code> object used to lock this object's
68      * lock file.
69      */

70     private FileLock JavaDoc fl;
71
72     /**
73      * Tries to obtain a valid NIO lock upon this object's lock file using
74      * this object's {@link #fl FileLock} attribute.
75      *
76      * @return true if a valid lock is obtained, else false.
77      * @throws Exception if an error occurs while attempting to obtain the lock
78      *
79      */

80     protected boolean lockImpl() throws Exception JavaDoc {
81
82         boolean isValid;
83
84         if (fl != null && fl.isValid()) {
85             return true;
86         }
87
88         trace("lockImpl(): fc = raf.getChannel()");
89
90         fc = raf.getChannel();
91
92         trace("lockImpl(): fl = fc.tryLock()");
93
94         fl = null;
95
96         try {
97             fl = fc.tryLock(0, MIN_LOCK_REGION, false);
98
99             trace("lockImpl(): fl = " + fl);
100         } catch (Exception JavaDoc e) {
101
102             // This will not work with a localized JVM
103
/*
104             if (-1 == e.toString().indexOf("No locks available")) {
105                 throw e;
106             } else {
107                 trace(e.toString());
108             }
109             */

110             trace(e.toString());
111         }
112
113 // In an ideal world, maybe?:
114
// try {
115
// fl = fc.tryLock();
116
//
117
// trace("lockImpl(): fl = " + fl);
118
// } catch (Exception e) {
119
// trace(e.toString());
120
//
121
// try {
122
// fl = fc.tryLock(0, MAX_NFS_LOCK_REGION, false);
123
//
124
// trace("lockImpl(): fl = " + fl);
125
// trace("Warning: possibly attempting to lock on NFS");
126
// } catch (Exception e2) {
127
// trace(e2.toString());
128
//
129
// try {
130
// fl = fc.tryLock(0, MIN_LOCK_REGION, false);
131
//
132
// trace("lockImpl(): fl = " + fl);
133
// trace("Warning: backed off to min lock region");
134
// trace("Warning: lock file may be unusable on reuse");
135
// } catch (Exception e3) {
136
// trace(e3.toString());
137
// }
138
// }
139
// }
140
trace("lockImpl(): f.deleteOnExit()");
141         f.deleteOnExit();
142
143         isValid = fl != null && fl.isValid();
144
145         trace("lockImpl():isValid(): " + isValid);
146
147         return isValid;
148     }
149
150     /**
151      * Tries to release any valid lock held upon this object's lock file using
152      * this object's {@link #fl FileLock} attribute.
153      *
154      * @return true if a valid lock is released, else false
155      * @throws Exception if na error occurs while attempting to release the lock
156      */

157     protected boolean releaseImpl() throws Exception JavaDoc {
158
159         // PRE: we know that this method is only called
160
// if isLocked() is true.
161
trace("releaseImpl(): fl = " + fl);
162
163         if (fl != null) {
164             trace("releaseImpl(): fl.release()");
165             fl.release();
166             trace("tryRelease(): fl = " + fl);
167
168             fl = null;
169         }
170
171         trace("releaseImpl(): fc = " + fc);
172
173         if (fc != null) {
174             trace("releaseImpl(): fc.close()");
175             fc.close();
176
177             fc = null;
178         }
179
180         // CHECKME:
181
// possibly overcomes some regarding full and
182
// true release of FileLock and maybe related
183
// NIO resources?
184
// System.gc();
185
return true;
186     }
187
188     /**
189      * Retrieves whether this object's {@link #fl FileLock} attribute represents
190      * a valid lock upon this object's lock file.
191      *
192      * @return true if this object's {@link #fl FileLock} attribute is valid,
193      * else false
194      */

195     public boolean isValid() {
196         return super.isValid() && (fl != null && fl.isValid());
197     }
198
199     /**
200      * Retrieves the String value: "fl =" + fl
201      * @return the String value: "fl =" + fl
202      */

203     protected String JavaDoc toStringImpl() {
204         return "fl =" + fl;
205     }
206 }
207
Popular Tags