Adding download manager that uses wagons to download arbitrary URLs, and removing...
authorJohn Dennis Casey <jdcasey@apache.org>
Tue, 28 Feb 2006 19:51:36 +0000 (19:51 +0000)
committerJohn Dennis Casey <jdcasey@apache.org>
Tue, 28 Feb 2006 19:51:36 +0000 (19:51 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/shared/trunk@381764 13f79535-47bb-0310-9956-ffa450edef68

pom.xml
src/main/java/org/apache/maven/shared/io/DefaultDownloadManager.java [new file with mode: 0644]
src/main/java/org/apache/maven/shared/io/DownloadFailedException.java [new file with mode: 0644]
src/main/java/org/apache/maven/shared/io/DownloadManager.java [new file with mode: 0644]
src/main/java/org/apache/maven/shared/io/Locator.java
src/main/resources/META-INF/plexus/components.xml [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 80cb4aa..0f97a71 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -7,7 +7,7 @@
   <parent>
     <groupId>org.apache.maven.shared</groupId>
     <artifactId>shared-components-parent</artifactId>
-    <version>1.0-SNAPSHOT</version>
+    <version>1</version>
   </parent>
 
   <artifactId>maven-shared-io</artifactId>
   
   <dependencies>
     <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-artifact-manager</artifactId>
+      <version>2.0</version>
+    </dependency>
+    <dependency>
       <groupId>org.codehaus.plexus</groupId>
       <artifactId>plexus-utils</artifactId>
       <version>1.0.4</version>
     </dependency>
-    <dependency>
-      <groupId>org.apache.maven.shared</groupId>
-      <artifactId>maven-shared-monitor</artifactId>
-      <version>1.0-SNAPSHOT</version>
-    </dependency>
   </dependencies>
 </project>
diff --git a/src/main/java/org/apache/maven/shared/io/DefaultDownloadManager.java b/src/main/java/org/apache/maven/shared/io/DefaultDownloadManager.java
new file mode 100644 (file)
index 0000000..b07ced3
--- /dev/null
@@ -0,0 +1,133 @@
+package org.apache.maven.shared.io;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.maven.artifact.manager.WagonManager;
+import org.apache.maven.wagon.ConnectionException;
+import org.apache.maven.wagon.ResourceDoesNotExistException;
+import org.apache.maven.wagon.TransferFailedException;
+import org.apache.maven.wagon.UnsupportedProtocolException;
+import org.apache.maven.wagon.Wagon;
+import org.apache.maven.wagon.authentication.AuthenticationException;
+import org.apache.maven.wagon.authorization.AuthorizationException;
+import org.apache.maven.wagon.events.TransferListener;
+import org.apache.maven.wagon.repository.Repository;
+
+public class DefaultDownloadManager implements DownloadManager
+{
+    
+    private WagonManager wagonManager;
+    
+    /* (non-Javadoc)
+     * @see org.apache.maven.shared.io.DownloadManager#download(java.lang.String)
+     */
+    public File download( String url ) throws DownloadFailedException
+    {
+        return download( url, Collections.EMPTY_LIST );
+    }
+    
+    /* (non-Javadoc)
+     * @see org.apache.maven.shared.io.DownloadManager#download(java.lang.String, java.util.List)
+     */
+    public File download( String url, List transferListeners ) throws DownloadFailedException
+    {
+        URL sourceUrl;
+        try
+        {
+            sourceUrl = new URL( url );
+        }
+        catch ( MalformedURLException e )
+        {
+            throw new DownloadFailedException( url, "Download failed due to invalid URL. Reason: " + e.getMessage(), e );
+        }
+
+        Wagon wagon = null;
+
+        try
+        {
+            // Retrieve the correct Wagon instance used to download the remote archive
+            wagon = wagonManager.getWagon( sourceUrl.getProtocol() );
+
+            // create the landing file in /tmp for the downloaded source archive
+            File downloaded = File.createTempFile( "source-archive-", null );
+            downloaded.deleteOnExit();
+
+            // split the download URL into base URL and remote path for connecting, then retrieving.
+            String remotePath = sourceUrl.getPath();
+            String baseUrl = url.substring( 0, url.length() - remotePath.length() );
+
+            for ( Iterator it = transferListeners.iterator(); it.hasNext(); )
+            {
+                TransferListener listener = (TransferListener) it.next();
+                wagon.addTransferListener( listener );
+            }
+
+            // connect to the remote site, and retrieve the archive. Note the separate methods in which
+            // base URL and remote path are used.
+            Repository repo = new Repository( sourceUrl.getHost(), baseUrl );
+            
+            wagon.connect( repo, wagonManager.getAuthenticationInfo( repo.getId() ), wagonManager.getProxy( sourceUrl
+                .getProtocol() ) );
+            
+            wagon.get( remotePath, downloaded );
+            
+            return downloaded;
+        }
+        catch ( UnsupportedProtocolException e )
+        {
+            throw new DownloadFailedException( url, "Download failed. Reason: " + e.getMessage(), e );
+        }
+        catch ( IOException e )
+        {
+            throw new DownloadFailedException( url, "Download failed. Reason: " + e.getMessage(), e );
+        }
+        catch ( TransferFailedException e )
+        {
+            throw new DownloadFailedException( url, "Download failed. Reason: " + e.getMessage(), e );
+        }
+        catch ( ResourceDoesNotExistException e )
+        {
+            throw new DownloadFailedException( url, "Download failed. Reason: " + e.getMessage(), e );
+        }
+        catch ( AuthorizationException e )
+        {
+            throw new DownloadFailedException( url, "Download failed. Reason: " + e.getMessage(), e );
+        }
+        catch ( ConnectionException e )
+        {
+            throw new DownloadFailedException( url, "Download failed. Reason: " + e.getMessage(), e );
+        }
+        catch ( AuthenticationException e )
+        {
+            throw new DownloadFailedException( url, "Download failed. Reason: " + e.getMessage(), e );
+        }
+        finally
+        {
+            // ensure the Wagon instance is closed out properly.
+            if ( wagon != null )
+            {
+                try
+                {
+                    wagon.disconnect();
+                }
+                catch ( ConnectionException e )
+                {
+//                    getLog().debug( "Failed to disconnect wagon for: " + url, e );
+                }
+
+                for ( Iterator it = transferListeners.iterator(); it.hasNext(); )
+                {
+                    TransferListener listener = (TransferListener) it.next();
+                    wagon.removeTransferListener( listener );
+                }
+            }
+        }
+    }
+
+}
diff --git a/src/main/java/org/apache/maven/shared/io/DownloadFailedException.java b/src/main/java/org/apache/maven/shared/io/DownloadFailedException.java
new file mode 100644 (file)
index 0000000..d5b2c86
--- /dev/null
@@ -0,0 +1,28 @@
+package org.apache.maven.shared.io;
+
+public class DownloadFailedException
+    extends Exception
+{
+    
+    private static final long serialVersionUID = 1L;
+    
+    private String url;
+
+    public DownloadFailedException( String url, String message, Throwable cause )
+    {
+        super( message, cause );
+        this.url = url;
+    }
+
+    public DownloadFailedException( String url, String message )
+    {
+        super( message );
+        this.url = url;
+    }
+    
+    public String getUrl()
+    {
+        return url;
+    }
+
+}
diff --git a/src/main/java/org/apache/maven/shared/io/DownloadManager.java b/src/main/java/org/apache/maven/shared/io/DownloadManager.java
new file mode 100644 (file)
index 0000000..8b643d8
--- /dev/null
@@ -0,0 +1,15 @@
+package org.apache.maven.shared.io;
+
+import java.io.File;
+import java.util.List;
+
+public interface DownloadManager
+{
+
+    File download( String url )
+        throws DownloadFailedException;
+
+    File download( String url, List transferListeners )
+        throws DownloadFailedException;
+
+}
\ No newline at end of file
index a226fd3..9598253 100644 (file)
@@ -16,8 +16,6 @@ package org.apache.maven.shared.io;
  * limitations under the License.
  */
 
-import org.apache.maven.shared.monitor.BasicMonitor;
-import org.apache.maven.shared.monitor.Monitor;
 import org.codehaus.plexus.util.FileUtils;
 import org.codehaus.plexus.util.StringUtils;
 
@@ -34,30 +32,13 @@ import java.net.URL;
  */
 public class Locator
 {
-    private Monitor monitor;
-
     /**
      * Create a Locator object.
      * 
      * @param logger the logger object to log with.
      */
-    public Locator( Monitor logger )
+    public Locator()
     {
-        this.monitor = logger;
-    }
-
-    /**
-     * Obtain a Log object.
-     * 
-     * @return the Log object.
-     */
-    private Monitor getLog()
-    {
-        if ( this.monitor == null )
-        {
-            this.monitor = new BasicMonitor( System.out );
-        }
-        return this.monitor;
     }
 
     /**
@@ -79,7 +60,6 @@ public class Locator
     public File resolveLocation( String location, String localfile )
         throws IOException
     {
-        getLog().debug( "resolveLocation(" + location + ", " + localfile + ")" );
         if ( StringUtils.isEmpty( location ) )
         {
             return null;
@@ -92,34 +72,28 @@ public class Locator
         {
             // Found a URL
             URL url = new URL( location );
-            getLog().debug( "Potential URL: " + url.toExternalForm() );
             FileUtils.copyURLToFile( url, retFile );
         }
         else
         {
-            getLog().debug( "Location is not a URL." );
             // Attempt a File.
             File fileLocation = new File( location );
             if ( fileLocation.exists() )
             {
                 // Found a File.
-                getLog().debug( "Potential File: " + fileLocation.getAbsolutePath() );
                 FileUtils.copyFile( fileLocation, retFile );
             }
             else
             {
-                getLog().debug( "Location is not a File." );
                 // Attempt a Resource.
                 URL url = this.getClass().getClassLoader().getResource( location );
                 if ( url != null )
                 {
                     // Found a Resource.
-                    getLog().debug( "Potential Resource: " + url.toExternalForm() );
                     FileUtils.copyURLToFile( url, retFile );
                 }
                 else
                 {
-                    getLog().debug( "Location is not a Resource." );
                     throw new IOException( "Unable to find location '" + location + "' as URL, File or Resource." );
                 }
             }
diff --git a/src/main/resources/META-INF/plexus/components.xml b/src/main/resources/META-INF/plexus/components.xml
new file mode 100644 (file)
index 0000000..a64e6f0
--- /dev/null
@@ -0,0 +1,14 @@
+<component-set>
+  <components>
+    <component>
+      <role>org.apache.maven.shared.io.DownloadManager</role>
+      <role-hint>default</role-hint>
+      <implementation>org.apache.maven.shared.io.DefaultDownloadManager</implementation>
+      <requirements>
+        <requirement>
+          <role>org.apache.maven.artifact.manager.WagonManager</role>
+        </requirement>
+      </requirements>
+    </component>
+  </components>
+</component-set>
\ No newline at end of file