]> source.dussan.org Git - archiva.git/commitdiff
Refactored classes and added some more interface methods
authorEdwin L. Punzalan <epunzalan@apache.org>
Thu, 22 Dec 2005 06:37:47 +0000 (06:37 +0000)
committerEdwin L. Punzalan <epunzalan@apache.org>
Thu, 22 Dec 2005 06:37:47 +0000 (06:37 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@358512 13f79535-47bb-0310-9956-ffa450edef68

maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexer.java
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexer.java
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexer.java

index 291d6d82bdcbfa0720d199ae7283ed111b125401..428f6d41ed2b8cb4ecd2df96a759ad31a6ce974b 100644 (file)
@@ -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 );
+        }
+    }
 }
index c7c83a47e6f85e16b4e670bf99bb64985f28591d..82708f8fc44d62165b1ff01158f9d500e5b1c386 100644 (file)
@@ -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 );
-        }
-    }
 }
index 3c236843aad9acfe300217c95889298691c9738d..1c5230d1f82a925c1e31252fa8ec2e68969e599e 100644 (file)
@@ -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;
 }