Debian Codegraph @

bundles/org.eclipse.equinox.p2.engine/src/org/eclipse/equinox/p2/engine/ProvisioningContext.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2008, 2023 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 * Sonatype, Inc. - ongoing development
15 * Karsten Thoms - Bug#527874
16 *******************************************************************************/
17 package org.eclipse.equinox.p2.engine;
18
19 import java.net.URI;
20 import java.util.*;
21 import org.eclipse.core.runtime.*;
22 import org.eclipse.equinox.internal.p2.engine.DebugHelper;
23 import org.eclipse.equinox.internal.p2.repository.Transport;
24 import org.eclipse.equinox.p2.core.IProvisioningAgent;
25 import org.eclipse.equinox.p2.core.ProvisionException;
26 import org.eclipse.equinox.p2.metadata.IArtifactKey;
27 import org.eclipse.equinox.p2.metadata.IInstallableUnit;
28 import org.eclipse.equinox.p2.query.*;
29 import org.eclipse.equinox.p2.repository.*;
30 import org.eclipse.equinox.p2.repository.artifact.*;
31 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository;
32 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
33
34 /**
35 * A provisioning context defines the scope in which a provisioning operation
36 * occurs. A context can be used to specify the set of repositories available
37 * to the planner and engine as they perform provisioning work.
38 * @since 2.0
39 */
40 public class ProvisioningContext {
41 private IProvisioningAgent agent;
42 private URI[] artifactRepositories; //artifact repositories to consult
43 private final List<IInstallableUnit> extraIUs = Collections.synchronizedList(new ArrayList<>());
44 private URI[] metadataRepositories; //metadata repositories to consult
45 private final Map<String, String> properties = new HashMap<>();
46 private Map<String, URI> referencedArtifactRepositories = null;
47 private Map<URI, IArtifactRepository> loadedArtifactRepositories = new HashMap<>();
48 private Map<URI, IMetadataRepository> loadedMetadataRepositories = new HashMap<>();
49 private Map<URI, IMetadataRepository> allLoadedMetadataRepositories;
50 private Map<URI, IArtifactRepository> allLoadedArtifactRepositories;
51 private Set<URI> failedArtifactRepositories = new HashSet<>();
52 private Set<URI> failedMetadataRepositories = new HashSet<>();
53
54 private static final String FILE_PROTOCOL = "file"; //$NON-NLS-1$
55
56 class ArtifactRepositoryQueryable implements IQueryable<IArtifactRepository> {
57 List<IArtifactRepository> repositories;
58
59 ArtifactRepositoryQueryable(List<IArtifactRepository> repositories) {
60 this.repositories = repositories;
61 }
62
63 @Override
64 public IQueryResult<IArtifactRepository> query(IQuery<IArtifactRepository> query, IProgressMonitor mon) {
65 return query.perform(repositories.listIterator());
66 }
67
68 @Override
69 public boolean contains(IArtifactRepository element) {
70 return repositories.contains(element);
71 }
72 }
73
74 /**
75 * This Comparator sorts the repositories such that local repositories are first
76 */
77 private static final Comparator<URI> LOCAL_FIRST_COMPARATOR = (arg0, arg1) -> {
78 String protocol0 = arg0.getScheme();
79 String protocol1 = arg1.getScheme();
80
81 if (FILE_PROTOCOL.equals(protocol0) && !FILE_PROTOCOL.equals(protocol1))
82 return -1;
83 if (!FILE_PROTOCOL.equals(protocol0) && FILE_PROTOCOL.equals(protocol1))
84 return 1;
85 return 0;
86 };
87
88 /**
89 * Instructs the provisioning context to follow metadata repository references when
90 * providing queryables for obtaining metadata and artifacts. When this property is set to
91 * "true", then metadata repository references that are encountered while loading the
92 * specified metadata repositories will be included in the provisioning
93 * context.
94 *
95 * @see #getMetadata(IProgressMonitor)
96 * @see #setMetadataRepositories(URI[])
97 */
98 public static final String FOLLOW_REPOSITORY_REFERENCES = "org.eclipse.equinox.p2.director.followRepositoryReferences"; //$NON-NLS-1$
99
100 /**
101 * Instructs the {@link PhaseSetFactory#PHASE_COLLECT collect phase} to check
102 * the originating sources of all installable units and artifacts.
103 *
104 * @see #getInstallableUnitSources(Collection, IProgressMonitor)
105 * @see #getArtifactSources(Collection, IProgressMonitor)
106 * @since 2.8
107 */
108 public static final String CHECK_AUTHORITIES = "org.eclipse.equinox.p2.director.checkAuthorities"; //$NON-NLS-1$
109
110 private static final String FOLLOW_ARTIFACT_REPOSITORY_REFERENCES = "org.eclipse.equinox.p2.director.followArtifactRepositoryReferences"; //$NON-NLS-1$
111
112 /**
113 * Creates a new provisioning context that includes all available metadata and
114 * artifact repositories available to the specified provisioning agent.
115 *
116 * @param agent the provisioning agent from which to obtain any necessary services.
117 */
118 public ProvisioningContext(IProvisioningAgent agent) {
119 this.agent = agent;
120 // null repos means look at them all
121 metadataRepositories = null;
122 artifactRepositories = null;
123 setProperty(FOLLOW_ARTIFACT_REPOSITORY_REFERENCES, Boolean.TRUE.toString());
124 }
125
126 /**
127 * Returns a queryable that can be used to obtain any artifact keys that
128 * are needed for the provisioning operation.
129 *
130 * @param monitor a progress monitor to be used when creating the queryable
131 * @return a queryable that can be used to query available artifact keys.
132 *
133 * @see #setArtifactRepositories(URI[])
134 */
135 public IQueryable<IArtifactKey> getArtifactKeys(IProgressMonitor monitor) {
136 return QueryUtil.compoundQueryable(getLoadedArtifactRepositories(monitor));
137 }
138
139 /**
140 * Returns a queryable that can be used to obtain any artifact descriptors that
141 * are needed for the provisioning operation.
142 *
143 * @param monitor a progress monitor to be used when creating the queryable
144 * @return a queryable that can be used to query available artifact descriptors.
145 *
146 * @see #setArtifactRepositories(URI[])
147 */
148 public IQueryable<IArtifactDescriptor> getArtifactDescriptors(IProgressMonitor monitor) {
149 List<IArtifactRepository> repos = getLoadedArtifactRepositories(monitor);
150 List<IQueryable<IArtifactDescriptor>> descriptorQueryables = new ArrayList<>();
151 for (IArtifactRepository repo : repos) {
152 descriptorQueryables.add(repo.descriptorQueryable());
153 }
154 return QueryUtil.compoundQueryable(descriptorQueryables);
155 }
156
157 /**
158 * Returns a queryable that can be used to obtain any artifact repositories that
159 * are needed for the provisioning operation.
160 *
161 * @param monitor a progress monitor to be used when creating the queryable
162 * @return a queryable that can be used to query available artifact repositories.
163 *
164 * @see #setArtifactRepositories(URI[])
165 */
166 public IQueryable<IArtifactRepository> getArtifactRepositories(IProgressMonitor monitor) {
167 return new ArtifactRepositoryQueryable(getLoadedArtifactRepositories(monitor));
168 }
169
170 /**
171 * Return an array of loaded artifact repositories.
172 */
173 private List<IArtifactRepository> getLoadedArtifactRepositories(IProgressMonitor monitor) {
174 IArtifactRepositoryManager repoManager = agent.getService(IArtifactRepositoryManager.class);
175 URI[] repositories = artifactRepositories == null ? repoManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL) : artifactRepositories;
176 Arrays.sort(repositories, LOCAL_FIRST_COMPARATOR);
177
178 List<IArtifactRepository> repos = new ArrayList<>();
179 SubMonitor sub = SubMonitor.convert(monitor, repositories.length + 1);
180 for (URI location : repositories) {
181 getLoadedRepository(location, repoManager, repos, sub.split(1));
182 // Remove this URI from the list of extra references if it is there.
183 if (referencedArtifactRepositories != null && location != null) {
184 referencedArtifactRepositories.remove(location.toString());
185 }
186 }
187 // Are there any extra artifact repository references to consider?
188 if (referencedArtifactRepositories != null && referencedArtifactRepositories.size() > 0 && shouldFollowArtifactReferences()) {
189 SubMonitor innerSub = SubMonitor.convert(sub.split(1), referencedArtifactRepositories.size());
190 for (URI referencedURI : referencedArtifactRepositories.values()) {
191 getLoadedRepository(referencedURI, repoManager, repos, innerSub.split(1));
192 }
193 }
194 return repos;
195 }
196
197 private void getLoadedRepository(URI location, IArtifactRepositoryManager repoManager,
198 List<IArtifactRepository> repos, IProgressMonitor monitor) {
199 if (failedArtifactRepositories.contains(location)) {
200 return;
201 }
202 try {
203 IArtifactRepository repository = loadedArtifactRepositories.get(location);
204 if (repository == null) {
205 repository = repoManager.loadRepository(location, monitor);
206 loadedArtifactRepositories.put(location, repository);
207 }
208 repos.add(repository);
209 } catch (ProvisionException e) {
210 //skip and remember unreadable repositories
211 failedArtifactRepositories.add(location);
212 }
213 }
214
215 private Set<IMetadataRepository> getLoadedMetadataRepositories(IProgressMonitor monitor) {
216 IMetadataRepositoryManager repoManager = agent.getService(IMetadataRepositoryManager.class);
217 URI[] repositories = metadataRepositories == null ? repoManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL) : metadataRepositories;
218
219 Map<String, IMetadataRepository> repos = new HashMap<>();
220 SubMonitor sub = SubMonitor.convert(monitor, repositories.length);
221
222 // Clear out the list of remembered artifact repositories
223 referencedArtifactRepositories = new HashMap<>();
224 for (URI repositorie : repositories) {
225 loadMetadataRepository(repoManager, repositorie, repos, shouldFollowReferences(), sub.split(1));
226 }
227 return new HashSet<>(repos.values());
228 }
229
230 private void loadMetadataRepository(IMetadataRepositoryManager manager, URI location,
231 Map<String, IMetadataRepository> repos, boolean followMetadataRepoReferences, IProgressMonitor monitor) {
232 // if we've already processed this repo, don't do it again. This keeps us from getting
233 // caught up in circular references.
234 if (repos.containsKey(location.toString()) || failedMetadataRepositories.contains(location)) {
235 return;
236 }
237
238 SubMonitor subMonitor = SubMonitor.convert(monitor, 2);
239 // First load the repository itself.
240 IMetadataRepository repository = loadedMetadataRepositories.get(location);
241 if (repository == null) {
242 try {
243 repository = manager.loadRepository(location, subMonitor.split(1));
244 loadedMetadataRepositories.put(location, repository);
245 } catch (ProvisionException e) {
246 failedMetadataRepositories.add(location);
247 return;
248 }
249 }
250 repos.put(location.toString(), repository);
251 Collection<IRepositoryReference> references = repository.getReferences();
252 // We always load artifact repositories referenced by this repository. We might load
253 // metadata repositories
254 if (!references.isEmpty()) {
255 IArtifactRepositoryManager artifactManager = agent.getService(IArtifactRepositoryManager.class);
256 SubMonitor repoMon = SubMonitor.convert(subMonitor.split(1), references.size());
257 for (IRepositoryReference ref : references) {
258 try {
259 if (ref.getType() == IRepository.TYPE_METADATA && followMetadataRepoReferences && isEnabled(manager, ref)) {
260 loadMetadataRepository(manager, ref.getLocation(), repos, followMetadataRepoReferences, repoMon.split(1));
261 } else if (ref.getType() == IRepository.TYPE_ARTIFACT && isEnabled(artifactManager, ref)) {
262 // We want to remember all enabled artifact repository locations.
263 referencedArtifactRepositories.put(ref.getLocation().toString(), ref.getLocation());
264 }
265 } catch (IllegalArgumentException e) {
266 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=311338
267 // ignore invalid location and keep going
268 }
269 }
270 }
271
272 }
273
274 // If the manager knows about the repo, consider its enablement state in the manager.
275 // If the manager does not know about the repo, consider the reference enablement state
276 @SuppressWarnings("rawtypes")
277 private boolean isEnabled(IRepositoryManager manager, IRepositoryReference reference) {
278 return (manager.contains(reference.getLocation()) && manager.isEnabled(reference.getLocation())) || ((!manager.contains(reference.getLocation())) && ((reference.getOptions() & IRepository.ENABLED) == IRepository.ENABLED));
279 }
280
281 private boolean shouldFollowReferences() {
282 return Boolean.parseBoolean(getProperty(FOLLOW_REPOSITORY_REFERENCES));
283 }
284
285 private boolean shouldFollowArtifactReferences() {
286 return Boolean.parseBoolean(getProperty(FOLLOW_ARTIFACT_REPOSITORY_REFERENCES));
287 }
288
289 /**
290 * Returns a queryable that can be used to obtain any metadata (installable units)
291 * that are needed for the provisioning operation.
292 *
293 * The provisioning context has a distinct lifecycle, whereby the metadata
294 * and artifact repositories to be used are determined when the client retrieves
295 * retrieves the metadata queryable. Clients should not reset the list of
296 * metadata repository locations or artifact repository locations once the
297 * metadata queryable has been retrieved.
298 *
299 * @param monitor a progress monitor to be used when creating the queryable
300 * @return a queryable that can be used to query available metadata.
301 *
302 * @see #setMetadataRepositories(URI[])
303 * @see #FOLLOW_REPOSITORY_REFERENCES
304 */
305 public IQueryable<IInstallableUnit> getMetadata(IProgressMonitor monitor) {
306 return QueryUtil.compoundQueryable(getLoadedMetadataRepositories(monitor));
307 }
308
309 /**
310 * Returns a map from simple metadata repository location to a subset of the
311 * given installable units available in that repository. All available
312 * {@link #setMetadataRepositories(URI...) metadata repositories} are
313 * considered, including all transitive
314 * {@link ICompositeRepository#getChildren() composite children} and all
315 * {@link IMetadataRepository#getReferences()} where applicable.
316 *
317 * @param ius the installable units to consider.
318 * @param monitor a progress monitor to be used when querying the repositories.
319 * @return a map from simple metadata repository location to a subset of the
320 * given installable units available in that repository.
321 * @since 2.8
322 */
323 public Map<URI, Set<IInstallableUnit>> getInstallableUnitSources(Collection<? extends IInstallableUnit> ius,
324 IProgressMonitor monitor) {
325 var result = new TreeMap<URI, Set<IInstallableUnit>>();
326 var transport = agent.getService(Transport.class);
327 for (var repository : getAllLoadedMetadataRepositories(monitor)) {
328 if (repository instanceof ICompositeRepository<?>) {
329 continue;
330 }
331 var location = getSecureLocation(repository.getLocation(), transport);
332 var repositoryIUs = new TreeSet<>(repository.query(QueryUtil.ALL_UNITS, monitor).toUnmodifiableSet());
333 repositoryIUs.retainAll(ius);
334 result.put(location, repositoryIUs);
335 }
336 return result;
337 }
338
339 private URI getSecureLocation(URI uri, Transport transport) {
340 try {
341 return transport.getSecureLocation(uri);
342 } catch (CoreException e) {
343 return uri;
344 }
345 }
346
347 private Collection<IMetadataRepository> getAllLoadedMetadataRepositories(IProgressMonitor monitor) {
348 if (allLoadedMetadataRepositories == null) {
349 SubMonitor subMonitor = SubMonitor.convert(monitor, 2);
350 var repoManager = agent.getService(IMetadataRepositoryManager.class);
351 getLoadedMetadataRepositories(subMonitor.split(1));
352 allLoadedMetadataRepositories = getAllLoadedRepositories(repoManager,
353 loadedMetadataRepositories, failedMetadataRepositories, subMonitor.split(1));
354 }
355 return allLoadedMetadataRepositories.values();
356 }
357
358 private Collection<IArtifactRepository> getAllLoadedArtifactRepositories(IProgressMonitor monitor) {
359 if (allLoadedArtifactRepositories == null) {
360 SubMonitor subMonitor = SubMonitor.convert(monitor, 2);
361 var repoManager = agent.getService(IArtifactRepositoryManager.class);
362 getLoadedArtifactRepositories(subMonitor.split(1));
363 allLoadedArtifactRepositories = getAllLoadedRepositories(repoManager,
364 loadedArtifactRepositories, failedArtifactRepositories, subMonitor.split(1));
365 }
366 return allLoadedArtifactRepositories.values();
367 }
368
369 private <T, R extends IRepository<T>> Map<URI, R> getAllLoadedRepositories(IRepositoryManager<T> manager,
370 Map<URI, R> loadedRepositories, Set<URI> failedRepositories, IProgressMonitor monitor) {
371 SubMonitor subMonitor = SubMonitor.convert(monitor, loadedRepositories.size());
372 var allLoadedRepositories = new HashMap<>(loadedRepositories);
373 for (var repository : loadedRepositories.values()) {
374 loadComposites(manager, repository, allLoadedRepositories, failedRepositories, subMonitor.split(1));
375 }
376 return allLoadedRepositories;
377 }
378
379 private <T, R extends IRepository<T>> void loadComposites(IRepositoryManager<T> manager, R repository,
380 Map<URI, R> repos, Set<URI> failedRepositories, IProgressMonitor monitor) {
381 if (repository instanceof ICompositeRepository<?> composite) {
382 List<URI> children = composite.getChildren();
383 SubMonitor subMonitor = SubMonitor.convert(monitor, children.size());
384 for (var location : children) {
385 loadRepository(manager, location, repos, failedRepositories, subMonitor.split(1));
386 }
387 }
388 }
389
390 private <T, R extends IRepository<T>> void loadRepository(IRepositoryManager<T> manager, URI location,
391 Map<URI, R> repos, Set<URI> failedRepositories,
392 IProgressMonitor monitor) {
393 SubMonitor subMonitor = SubMonitor.convert(monitor, 2);
394
395 if (failedMetadataRepositories.contains(location)) {
396 return;
397 }
398 if (!repos.containsKey(location)) { // A previous attempt may have failed
399 try {
400 @SuppressWarnings("unchecked")
401 R repository = (R) manager.loadRepository(location, subMonitor.split(1));
402 repos.put(location, repository);
403 loadComposites(manager, repository, repos, failedRepositories, subMonitor.split(1));
404 } catch (ProvisionException e) {
405 failedMetadataRepositories.add(location);
406 }
407 }
408 }
409
410 private static final Comparator<IArtifactKey> ARTIFACT_KEY_COMPARATOR = Comparator //
411 .comparing(IArtifactKey::getId) //
412 .thenComparing(IArtifactKey::getVersion);
413
414 /**
415 * Returns a map from simple artifact repository location to a subset of the
416 * given artifact keys available in that repository. All available
417 * {@link #setArtifactRepositories(URI...) artifacts repositories} are
418 * considered, including all transitive
419 * {@link ICompositeRepository#getChildren() composite children} and all
420 * {@link IMetadataRepository#getReferences()} where applicable.
421 *
422 * @param keys the artifact keys to consider.
423 * @param monitor a progress monitor to be used when querying the repositories.
424 *
425 * @return a map from simple artifact repository location to a subset of the
426 * given artifact keys available in that repository.
427 * @since 2.8
428 */
429 public Map<URI, Set<IArtifactKey>> getArtifactSources(Collection<? extends IArtifactKey> keys,
430 IProgressMonitor monitor) {
431 var transport = agent.getService(Transport.class);
432 var result = new TreeMap<URI, Set<IArtifactKey>>();
433 for (var repository : getAllLoadedArtifactRepositories(monitor)) {
434 if (repository instanceof ICompositeRepository<?>) {
435 continue;
436 }
437 var location = getSecureLocation(repository.getLocation(), transport);
438 for (var key : keys) {
439 if (repository.contains(key)) {
440 result.computeIfAbsent(location, it -> new TreeSet<>(ARTIFACT_KEY_COMPARATOR)).add(key);
441 }
442 }
443 }
444 return result;
445 }
446
447 /**
448 * Returns the list of additional installable units that should be considered as
449 * available for installation by the planner. Returns an empty list if
450 * there are no extra installable units to consider. This method has no effect on the
451 * execution of the engine.
452 *
453 * @return The extra installable units that are available
454 */
455 public List<IInstallableUnit> getExtraInstallableUnits() {
456 return extraIUs;
457 }
458
459 /**
460 * Returns the properties that are defined in this context. Context properties can
461 * be used to influence the behavior of either the planner or engine.
462 *
463 * @return the defined provisioning context properties
464 */
465 public Map<String, String> getProperties() {
466 return properties;
467 }
468
469 /**
470 * Returns the value of the property with the given key, or <code>null</code>
471 * if no such property is defined
472 * @param key the property key
473 * @return the property value, or <code>null</code>
474 */
475 public String getProperty(String key) {
476 return properties.get(key);
477 }
478
479 /**
480 * Sets the artifact repositories to consult when performing an operation.
481 * <p>
482 * The provisioning context has a distinct lifecycle, whereby the metadata
483 * and artifact repositories to be used are determined when the client
484 * retrieves the metadata queryable. Clients should not reset the list of
485 * artifact repository locations once the metadata queryable has been retrieved.
486 *
487 * @param artifactRepositories the artifact repository locations
488 */
489 public void setArtifactRepositories(URI... artifactRepositories) {
490 this.artifactRepositories = artifactRepositories;
491 }
492
493 /**
494 * Sets the metadata repositories to consult when performing an operation.
495 * <p>
496 * The provisioning context has a distinct lifecycle, whereby the metadata
497 * and artifact repositories to be used are determined when the client
498 * retrieves the metadata queryable. Clients should not reset the list of
499 * metadata repository locations once the metadata queryable has been retrieved.
500 * @param metadataRepositories the metadata repository locations
501 */
502 public void setMetadataRepositories(URI... metadataRepositories) {
503 this.metadataRepositories = metadataRepositories;
504 }
505
506 /**
507 * Sets the list of additional installable units that should be considered as
508 * available for installation by the planner. This method has no effect on the
509 * execution of the engine.
510 * @param extraIUs the extra installable units
511 */
512 public void setExtraInstallableUnits(List<IInstallableUnit> extraIUs) {
513 this.extraIUs.clear();
514 //copy the list to prevent future client tampering
515 if (extraIUs != null)
516 this.extraIUs.addAll(extraIUs);
517 }
518
519 /**
520 * Sets a property on this provisioning context. Context properties can
521 * be used to influence the behavior of either the planner or engine.
522 * @param key the property key
523 * @param value the property value
524 */
525 public void setProperty(String key, String value) {
526 properties.put(key, value);
527 }
528
529 @Override
530 public String toString() {
531 StringBuilder buffer = new StringBuilder();
532 buffer.append("{artifactRepos=" + DebugHelper.formatArray(null != artifactRepositories ? Arrays.asList(artifactRepositories) : null, true, false)); //$NON-NLS-1$
533 buffer.append(", metadataRepos=" + DebugHelper.formatArray(null != metadataRepositories ? Arrays.asList(metadataRepositories) : null, true, false)); //$NON-NLS-1$
534 buffer.append(", properties=" + getProperties() + "}"); //$NON-NLS-1$ //$NON-NLS-2$
535 return buffer.toString();
536 }
537 }