You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2010/06/07 22:39:23 UTC

svn commit: r952423 - /maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/source/RemoteCatalogArchetypeDataSource.java

Author: hboutemy
Date: Mon Jun  7 20:39:23 2010
New Revision: 952423

URL: http://svn.apache.org/viewvc?rev=952423&view=rev
Log:
[ARCHETYPE-202] use "archetype" repository configuration from settings.xml when downloading archetype catalog
after a patch submitted by: Marco Rico-Gomez

Modified:
    maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/source/RemoteCatalogArchetypeDataSource.java

Modified: maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/source/RemoteCatalogArchetypeDataSource.java
URL: http://svn.apache.org/viewvc/maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/source/RemoteCatalogArchetypeDataSource.java?rev=952423&r1=952422&r2=952423&view=diff
==============================================================================
--- maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/source/RemoteCatalogArchetypeDataSource.java (original)
+++ maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/source/RemoteCatalogArchetypeDataSource.java Mon Jun  7 20:39:23 2010
@@ -20,15 +20,19 @@ package org.apache.maven.archetype.sourc
  */
 
 import org.apache.maven.archetype.catalog.Archetype;
+import org.apache.maven.archetype.catalog.ArchetypeCatalog;
 import org.apache.maven.artifact.manager.WagonManager;
 import org.apache.maven.wagon.Wagon;
+import org.apache.maven.wagon.WagonException;
+import org.apache.maven.wagon.authentication.AuthenticationInfo;
+import org.apache.maven.wagon.proxy.ProxyInfo;
 import org.apache.maven.wagon.repository.Repository;
+import org.codehaus.plexus.util.ReaderFactory;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.List;
 import java.util.Properties;
-import org.apache.maven.archetype.catalog.ArchetypeCatalog;
-import org.codehaus.plexus.util.ReaderFactory;
 
 /**
  * @plexus.component role-hint="remote-catalog"
@@ -42,6 +46,12 @@ public class RemoteCatalogArchetypeDataS
 
     public static final String REPOSITORY_PROPERTY = "repository";
 
+    /**
+     * Id of the repository used to download catalog file. Proxy or authentication info can
+     * be setup in settings.xml.
+     */
+    public static final String REPOSITORY_ID = "archetype";
+
     public ArchetypeCatalog getArchetypeCatalog( Properties properties )
         throws ArchetypeDataSourceException
     {
@@ -52,55 +62,27 @@ public class RemoteCatalogArchetypeDataS
             throw new ArchetypeDataSourceException( "To use the remote catalog you must specify the 'repository' property with an URL." );
         }
 
-        try
+        if ( repository.endsWith( "/" ) )
         {
-            if ( repository.endsWith( "/" ) )
-            {
-                repository = repository.substring( 0, repository.length() - 1 );
-            }
+            repository = repository.substring( 0, repository.length() - 1 );
+        }
 
-            getLogger().debug( "Searching for remote catalog: " + repository + "/archetype-catalog.xml" );
-            // We use wagon to take advantage of a Proxy that has already been setup in a Maven environment.
-            Repository wagonRepository = new Repository( "archetype", repository );
-            Wagon wagon = wagonManager.getWagon( wagonRepository );
-            File catalog = File.createTempFile( "archetype-catalog", ".xml" );
-            try
-            {
-                wagon.connect( wagonRepository );
-                wagon.get( "archetype-catalog.xml", catalog );
-            }
-            finally
-            {
-                disconnectWagon( wagon );
-            }
-            return readCatalog( ReaderFactory.newXmlReader( catalog ) );
+        try
+        {
+            return downloadCatalog( repository, ARCHETYPE_CATALOG_FILENAME );
         }
         catch ( ArchetypeDataSourceException e )
         {
             throw e;
         }
         catch ( Exception e )
