1 package org.apache.maven.repository.indexing;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.lucene.index.IndexReader;
20 import org.apache.lucene.index.IndexWriter;
21 import org.codehaus.plexus.logging.AbstractLogEnabled;
24 import java.io.IOException;
25 import java.util.Collection;
28 * Abstract class for RepositoryIndexers
30 * @author Edwin Punzalan
32 public abstract class AbstractRepositoryIndex
33 extends AbstractLogEnabled
34 implements RepositoryIndex
36 private String indexPath;
38 private boolean indexOpen;
40 private IndexReader indexReader;
42 private IndexWriter indexWriter;
45 * method to encapsulate the optimize() method for lucene
47 public void optimize()
48 throws RepositoryIndexException
52 throw new RepositoryIndexException( "Unable to optimize index on a closed index" );
57 indexWriter.optimize();
59 catch ( IOException ioe )
61 throw new RepositoryIndexException( "Failed to optimize index", ioe );
66 * method used to query the index status
68 * @return true if the index is open.
70 public boolean isOpen()
76 * method used to close all open streams to the index directory
79 throws RepositoryIndexException
83 if ( indexWriter != null )
89 if ( indexReader != null )
97 catch ( IOException e )
99 throw new RepositoryIndexException( e.getMessage(), e );
104 * method for opening the index directory for indexing operations
106 public void open( String indexPath )
107 throws RepositoryIndexException
111 this.indexPath = indexPath;
114 catch ( IOException e )
116 throw new RepositoryIndexException( e );
120 public String getIndexPath()
125 protected IndexWriter getIndexWriter()
128 if ( indexWriter == null )
130 indexWriter = new IndexWriter( indexPath, getAnalyzer(), false );
135 private IndexReader getIndexReader()
138 if ( indexReader == null )
140 indexReader = IndexReader.open( indexPath );
146 * method for validating an index directory
148 * @throws RepositoryIndexException if the given indexPath is not valid for this type of RepositoryIndex
150 private void validateIndex()
151 throws RepositoryIndexException, IOException
153 File indexDir = new File( indexPath );
154 if ( IndexReader.indexExists( indexDir ) )
156 IndexReader indexReader = getIndexReader();
157 if ( indexReader.numDocs() > 0 )
159 Collection fields = indexReader.getFieldNames();
160 String[] indexFields = getIndexFields();
161 for ( int idx = 0; idx < indexFields.length; idx++ )
163 if ( !fields.contains( indexFields[idx] ) )
165 throw new RepositoryIndexException(
166 "The Field " + indexFields[idx] + " does not exist in " + "index path " + indexPath + "." );
172 getLogger().info( "Skipping index field validations for empty index." );
175 else if ( !indexDir.exists() )
177 indexWriter = new IndexWriter( indexPath, getAnalyzer(), true );
178 getLogger().info( "New index directory created in: " + indexDir.getAbsolutePath() );
180 else if ( indexDir.isDirectory() )
182 throw new RepositoryIndexException( indexPath + " is not a valid index directory." );
186 throw new RepositoryIndexException( indexPath + " is not a directory." );