maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From br...@apache.org
Subject svn commit: r412631 - in /maven/repository-manager/trunk/maven-repository-indexer/src: main/java/org/apache/maven/repository/indexing/ test/java/org/apache/maven/repository/indexing/
Date Thu, 08 Jun 2006 02:19:01 GMT
Author: brett
Date: Wed Jun  7 19:19:01 2006
New Revision: 412631

URL: http://svn.apache.org/viewvc?rev=412631&view=rev
Log:
[MRM-118] remove code that manually constructs repository paths

Modified:
    maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexSearcher.java
    maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/MetadataRepositoryIndex.java
    maven/repository-manager/trunk/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/MetadataRepositoryIndexingTest.java
    maven/repository-manager/trunk/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/RepositoryIndexSearchLayerTest.java

Modified: maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexSearcher.java
URL: http://svn.apache.org/viewvc/maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexSearcher.java?rev=412631&r1=412630&r2=412631&view=diff
==============================================================================
--- maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexSearcher.java
(original)
+++ maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexSearcher.java
Wed Jun  7 19:19:01 2006
@@ -24,24 +24,25 @@
 import org.apache.maven.artifact.factory.ArtifactFactory;
 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
 import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
+import org.apache.maven.artifact.repository.metadata.Metadata;
 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
 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.indexing.query.Query;
 import org.codehaus.plexus.logging.AbstractLogEnabled;
+import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 
 import java.io.File;
-import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.net.MalformedURLException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.StringTokenizer;
@@ -113,11 +114,6 @@
         {
             throw new RepositoryIndexSearchException( "Unable to search index: " + e.getMessage(),
e );
         }
