Bug 335610 - Regression: p2 downloads are much slower due to picking remote artifacts even when a local file: URL is available
diff --git a/bundles/org.eclipse.equinox.p2.engine/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.p2.engine/META-INF/MANIFEST.MF
index d178f77..48d9847 100644
--- a/bundles/org.eclipse.equinox.p2.engine/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.equinox.p2.engine/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@
 Bundle-ManifestVersion: 2
 Bundle-Name: %pluginName
 Bundle-SymbolicName: org.eclipse.equinox.p2.engine;singleton:=true
-Bundle-Version: 2.0.0.qualifier
+Bundle-Version: 2.0.1.qualifier
 Bundle-Activator: org.eclipse.equinox.internal.p2.engine.EngineActivator
 Bundle-Vendor: %providerName
 Bundle-Localization: plugin
diff --git a/bundles/org.eclipse.equinox.p2.engine/src/org/eclipse/equinox/internal/p2/engine/DownloadManager.java b/bundles/org.eclipse.equinox.p2.engine/src/org/eclipse/equinox/internal/p2/engine/DownloadManager.java
index 833aad8..8daa26b 100644
--- a/bundles/org.eclipse.equinox.p2.engine/src/org/eclipse/equinox/internal/p2/engine/DownloadManager.java
+++ b/bundles/org.eclipse.equinox.p2.engine/src/org/eclipse/equinox/internal/p2/engine/DownloadManager.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2009 IBM Corporation and others.
+ * Copyright (c) 2007, 2011 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -11,8 +11,7 @@
  *******************************************************************************/
 package org.eclipse.equinox.internal.p2.engine;
 
-import java.util.ArrayList;
-import java.util.Iterator;
+import java.util.*;
 import org.eclipse.core.runtime.*;
 import org.eclipse.equinox.internal.p2.engine.phases.Collect;
 import org.eclipse.equinox.p2.core.IProvisioningAgent;
@@ -27,6 +26,25 @@
 	ArrayList<IArtifactRequest> requestsToProcess = new ArrayList<IArtifactRequest>();
 	private IProvisioningAgent agent = null;
 
+	/**
+	 * This Comparator sorts the repositories such that local repositories are first.
+	 * TODO: This is copied from the ProvisioningContext class. Can we combine them? 
+	 * See https://bugs.eclipse.org/335153.
+	 */
+	private static final Comparator<IArtifactRepository> LOCAL_FIRST_COMPARATOR = new Comparator<IArtifactRepository>() {
+		private static final String FILE_PROTOCOL = "file"; //$NON-NLS-1$
+
+		public int compare(IArtifactRepository arg0, IArtifactRepository arg1) {
+			String protocol0 = arg0.getLocation().getScheme();
+			String protocol1 = arg1.getLocation().getScheme();
+			if (FILE_PROTOCOL.equals(protocol0) && !FILE_PROTOCOL.equals(protocol1))
+				return -1;
+			if (!FILE_PROTOCOL.equals(protocol0) && FILE_PROTOCOL.equals(protocol1))
+				return 1;
+			return 0;
+		}
+	};
+
 	public DownloadManager(ProvisioningContext context, IProvisioningAgent agent) {
 		provContext = context;
 		this.agent = agent;
@@ -74,6 +92,10 @@
 			IArtifactRepository[] repositories = repoQueryable.query(all, subMonitor.newChild(250)).toArray(IArtifactRepository.class);
 			if (repositories.length == 0)
 				return new Status(IStatus.ERROR, EngineActivator.ID, Messages.download_no_repository, new Exception(Collect.NO_ARTIFACT_REPOSITORIES_AVAILABLE));
+			// Although we get a sorted list back from the ProvisioningContext above, it 
+			// gets unsorted when we convert the queryable into an array so we must re-sort it.
+			// See https://bugs.eclipse.org/335153.
+			Arrays.sort(repositories, LOCAL_FIRST_COMPARATOR);
 			fetch(repositories, subMonitor.newChild(500));
 			return overallStatus(monitor);
 		} finally {