pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / store / SimpleFSLockFactory.java
1 package org.apache.lucene.store;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.File;
21 import java.io.IOException;
22
23 /**
24  * <p>Implements {@link LockFactory} using {@link
25  * File#createNewFile()}.</p>
26  *
27  * <p><b>NOTE:</b> the <a target="_top"
28  * href="http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html#createNewFile()">javadocs
29  * for <code>File.createNewFile</code></a> contain a vague
30  * yet spooky warning about not using the API for file
31  * locking.  This warning was added due to <a target="_top"
32  * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4676183">this
33  * bug</a>, and in fact the only known problem with using
34  * this API for locking is that the Lucene write lock may
35  * not be released when the JVM exits abnormally.</p>
36
37  * <p>When this happens, a {@link LockObtainFailedException}
38  * is hit when trying to create a writer, in which case you
39  * need to explicitly clear the lock file first.  You can
40  * either manually remove the file, or use the {@link
41  * org.apache.lucene.index.IndexWriter#unlock(Directory)}
42  * API.  But, first be certain that no writer is in fact
43  * writing to the index otherwise you can easily corrupt
44  * your index.</p>
45  *
46  * <p>If you suspect that this or any other LockFactory is
47  * not working properly in your environment, you can easily
48  * test it by using {@link VerifyingLockFactory}, {@link
49  * LockVerifyServer} and {@link LockStressTest}.</p>
50  *
51  * @see LockFactory
52  */
53
54 public class SimpleFSLockFactory extends FSLockFactory {
55
56   /**
57    * Create a SimpleFSLockFactory instance, with null (unset)
58    * lock directory. When you pass this factory to a {@link FSDirectory}
59    * subclass, the lock directory is automatically set to the
60    * directory itself. Be sure to create one instance for each directory
61    * your create!
62    */
63   public SimpleFSLockFactory() throws IOException {
64     this((File) null);
65   }
66
67   /**
68    * Instantiate using the provided directory (as a File instance).
69    * @param lockDir where lock files should be created.
70    */
71   public SimpleFSLockFactory(File lockDir) throws IOException {
72     setLockDir(lockDir);
73   }
74
75   /**
76    * Instantiate using the provided directory name (String).
77    * @param lockDirName where lock files should be created.
78    */
79   public SimpleFSLockFactory(String lockDirName) throws IOException {
80     setLockDir(new File(lockDirName));
81   }
82
83   @Override
84   public Lock makeLock(String lockName) {
85     if (lockPrefix != null) {
86       lockName = lockPrefix + "-" + lockName;
87     }
88     return new SimpleFSLock(lockDir, lockName);
89   }
90
91   @Override
92   public void clearLock(String lockName) throws IOException {
93     if (lockDir.exists()) {
94       if (lockPrefix != null) {
95         lockName = lockPrefix + "-" + lockName;
96       }
97       File lockFile = new File(lockDir, lockName);
98       if (lockFile.exists() && !lockFile.delete()) {
99         throw new IOException("Cannot delete " + lockFile);
100       }
101     }
102   }
103 }
104
105 class SimpleFSLock extends Lock {
106
107   File lockFile;
108   File lockDir;
109
110   public SimpleFSLock(File lockDir, String lockFileName) {
111     this.lockDir = lockDir;
112     lockFile = new File(lockDir, lockFileName);
113   }
114
115   @Override
116   public boolean obtain() throws IOException {
117
118     // Ensure that lockDir exists and is a directory:
119     if (!lockDir.exists()) {
120       if (!lockDir.mkdirs())
121         throw new IOException("Cannot create directory: " +
122                               lockDir.getAbsolutePath());
123     } else if (!lockDir.isDirectory()) {
124       throw new IOException("Found regular file where directory expected: " + 
125                             lockDir.getAbsolutePath());
126     }
127     return lockFile.createNewFile();
128   }
129
130   @Override
131   public void release() throws LockReleaseFailedException {
132     if (lockFile.exists() && !lockFile.delete())
133       throw new LockReleaseFailedException("failed to delete " + lockFile);
134   }
135
136   @Override
137   public boolean isLocked() {
138     return lockFile.exists();
139   }
140
141   @Override
142   public String toString() {
143     return "SimpleFSLock@" + lockFile;
144   }
145 }