-        catch ( XmlPullParserException xe )
-        {
-            throw new RepositoryIndexSearchException( "Unable to parse metadata file: " +
xe.getMessage(), xe );
-        }
-
         finally
         {
             try
@@ -151,10 +147,9 @@
      *
      * @param hits the search result set
      * @return List
-     * @throws IOException
      */
     private List buildList( Hits hits )
-        throws MalformedURLException, IOException, XmlPullParserException
+        throws RepositoryIndexSearchException, IOException
     {
         for ( int i = 0; i < hits.length(); i++ )
         {
@@ -172,16 +167,16 @@
      * @return Object
      */
     protected RepositoryIndexSearchHit createSearchedObjectFromIndexDocument( Document doc
)
-        throws MalformedURLException, IOException, XmlPullParserException
+        throws RepositoryIndexSearchException
     {
         RepositoryIndexSearchHit searchHit = null;
 
         // the document is of type artifact
+        String groupId = doc.get( RepositoryIndex.FLD_GROUPID );
+        String artifactId = doc.get( RepositoryIndex.FLD_ARTIFACTID );
+        String version = doc.get( RepositoryIndex.FLD_VERSION );
         if ( doc.get( RepositoryIndex.FLD_DOCTYPE ).equals( RepositoryIndex.ARTIFACT ) )
         {
-            String groupId = doc.get( RepositoryIndex.FLD_GROUPID );
-            String artifactId = doc.get( RepositoryIndex.FLD_ARTIFACTID );
-            String version = doc.get( RepositoryIndex.FLD_VERSION );
             String packaging = doc.get( RepositoryIndex.FLD_PACKAGING );
             Artifact artifact = factory.createBuildArtifact( groupId, artifactId, version,
packaging );
 
@@ -203,15 +198,10 @@
         // the document is of type model
         else if ( doc.get( RepositoryIndex.FLD_DOCTYPE ).equals( RepositoryIndex.POM ) )
         {
-            InputStream is = new FileInputStream( new File( index.getRepository().getBasedir()
+
-                doc.get( RepositoryIndex.FLD_GROUPID ).replace( '.', '/' ) + "/" +
-                doc.get( RepositoryIndex.FLD_ARTIFACTID ) + "/" + doc.get( RepositoryIndex.FLD_VERSION
) + "/" +
-                doc.get( RepositoryIndex.FLD_ARTIFACTID ) + "-" + doc.get( RepositoryIndex.FLD_VERSION
) + ".pom" ) );
-            MavenXpp3Reader reader = new MavenXpp3Reader();
+            Artifact pomArtifact = factory.createProjectArtifact( groupId, artifactId, version
);
 
             searchHit = new RepositoryIndexSearchHit( false, false, true );
-            searchHit.setObject( reader.read( new InputStreamReader( is ) ) );
-
+            searchHit.setObject( readPom( pomArtifact ) );
         }
         // the document is of type metadata
         else if ( doc.get( RepositoryIndex.FLD_DOCTYPE ).equals( RepositoryIndex.METADATA
) )
@@ -224,28 +214,27 @@
             }
 
             Collections.reverse( pathParts );
-            Iterator it = pathParts.iterator();
-            String metadataFile = (String) it.next();
-            String tmpDir = (String) it.next();
+            String tmpDir = (String) pathParts.get( 1 );
+
+            RepositoryMetadata repoMetadata;
 
-            String metadataType;
-            if ( tmpDir.equals( doc.get( RepositoryIndex.FLD_VERSION ) ) )
+            if ( tmpDir.equals( version ) )
             {
-                metadataType = MetadataRepositoryIndex.SNAPSHOT_METADATA;
+                repoMetadata = new SnapshotArtifactRepositoryMetadata(
+                    factory.createBuildArtifact( groupId, artifactId, version, "jar" ) );
             }
-            else if ( tmpDir.equals( doc.get( RepositoryIndex.FLD_ARTIFACTID ) ) )
+            else if ( tmpDir.equals( artifactId ) )
             {
-                metadataType = MetadataRepositoryIndex.ARTIFACT_METADATA;
+                repoMetadata = new ArtifactRepositoryMetadata(
+                    factory.createBuildArtifact( groupId, artifactId, version, "jar" ) );
             }
             else
             {
-                metadataType = MetadataRepositoryIndex.GROUP_METADATA;
+                repoMetadata = new GroupRepositoryMetadata( groupId );
             }
 
-            RepositoryMetadata repoMetadata = getMetadata( doc.get( RepositoryIndex.FLD_GROUPID
),
-                                                           doc.get( RepositoryIndex.FLD_ARTIFACTID
),
-                                                           doc.get( RepositoryIndex.FLD_VERSION
), metadataFile,
-                                                           metadataType );
+            repoMetadata.setMetadata( readMetadata( repoMetadata ) );
+
             searchHit = new RepositoryIndexSearchHit( false, true, false );
             searchHit.setObject( repoMetadata );
         }
@@ -256,56 +245,74 @@
     /**
      * Create RepositoryMetadata object.
      *
-     * @param groupId      the groupId to be set
-     * @param artifactId   the artifactId to be set
-     * @param version      the version to be set
-     * @param filename     the name of the metadata file
-     * @param metadataType the type of RepositoryMetadata object to be created (GROUP, ARTIFACT
or SNAPSHOT)
      * @return RepositoryMetadata
-     * @throws IOException
-     * @throws XmlPullParserException
      */
-    private RepositoryMetadata getMetadata( String groupId, String artifactId, String version,
String filename,
-                                            String metadataType )
-        throws IOException, XmlPullParserException
+    private Metadata readMetadata( RepositoryMetadata repoMetadata )
+        throws RepositoryIndexSearchException
     {
-        RepositoryMetadata repoMetadata = null;
+        File file = new File( index.getRepository().getBasedir(),
+                              index.getRepository().pathOfRemoteRepositoryMetadata( repoMetadata
) );
 
-        // TODO! file handles left open
-        InputStream is;
         MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
 
-        //group metadata
-        if ( metadataType.equals( MetadataRepositoryIndex.GROUP_METADATA ) )
+        FileReader reader = null;
+        try
+        {
+            reader = new FileReader( file );
+            return metadataReader.read( reader );
+        }
+        catch ( FileNotFoundException e )
+        {
+            throw new RepositoryIndexSearchException( "Unable to find metadata file: " +
e.getMessage(), e );
+        }
+        catch ( IOException e )
+        {
+            throw new RepositoryIndexSearchException( "Unable to read metadata file: " +
e.getMessage(), e );
+        }
+        catch ( XmlPullParserException xe )
+        {
+            throw new RepositoryIndexSearchException( "Unable to parse metadata file: " +
xe.getMessage(), xe );
+        }
+        finally
         {
-            // TODO! use pathOfMetadata
-            is = new FileInputStream(
-                new File( index.getRepository().getBasedir() + groupId.replace( '.', '/'
) + "/" + filename ) );
-            repoMetadata = new GroupRepositoryMetadata( groupId );
-            repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) )
);
-        }
-        //artifact metadata
-        else if ( metadataType.equals( MetadataRepositoryIndex.ARTIFACT_METADATA ) )
-        {
-            // TODO! use pathOfMetadata
-            is = new FileInputStream( new File( index.getRepository().getBasedir() + groupId.replace(
'.', '/' ) + "/" +
-                artifactId + "/" + filename ) );
-            repoMetadata =
-                new ArtifactRepositoryMetadata( factory.createBuildArtifact( groupId, artifactId,
version, "jar" ) );
-            repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) )
);
-        }
-        //snapshot/version metadata
-        else if ( metadataType.equals( MetadataRepositoryIndex.SNAPSHOT_METADATA ) )
-        {
-            // TODO! use pathOfMetadata
-            is = new FileInputStream( new File( index.getRepository().getBasedir() + groupId.replace(
'.', '/' ) + "/" +
-                artifactId + "/" + version + "/" + filename ) );
-            repoMetadata = new SnapshotArtifactRepositoryMetadata(
-                factory.createBuildArtifact( groupId, artifactId, version, "jar" ) );
-            repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) )
);
+            IOUtil.close( reader );
         }
