Currently if you specify a dependancy on a jar, it is downloaded from only
one place - the URL pointed to by the maven.repo.remote property.  This is 
fine for maven itself, but what about organizations that want a "local" 
remote repository of jars in addition to those provided by Maven?

This patch permits the maven.repo.remote property to be a comma separated 
list of URLs, providing the ability to have multiple "remote" repositories 
of pre-built jars.

For example:

maven.repo.remote = http://jakarta.apache.org/turbine/jars,\
http://primary.corp.server/jars,\
http://secondary.corp.server/jars

Keep in mind that unless you are willing to mirror the primary Maven 
remote repository, the first entry in the list should be

http://jakarta.apache.org/turbine/jars

(or whatever the primary repo URL is at the time).

The idea for this patch was originally presented by Henri Yandell on April 
4th, but so much code has changed since his patch that it can no longer be 
applied.

Glenn McAllister
SOMA Networks, Inc.
Index: src/java/org/apache/maven/ProjectVerifier.java
===================================================================
RCS file: 
/home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/ProjectVerifier.java,v
retrieving revision 1.1
diff -u -r1.1 ProjectVerifier.java
--- src/java/org/apache/maven/ProjectVerifier.java      22 Apr 2002 20:21:57 -0000     
 1.1
+++ src/java/org/apache/maven/ProjectVerifier.java      30 Apr 2002 03:33:59 -0000
@@ -59,12 +59,15 @@
 import java.io.FileReader;
 
 import java.net.URL;
+import java.net.MalformedURLException;
 
 import java.util.Iterator;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
+import java.util.ArrayList;
 import java.util.Map;
+import java.util.StringTokenizer;
 
 import org.apache.commons.lang.Strings;
 
@@ -92,9 +95,9 @@
     private String mavenLocalRepo;
 
     /**
-     * Maven remote repo
+     * Maven remote repo locations
      */
-    private String mavenRemoteRepo;
+    private List mavenRemoteRepos = new ArrayList();
 
     /**
      * Verbosity during downloading of missing resources.
@@ -148,7 +151,7 @@
     }
 
     /**
-     * Sets the mavenLocalRepo attribute of the Get object
+     * Sets the mavenLocalRepo attribute.
      */
     public void setMavenLocalRepo(String mavenLocalRepo)
     {
@@ -164,19 +167,24 @@
     }
 
     /**
-     * Sets the mavenRemoteRepo attribute of the Get object
+     * Sets the mavenRemoteRepo attribute.  This can be a comma separated list
+     * of URLs.
      */
     public void setMavenRemoteRepo(String mavenRemoteRepo)
     {
-        this.mavenRemoteRepo = mavenRemoteRepo;
+        StringTokenizer st = new StringTokenizer( mavenRemoteRepo, "," );
+        while (st.hasMoreTokens())
+        {
+            mavenRemoteRepos.add( st.nextToken() );
+        }
     }
 
     /**
-     * Get the maven remote repo.
+     * Get the maven remote repository list of URLs.
      */
