lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nathan Howard <>
Subject Using a precompiled index with a WAR archive
Date Thu, 12 Nov 2009 17:06:01 GMT
I'm trying to use a precompiled Lucene index from within a WAR archive, and
was having difficulty, but found a possible solution:

The gotcha to the solution: it's written for Lucene 1.2, and I'm having some
difficulties getting it to work correctly with 2.4.1.  Any help in updating
the class would be most appreciated.

Class from the archive (Originally written by Les Hughes):


import java.util.Iterator;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;


*  A delegating Directory that wraps either a RAMDirectory or
*  FSDirectory and allows an index to be distributed in a WAR.
*  An index is stored in the WAR under /WEB-INF (eg /WEB-INF/myindex)
*  and deployed into RAM or FS depending upon the context params below.
*  By registering this class in the web.xml
*  we will receive ServletContextEvents on web app initialisation.
*  The same could be achieved in an old skool style startup-servlet.
*  Add lines like






*  Once initialised, the directory can be accessed via the
* attribute. Eg in a jsp do
*  Directory dir =
*  Searcher searcher = new IndexSearcher(dir);
*  etc etc
*  Limitations:  Only tested in Tomcat > 4.1 (Servlet 2.4?)
*  ToDo: Change RAM/FSDirectory selection to allow passing of a classname
*        Needs changes for lucene > 1.2 ??

* @author <a href="">Les Hughes</a>

public class WARDirectory extends Directory implements
ServletContextListener {

    protected Directory directory;
    protected ServletContext context;
    protected Set list;
    protected boolean useRam = false;
    String index;

    //Shame this method does not allow us to throw an exception.....Erm
bogus use of RuntimeException...

    public void contextInitialized(ServletContextEvent sce) {
        context = sce.getServletContext();

        if((index = context.getInitParameter("org.apache.lucene.index")) ==
null) {
            String msg = "Lucene 'org.apache.lucene.index' context-param
missing in web.xml";
            throw new RuntimeException(msg);

        useRam =

        context.log("Searching for index "+index);
        list = context.getResourcePaths("/WEB-INF/"+index);
        if(list == null || list.isEmpty()) {
            String msg = "Index /WEB-INF/"+index+" appears to be missing or
            throw new RuntimeException(msg);

        //Now copy index from WAR into Directory
        try {

            if(useRam) {
                context.log("Creating a new RAMDirectory");
                directory = new RAMDirectory();
            } else {
                File temp =
(File)context.getAttribute("javax.servlet.context.tempdir"); //Sevlet Spec
                String dirPath = temp.getPath() + "/"+ index;
                context.log("Creating a new FSDirectory at "+dirPath);
                directory = FSDirectory.getDirectory(dirPath, true);

            //Add files from WAR to Dir...
            OutputStream out;

            int numBytes;
            byte[] buffer = new byte[4096]; //4k buffer
            String inresname, outresname;
            Iterator iter = list.iterator();

            while(iter.hasNext()) {
                inresname = (String);
                outresname =
                context.log("Reading "+inresname+" will create
                in = new

                out =  directory.createFile(outresname);

                numBytes = 0;
                while( (numBytes =, 0 , buffer.length)) !=
-1) {
                    out.writeBytes(buffer, numBytes);


            //All done?

        }catch(IOException ioe) {
            //We fail fast if there was an exception on creating the
            //Perhaps later we can re-try with an FSDirectory if it was an
outof memory issue?

            context.log("Problem creating WARDirectory "+ioe.getMessage());
            throw new RuntimeException("Problem creating WARDirectory

        //Finally put dir ref somewhere we can use it...
        context.setAttribute("", this);
        context.log("WARDirectory done initialising.");

//    Implement Directory's Interface.

    /* Called on app shutdown */
    public void contextDestroyed(ServletContextEvent sce){}

    /** Returns an array of strings, one for each file in the directory. */
    public String[] list() throws IOException, SecurityException{
        return directory.list();

    /** Returns true if a file with the given name exists. */
    public boolean fileExists(String name) throws IOException,
        return directory.fileExists(name);

    /** Returns the time the named file was last modified. */
    public long fileModified(String name) throws IOException,
        return directory.fileModified(name);

    /** Returns the length of a file in the directory. */
    public long fileLength(String name) throws IOException,
        return directory.fileLength(name);

    /** Returns a stream reading an existing file. */
    public InputStream openFile(String name) throws IOException,
        return directory.openFile(name);

    /** Closes the store. */
    public void close() throws IOException, SecurityException {

    /** Removes an existing file in the directory. */
    public void deleteFile(String name) throws IOException,
SecurityException  {

    /** Renames an existing file in the directory.
        If a file already exists with the new name, then it is replaced.
        This replacement should be atomic. */
    public void renameFile(String from, String to) throws IOException,
SecurityException {
        directory.renameFile(from, to);

    /** Creates a new, empty file in the directory with the given name.
        Returns a stream writing this file. */
    public OutputStream createFile(String name) throws IOException,
SecurityException {
        return directory.createFile(name);

    /** Construct a {@link Lock}.
    * @param name the name of the lock file
    public Lock makeLock(String name) {
        return directory.makeLock(name);

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message