Index: src/test/org/apache/lucene/store/TestDirectory.java =================================================================== --- src/test/org/apache/lucene/store/TestDirectory.java (revision 713425) +++ src/test/org/apache/lucene/store/TestDirectory.java (working copy) @@ -38,5 +38,36 @@ } catch (AlreadyClosedException ace) { } } + + public void testFSDirectory() throws Throwable { + String path = System.getProperty("tempDir"); + + Directory dir = FSDirectory.getDirectory(path); + + // ensure that the same directory instance is returned, even though + // it's being opened from a different subclass + Directory dir2 = NIOFSDirectory.getDirectory(path); + assertTrue(dir == dir2); + dir2.close(); + + // ensure that the same directory instance is returned, even though + // it's being opened from a different subclass + dir2 = MMapDirectory.getDirectory(path); + assertTrue(dir == dir2); + dir2.close(); + + dir.close(); + // after the original directory is closed, we should be able to + // get different subclass implementations + dir = NIOFSDirectory.getDirectory(path); + assertTrue(dir instanceof NIOFSDirectory); + dir.close(); + + dir = MMapDirectory.getDirectory(path); + assertTrue(dir instanceof MMapDirectory); + dir.close(); + } + + } Index: src/java/org/apache/lucene/store/NIOFSDirectory.java =================================================================== --- src/java/org/apache/lucene/store/NIOFSDirectory.java (revision 713425) +++ src/java/org/apache/lucene/store/NIOFSDirectory.java (working copy) @@ -43,6 +43,43 @@ public class NIOFSDirectory extends FSDirectory { + /** Returns the directory instance for the named location. + * @param path the path to the directory. + * @return the FSDirectory for the named file. */ + public static FSDirectory getDirectory(String path) + throws IOException { + return getDirectory(new File(path), null); + } + + /** Returns the directory instance for the named location. + * @param path the path to the directory. + * @param lockFactory instance of {@link LockFactory} providing the + * locking implementation. + * @return the FSDirectory for the named file. */ + public static FSDirectory getDirectory(String path, LockFactory lockFactory) + throws IOException { + return getDirectory(new File(path), lockFactory); + } + + /** Returns the directory instance for the named location. + * @param file the path to the directory. + * @return the FSDirectory for the named file. */ + public static FSDirectory getDirectory(File file) + throws IOException { + return getDirectory(file, null); + } + + /** Returns the directory instance for the named location. + * @param file the path to the directory. + * @param lockFactory instance of {@link LockFactory} providing the + * locking implementation. + * @return the FSDirectory for the named file. */ + public static FSDirectory getDirectory(File file, LockFactory lockFactory) + throws IOException + { + return getDirectory(NIOFSDirectory.class, file, lockFactory); + } + // Inherit javadoc public IndexInput openInput(String name, int bufferSize) throws IOException { ensureOpen(); Index: src/java/org/apache/lucene/store/MMapDirectory.java =================================================================== --- src/java/org/apache/lucene/store/MMapDirectory.java (revision 713425) +++ src/java/org/apache/lucene/store/MMapDirectory.java (working copy) @@ -33,6 +33,44 @@ */ public class MMapDirectory extends FSDirectory { + /** Returns the directory instance for the named location. + * @param path the path to the directory. + * @return the FSDirectory for the named file. */ + public static FSDirectory getDirectory(String path) + throws IOException { + return getDirectory(new File(path), null); + } + + /** Returns the directory instance for the named location. + * @param path the path to the directory. + * @param lockFactory instance of {@link LockFactory} providing the + * locking implementation. + * @return the FSDirectory for the named file. */ + public static FSDirectory getDirectory(String path, LockFactory lockFactory) + throws IOException { + return getDirectory(new File(path), lockFactory); + } + + /** Returns the directory instance for the named location. + * @param file the path to the directory. + * @return the FSDirectory for the named file. */ + public static FSDirectory getDirectory(File file) + throws IOException { + return getDirectory(file, null); + } + + /** Returns the directory instance for the named location. + * @param file the path to the directory. + * @param lockFactory instance of {@link LockFactory} providing the + * locking implementation. + * @return the FSDirectory for the named file. */ + public static FSDirectory getDirectory(File file, LockFactory lockFactory) + throws IOException + { + return getDirectory(MMapDirectory.class, file, lockFactory); + } + + private static class MMapIndexInput extends IndexInput { private ByteBuffer buffer; Index: src/java/org/apache/lucene/store/FSDirectory.java =================================================================== --- src/java/org/apache/lucene/store/FSDirectory.java (revision 713425) +++ src/java/org/apache/lucene/store/FSDirectory.java (working copy) @@ -157,7 +157,8 @@ return getDirectory(file, null); } - /** Returns the directory instance for the named location. + + /** Returns the directory instance for the named location. * @param file the path to the directory. * @param lockFactory instance of {@link LockFactory} providing the * locking implementation. @@ -165,6 +166,18 @@ public static FSDirectory getDirectory(File file, LockFactory lockFactory) throws IOException { + return getDirectory(IMPL, file, lockFactory); + } + + /** Returns the directory instance for the named location, using directoryImpl + * to create a new FSDirectory class or subclass if one does not already exist. + * @param file the path to the directory. + * @param lockFactory instance of {@link LockFactory} providing the + * locking implementation. + * @return the FSDirectory for the named file. */ + protected static FSDirectory getDirectory(Class directoryImpl, File file, LockFactory lockFactory) + throws IOException + { file = new File(file.getCanonicalPath()); if (file.exists() && !file.isDirectory()) @@ -179,7 +192,7 @@ dir = (FSDirectory)DIRECTORIES.get(file); if (dir == null) { try { - dir = (FSDirectory)IMPL.newInstance(); + dir = (FSDirectory)directoryImpl.newInstance(); } catch (Exception e) { throw new RuntimeException("cannot load FSDirectory class: " + e.toString(), e); }