-        { // When the default archetype catalog names doesn't works, we assume the repository is the URL to a file
+        { // When the default archetype catalog name doesn't work, we assume the repository is the URL to a file
+            String repositoryPath = repository.substring( 0, repository.lastIndexOf( "/" ) );
+            String filename = repository.substring( repository.lastIndexOf( "/" ) + 1 );
+
             try
             {
-                String repositoryPath = repository.substring( 0, repository.lastIndexOf( "/" ) );
-                String fileName = repository.substring( repository.lastIndexOf( "/" ) + 1 );
-
-                getLogger().debug( "Searching for remote catalog: " + repositoryPath + "/" + fileName );
-                // We use wagon to take advantage of a Proxy that has already been setup in a Maven environment.
-                Repository wagonRepository = new Repository( "archetype", repositoryPath );
-                Wagon wagon = wagonManager.getWagon( wagonRepository );
-                File catalog = File.createTempFile( "archetype-catalog", ".xml" );
-                try
-                {
-                    wagon.connect( wagonRepository );
-                    wagon.get( fileName, catalog );
-                }
-                finally
-                {
-                    disconnectWagon( wagon );
-                }
-                return readCatalog( ReaderFactory.newXmlReader( catalog ) );
+                return downloadCatalog( repositoryPath, filename );
             }
             catch ( Exception ex )
             {
@@ -120,28 +102,16 @@ public class RemoteCatalogArchetypeDataS
             throw new ArchetypeDataSourceException( "To use the remote catalog you must specify the 'remote-catalog.repository' property correctly in your ~/.m2/archetype-catalog.properties file." );
         }
 
-        try
+        if ( repository.endsWith( "/" ) )
         {
-            if ( repository.endsWith( "/" ) )
-            {
-                repository = repository.substring( 0, repository.length() - 1 );
-            }
-
-            // We use wagon to take advantage of a Proxy that has already been setup in a Maven environment.
-
-            Repository wagonRepository = new Repository( "archetype", repository );
-
-            Wagon wagon = wagonManager.getWagon( wagonRepository );
-
-            File catalog = File.createTempFile( "archetype-catalog", ".xml" );
-
-            wagon.connect( wagonRepository );
-
-            wagon.get( "archetype-catalog.xml", catalog );
+            repository = repository.substring( 0, repository.length() - 1 );
+        }
 
-            wagon.disconnect();
+        try
+        {
+            ArchetypeCatalog catalog = downloadCatalog( repository, ARCHETYPE_CATALOG_FILENAME );
 
-            return createArchetypeMap( readCatalog( ReaderFactory.newXmlReader( catalog ) ) );
+            return createArchetypeMap( catalog );
         }
         catch ( Exception e )
         {
@@ -155,6 +125,31 @@ public class RemoteCatalogArchetypeDataS
         throw new ArchetypeDataSourceException( "Not supported yet." );
     }
 
+    private ArchetypeCatalog downloadCatalog( String repositoryPath, String filename )
+        throws WagonException, IOException, ArchetypeDataSourceException
+    {
+        getLogger().debug( "Searching for remote catalog: " + repositoryPath + "/" + filename );
+
+        // We use wagon to take advantage of a Proxy that has already been setup in a Maven environment.
+        Repository wagonRepository = new Repository( REPOSITORY_ID, repositoryPath );
+        AuthenticationInfo authInfo = wagonManager.getAuthenticationInfo( wagonRepository.getId() );
+        ProxyInfo proxyInfo = wagonManager.getProxy( wagonRepository.getProtocol() );
+
+        Wagon wagon = wagonManager.getWagon( wagonRepository );
+
+        File catalog = File.createTempFile( "archetype-catalog", ".xml" );
+        try
+        {
+            wagon.connect( wagonRepository, authInfo, proxyInfo );
+            wagon.get( filename, catalog );
+        }
+        finally
+        {
+            disconnectWagon( wagon );
+        }
+        return readCatalog( ReaderFactory.newXmlReader( catalog ) );
+    }
+
     private void disconnectWagon( Wagon wagon )
     {
         try