+    }
 
-        return repoMetadata;
+    /**
+     * Create RepositoryMetadata object.
+     *
+     * @return RepositoryMetadata
+     */
+    private Model readPom( Artifact pomArtifact )
+        throws RepositoryIndexSearchException
+    {
+        File file = new File( index.getRepository().getBasedir(), index.getRepository().pathOf(
pomArtifact ) );
+
+        MavenXpp3Reader r = new MavenXpp3Reader();
+
+        FileReader reader = null;
+        try
+        {
+            reader = new FileReader( file );
+            return r.read( reader );
+        }
+        catch ( FileNotFoundException e )
+        {
+            throw new RepositoryIndexSearchException( "Unable to find requested POM: " +
e.getMessage(), e );
+        }
+        catch ( IOException e )
+        {
+            throw new RepositoryIndexSearchException( "Unable to read POM: " + e.getMessage(),
e );
+        }
+        catch ( XmlPullParserException xe )
+        {
+            throw new RepositoryIndexSearchException( "Unable to parse POM: " + xe.getMessage(),
xe );
+        }
+        finally
+        {
+            IOUtil.close( reader );
+        }
     }
 
 }

Modified: maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/MetadataRepositoryIndex.java
URL: http://svn.apache.org/viewvc/maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/MetadataRepositoryIndex.java?rev=412631&r1=412630&r2=412631&view=diff
==============================================================================
--- maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/MetadataRepositoryIndex.java
(original)
+++ maven/repository-manager/trunk/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/MetadataRepositoryIndex.java
Wed Jun  7 19:19:01 2006
@@ -34,12 +34,6 @@
 public class MetadataRepositoryIndex
     extends AbstractRepositoryIndex
 {
-    protected static final String GROUP_METADATA = "GROUP_METADATA";
-
-    protected static final String ARTIFACT_METADATA = "ARTIFACT_METADATA";
-
-    protected static final String SNAPSHOT_METADATA = "SNAPSHOT_METADATA";
-
     /**
      * Class Constructor
      *
@@ -86,32 +80,9 @@
         // artifactId, version based on its location
         Document doc = new Document();
         doc.add( Field.Keyword( FLD_ID, (String) repoMetadata.getKey() ) );
-        String path = "";
         Metadata metadata = repoMetadata.getMetadata();
 
-        if ( repoMetadata.storedInGroupDirectory() && !repoMetadata.storedInArtifactVersionDirectory()
)
-        {
-            path = repoMetadata.getGroupId() + "/";
-        }
-        else if ( !repoMetadata.storedInGroupDirectory() && !repoMetadata.storedInArtifactVersionDirectory()
)
-        {
-            path = repoMetadata.getGroupId() + "/" + repoMetadata.getArtifactId() + "/";
-        }
-        else if ( !repoMetadata.storedInGroupDirectory() && repoMetadata.storedInArtifactVersionDirectory()
)
-        {
-            path = repoMetadata.getGroupId() + "/" + repoMetadata.getArtifactId() + "/" +
-                repoMetadata.getBaseVersion() + "/";
-        }
-
-        if ( !"".equals( repoMetadata.getRemoteFilename() ) && repoMetadata.getRemoteFilename()
!= null )
-        {
-            path = path + repoMetadata.getRemoteFilename();
-        }
-        else
-        {
-            path = path + repoMetadata.getLocalFilename( repository );
-        }
-        doc.add( Field.Text( FLD_NAME, path ) );
+        doc.add( Field.Text( FLD_NAME, repository.pathOfRemoteRepositoryMetadata( repoMetadata
) ) );
 
         Versioning versioning = metadata.getVersioning();
         if ( versioning != null )

Modified: maven/repository-manager/trunk/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/MetadataRepositoryIndexingTest.java
URL: http://svn.apache.org/viewvc/maven/repository-manager/trunk/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/MetadataRepositoryIndexingTest.java?rev=412631&r1=412630&r2=412631&view=diff
==============================================================================
--- maven/repository-manager/trunk/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/MetadataRepositoryIndexingTest.java
(original)
+++ maven/repository-manager/trunk/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/MetadataRepositoryIndexingTest.java
Wed Jun  7 19:19:01 2006
@@ -34,11 +34,13 @@
 import org.apache.maven.repository.indexing.query.SinglePhraseQuery;
 import org.codehaus.plexus.PlexusTestCase;
 import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 
 import java.io.File;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.net.URL;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
 import java.util.Iterator;
 import java.util.List;
 
@@ -98,25 +100,27 @@
         RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE
);
         MetadataRepositoryIndex indexer = factory.createMetadataRepositoryIndex( indexPath,
repository );
 
-        RepositoryMetadata repoMetadata =
-            getMetadata( "org.apache.maven", null, null, "maven-metadata.xml", MetadataRepositoryIndex.GROUP_METADATA
);
+        RepositoryMetadata repoMetadata = new GroupRepositoryMetadata( "org.apache.maven"
);
+        repoMetadata.setMetadata( readMetadata( repoMetadata ) );
         indexer.index( repoMetadata );
         indexer.optimize();
         indexer.close();
 
-        repoMetadata = getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml",
-                                    MetadataRepositoryIndex.ARTIFACT_METADATA );
+        repoMetadata = new ArtifactRepositoryMetadata( getArtifact( "org.apache.maven", "maven-artifact",
"2.0.1" ) );
+        repoMetadata.setMetadata( readMetadata( repoMetadata ) );
         indexer.index( repoMetadata );
         indexer.optimize();
         indexer.close();
 
-        repoMetadata = getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml",
-                                    MetadataRepositoryIndex.SNAPSHOT_METADATA );
+        repoMetadata =
+            new SnapshotArtifactRepositoryMetadata( getArtifact( "org.apache.maven", "maven-artifact",
"2.0.1" ) );
+        repoMetadata.setMetadata( readMetadata( repoMetadata ) );
         indexer.index( repoMetadata );
         indexer.optimize();
         indexer.close();
 
-        repoMetadata = getMetadata( "test", null, null, "maven-metadata.xml", MetadataRepositoryIndex.GROUP_METADATA
);
+        repoMetadata = new GroupRepositoryMetadata( "test" );
+        repoMetadata.setMetadata( readMetadata( repoMetadata ) );
         indexer.index( repoMetadata );
         indexer.optimize();
         indexer.close();
@@ -254,8 +258,8 @@
         RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE
);
         MetadataRepositoryIndex indexer = factory.createMetadataRepositoryIndex( indexPath,
repository );
 
-        RepositoryMetadata repoMetadata =
-            getMetadata( "org.apache.maven", null, null, "maven-metadata.xml", MetadataRepositoryIndex.GROUP_METADATA
);
+        RepositoryMetadata repoMetadata = new GroupRepositoryMetadata( "org.apache.maven"
);
+        repoMetadata.setMetadata( readMetadata( repoMetadata ) );
         indexer.deleteDocument( RepositoryIndex.FLD_ID, (String) repoMetadata.getKey() );
 
         RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher(
indexer );
@@ -264,56 +268,6 @@
         assertEquals( 0, metadataList.size() );
     }
 
-    /**
-     * Create RepositoryMetadata object.
-     *
-     * @param groupId      the groupId to be set
-     * @param artifactId   the artifactId to be set
-     * @param version      the version to be set
-     * @param filename     the name of the metadata file
-     * @param metadataType the type of RepositoryMetadata object to be created (GROUP, ARTIFACT
or SNAPSHOT)
-     * @return RepositoryMetadata
-     * @throws Exception
-     */
-    private RepositoryMetadata getMetadata( String groupId, String artifactId, String version,
String filename,
-                                            String metadataType )
-        throws Exception
-    {
-        RepositoryMetadata repoMetadata = null;
-        URL url;
-        InputStream is;
-        MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
-
-        //group metadata
-        if ( metadataType.equals( MetadataRepositoryIndex.GROUP_METADATA ) )
-        {
-            url = new File( repository.getBasedir() + groupId.replace( '.', '/' ) + "/" +
filename ).toURL();
-            is = url.openStream();
-            repoMetadata = new GroupRepositoryMetadata( groupId );
-            repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) )
);
-        }
-        //artifact metadata
-        else if ( metadataType.equals( MetadataRepositoryIndex.ARTIFACT_METADATA ) )
-        {
-            url = new File(
-                repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + artifactId
+ "/" + filename ).toURL();
-            is = url.openStream();
-            repoMetadata = new ArtifactRepositoryMetadata( getArtifact( groupId, artifactId,
version ) );
-            repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) )
);
-        }
-        //snapshot/version metadata
-        else if ( metadataType.equals( MetadataRepositoryIndex.SNAPSHOT_METADATA ) )
-        {
-            url = new File( repository.getBasedir() + groupId.replace( '.', '/' ) + "/" +
artifactId + "/" + version +
-                "/" + filename ).toURL();
-            is = url.openStream();
-            repoMetadata = new SnapshotArtifactRepositoryMetadata( getArtifact( groupId,
artifactId, version ) );
-            repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) )
);
-        }
-
-        return repoMetadata;
-    }
-
 
     /**
      * Create artifact object.
@@ -332,5 +286,41 @@
             artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
         }
         return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );
+    }
+
+    /**
+     * Create RepositoryMetadata object.
+     *
+     * @return RepositoryMetadata
+     */
+    private Metadata readMetadata( RepositoryMetadata repoMetadata )
+        throws RepositoryIndexSearchException
+    {
+        File file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata(
repoMetadata ) );
+
+        MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
+
+        FileReader reader = null;
+        try
+        {
+            reader = new FileReader( file );
+            return metadataReader.read( reader );
+        }
+        catch ( FileNotFoundException e )
+        {
+            throw new RepositoryIndexSearchException( "Unable to find metadata file: " +
e.getMessage(), e );
+        }
+        catch ( IOException e )
+        {
+            throw new RepositoryIndexSearchException( "Unable to read metadata file: " +
e.getMessage(), e );
+        }
+        catch ( XmlPullParserException xe )
+        {
+            throw new RepositoryIndexSearchException( "Unable to parse metadata file: " +
xe.getMessage(), xe );
+        }
+        finally
+        {
+            IOUtil.close( reader );
+        }
     }
 }

