*/
import java.io.IOException;
+import java.util.Collection;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
implements RepositoryIndexer
{
protected String indexPath;
+ protected boolean indexOpen;
protected IndexReader indexReader;
protected IndexWriter indexWriter;
+
+ public void optimize()
+ throws RepositoryIndexerException
+ {
+ if ( !isOpen() )
+ {
+ throw new RepositoryIndexerException( "Unable to optimize index on a closed index" );
+ }
+
+ try
+ {
+ indexWriter.optimize();
+ }
+ catch ( IOException ioe )
+ {
+ throw new RepositoryIndexerException( "Failed to optimize index", ioe );
+ }
+ }
+
+ public boolean isOpen()
+ {
+ return indexOpen;
+ }
+
+ public void close()
+ throws RepositoryIndexerException
+ {
+ if ( indexOpen )
+ {
+ try
+ {
+ if ( indexWriter != null )
+ {
+ indexWriter.close();
+ indexWriter = null;
+ }
+
+ if ( indexReader != null )
+ {
+ indexReader.close();
+ indexReader = null;
+ }
+
+ indexOpen = false;
+ }
+ catch ( Exception e )
+ {
+ throw new RepositoryIndexerException( e );
+ }
+ }
+ }
+
+ public void open()
+ throws RepositoryIndexerException
+ {
+ try
+ {
+ if ( !indexOpen )
+ {
+ validateIndex();
+ }
+ }
+ catch ( Exception e )
+ {
+ throw new RepositoryIndexerException( e );
+ }
+ }
+
protected void getIndexWriter()
throws IOException
{
return new StandardAnalyzer();
}
+
+ protected void validateIndex()
+ throws RepositoryIndexerException
+ {
+ try
+ {
+ getIndexReader();
+ Collection fields = indexReader.getFieldNames();
+ String[] indexFields = getIndexFields();
+ for( int idx=0; idx<indexFields.length; idx++ )
+ {
+ if ( !fields.contains( indexFields[ idx ] ) )
+ {
+ throw new RepositoryIndexerException( "The Field " + indexFields[ idx ] + " does not exist in " +
+ "index path " + indexPath + "." );
+ }
+ }
+ indexOpen = true;
+ }
+ catch ( IOException e )
+ {
+ throw new RepositoryIndexerException( e );
+ }
+ }
}
private static final String PACKAGES = "packages";
private static final String FILES = "files";
- private static final String[] FIELDS = { NAME, GROUPID, ARTIFACTID, VERSION, SHA1, MD5, CLASSES, PACKAGES, FILES };
+ private static final String[] FIELDS = { NAME, GROUPID, ARTIFACTID, VERSION, SHA1, MD5,
+ CLASSES, PACKAGES, FILES };
private ArtifactRepository repository;
indexPath = path;
validateIndex();
}
+
+ public String[] getIndexFields()
+ {
+ return FIELDS;
+ }
+
+ public void addObjectIndex(Object obj)
+ throws RepositoryIndexerException
+ {
+ if ( obj instanceof Artifact )
+ {
+ addArtifactIndex( (Artifact) obj );
+ }
+ else
+ {
+ throw new RepositoryIndexerException( "This instance of indexer cannot index instances of " +
+ obj.getClass().getName() );
+ }
+ }
public void addArtifactIndex( Artifact artifact )
throws RepositoryIndexerException
{
+ if ( !isOpen() )
+ {
+ throw new RepositoryIndexerException( "Unable to add artifact index on a closed index" );
+ }
+
try
{
getIndexWriter();
+ initBuffers();
processArtifactContents( artifact.getFile() );
+ //@todo should some of these fields be Keyword instead of Text ?
Document doc = new Document();
doc.add( Field.Text( NAME, repository.pathOf( artifact ) ) );
doc.add( Field.Text( GROUPID, artifact.getGroupId() ) );
doc.add( Field.Text( PACKAGES, packages.toString() ) );
doc.add( Field.Text( FILES, files.toString() ) );
indexWriter.addDocument( doc );
+
+ removeBuffers();
}
catch( Exception e )
{
}
}
- public void optimize()
- throws RepositoryIndexerException
- {
- try
- {
- indexWriter.optimize();
- }
- catch ( IOException ioe )
- {
- throw new RepositoryIndexerException( "Failed to optimize index", ioe );
- }
- }
-
private String getSha1( Artifact artifact )
throws FileNotFoundException, IOException, NoSuchAlgorithmException
{
return complete.digest();
}
- private void processArtifactContents( File artifact )
- throws IOException, ZipException
+ private void initBuffers()
{
classes = new StringBuffer();
packages = new StringBuffer();
files = new StringBuffer();
-
+ }
+
+ private void removeBuffers()
+ {
+ classes = null;
+ packages = null;
+ files = null;
+ }
+
+ private void processArtifactContents( File artifact )
+ throws IOException, ZipException
+ {
ZipFile jar = new ZipFile( artifact );
for ( Enumeration entries = jar.entries(); entries.hasMoreElements(); )
{
return isAdded;
}
-
- private void validateIndex()
- throws RepositoryIndexerException
- {
- try
- {
- getIndexReader();
- Collection fields = indexReader.getFieldNames();
- for( int idx=0; idx<FIELDS.length; idx++ )
- {
- if ( !fields.contains( FIELDS[ idx ] ) )
- {
- throw new RepositoryIndexerException( "The Field " + FIELDS[ idx ] + " does not exist in index" +
- " path " + indexPath + "." );
- }
- }
- }
- catch ( IOException e )
- {
- throw new RepositoryIndexerException( e );
- }
- }
}