]> source.dussan.org Git - archiva.git/commitdiff
[MRM-127] read plugin metadata from inside the JAR instead of from the repository...
authorBrett Porter <brett@apache.org>
Thu, 27 Jul 2006 03:32:25 +0000 (03:32 +0000)
committerBrett Porter <brett@apache.org>
Thu, 27 Jul 2006 03:32:25 +0000 (03:32 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@425940 13f79535-47bb-0310-9956-ffa450edef68

maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/AbstractArtifactIndexRecordFactory.java
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactory.java
maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/maven-metadata.xml [deleted file]

index 89ded1b624ae85cbded628b1e000d3c6e65976dc..f077da863cf2bcaf5135260607db763ca5a0e3ae 100644 (file)
@@ -61,13 +61,21 @@ public abstract class AbstractArtifactIndexRecordFactory
         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() );
 
-            files.add( entry.getName() );
+            for ( Enumeration entries = zipFile.entries(); entries.hasMoreElements(); )
+            {
+                ZipEntry entry = (ZipEntry) entries.nextElement();
+
+                files.add( entry.getName() );
+            }
+        }
+        finally
+        {
+            closeQuietly( zipFile );
         }
         return files;
     }
@@ -77,4 +85,19 @@ public abstract class AbstractArtifactIndexRecordFactory
         // 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
+        }
+    }
 }
index ac93097fb484d8f923eb8a1520f83cfa10085d86..eb706d4f0c25b96f8ac73185d2d042cef627b223 100644 (file)
@@ -18,29 +18,30 @@ package org.apache.maven.repository.indexing.record;
 
 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 @@ public class StandardArtifactIndexRecordFactory
      */
     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 @@ public class StandardArtifactIndexRecordFactory
                 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 @@ public class StandardArtifactIndexRecordFactory
                 {
                     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 @@ public class StandardArtifactIndexRecordFactory
         }
     }
 
-    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 @@ public class StandardArtifactIndexRecordFactory
                 {
                     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 @@ public class StandardArtifactIndexRecordFactory
         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() );
         }
     }
 }
diff --git a/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/maven-metadata.xml b/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/maven-metadata.xml
deleted file mode 100644 (file)
index afbc79b..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-<!--
-  ~ Copyright 2005-2006 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.
-  -->
-
-<metadata>
-  <groupId>org.apache.maven.repository.record</groupId>
-  <plugins>
-    <plugin>
-      <prefix>test</prefix>
-      <artifactId>test-plugin</artifactId>
-    </plugin>
-  </plugins>
-</metadata>
\ No newline at end of file