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.analysis.Analyzer;
20 import org.apache.lucene.analysis.SimpleAnalyzer;
21 import org.apache.lucene.index.IndexReader;
22 import org.apache.lucene.index.IndexWriter;
23 import org.apache.lucene.index.Term;
24 import org.apache.maven.artifact.repository.ArtifactRepository;
27 import java.io.IOException;
28 import java.util.Collection;
31 * Abstract class for RepositoryIndexers
33 * @author Edwin Punzalan
35 public abstract class AbstractRepositoryIndex
36 implements RepositoryIndex
38 private String indexPath;
40 private boolean indexOpen;
42 private IndexWriter indexWriter;
44 protected ArtifactRepository repository;
46 protected boolean indexExists;
48 private Analyzer analyzer;
55 * @throws RepositoryIndexException
57 protected AbstractRepositoryIndex( String indexPath, ArtifactRepository repository )
58 throws RepositoryIndexException
60 this.repository = repository;
61 this.indexPath = indexPath;
65 * Method to open the IndexWriter
67 * @throws RepositoryIndexException
70 throws RepositoryIndexException
76 indexWriter = new IndexWriter( indexPath, getAnalyzer(), false );
80 indexWriter = new IndexWriter( indexPath, getAnalyzer(), true );
83 catch ( IOException ie )
85 throw new RepositoryIndexException( ie );
91 * @see org.apache.maven.repository.indexing.RepositoryIndex#optimize()
93 public void optimize()
94 throws RepositoryIndexException
98 throw new RepositoryIndexException( "Unable to optimize index on a closed index" );
103 indexWriter.optimize();
105 catch ( IOException ioe )
107 throw new RepositoryIndexException( "Failed to optimize index", ioe );
112 * @see org.apache.maven.repository.indexing.RepositoryIndex#isOpen()
114 public boolean isOpen()
120 * @see org.apache.maven.repository.indexing.RepositoryIndex#close()
123 throws RepositoryIndexException
127 if ( indexWriter != null )
135 catch ( IOException e )
137 throw new RepositoryIndexException( e.getMessage(), e );
142 * @see org.apache.maven.repository.indexing.RepositoryIndex#getIndexPath()
144 public String getIndexPath()
150 * Method to retrieve the lucene IndexWriter used in creating/updating the index
152 * @return the lucene IndexWriter object used to update the index
153 * @throws IOException
155 protected IndexWriter getIndexWriter()
158 if ( indexWriter == null )
160 indexWriter = new IndexWriter( indexPath, getAnalyzer(), false );
166 * method for validating an index directory
169 * @throws RepositoryIndexException if the given indexPath is not valid for this type of RepositoryIndex
171 protected void validateIndex( String[] indexFields )
172 throws RepositoryIndexException, IOException
174 IndexReader indexReader = IndexReader.open( indexPath );
177 if ( indexReader.numDocs() > 0 )
179 Collection fields = indexReader.getFieldNames();
180 for ( int idx = 0; idx < indexFields.length; idx++ )
182 if ( !fields.contains( indexFields[idx] ) )
184 throw new RepositoryIndexException(
185 "The Field " + indexFields[idx] + " does not exist in index " + indexPath + "." );
197 * @see org.apache.maven.repository.indexing.RepositoryIndex#getRepository()
199 public ArtifactRepository getRepository()
205 * Delete the document(s) that contains the specified value on the specified field.
209 * @throws RepositoryIndexException
210 * @throws IOException
212 protected void deleteDocument( String field, String value )
213 throws RepositoryIndexException, IOException
215 IndexReader indexReader = null;
218 indexReader = IndexReader.open( indexPath );
219 indexReader.delete( new Term( field, value ) );
221 catch ( IOException ie )
223 throw new RepositoryIndexException( indexPath + "is not a valid directory." );
227 if ( indexReader != null )
235 * Check if the index already exists.
237 * @throws IOException
238 * @throws RepositoryIndexException
240 protected void checkIfIndexExists()
241 throws IOException, RepositoryIndexException
243 File indexDir = new File( indexPath );
245 if ( IndexReader.indexExists( indexDir ) )
249 else if ( !indexDir.exists() )
253 else if ( indexDir.isDirectory() )
255 throw new RepositoryIndexException( indexPath + " is not a valid index directory." );
259 throw new RepositoryIndexException( indexPath + " is not a directory." );
264 * Checks if the object has already been indexed.
266 * @param object the object to be indexed.
267 * @throws RepositoryIndexException
268 * @throws IOException
270 abstract void isIndexed( Object object )
271 throws RepositoryIndexException, IOException;
274 * @see org.apache.maven.repository.indexing.RepositoryIndex#getAnalyzer()
276 public Analyzer getAnalyzer()
278 if ( analyzer == null )
280 analyzer = new ArtifactRepositoryIndexAnalyzer( new SimpleAnalyzer() );
287 * @see RepositoryIndex#isKeywordField(String)
289 public boolean isKeywordField( String field )
291 return KEYWORD_FIELDS.contains( field );