KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > store > Lock


1 package org.apache.lucene.store;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.lucene.index.IndexWriter;
20
21 import java.io.IOException JavaDoc;
22
23 /** An interprocess mutex lock.
24  * <p>Typical use might look like:<pre>
25  * new Lock.With(directory.makeLock("my.lock")) {
26  * public Object doBody() {
27  * <i>... code to execute while locked ...</i>
28  * }
29  * }.run();
30  * </pre>
31  *
32  * @author Doug Cutting
33  * @version $Id: Lock.java 179414 2005-06-01 20:10:58Z dnaber $
34  * @see Directory#makeLock(String)
35  */

36 public abstract class Lock {
37   public static long LOCK_POLL_INTERVAL = 1000;
38
39   /** Attempts to obtain exclusive access and immediately return
40    * upon success or failure.
41    * @return true iff exclusive access is obtained
42    */

43   public abstract boolean obtain() throws IOException JavaDoc;
44
45   /** Attempts to obtain an exclusive lock within amount
46    * of time given. Currently polls once per second until
47    * lockWaitTimeout is passed.
48    * @param lockWaitTimeout length of time to wait in ms
49    * @return true if lock was obtained
50    * @throws IOException if lock wait times out or obtain() throws an IOException
51    */

52   public boolean obtain(long lockWaitTimeout) throws IOException JavaDoc {
53     boolean locked = obtain();
54     int maxSleepCount = (int)(lockWaitTimeout / LOCK_POLL_INTERVAL);
55     int sleepCount = 0;
56     while (!locked) {
57       if (sleepCount++ == maxSleepCount) {
58         throw new IOException JavaDoc("Lock obtain timed out: " + this.toString());
59       }
60       try {
61         Thread.sleep(LOCK_POLL_INTERVAL);
62       } catch (InterruptedException JavaDoc e) {
63         throw new IOException JavaDoc(e.toString());
64       }
65       locked = obtain();
66     }
67     return locked;
68   }
69
70   /** Releases exclusive access. */
71   public abstract void release();
72
73   /** Returns true if the resource is currently locked. Note that one must
74    * still call {@link #obtain()} before using the resource. */

75   public abstract boolean isLocked();
76
77
78   /** Utility class for executing code with exclusive access. */
79   public abstract static class With {
80     private Lock lock;
81     private long lockWaitTimeout;
82
83     /** Constructs an executor that will grab the named lock.
84      * Defaults lockWaitTimeout to Lock.COMMIT_LOCK_TIMEOUT.
85      * @deprecated Kept only to avoid breaking existing code.
86      */

87     public With(Lock lock)
88     {
89       this(lock, IndexWriter.COMMIT_LOCK_TIMEOUT);
90     }
91
92     /** Constructs an executor that will grab the named lock. */
93     public With(Lock lock, long lockWaitTimeout) {
94       this.lock = lock;
95       this.lockWaitTimeout = lockWaitTimeout;
96     }
97
98     /** Code to execute with exclusive access. */
99     protected abstract Object JavaDoc doBody() throws IOException JavaDoc;
100
101     /** Calls {@link #doBody} while <i>lock</i> is obtained. Blocks if lock
102      * cannot be obtained immediately. Retries to obtain lock once per second
103      * until it is obtained, or until it has tried ten times. Lock is released when
104      * {@link #doBody} exits. */

105     public Object JavaDoc run() throws IOException JavaDoc {
106       boolean locked = false;
107       try {
108          locked = lock.obtain(lockWaitTimeout);
109          return doBody();
110       } finally {
111         if (locked)
112           lock.release();
113       }
114     }
115   }
116
117 }
118
Popular Tags