Modified: maven/repository-manager/trunk/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/RepositoryIndexSearchLayerTest.java
URL: http://svn.apache.org/viewvc/maven/repository-manager/trunk/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/RepositoryIndexSearchLayerTest.java?rev=412631&r1=412630&r2=412631&view=diff
==============================================================================
--- maven/repository-manager/trunk/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/RepositoryIndexSearchLayerTest.java
(original)
+++ maven/repository-manager/trunk/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/RepositoryIndexSearchLayerTest.java
Wed Jun  7 19:19:01 2006
@@ -23,6 +23,7 @@
 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
 import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
+import org.apache.maven.artifact.repository.metadata.Metadata;
 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
@@ -30,12 +31,13 @@
 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
 import org.codehaus.plexus.PlexusTestCase;
 import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.FileReader;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.net.URL;
+import java.io.IOException;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -109,25 +111,27 @@
         indexer.close();
 
         MetadataRepositoryIndex metaIndexer = factory.createMetadataRepositoryIndex( indexPath,
repository );
-        RepositoryMetadata repoMetadata =
-            getMetadata( "org.apache.maven", null, null, "maven-metadata.xml", MetadataRepositoryIndex.GROUP_METADATA
);
+        RepositoryMetadata repoMetadata = new GroupRepositoryMetadata( "org.apache.maven"
);
+        repoMetadata.setMetadata( readMetadata( repoMetadata ) );
         metaIndexer.index( repoMetadata );
         metaIndexer.optimize();
         metaIndexer.close();
 
