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.
21 import java.io.IOException;
22 import java.util.Collection;
24 import org.apache.lucene.analysis.Analyzer;
25 import org.apache.lucene.analysis.SimpleAnalyzer;
26 import org.apache.lucene.index.IndexReader;
27 import org.apache.lucene.index.IndexWriter;
30 * Abstract class for RepositoryIndexers
32 * @author Edwin Punzalan
34 public abstract class AbstractRepositoryIndexer
35 implements RepositoryIndexer
37 protected String indexPath;
38 protected boolean indexOpen;
39 protected IndexReader indexReader;
40 protected IndexWriter indexWriter;
43 * method to encapsulate the optimize() method for lucene
45 public void optimize()
46 throws RepositoryIndexerException
50 throw new RepositoryIndexerException( "Unable to optimize index on a closed index" );
55 indexWriter.optimize();
57 catch ( IOException ioe )
59 throw new RepositoryIndexerException( "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 RepositoryIndexerException
81 if ( indexWriter != null )
87 if ( indexReader != null )
97 throw new RepositoryIndexerException( e );
102 * method for opening the index directory for indexing operations
105 throws RepositoryIndexerException
111 catch ( Exception e )
113 throw new RepositoryIndexerException( e );
117 protected void getIndexWriter()
120 if ( indexWriter == null )
122 indexWriter = new IndexWriter( indexPath, getAnalyzer(), false );
126 protected void getIndexReader()
129 if ( indexReader == null )
131 indexReader = IndexReader.open( indexPath );
135 protected Analyzer getAnalyzer()
137 return new ArtifactRepositoryIndexAnalyzer( new SimpleAnalyzer() );
141 * method for validating an index directory
143 * @throws RepositoryIndexerException if the given indexPath is not valid for this type of RepositoryIndexer
145 protected void validateIndex()
146 throws RepositoryIndexerException
148 File indexDir = new File( indexPath );
149 if ( indexDir.exists() )
151 if ( indexDir.isDirectory() )
153 if ( indexDir.listFiles().length > 1 )
158 Collection fields = indexReader.getFieldNames();
159 String[] indexFields = getIndexFields();
160 for( int idx=0; idx<indexFields.length; idx++ )
162 if ( !fields.contains( indexFields[ idx ] ) )
164 throw new RepositoryIndexerException( "The Field " + indexFields[ idx ] + " does not exist in " +
165 "index path " + indexPath + "." );
169 catch ( IOException e )
171 throw new RepositoryIndexerException( e );
176 System.out.println( "Skipping validation of an empty index in: " + indexDir.getAbsolutePath() );
181 throw new RepositoryIndexerException( "Specified index path is not a directory: " +
182 indexDir.getAbsolutePath() );
189 indexWriter = new IndexWriter( indexPath, getAnalyzer(), true );
190 System.out.println( "New index directory created in: " + indexDir.getAbsolutePath() );
194 throw new RepositoryIndexerException( e );