blob: c8a459ae46c82f534441f09a762968397c267933 [file] [log] [blame]
Index: src/test/org/apache/lucene/store/TestDirectory.java
===================================================================
--- src/test/org/apache/lucene/store/TestDirectory.java (revision 0)
+++ src/test/org/apache/lucene/store/TestDirectory.java (revision 0)
@@ -0,0 +1,42 @@
+package org.apache.lucene.store;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.lucene.util.LuceneTestCase;
+
+public class TestDirectory extends LuceneTestCase {
+
+ public void testDetectClose() throws Throwable {
+ Directory dir = new RAMDirectory();
+ dir.close();
+ try {
+ dir.createOutput("test");
+ fail("did not hit expected exception");
+ } catch (AlreadyClosedException ace) {
+ }
+
+ dir = FSDirectory.getDirectory(System.getProperty("tempDir"));
+ dir.close();
+ try {
+ dir.createOutput("test");
+ fail("did not hit expected exception");
+ } catch (AlreadyClosedException ace) {
+ }
+ }
+}
+
Property changes on: src/test/org/apache/lucene/store/TestDirectory.java
___________________________________________________________________
Name: svn:eol-style
+ native
Index: src/test/org/apache/lucene/index/TestDoc.java
===================================================================
--- src/test/org/apache/lucene/index/TestDoc.java (revision 674858)
+++ src/test/org/apache/lucene/index/TestDoc.java (working copy)
@@ -22,11 +22,9 @@
import org.apache.lucene.analysis.SimpleAnalyzer;
-import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.Directory;
import org.apache.lucene.document.Document;
-import org.apache.lucene.search.Similarity;
import org.apache.lucene.demo.FileDocument;
import java.io.*;
@@ -115,7 +113,6 @@
SegmentInfo si2 = indexDoc(writer, "test2.txt");
printSegment(out, si2);
writer.close();
- directory.close();
SegmentInfo siMerge = merge(si1, si2, "merge", false);
printSegment(out, siMerge);
@@ -126,6 +123,7 @@
SegmentInfo siMerge3 = merge(siMerge, siMerge2, "merge3", false);
printSegment(out, siMerge3);
+ directory.close();
out.close();
sw.close();
String multiFileOutput = sw.getBuffer().toString();
@@ -143,7 +141,6 @@
si2 = indexDoc(writer, "test2.txt");
printSegment(out, si2);
writer.close();
- directory.close();
siMerge = merge(si1, si2, "merge", true);
printSegment(out, siMerge);
@@ -154,6 +151,7 @@
siMerge3 = merge(siMerge, siMerge2, "merge3", true);
printSegment(out, siMerge3);
+ directory.close();
out.close();
sw.close();
String singleFileOutput = sw.getBuffer().toString();
Index: src/java/org/apache/lucene/store/RAMDirectory.java
===================================================================
--- src/java/org/apache/lucene/store/RAMDirectory.java (revision 674858)
+++ src/java/org/apache/lucene/store/RAMDirectory.java (working copy)
@@ -237,15 +237,7 @@
/** Closes the store to future operations, releasing associated memory. */
public void close() {
+ isOpen = false;
fileMap = null;
}
-
- /**
- * @throws AlreadyClosedException if this IndexReader is closed
- */
- protected final void ensureOpen() throws AlreadyClosedException {
- if (fileMap == null) {
- throw new AlreadyClosedException("this RAMDirectory is closed");
- }
- }
}
Index: src/java/org/apache/lucene/store/Directory.java
===================================================================
--- src/java/org/apache/lucene/store/Directory.java (revision 674858)
+++ src/java/org/apache/lucene/store/Directory.java (working copy)
@@ -38,6 +38,8 @@
*/
public abstract class Directory {
+ volatile boolean isOpen = true;
+
/** Holds the LockFactory instance (implements locking for
* this Directory instance). */
protected LockFactory lockFactory;
@@ -210,4 +212,12 @@
if(closeDirSrc)
src.close();
}
+
+ /**
+ * @throws AlreadyClosedException if this Directory is closed
+ */
+ protected final void ensureOpen() throws AlreadyClosedException {
+ if (!isOpen)
+ throw new AlreadyClosedException("this Directory is closed");
+ }
}
Index: src/java/org/apache/lucene/store/FSDirectory.java
===================================================================
--- src/java/org/apache/lucene/store/FSDirectory.java (revision 674858)
+++ src/java/org/apache/lucene/store/FSDirectory.java (working copy)
@@ -317,17 +317,20 @@
/** Returns an array of strings, one for each Lucene index file in the directory. */
public String[] list() {
+ ensureOpen();
return directory.list(IndexFileNameFilter.getFilter());
}
/** Returns true iff a file with the given name exists. */
public boolean fileExists(String name) {
+ ensureOpen();
File file = new File(directory, name);
return file.exists();
}
/** Returns the time the named file was last modified. */
public long fileModified(String name) {
+ ensureOpen();
File file = new File(directory, name);
return file.lastModified();
}
@@ -340,18 +343,21 @@
/** Set the modified time of an existing file to now. */
public void touchFile(String name) {
+ ensureOpen();
File file = new File(directory, name);
file.setLastModified(System.currentTimeMillis());
}
/** Returns the length in bytes of a file in the directory. */
public long fileLength(String name) {
+ ensureOpen();
File file = new File(directory, name);
return file.length();
}
/** Removes an existing file in the directory. */
public void deleteFile(String name) throws IOException {
+ ensureOpen();
File file = new File(directory, name);
if (!file.delete())
throw new IOException("Cannot delete " + file);
@@ -363,6 +369,7 @@
*/
public synchronized void renameFile(String from, String to)
throws IOException {
+ ensureOpen();
File old = new File(directory, from);
File nu = new File(directory, to);
@@ -427,7 +434,7 @@
/** Creates a new, empty file in the directory with the given name.
Returns a stream writing this file. */
public IndexOutput createOutput(String name) throws IOException {
-
+ ensureOpen();
File file = new File(directory, name);
if (file.exists() && !file.delete()) // delete existing, if any
throw new IOException("Cannot overwrite: " + file);
@@ -436,6 +443,7 @@
}
public void sync(String name) throws IOException {
+ ensureOpen();
File fullFile = new File(directory, name);
boolean success = false;
int retryCount = 0;
@@ -470,11 +478,13 @@
// Inherit javadoc
public IndexInput openInput(String name) throws IOException {
+ ensureOpen();
return openInput(name, BufferedIndexInput.BUFFER_SIZE);
}
// Inherit javadoc
public IndexInput openInput(String name, int bufferSize) throws IOException {
+ ensureOpen();
return new FSIndexInput(new File(directory, name), bufferSize);
}
@@ -486,6 +496,7 @@
public String getLockID() {
+ ensureOpen();
String dirName; // name to be hashed
try {
dirName = directory.getCanonicalPath();
@@ -510,7 +521,8 @@
/** Closes the store to future operations. */
public synchronized void close() {
- if (--refCount <= 0) {
+ if (isOpen && --refCount <= 0) {
+ isOpen = false;
synchronized (DIRECTORIES) {
DIRECTORIES.remove(directory);
}
@@ -518,6 +530,7 @@
}
public File getFile() {
+ ensureOpen();
return directory;
}