|
Lines 10-21
Link Here
|
| 10 |
*******************************************************************************/ |
10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.equinox.p2.download; |
11 |
package org.eclipse.equinox.p2.download; |
| 12 |
|
12 |
|
| 13 |
import java.util.ArrayList; |
13 |
import java.util.*; |
| 14 |
import java.util.Iterator; |
|
|
| 15 |
import org.eclipse.core.runtime.*; |
14 |
import org.eclipse.core.runtime.*; |
| 16 |
import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper; |
15 |
import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper; |
| 17 |
import org.eclipse.equinox.internal.p2.download.DownloadActivator; |
16 |
import org.eclipse.equinox.internal.p2.download.DownloadActivator; |
| 18 |
import org.eclipse.equinox.p2.artifact.repository.*; |
17 |
import org.eclipse.equinox.p2.artifact.repository.*; |
|
|
18 |
import org.eclipse.equinox.p2.download.strategy.*; |
| 19 |
|
19 |
|
| 20 |
public class DownloadManager { |
20 |
public class DownloadManager { |
| 21 |
ArrayList requestsToProcess = new ArrayList(); |
21 |
ArrayList requestsToProcess = new ArrayList(); |
|
Lines 51-94
Link Here
|
| 51 |
try { |
51 |
try { |
| 52 |
if (requestsToProcess.isEmpty()) |
52 |
if (requestsToProcess.isEmpty()) |
| 53 |
return Status.OK_STATUS; |
53 |
return Status.OK_STATUS; |
|
|
54 |
|
| 55 |
IDownloadStrategyManager strategyMgr = (IDownloadStrategyManager) ServiceHelper.getService(DownloadActivator.context, IDownloadStrategyManager.class.getName()); |
| 56 |
IDownloadStrategy[] strategies = strategyMgr.getKnownStrategies(); |
| 57 |
if (strategies.length == 0) |
| 58 |
return new Status(IStatus.ERROR, DownloadActivator.ID, "No strategy available"); |
| 59 |
|
| 54 |
IArtifactRepositoryManager repoMgr = (IArtifactRepositoryManager) ServiceHelper.getService(DownloadActivator.context, IArtifactRepositoryManager.class.getName()); |
60 |
IArtifactRepositoryManager repoMgr = (IArtifactRepositoryManager) ServiceHelper.getService(DownloadActivator.context, IArtifactRepositoryManager.class.getName()); |
| 55 |
IArtifactRepository[] repositories = repoMgr.getKnownRepositories(); |
61 |
IArtifactRepository[] repositories = repoMgr.getKnownRepositories(); |
| 56 |
if (repositories.length == 0) |
62 |
if (repositories.length == 0) |
| 57 |
return new Status(IStatus.ERROR, DownloadActivator.ID, "No repository available"); |
63 |
return new Status(IStatus.ERROR, DownloadActivator.ID, "No repository available"); |
| 58 |
// TODO for now ensure that we try local repos first |
64 |
|
| 59 |
if (!fetch(sortRepositories(repositories, true), subMonitor)) |
65 |
// determine which strategies will work best for these repositories |
| 60 |
return overallStatus(monitor); |
66 |
StrategyRequests[] strategyRequests = determineStrategies(monitor, strategies, repositories); |
| 61 |
// TODO then try the othe repositories. |
67 |
|
| 62 |
fetch(sortRepositories(repositories, false), subMonitor); |
68 |
// delegate work to the various strategies |
|
|
69 |
for (int i = 0; i < strategyRequests.length && !requestsToProcess.isEmpty() && !monitor.isCanceled(); i++) { |
| 70 |
IStatus dlStatus = strategyRequests[i].process(subMonitor); |
| 71 |
if (dlStatus.getSeverity() == IStatus.CANCEL) |
| 72 |
return overallStatus(monitor); |
| 73 |
filterUnfetched(); |
| 74 |
subMonitor.setWorkRemaining(requestsToProcess.size()); |
| 75 |
// notifyFetched(); |
| 76 |
} |
| 63 |
return overallStatus(monitor); |
77 |
return overallStatus(monitor); |
| 64 |
} finally { |
78 |
} finally { |
| 65 |
subMonitor.done(); |
79 |
subMonitor.done(); |
| 66 |
} |
80 |
} |
| 67 |
} |
81 |
} |
| 68 |
|
82 |
|
| 69 |
private IArtifactRepository[] sortRepositories(IArtifactRepository[] repositories, boolean local) { |
83 |
private StrategyRequests[] determineStrategies(IProgressMonitor monitor, IDownloadStrategy[] strategies, IArtifactRepository[] repositories) { |
| 70 |
ArrayList result = new ArrayList(repositories.length); |
84 |
StrategyRequests[] strategyRequests = new StrategyRequests[strategies.length]; |
| 71 |
for (int i = 0; i < repositories.length; i++) { |
85 |
for (int i = 0; i < repositories.length && !monitor.isCanceled(); i++) { |
| 72 |
IArtifactRepository repository = repositories[i]; |
|
|
| 73 |
if (local && repository.getLocation().getProtocol().equals("file")) |
| 74 |
result.add(repository); |
| 75 |
else if (!local && !repository.getLocation().getProtocol().equals("file")) |
| 76 |
result.add(repository); |
| 77 |
} |
| 78 |
return (IArtifactRepository[]) result.toArray(new IArtifactRepository[result.size()]); |
| 79 |
} |
| 80 |
|
| 81 |
private boolean fetch(IArtifactRepository[] repositories, SubMonitor monitor) { |
| 82 |
for (int i = 0; i < repositories.length && !requestsToProcess.isEmpty() && !monitor.isCanceled(); i++) { |
| 83 |
IArtifactRequest[] requests = getRequestsForRepository(repositories[i]); |
86 |
IArtifactRequest[] requests = getRequestsForRepository(repositories[i]); |
| 84 |
IStatus dlStatus = repositories[i].getArtifacts(requests, monitor.newChild(requests.length)); |
87 |
if (requests.length == 0) |
| 85 |
if (dlStatus.getSeverity() == IStatus.CANCEL) |
88 |
continue; |
| 86 |
return false; |
89 |
|
| 87 |
filterUnfetched(); |
90 |
IDownloadStrategy bestStrategy = null; |
| 88 |
monitor.setWorkRemaining(requestsToProcess.size()); |
91 |
int strategyIdx = -1; |
| 89 |
// notifyFetched(); |
92 |
for (int j = 0; j < strategies.length; j++) { |
|
|
93 |
if (!strategies[j].supportsRepository(repositories[i])) |
| 94 |
break; |
| 95 |
if (bestStrategy == null || strategies[j].isBetterThan(bestStrategy, repositories[i])) { |
| 96 |
bestStrategy = strategies[j]; |
| 97 |
strategyIdx = j; |
| 98 |
} |
| 99 |
} |
| 100 |
if (bestStrategy == null) |
| 101 |
continue; |
| 102 |
|
| 103 |
if (strategyRequests[strategyIdx] == null) |
| 104 |
strategyRequests[strategyIdx] = new StrategyRequests(strategies[strategyIdx]); |
| 105 |
strategyRequests[strategyIdx].queueWork(repositories[i], requests); |
| 90 |
} |
106 |
} |
| 91 |
return true; |
107 |
|
|
|
108 |
List toUse = new ArrayList(strategyRequests.length); |
| 109 |
for (int i = 0; i < strategyRequests.length; i++) |
| 110 |
if (strategyRequests[i] != null) |
| 111 |
toUse.add(strategyRequests[i]); |
| 112 |
return (StrategyRequests[]) toUse.toArray(new StrategyRequests[toUse.size()]); |
| 92 |
} |
113 |
} |
| 93 |
|
114 |
|
| 94 |
private IArtifactRequest[] getRequestsForRepository(IArtifactRepository repository) { |
115 |
private IArtifactRequest[] getRequestsForRepository(IArtifactRepository repository) { |
|
Lines 115-126
Link Here
|
| 115 |
if (requestsToProcess.size() == 0) |
136 |
if (requestsToProcess.size() == 0) |
| 116 |
return Status.OK_STATUS; |
137 |
return Status.OK_STATUS; |
| 117 |
|
138 |
|
| 118 |
MultiStatus result = new MultiStatus(DownloadActivator.ID, IStatus.OK, null, null); |
139 |
MultiStatus result = new MultiStatus(); |
| 119 |
for (Iterator iterator = requestsToProcess.iterator(); iterator.hasNext();) { |
140 |
for (Iterator iterator = requestsToProcess.iterator(); iterator.hasNext();) { |
| 120 |
IStatus failed = ((IArtifactRequest) iterator.next()).getResult(); |
141 |
IArtifactRequest failed = (IArtifactRequest) iterator.next(); |
| 121 |
if (failed != null && !failed.isOK()) |
142 |
result.add(failed.getResult()); |
| 122 |
result.add(failed); |
|
|
| 123 |
} |
143 |
} |
| 124 |
return result; |
144 |
return result; |
| 125 |
} |
145 |
} |
| 126 |
} |
146 |
|
|
|
147 |
private static final class StrategyRequests { |
| 148 |
|
| 149 |
private final IDownloadStrategy strategy; |
| 150 |
private final List strategyRequests = new ArrayList(); |
| 151 |
private final Set totalRequests = new HashSet(); |
| 152 |
|
| 153 |
StrategyRequests(IDownloadStrategy strategy) { |
| 154 |
this.strategy = strategy; |
| 155 |
} |
| 156 |
|
| 157 |
void queueWork(IArtifactRepository artifactRepository, IArtifactRequest[] requests) { |
| 158 |
totalRequests.addAll(Arrays.asList(requests)); |
| 159 |
strategyRequests.add(new DownloadStrategyRequests(artifactRepository, requests)); |
| 160 |
} |
| 161 |
|
| 162 |
IStatus process(SubMonitor subMonitor) { |
| 163 |
DownloadStrategyRequests requests[] = (DownloadStrategyRequests[]) strategyRequests.toArray(new DownloadStrategyRequests[strategyRequests.size()]); |
| 164 |
return strategy.getArtifacts(requests, subMonitor.newChild(totalRequests.size())); |
| 165 |
} |
| 166 |
} |
| 167 |
} |