]> source.dussan.org Git - archiva.git/commitdiff
Updated working base
authorEdwin L. Punzalan <epunzalan@apache.org>
Wed, 21 Dec 2005 09:01:32 +0000 (09:01 +0000)
committerEdwin L. Punzalan <epunzalan@apache.org>
Wed, 21 Dec 2005 09:01:32 +0000 (09:01 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@358229 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/DefaultRepositoryIndexerFactory.java
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexer.java
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerException.java [new file with mode: 0644]
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerFactory.java

index da8fa397f68b672f3ca9bc30486a403575cc26f0..e2a5784896b3645e5e00333dca41f988003d56d7 100644 (file)
@@ -17,6 +17,13 @@ package org.apache.maven.repository.indexing;
  * limitations under the License.
  */
 
+import java.io.IOException;
+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;
+
 /**
  *
  * @author Edwin Punzalan
@@ -24,9 +31,30 @@ package org.apache.maven.repository.indexing;
 public abstract class AbstractRepositoryIndexer
     implements RepositoryIndexer
 {
-    
-    protected void addDocument( Object obj )
+    protected String indexPath;
+    protected IndexReader indexReader;
+    protected IndexWriter indexWriter;
+
+    protected void getIndexWriter()
+        throws IOException
+    {
+        if ( indexWriter == null )
+        {
+            indexWriter = new IndexWriter( indexPath, getAnalyzer(), true );
+        }
+    }
+
+    protected void getIndexReader()
+        throws IOException
+    {
+        if ( indexReader == null )
+        {
+            indexReader = IndexReader.open( indexPath );
+        }
+    }
+
+    protected Analyzer getAnalyzer()
     {
-        
+        return new StandardAnalyzer();
     }
 }
index 8d6d45d15f128234a0a97ae7c4ac2ffabb0477b8..2200b12f54270df58d6a043b6aab792d4fee7429 100644 (file)
@@ -24,12 +24,17 @@ import java.io.InputStream;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.util.Collection;
+import java.util.Enumeration;
+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;
 
@@ -51,39 +56,51 @@ public class ArtifactRepositoryIndexer
     
     private static final String[] FIELDS = { NAME, GROUPID, ARTIFACTID, VERSION, SHA1, MD5, CLASSES, PACKAGES };
     
-    String indexPath;
     ArtifactRepository repository;
-    IndexReader indexReader;
-    IndexWriter indexWriter;
     
     public ArtifactRepositoryIndexer( ArtifactRepository repository, String path )
+        throws RepositoryIndexerException
+    {
+        this.repository = repository;
+        indexPath = path;
+        validateIndex();
+    }
+
+    public void addArtifactIndex( Artifact artifact )
+        throws RepositoryIndexerException
     {
         try
         {
-            this.repository = repository;
-            validateIndex();
+            getIndexWriter();
+
+            Document doc = new Document();
+            doc.add( Field.Text( NAME, repository.pathOf( artifact ) ) );
+            doc.add( Field.Text( GROUPID, artifact.getGroupId() ) );
+            doc.add( Field.Text( ARTIFACTID, artifact.getArtifactId() ) );
+            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 ) ) );
+            indexWriter.addDocument( doc );
         }
-        catch ( IOException e )
+        catchException e )
         {
-            
+            throw new RepositoryIndexerException( e );
         }
     }
 
-    public void addArtifactIndex( Artifact artifact )
-        throws IOException, NoSuchAlgorithmException
+    public void optimize()
+        throws RepositoryIndexerException
     {
-        getIndexWriter();
-
-        Document doc = new Document();
-        doc.add( Field.Text( NAME, repository.pathOf( artifact ) ) );
-        doc.add( Field.Text( GROUPID, artifact.getGroupId() ) );
-        doc.add( Field.Text( ARTIFACTID, artifact.getArtifactId() ) );
-        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 ) ) );
-        indexWriter.addDocument( doc );
+        try
+        {
+            indexWriter.optimize();
+        }
+        catch ( IOException ioe )
+        {
+            throw new RepositoryIndexerException( "Failed to optimize index", ioe );
+        }
     }
 
     private String getSha1( Artifact artifact )
@@ -121,44 +138,78 @@ public class ArtifactRepositoryIndexer
     }
 
     private String getClasses( Artifact artifact )
+        throws IOException, ZipException
     {
-        return null;
-    }
-
-    private String getPackages( Artifact artifact )
-    {
-        return null;
-    }
+        StringBuffer sb = new StringBuffer();
 
-    private void getIndexWriter() 
-        throws IOException
-    {
-        if ( indexWriter == null )
+        ZipFile jar = new ZipFile( artifact.getFile() );
+        for( Enumeration en = jar.entries(); en.hasMoreElements(); )
         {
-            indexWriter = new IndexWriter( indexPath, new StandardAnalyzer(), true );
+            ZipEntry e = ( ZipEntry ) en.nextElement();
+            String name = e.getName();
+            if( name.endsWith( ".class") )
+            {
+                // 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" );
+                }
+            }
         }
+
+        return sb.toString();
     }
-    
-    private void getIndexReader()
-        throws IOException
+
+    private String getPackages( Artifact artifact )
+        throws IOException, ZipException
     {
-        if ( indexReader == null )
+        StringBuffer sb = new StringBuffer();
+
+        ZipFile jar = new ZipFile( artifact.getFile() );
+        for( Enumeration en = jar.entries(); en.hasMoreElements(); )
         {
-            indexReader = IndexReader.open( indexPath );
+            ZipEntry e = ( ZipEntry ) en.nextElement();
+            String name = e.getName();
+            //only include packages with accompanying classes
+            if ( name.endsWith( ".class" ) )
+            {
+                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" );
+                    }
+                }
+            }
         }
+
+        return sb.toString();
     }
-    
+
     private void validateIndex()
-        throws IOException
+        throws RepositoryIndexerException
     {
-        getIndexReader();
-        Collection fields = indexReader.getFieldNames();
-        for( int idx=0; idx<FIELDS.length; idx++ )
+        try
         {
-            if ( !fields.contains( FIELDS[ idx ] ) )
+            getIndexReader();
+            Collection fields = indexReader.getFieldNames();
+            for( int idx=0; idx<FIELDS.length; idx++ )
             {
-                //should throw something
+                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 aa106524c73d515943e98a056f2f8f9064af6a20..e71be304b2cbc2129a4b433ddef50e47585c3b51 100644 (file)
@@ -31,24 +31,18 @@ public class DefaultRepositoryIndexerFactory
 {
     Map indexerMap = new HashMap();
     
-    public RepositoryIndexer getRepositoryIndexer( String indexPath, ArtifactRepository repository, Class indexType )
+    public RepositoryIndexer getArtifactRepositoryIndexer( String indexPath, ArtifactRepository repository )
+        throws RepositoryIndexerException
     {
-        if ( !indexerMap.containsKey( indexType ) )
+        RepositoryIndexer indexer;
+
+        if ( !indexerMap.containsKey( "Artifact" ) )
         {
-            RepositoryIndexer indexer;
-            
-            if ( Artifact.class == indexType )
-            {
-                indexer = new ArtifactRepositoryIndexer( repository, indexPath );
-            }
-            else
-            {
-                indexer = null;
-            }
+            indexer = new ArtifactRepositoryIndexer( repository, indexPath );
             
-            indexerMap.put( indexType, indexer );
+            indexerMap.put( "Artifact", indexer );
         }
 
-        return (RepositoryIndexer) indexerMap.get( indexType );
+        return (RepositoryIndexer) indexerMap.get( "Artifact" );
     }
 }
index dd1d641ebe08d7d03d57a64490c90104f922416a..3c236843aad9acfe300217c95889298691c9738d 100644 (file)
@@ -17,6 +17,8 @@ package org.apache.maven.repository.indexing;
  * limitations under the License.
  */
 
+import java.io.IOException;
+
 /**
  *
  * @author Edwin Punzalan
@@ -25,5 +27,5 @@ public interface RepositoryIndexer
 {
     String ROLE = RepositoryIndexer.class.getName();
 
-    
+    void optimize() throws RepositoryIndexerException;
 }
diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerException.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerException.java
new file mode 100644 (file)
index 0000000..d638217
--- /dev/null
@@ -0,0 +1,41 @@
+package org.apache.maven.repository.indexing;
+
+/*
+ * Copyright 2001-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ *
+ * @author Edwin Punzalan
+ */
+public class RepositoryIndexerException
+    extends Exception
+{
+    public RepositoryIndexerException( String message, Throwable cause )
+    {
+        super( message, cause );
+    }
+    
+    public RepositoryIndexerException( Throwable cause )
+    {
+        super( cause );
+    }
+    
+    public RepositoryIndexerException( String message )
+    {
+        super( message );
+    }
+}
index 6760eba64f6bd15f7645b9a9cd6d76f70f7f8ad5..e27092447141ec70ab8744cc745de065e7135080 100644 (file)
@@ -27,5 +27,6 @@ public interface RepositoryIndexerFactory
 {
     String ROLE = RepositoryIndexerFactory.class.getName();
     
-    RepositoryIndexer getRepositoryIndexer( String indexPath, ArtifactRepository repository, Class indexType );
+    RepositoryIndexer getArtifactRepositoryIndexer( String indexPath, ArtifactRepository repository )
+        throws RepositoryIndexerException;
 }