-        repoMetadata = getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml",
-                                    MetadataRepositoryIndex.ARTIFACT_METADATA );
+        repoMetadata = new ArtifactRepositoryMetadata( getArtifact( "org.apache.maven", "maven-artifact",
"2.0.1" ) );
+        repoMetadata.setMetadata( readMetadata( repoMetadata ) );
         metaIndexer.index( repoMetadata );
         metaIndexer.optimize();
         metaIndexer.close();
 
-        repoMetadata = getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml",
-                                    MetadataRepositoryIndex.SNAPSHOT_METADATA );
+        repoMetadata =
+            new SnapshotArtifactRepositoryMetadata( getArtifact( "org.apache.maven", "maven-artifact",
"2.0.1" ) );
+        repoMetadata.setMetadata( readMetadata( repoMetadata ) );
         metaIndexer.index( repoMetadata );
         metaIndexer.optimize();
         metaIndexer.close();
 
-        repoMetadata = getMetadata( "test", null, null, "maven-metadata.xml", MetadataRepositoryIndex.GROUP_METADATA
);
+        repoMetadata = new GroupRepositoryMetadata( "test" );
+        repoMetadata.setMetadata( readMetadata( repoMetadata ) );
         metaIndexer.index( repoMetadata );
         metaIndexer.optimize();
         metaIndexer.close();
