mirror of https://github.com/apache/archiva.git
Refactored classes and added some more interface methods
git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@358512 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f5fd4662fe
commit
58d71d96ef
|
@ -18,6 +18,7 @@ package org.apache.maven.repository.indexing;
|
|||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.apache.lucene.analysis.Analyzer;
|
||||
import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
||||
|
@ -32,8 +33,77 @@ public abstract class AbstractRepositoryIndexer
|
|||
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
|
||||
|
@ -57,4 +127,28 @@ public abstract class AbstractRepositoryIndexer
|
|||
{
|
||||
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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,7 +53,8 @@ public class ArtifactRepositoryIndexer
|
|||
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;
|
||||
|
||||
|
@ -68,16 +69,42 @@ public class ArtifactRepositoryIndexer
|
|||
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() ) );
|
||||
|
@ -89,6 +116,8 @@ public class ArtifactRepositoryIndexer
|
|||
doc.add( Field.Text( PACKAGES, packages.toString() ) );
|
||||
doc.add( Field.Text( FILES, files.toString() ) );
|
||||
indexWriter.addDocument( doc );
|
||||
|
||||
removeBuffers();
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
|
@ -96,19 +125,6 @@ public class ArtifactRepositoryIndexer
|
|||
}
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
|
@ -143,13 +159,23 @@ public class ArtifactRepositoryIndexer
|
|||
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(); )
|
||||
{
|
||||
|
@ -220,26 +246,4 @@ public class ArtifactRepositoryIndexer
|
|||
|
||||
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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,6 +26,16 @@ import java.io.IOException;
|
|||
public interface RepositoryIndexer
|
||||
{
|
||||
String ROLE = RepositoryIndexer.class.getName();
|
||||
|
||||
String[] getIndexFields();
|
||||
|
||||
boolean isOpen();
|
||||
|
||||
void addObjectIndex( Object obj ) throws RepositoryIndexerException;
|
||||
|
||||
void close() throws RepositoryIndexerException;
|
||||
|
||||
void open() throws RepositoryIndexerException;
|
||||
|
||||
void optimize() throws RepositoryIndexerException;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue