1 package org.apache.lucene.store;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 import java.io.IOException;
24 * <p>Implements {@link LockFactory} using {@link
25 * File#createNewFile()}.</p>
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>
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
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>
54 public class SimpleFSLockFactory extends FSLockFactory {
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
63 public SimpleFSLockFactory() throws IOException {
68 * Instantiate using the provided directory (as a File instance).
69 * @param lockDir where lock files should be created.
71 public SimpleFSLockFactory(File lockDir) throws IOException {
76 * Instantiate using the provided directory name (String).
77 * @param lockDirName where lock files should be created.
79 public SimpleFSLockFactory(String lockDirName) throws IOException {
80 setLockDir(new File(lockDirName));
84 public Lock makeLock(String lockName) {
85 if (lockPrefix != null) {
86 lockName = lockPrefix + "-" + lockName;
88 return new SimpleFSLock(lockDir, lockName);
92 public void clearLock(String lockName) throws IOException {
93 if (lockDir.exists()) {
94 if (lockPrefix != null) {
95 lockName = lockPrefix + "-" + lockName;
97 File lockFile = new File(lockDir, lockName);
98 if (lockFile.exists() && !lockFile.delete()) {
99 throw new IOException("Cannot delete " + lockFile);
105 class SimpleFSLock extends Lock {
110 public SimpleFSLock(File lockDir, String lockFileName) {
111 this.lockDir = lockDir;
112 lockFile = new File(lockDir, lockFileName);
116 public boolean obtain() throws IOException {
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());
127 return lockFile.createNewFile();
131 public void release() throws LockReleaseFailedException {
132 if (lockFile.exists() && !lockFile.delete())
133 throw new LockReleaseFailedException("failed to delete " + lockFile);
137 public boolean isLocked() {
138 return lockFile.exists();
142 public String toString() {
143 return "SimpleFSLock@" + lockFile;