@@ -343,56 +347,6 @@
 
 
     /**
-     * Method for creating RepositoryMetadata object
-     *
-     * @param groupId      the groupId to be set
-     * @param artifactId   the artifactId to be set
-     * @param version      the version to be set
-     * @param filename     the name of the metadata file
-     * @param metadataType the type of RepositoryMetadata object to be created (GROUP, ARTIFACT
or SNAPSHOT)
-     * @return RepositoryMetadata
-     * @throws Exception
-     */
-    private RepositoryMetadata getMetadata( String groupId, String artifactId, String version,
String filename,
-                                            String metadataType )
-        throws Exception
-    {
-        RepositoryMetadata repoMetadata = null;
-        URL url;
-        InputStream is;
-        MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
-
-        //group metadata
-        if ( metadataType.equals( MetadataRepositoryIndex.GROUP_METADATA ) )
-        {
-            url = new File( repository.getBasedir() + groupId.replace( '.', '/' ) + "/" +
filename ).toURL();
-            is = url.openStream();
-            repoMetadata = new GroupRepositoryMetadata( groupId );
-            repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) )
);
-        }
-        //artifact metadata
-        else if ( metadataType.equals( MetadataRepositoryIndex.ARTIFACT_METADATA ) )
-        {
-            url = new File(
-                repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + artifactId
+ "/" + filename ).toURL();
-            is = url.openStream();
-            repoMetadata = new ArtifactRepositoryMetadata( getArtifact( groupId, artifactId,
version ) );
-            repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) )
);
-        }
-        //snapshot/version metadata
-        else if ( metadataType.equals( MetadataRepositoryIndex.SNAPSHOT_METADATA ) )
-        {
-            url = new File( repository.getBasedir() + groupId.replace( '.', '/' ) + "/" +
artifactId + "/" + version +
-                "/" + filename ).toURL();
-            is = url.openStream();
-            repoMetadata = new SnapshotArtifactRepositoryMetadata( getArtifact( groupId,
artifactId, version ) );
-            repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) )
);
-        }
-
-        return repoMetadata;
-    }
-
-    /**
      * Method for creating Artifact object
      *
      * @param groupId    the groupId of the artifact to be created
@@ -457,4 +411,39 @@
         return new File( path.substring( 0, path.lastIndexOf( '.' ) ) + ".pom" );
     }
 
+    /**
+     * Create RepositoryMetadata object.
+     *
+     * @return RepositoryMetadata
+     */
+    private Metadata readMetadata( RepositoryMetadata repoMetadata )
+        throws RepositoryIndexSearchException
+    {
+        File file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata(
repoMetadata ) );
+
+        MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
+
+        FileReader reader = null;
+        try
+        {
+            reader = new FileReader( file );
+            return metadataReader.read( reader );
+        }
+        catch ( FileNotFoundException e )
+        {
+            throw new RepositoryIndexSearchException( "Unable to find metadata file: " +
e.getMessage(), e );
+        }
+        catch ( IOException e )
+        {
+            throw new RepositoryIndexSearchException( "Unable to read metadata file: " +
e.getMessage(), e );
+        }
+        catch ( XmlPullParserException xe )
+        {
+            throw new RepositoryIndexSearchException( "Unable to parse metadata file: " +
xe.getMessage(), xe );
+        }
+        finally
+        {
+            IOUtil.close( reader );
+        }
+    }
 }



Mime
View raw message