Debian Codegraph @

bundles/org.eclipse.equinox.p2.engine/src/org/eclipse/equinox/internal/p2/engine/DownloadManager.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2007, 2017 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 * WindRiver - https://bugs.eclipse.org/bugs/show_bug.cgi?id=227372
14 *******************************************************************************/
15 package org.eclipse.equinox.internal.p2.engine;
16
17 import java.util.*;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.equinox.internal.p2.engine.phases.Collect;
20 import org.eclipse.equinox.internal.provisional.p2.core.eventbus.IProvisioningEventBus;
21 import org.eclipse.equinox.p2.core.IProvisioningAgent;
22 import org.eclipse.equinox.p2.engine.ProvisioningContext;
23 import org.eclipse.equinox.p2.metadata.expression.ExpressionUtil;
24 import org.eclipse.equinox.p2.query.*;
25 import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository;
26 import org.eclipse.equinox.p2.repository.artifact.IArtifactRequest;
27
28 public class DownloadManager {
29 private ProvisioningContext provContext = null;
30 ArrayList<IArtifactRequest> requestsToProcess = new ArrayList<>();
31 private IProvisioningAgent agent = null;
32
33 /**
34 * This Comparator sorts the repositories such that local repositories are first.
35 * TODO: This is copied from the ProvisioningContext class. Can we combine them?
36 * See https://bugs.eclipse.org/335153.
37 */
38 private static final Comparator<IArtifactRepository> LOCAL_FIRST_COMPARATOR = new Comparator<>() {
39 private static final String FILE_PROTOCOL = "file"; //$NON-NLS-1$
40
41 @Override
42 public int compare(IArtifactRepository arg0, IArtifactRepository arg1) {
43 String protocol0 = arg0.getLocation().getScheme();
44 String protocol1 = arg1.getLocation().getScheme();
45 if (FILE_PROTOCOL.equals(protocol0) && !FILE_PROTOCOL.equals(protocol1))
46 return -1;
47 if (!FILE_PROTOCOL.equals(protocol0) && FILE_PROTOCOL.equals(protocol1))
48 return 1;
49 return 0;
50 }
51 };
52
53 public DownloadManager(ProvisioningContext context, IProvisioningAgent agent) {
54 provContext = context;
55 this.agent = agent;
56 }
57
58 /*
59 * Add the given artifact to the download queue. When it
60 * is downloaded, put it in the specified location.
61 */
62 public void add(IArtifactRequest toAdd) {
63 Assert.isNotNull(toAdd);
64 requestsToProcess.add(toAdd);
65 }
66
67 public void add(IArtifactRequest[] toAdd) {
68 Assert.isNotNull(toAdd);
69 for (IArtifactRequest element : toAdd) {
70 add(element);
71 }
72 }
73
74 private void filterUnfetched() {
75 for (Iterator<IArtifactRequest> iterator = requestsToProcess.iterator(); iterator.hasNext();) {
76 IArtifactRequest request = iterator.next();
77 if (request.getResult() != null && request.getResult().isOK()) {
78 iterator.remove();
79 }
80 }
81 }
82
83 /*
84 * Start the downloads. Return a status message indicating success or failure of the overall operation
85 */
86 public IStatus start(IProgressMonitor monitor) {
87 SubMonitor subMonitor = SubMonitor.convert(monitor, Messages.download_artifact, 1000);
88 try {
89 if (requestsToProcess.isEmpty())
90 return Status.OK_STATUS;
91
92 if (provContext == null)
93 provContext = new ProvisioningContext(agent);
94
95 IArtifactRepository[] repositories = getArtifactRepositories(subMonitor);
96 if (repositories.length == 0)
97 return new Status(IStatus.ERROR, EngineActivator.ID, Messages.download_no_repository, new Exception(Collect.NO_ARTIFACT_REPOSITORIES_AVAILABLE));
98 fetch(repositories, subMonitor.newChild(500));
99 return overallStatus(monitor);
100 } finally {
101 subMonitor.done();
102 }
103 }
104
105 /**
106 * @return artifact repositories sorted according to LOCAL_FIRST_COMPARATOR
107 */
108 private IArtifactRepository[] getArtifactRepositories(SubMonitor subMonitor) {
109 IQuery<IArtifactRepository> queryArtifactRepositories = new ExpressionMatchQuery<>(IArtifactRepository.class, ExpressionUtil.TRUE_EXPRESSION);
110 IQueryable<IArtifactRepository> artifactRepositories = provContext.getArtifactRepositories(subMonitor.newChild(250));
111 IQueryResult<IArtifactRepository> queryResult = artifactRepositories.query(queryArtifactRepositories, subMonitor.newChild(250));
112 IArtifactRepository[] repositories = queryResult.toArray(IArtifactRepository.class);
113
114 // Although we get a sorted list back from the ProvisioningContext above, it
115 // gets unsorted when we convert the queryable into an array so we must re-sort it.
116 // See https://bugs.eclipse.org/335153.
117 Arrays.sort(repositories, LOCAL_FIRST_COMPARATOR);
118
119 return repositories;
120 }
121
122 private void fetch(IArtifactRepository[] repositories, IProgressMonitor mon) {
123 SubMonitor monitor = SubMonitor.convert(mon, requestsToProcess.size());
124 for (int i = 0; i < repositories.length && !requestsToProcess.isEmpty() && !monitor.isCanceled(); i++) {
125 IArtifactRequest[] requests = getRequestsForRepository(repositories[i]);
126 publishDownloadEvent(new CollectEvent(CollectEvent.TYPE_REPOSITORY_START, repositories[i], provContext, requests));
127 IStatus dlStatus = repositories[i].getArtifacts(requests, monitor.newChild(requests.length));
128 publishDownloadEvent(new CollectEvent(CollectEvent.TYPE_REPOSITORY_END, repositories[i], provContext, requests));
129 if (dlStatus.getSeverity() == IStatus.CANCEL)
130 return;
131 filterUnfetched();
132 monitor.setWorkRemaining(requestsToProcess.size());
133 }
134 }
135
136 private void publishDownloadEvent(CollectEvent event) {
137 IProvisioningEventBus bus = agent.getService(IProvisioningEventBus.class);
138 if (bus != null)
139 bus.publishEvent(event);
140 }
141
142 private IArtifactRequest[] getRequestsForRepository(IArtifactRepository repository) {
143 ArrayList<IArtifactRequest> applicable = new ArrayList<>();
144 for (IArtifactRequest request : requestsToProcess) {
145 if (repository.contains(request.getArtifactKey()))
146 applicable.add(request);
147 }
148 return applicable.toArray(new IArtifactRequest[applicable.size()]);
149 }
150
151 // private void notifyFetched() {
152 // ProvisioningEventBus bus = (ProvisioningEventBus) ServiceHelper.getService(DownloadActivator.context, ProvisioningEventBus.class);
153 // bus.publishEvent();
154 // }
155
156 private IStatus overallStatus(IProgressMonitor monitor) {
157 if (monitor != null && monitor.isCanceled())
158 return Status.CANCEL_STATUS;
159
160 if (requestsToProcess.size() == 0)
161 return Status.OK_STATUS;
162
163 MultiStatus result = new MultiStatus(EngineActivator.ID, IStatus.OK, null, null);
164 for (IArtifactRequest request : requestsToProcess) {
165 IStatus failed = request.getResult();
166 if (failed != null && !failed.isOK())
167 result.add(failed);
168 }
169 return result;
170 }
171 }