Author: epunzalan Date: Wed Dec 21 22:37:47 2005 New Revision: 358512 URL: http://svn.apache.org/viewcvs?rev=358512&view=rev Log: Refactored classes and added some more interface methods
Modified: maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexer.java maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexer.java maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexer.java Modified: maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexer.java URL: http://svn.apache.org/viewcvs/maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexer.java?rev=358512&r1=358511&r2=358512&view=diff ============================================================================== --- maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexer.java (original) +++ maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexer.java Wed Dec 21 22:37:47 2005 @@ -18,6 +18,7 @@ */ 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 @@ 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 @@ -56,5 +126,29 @@ protected Analyzer getAnalyzer() { 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 ); + } } } Modified: maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexer.java URL: http://svn.apache.org/viewcvs/maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexer.java?rev=358512&r1=358511&r2=358512&view=diff ============================================================================== --- maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexer.java (original) +++ maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexer.java Wed Dec 21 22:37:47 2005 @@ -53,7 +53,8 @@ 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 @@ 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 @@ 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 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 @@ 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(); ) { @@ -219,27 +245,5 @@ } 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 ); - } } } Modified: maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexer.java URL: http://svn.apache.org/viewcvs/maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexer.java?rev=358512&r1=358511&r2=358512&view=diff ============================================================================== --- maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexer.java (original) +++ maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexer.java Wed Dec 21 22:37:47 2005 @@ -26,6 +26,16 @@ 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; }