-    public String getMavenRemoteRepo()
+    public List getMavenRemoteRepo()
     {
-        return mavenRemoteRepo;
+        return mavenRemoteRepos;
     }
 
     /**
@@ -217,19 +225,34 @@
         // them for the user.
         if (failedDependencies.size() > 0)
         {
-            File f = new File(getMavenLocalRepo(), NON_DIST_JAR_LIST);        
-            
-            HttpUtils.getFile(new URL(getMavenRemoteRepo() + NON_DIST_JAR_LIST),
-                              f, NON_DIST_JAR_LIST,verbose, 
-                              ignoreErrors,useTimestamp,"","");
+            File f = new File(getMavenLocalRepo(), NON_DIST_JAR_LIST);
 
-            Map nonDistMap = getNonDistMap(f);
+            Map nonDistMap;
+            if (!getNonDistFile( f ))
+            {
+                StringBuffer msg = new StringBuffer( 128 );
+                msg.append("-----------------------------------------------\n")
+                   .append("W A R N I N G\n")
+                   .append("-----------------------------------------------\n")
+                   .append("Failed to download list of non-distributable\n")
+                   .append("jars ")
+                   .append(NON_DIST_JAR_LIST)
+                   .append("\n")
+                   .append("Will continue with attempted downloads of ")
+                   .append("dependent jars.\n\n" );
+
+                log( msg.toString() );
+
+                nonDistMap = new HashMap(0);
+            }
+            else
+            {
+                nonDistMap = getNonDistMap(f);
+            }
             
             for (Iterator i = failedDependencies.iterator(); i.hasNext();)
             {
                 String dependency = (String) i.next();
-                log("Retrieving missing dependency: " + dependency);
-                URL url = new URL(mavenRemoteRepo + dependency);
                 File destinationFile = new File(getMavenLocalRepo(),dependency);
                 
                 if (nonDistMap.containsKey(dependency))
@@ -254,9 +277,16 @@
                     }
                     continue;
                 }
-                
-                HttpUtils.getFile(url, destinationFile, dependency, verbose, 
-                                  ignoreErrors, useTimestamp,"","");
+
+                if (!getRemoteFile( dependency, destinationFile ))
+                {
+                    
+warnings.append("-------------------------------------------------\n")
+                            .append("W A R N I N G\n")
+                            
+.append("------------------------------------------------\n")
+                            .append("Failed to download dependent file ")
+                            .append(dependency)
+                            .append("\n\n");
+                }
             }
         }
         
@@ -269,6 +299,40 @@
             // continue.
             getProject().setProperty("verificationFailed","true");
         }            
+    }
+
+    /**
+     * Download the list of non-distributable jars.
+     */
+    private boolean getNonDistFile(File f)
+    {
+        return getRemoteFile( f.getName(), f );
+    }
+
+    private boolean getRemoteFile( String remoteFile, File localFile )
+    {
+        boolean gotFile = false;
+        Iterator remoteIter = getMavenRemoteRepo().iterator();
+
+        while (!gotFile && remoteIter.hasNext())
+        {
+            String remoteRepo = (String)remoteIter.next();
+            try
+            {
+                gotFile = HttpUtils.getFile( new URL(remoteRepo + remoteFile),
+                                            localFile, remoteFile, verbose,
+                                            ignoreErrors, useTimestamp, 
+                                            "", "" );
+            }
+            catch (MalformedURLException mue)
+            {
+                mue.printStackTrace();
+                log("Cannot retrieve " + remoteFile + " from " + remoteRepo +
+                        "; malformed URL" );
+            }
+        }
+
+        return gotFile;
     }
     
     /**
Index: src/java/org/apache/maven/util/HttpUtils.java
===================================================================
RCS file: 
/home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/util/HttpUtils.java,v
retrieving revision 1.1
diff -u -r1.1 HttpUtils.java
--- src/java/org/apache/maven/util/HttpUtils.java       11 Apr 2002 11:57:40 -0000     
 1.1
+++ src/java/org/apache/maven/util/HttpUtils.java       30 Apr 2002 03:33:59 -0000
@@ -54,23 +54,24 @@
  * <http://www.apache.org/>.
  */
 
+import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
+import java.io.FileReader;
 import java.io.InputStream;
 import java.io.IOException;
 
 import java.net.URL;
 import java.net.URLConnection;
 import java.net.HttpURLConnection;
-import java.util.Date;
 
+import java.util.Date;
 import java.util.List;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.HashMap;
-import java.io.BufferedReader;
-import java.io.FileReader;
 
 /**
  * Http utils for retrieving files.
@@ -81,10 +82,15 @@
  */
 public class HttpUtils
 {
-    public static void getFile(URL source, File destinationFile, String file,
+    /**
+     * Retrieve a remote file.  Returns true if the file was successfully
+     * retrieved or if it is up to date (when the useTimestamp flag is set).
+     */
+    public static boolean getFile(URL source, File destinationFile, String file,
                                boolean verbose, boolean ignoreErrors, boolean 
useTimestamp,
                                String uname, String pword)
     {
+        boolean retrievedFile = false;
         try
         {
             logx("Getting: " + source);
@@ -147,13 +153,13 @@
                     //and trace out something so the user doesn't think that the
                     //download happened when it didnt
                     logx("Not modified - so not downloaded");
-                    return;
+                    return true;
                 }
                 // test for 401 result (HTTP only)
                 if (httpConnection.getResponseCode() == 
HttpURLConnection.HTTP_UNAUTHORIZED)
                 {
                     logx("Not authorized - check " + destinationFile + " for 
details");
-                    return;
+                    return false;
                 }
             }
 
@@ -182,7 +188,7 @@
                 logx("Can't get " + file + " to " + destinationFile);
                 if (ignoreErrors)
                 {
-                    return;
+                    return false;
                 }
                 throw new Exception(
                     "Can't get " + file + " to " + destinationFile);
@@ -217,25 +223,20 @@
                     touchFile(destinationFile, remoteTimestamp);
                 }
             }
+
+            retrievedFile = true;
         }
-        catch (IOException ioe)
+        catch (FileNotFoundException fnfe)
         {
-            ioe.printStackTrace();
-            
-            logx("Error getting " + file + " to " + destinationFile);
-            if (ignoreErrors)
-            {
-                return;
-            }
-
-            // We don't ever really want to throw an exception
-            // just want to truck along.
-            //throw new Exception(ioe.getMessage());
+            logx( "Error getting " + source + " ; it does not exist." );
         }
         catch (Exception e)
         {
             e.printStackTrace();
+            logx("Error getting " + file + " to " + destinationFile);
         }
+
+        return retrievedFile;
     }
 
 

Reply via email to