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;
23 import java.io.IOException;
24 import java.util.Collection;
27 * Abstract class for RepositoryIndexers
29 * @author Edwin Punzalan
31 public abstract class AbstractRepositoryIndex
32 implements RepositoryIndex
34 protected String indexPath;
36 protected boolean indexOpen;
38 protected IndexReader indexReader;
40 protected IndexWriter indexWriter;
43 * method to encapsulate the optimize() method for lucene
45 public void optimize()
46 throws RepositoryIndexException
50 throw new RepositoryIndexException( "Unable to optimize index on a closed index" );
55 indexWriter.optimize();
57 catch ( IOException ioe )
59 throw new RepositoryIndexException( "Failed to optimize index", ioe );
64 * method used to query the index status
66 * @param true if the index is open.
68 public boolean isOpen()
74 * method used to close all open streams to the index directory
77 throws RepositoryIndexException
81 if ( indexWriter != null )
87 if ( indexReader != null )
97 throw new RepositoryIndexException( e );
102 * method for opening the index directory for indexing operations
104 public void open( String indexPath )
105 throws RepositoryIndexException
109 this.indexPath = indexPath;
112 catch ( IOException e )
114 throw new RepositoryIndexException( e );
118 public String getIndexPath()
123 protected void getIndexWriter()
126 if ( indexWriter == null )
128 indexWriter = new IndexWriter( indexPath, getAnalyzer(), false );
132 protected void getIndexReader()
135 if ( indexReader == null )
137 indexReader = IndexReader.open( indexPath );
142 * method for validating an index directory
144 * @throws RepositoryIndexException if the given indexPath is not valid for this type of RepositoryIndex
146 protected void validateIndex()
147 throws RepositoryIndexException, IOException
149 File indexDir = new File( indexPath );
150 if ( IndexReader.indexExists( indexDir ) )
153 if ( indexReader.numDocs() > 0 )
155 Collection fields = indexReader.getFieldNames();
156 String[] indexFields = getIndexFields();
157 for ( int idx = 0; idx < indexFields.length; idx++ )
159 if ( !fields.contains( indexFields[idx] ) )
161 throw new RepositoryIndexException(
162 "The Field " + indexFields[idx] + " does not exist in " + "index path " + indexPath + "." );
168 System.out.println( "Skipping index field validations for empty index." );
171 else if ( !indexDir.exists() )
173 indexWriter = new IndexWriter( indexPath, getAnalyzer(), true );
174 System.out.println( "New index directory created in: " + indexDir.getAbsolutePath() );
176 else if ( indexDir.isDirectory() )
178 throw new RepositoryIndexException( indexPath + " is not a valid index directory." );
182 throw new RepositoryIndexException( indexPath + " is not a directory." );