maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From epunza...@apache.org
Subject svn commit: r358229 - /maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/
Date Wed, 21 Dec 2005 09:01:57 GMT
Author: epunzalan
Date: Wed Dec 21 01:01:32 2005
New Revision: 358229

URL: http://svn.apache.org/viewcvs?rev=358229&view=rev
Log:
Updated working base

Added:
    maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerException.java
  (with props)
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/DefaultRepositoryIndexerFactory.java
    maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexer.java
    maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerFactory.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=358229&r1=358228&r2=358229&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 01:01:32 2005
@@ -17,6 +17,13 @@
  * 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 @@
 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();
     }
 }

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=358229&r1=358228&r2=358229&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 01:01:32 2005
@@ -24,12 +24,17 @@
 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 @@
     
     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 )
+        catch( Exception 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 @@
     }
 
     private String getClasses( Artifact artifact )
+        throws IOException, ZipException
     {
-        return null;
-    }
+        StringBuffer sb = new StringBuffer();
 
-    private String getPackages( Artifact artifact )
-    {
-        return null;
-    }
-
-    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 );
         }
     }
 }

Modified: maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexerFactory.java
URL: http://svn.apache.org/viewcvs/maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexerFactory.java?rev=358229&r1=358228&r2=358229&view=diff
==============================================================================
--- maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexerFactory.java
(original)
+++ maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexerFactory.java
Wed Dec 21 01:01:32 2005
@@ -31,24 +31,18 @@
 {
     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" );
     }
 }

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=358229&r1=358228&r2=358229&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 01:01:32 2005
@@ -17,6 +17,8 @@
  * limitations under the License.
  */
 
+import java.io.IOException;
+
 /**
  *
  * @author Edwin Punzalan
@@ -25,5 +27,5 @@
 {
     String ROLE = RepositoryIndexer.class.getName();
 
-    
+    void optimize() throws RepositoryIndexerException;
 }

Added: maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerException.java
URL: http://svn.apache.org/viewcvs/maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerException.java?rev=358229&view=auto
==============================================================================
--- maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerException.java
(added)
+++ maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerException.java
Wed Dec 21 01:01:32 2005
@@ -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 );
+    }
+}

Propchange: maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerException.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Modified: maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerFactory.java
URL: http://svn.apache.org/viewcvs/maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerFactory.java?rev=358229&r1=358228&r2=358229&view=diff
==============================================================================
--- maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerFactory.java
(original)
+++ maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexerFactory.java
Wed Dec 21 01:01:32 2005
@@ -27,5 +27,6 @@
 {
     String ROLE = RepositoryIndexerFactory.class.getName();
     
-    RepositoryIndexer getRepositoryIndexer( String indexPath, ArtifactRepository repository,
Class indexType );
+    RepositoryIndexer getArtifactRepositoryIndexer( String indexPath, ArtifactRepository
repository )
+        throws RepositoryIndexerException;
 }



Mime
View raw message