1 package org.apache.maven.repository.indexing;
4 * Copyright 2001-2005 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
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.IOException;
21 import java.util.Collection;
23 import org.apache.lucene.analysis.Analyzer;
24 import org.apache.lucene.analysis.standard.StandardAnalyzer;
25 import org.apache.lucene.index.IndexReader;
26 import org.apache.lucene.index.IndexWriter;
30 * @author Edwin Punzalan
32 public abstract class AbstractRepositoryIndexer
33 implements RepositoryIndexer
35 protected String indexPath;
36 protected boolean indexOpen;
37 protected IndexReader indexReader;
38 protected IndexWriter indexWriter;
40 public void optimize()
41 throws RepositoryIndexerException
45 throw new RepositoryIndexerException( "Unable to optimize index on a closed index" );
50 indexWriter.optimize();
52 catch ( IOException ioe )
54 throw new RepositoryIndexerException( "Failed to optimize index", ioe );
58 public boolean isOpen()
64 throws RepositoryIndexerException
70 if ( indexWriter != null )
76 if ( indexReader != null )
86 throw new RepositoryIndexerException( e );
92 throws RepositoryIndexerException
101 catch ( Exception e )
103 throw new RepositoryIndexerException( e );
108 protected void getIndexWriter()
111 if ( indexWriter == null )
113 indexWriter = new IndexWriter( indexPath, getAnalyzer(), true );
117 protected void getIndexReader()
120 if ( indexReader == null )
122 indexReader = IndexReader.open( indexPath );
126 protected Analyzer getAnalyzer()
128 return new StandardAnalyzer();
131 protected void validateIndex()
132 throws RepositoryIndexerException
139 Collection fields = indexReader.getFieldNames();
140 String[] indexFields = getIndexFields();
141 for( int idx=0; idx<indexFields.length; idx++ )
143 if ( !fields.contains( indexFields[ idx ] ) )
145 throw new RepositoryIndexerException( "The Field " + indexFields[ idx ] + " does not exist in " +
146 "index path " + indexPath + "." );
151 catch ( IOException e )
153 throw new RepositoryIndexerException( e );