Author: brett
Date: Wed Jul 26 20:32:25 2006
New Revision: 425940

URL: http://svn.apache.org/viewvc?rev=425940&view=rev
Log:
[MNG-127] read plugin metadata from inside the JAR instead of from the 
repository metadata

Removed:
    
maven/repository-manager/trunk/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/maven-metadata.xml
Modified:
    
maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/AbstractArtifactIndexRecordFactory.java
    
maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactory.java

Modified: 
maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/AbstractArtifactIndexRecordFactory.java
URL: 
http://svn.apache.org/viewvc/maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/AbstractArtifactIndexRecordFactory.java?rev=425940&r1=425939&r2=425940&view=diff
==============================================================================
--- 
maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/AbstractArtifactIndexRecordFactory.java
 (original)
+++ 
maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/AbstractArtifactIndexRecordFactory.java
 Wed Jul 26 20:32:25 2006
@@ -61,13 +61,21 @@
         throws IOException
     {
         ZipFile zipFile = new ZipFile( file );
-        List files = new ArrayList( zipFile.size() );
-
-        for ( Enumeration entries = zipFile.entries(); 
entries.hasMoreElements(); )
+        List files;
+        try
         {
-            ZipEntry entry = (ZipEntry) entries.nextElement();
+            files = new ArrayList( zipFile.size() );
+
+            for ( Enumeration entries = zipFile.entries(); 
entries.hasMoreElements(); )
+            {
+                ZipEntry entry = (ZipEntry) entries.nextElement();
 
-            files.add( entry.getName() );
+                files.add( entry.getName() );
+            }
+        }
+        finally
+        {
+            closeQuietly( zipFile );
         }
         return files;
     }
@@ -76,5 +84,20 @@
     {
         // TODO: verify if class is public or protected (this might require 
the original ZipEntry)
         return name.endsWith( ".class" ) && name.lastIndexOf( "$" ) < 0;
+    }
+
+    protected static void closeQuietly( ZipFile zipFile )
+    {
+        try
+        {
+            if ( zipFile != null )
+            {
+                zipFile.close();
+            }
+        }
+        catch ( IOException e )
+        {
+            // ignored
+        }
     }
 }

Modified: 
maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactory.java
URL: 
http://svn.apache.org/viewvc/maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactory.java?rev=425940&r1=425939&r2=425940&view=diff
==============================================================================
--- 
maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactory.java
 (original)
+++ 
maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactory.java
 Wed Jul 26 20:32:25 2006
@@ -18,29 +18,30 @@
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.factory.ArtifactFactory;
-import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
 import org.apache.maven.artifact.repository.metadata.Metadata;
-import org.apache.maven.artifact.repository.metadata.Plugin;
-import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
 import 
org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
 import org.apache.maven.model.Model;
 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
 import org.apache.maven.repository.digest.Digester;
 import org.apache.maven.repository.indexing.RepositoryIndexException;
 import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.xml.Xpp3Dom;
+import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.util.Arrays;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
 import java.util.Set;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipException;
+import java.util.zip.ZipFile;
 
 /**
  * An index record type for the standard index.
@@ -65,6 +66,8 @@
      */
     private ArtifactFactory artifactFactory;
 
+    private static final String PLUGIN_METADATA_NAME = 
"META-INF/maven/plugin.xml";
+
     public RepositoryIndexRecord createRecord( Artifact artifact )
         throws RepositoryIndexException
     {
@@ -105,7 +108,7 @@
                 record.setRepository( artifact.getRepository().getId() );
                 if ( files != null )
                 {
-                    populateArchiveEntries( files, record );
+                    populateArchiveEntries( files, record, artifact.getFile() 
);
                 }
 
                 if ( !"pom".equals( artifact.getType() ) )
@@ -124,20 +127,6 @@
                 {
                     populatePomEntries( readPom( file ), record );
                 }
-
-                if ( "maven-plugin".equals( record.getPackaging() ) )
-                {
-                    // Typically discovered as a JAR
-                    record.setType( record.getPackaging() );
-
-                    RepositoryMetadata metadata = new GroupRepositoryMetadata( 
artifact.getGroupId() );
-                    File metadataFile = new File( 
artifact.getRepository().getBasedir(),
-                                                  
artifact.getRepository().pathOfRemoteRepositoryMetadata( metadata ) );
-                    if ( metadataFile.exists() )
-                    {
-                        populatePluginEntries( readMetadata( metadataFile ), 
record );
-                    }
-                }
             }
         }
 
@@ -217,7 +206,8 @@
         }
     }
 
-    private void populateArchiveEntries( List files, 
StandardArtifactIndexRecord record )
+    private void populateArchiveEntries( List files, 
StandardArtifactIndexRecord record, File artifactFile )
+        throws RepositoryIndexException
     {
         StringBuffer classes = new StringBuffer();
         StringBuffer fileBuffer = new StringBuffer();
@@ -235,6 +225,13 @@
                 {
                     classes.append( name.substring( 0, name.length() - 6 
).replace( '/', '.' ) ).append( "\n" );
                 }
+                else
+                {
+                    if ( PLUGIN_METADATA_NAME.equals( name ) )
+                    {
+                        populatePluginEntries( readPluginMetadata( 
artifactFile ), record );
+                    }
+                }
             }
         }
 
@@ -242,23 +239,48 @@
         record.setFiles( fileBuffer.toString() );
     }
 
-    public void populatePluginEntries( Metadata metadata, 
StandardArtifactIndexRecord record )
+    private Xpp3Dom readPluginMetadata( File file )
+        throws RepositoryIndexException
     {
-        Map prefixes = new HashMap();
-        for ( Iterator i = metadata.getPlugins().iterator(); i.hasNext(); )
-        {
-            Plugin plugin = (Plugin) i.next();
+        // TODO: would be more efficient with original ZipEntry still around
 
-            prefixes.put( plugin.getArtifactId(), plugin.getPrefix() );
+        Xpp3Dom xpp3Dom;
+        ZipFile zipFile = null;
+        try
+        {
+            zipFile = new ZipFile( file );
+            ZipEntry entry = zipFile.getEntry( PLUGIN_METADATA_NAME );
+            xpp3Dom = Xpp3DomBuilder.build( new InputStreamReader( 
zipFile.getInputStream( entry ) ) );
         }
+        catch ( ZipException e )
+        {
+            throw new RepositoryIndexException( "Unable to read plugin 
metadata: " + e.getMessage(), e );
+        }
+        catch ( IOException e )
+        {
+            throw new RepositoryIndexException( "Unable to read plugin 
metadata: " + e.getMessage(), e );
+        }
+        catch ( XmlPullParserException e )
+        {
+            throw new RepositoryIndexException( "Unable to read plugin 
metadata: " + e.getMessage(), e );
+        }
+        finally
+        {
+            closeQuietly( zipFile );
+        }
+        return xpp3Dom;
+    }
+
+    public void populatePluginEntries( Xpp3Dom metadata, 
StandardArtifactIndexRecord record )
+    {
+        // Typically discovered as a JAR
+        record.setType( "maven-plugin" );
+
+        Xpp3Dom prefix = metadata.getChild( "goalPrefix" );
 
-        if ( record.getGroupId().equals( metadata.getGroupId() ) )
+        if ( prefix != null )
         {
-            String prefix = (String) prefixes.get( record.getArtifactId() );
-            if ( prefix != null )
-            {
-                record.setPluginPrefix( prefix );
-            }
+            record.setPluginPrefix( prefix.getValue() );
         }
     }
 }


Reply via email to