]> source.dussan.org Git - archiva.git/commitdiff
PR: MRM-33
authorEdwin L. Punzalan <epunzalan@apache.org>
Thu, 22 Dec 2005 05:53:53 +0000 (05:53 +0000)
committerEdwin L. Punzalan <epunzalan@apache.org>
Thu, 22 Dec 2005 05:53:53 +0000 (05:53 +0000)
Added indexer for an artifact jar

git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@358507 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

index e2a5784896b3645e5e00333dca41f988003d56d7..291d6d82bdcbfa0720d199ae7283ed111b125401 100644 (file)
@@ -18,8 +18,8 @@ package org.apache.maven.repository.indexing;
  */
 
 import java.io.IOException;
-import org.apache.lucene.analysis.Analyzer;
 
+import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.analysis.standard.StandardAnalyzer;
 import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.index.IndexWriter;
index 2200b12f54270df58d6a043b6aab792d4fee7429..c7c83a47e6f85e16b4e670bf99bb64985f28591d 100644 (file)
@@ -17,6 +17,7 @@ package org.apache.maven.repository.indexing;
  * limitations under the License.
  */
 
+import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
@@ -29,11 +30,8 @@ import java.util.zip.ZipEntry;
 import java.util.zip.ZipException;
 import java.util.zip.ZipFile;
 
-import org.apache.lucene.analysis.standard.StandardAnalyzer;
 import org.apache.lucene.document.Document;
 import org.apache.lucene.document.Field;
-import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.index.IndexWriter;
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.repository.ArtifactRepository;
@@ -53,10 +51,15 @@ public class ArtifactRepositoryIndexer
     private static final String MD5 = "md5";
     private static final String CLASSES = "classes";
     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 };
+    private static final String[] FIELDS = { NAME, GROUPID, ARTIFACTID, VERSION, SHA1, MD5, CLASSES, PACKAGES, FILES };
     
-    ArtifactRepository repository;
+    private ArtifactRepository repository;
+
+    private StringBuffer classes;
+    private StringBuffer packages;
+    private StringBuffer files;
     
     public ArtifactRepositoryIndexer( ArtifactRepository repository, String path )
         throws RepositoryIndexerException
@@ -73,6 +76,8 @@ public class ArtifactRepositoryIndexer
         {
             getIndexWriter();
 
+            processArtifactContents( artifact.getFile() );
+            
             Document doc = new Document();
             doc.add( Field.Text( NAME, repository.pathOf( artifact ) ) );
             doc.add( Field.Text( GROUPID, artifact.getGroupId() ) );
@@ -80,8 +85,9 @@ public class ArtifactRepositoryIndexer
             doc.add( Field.Text( VERSION, artifact.getVersion() ) );
             doc.add( Field.Text( SHA1, getSha1( artifact ) ) );
             doc.add( Field.Text( MD5, getMd5( artifact ) ) );
-            doc.add( Field.Text( CLASSES, getClasses( artifact ) ) );
-            doc.add( Field.Text( PACKAGES, getPackages( artifact ) ) );
+            doc.add( Field.Text( CLASSES, classes.toString() ) );
+            doc.add( Field.Text( PACKAGES, packages.toString() ) );
+            doc.add( Field.Text( FILES, files.toString() ) );
             indexWriter.addDocument( doc );
         }
         catch( Exception e )
@@ -137,58 +143,82 @@ public class ArtifactRepositoryIndexer
         return complete.digest();
     }
 
-    private String getClasses( Artifact artifact )
+    private void processArtifactContents( File artifact )
         throws IOException, ZipException
     {
-        StringBuffer sb = new StringBuffer();
-
-        ZipFile jar = new ZipFile( artifact.getFile() );
-        for( Enumeration en = jar.entries(); en.hasMoreElements(); )
+        classes = new StringBuffer();
+        packages = new StringBuffer();
+        files = new StringBuffer();
+        
+        ZipFile jar = new ZipFile( artifact );
+        for ( Enumeration entries = jar.entries(); entries.hasMoreElements(); )
         {
-            ZipEntry e = ( ZipEntry ) en.nextElement();
-            String name = e.getName();
-            if( name.endsWith( ".class") )
+            ZipEntry entry = (ZipEntry) entries.nextElement();
+            if ( addIfClassEntry( entry ) )
             {
-                // TODO verify if class is public or protected
-                // TODO skipp all inner classes for now
-                if( name.lastIndexOf( "$" ) == -1)
-                {
-                    int idx = name.lastIndexOf( '/' );
-                    if ( idx < 0 ) idx = 0;
-                    sb.append( name.substring( idx, name.length() - 6 ) ).append( "\n" );
-                }
+                addClassPackage( entry.getName() );
             }
+            addFile( entry );
         }
+    }
 
-        return sb.toString();
+    private boolean addIfClassEntry( ZipEntry entry )
+    {
+        boolean isAdded = false;
+        
+        String name = entry.getName();
+        if( name.endsWith( ".class") )
+        {
+            // TODO verify if class is public or protected
+            if( name.lastIndexOf( "$" ) == -1)
+            {
+                int idx = name.lastIndexOf( '/' );
+                if ( idx < 0 ) idx = 0;
+                String classname = name.substring( idx, name.length() - 6 );
+                classes.append( classname ).append( "\n" );
+                isAdded = true;
+            }
+        }
+        
+        return isAdded;
     }
 
-    private String getPackages( Artifact artifact )
-        throws IOException, ZipException
+    private boolean addClassPackage( String name )
     {
-        StringBuffer sb = new StringBuffer();
+        boolean isAdded = false;
 
-        ZipFile jar = new ZipFile( artifact.getFile() );
-        for( Enumeration en = jar.entries(); en.hasMoreElements(); )
+        int idx = name.lastIndexOf( '/' );
+        if ( idx > 0 )
         {
-            ZipEntry e = ( ZipEntry ) en.nextElement();
-            String name = e.getName();
-            //only include packages with accompanying classes
-            if ( name.endsWith( ".class" ) )
+            String packageName = name.substring( 0, idx ).replace( '/', '.' ) + "\n";
+            if ( packages.indexOf( packageName ) < 0 )
             {
-                int idx = name.lastIndexOf( '/' );
-                if ( idx > 0 )
-                {
-                    String packageName = name.substring( 0, idx ).replace( '/', '.' ) + "\n";
-                    if ( sb.indexOf( packageName ) < 0 )
-                    {
-                        sb.append( packageName ).append( "\n" );
-                    }
-                }
+                packages.append( packageName ).append( "\n" );
             }
+            isAdded = true;
         }
+        
+        return isAdded;
+    }
+
+    private boolean addFile( ZipEntry entry )
+    {
+        boolean isAdded = false;
 
-        return sb.toString();
+        String name = entry.getName();
+        int idx = name.lastIndexOf( '/' );
+        if ( idx >= 0 )
+        {
+            name = name.substring( idx + 1 );
+        }
+
+        if ( files.indexOf( name + "\n" ) < 0 )
+        {
+            files.append( name ).append( "\n" );
+            isAdded = true;
+        }
+        
+        return isAdded;
     }
 
     private void validateIndex()
@@ -202,8 +232,8 @@ public class ArtifactRepositoryIndexer
             {
                 if ( !fields.contains( FIELDS[ idx ] ) )
                 {
-                    throw new RepositoryIndexerException( "The Field " + FIELDS[ idx ] + " does not exist in index path " +
-                            indexPath + "." );
+                    throw new RepositoryIndexerException( "The Field " + FIELDS[ idx ] + " does not exist in index" +
+                            " path " + indexPath + "." );
                 }
             }
         }