forked from eclipse-tycho/tycho
/
AbstractSlicerResolutionStrategy.java
165 lines (141 loc) · 7.87 KB
/
AbstractSlicerResolutionStrategy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*******************************************************************************
* Copyright (c) 2008, 2022 Sonatype Inc. and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Sonatype Inc. - initial API and implementation
* Christoph Läubrich - #462 - Delay Pom considered items to the final Target Platform calculation
*******************************************************************************/
package org.eclipse.tycho.p2.util.resolution;
import static org.eclipse.tycho.p2.util.resolution.ResolverDebugUtils.toDebugString;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.equinox.internal.p2.director.Slicer;
import org.eclipse.equinox.internal.p2.metadata.IRequiredCapability;
import org.eclipse.equinox.internal.p2.metadata.RequiredCapability;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.IProvidedCapability;
import org.eclipse.equinox.p2.metadata.IRequirement;
import org.eclipse.equinox.p2.metadata.MetadataFactory;
import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitDescription;
import org.eclipse.equinox.p2.metadata.Version;
import org.eclipse.equinox.p2.metadata.VersionRange;
import org.eclipse.equinox.p2.metadata.expression.IMatchExpression;
import org.eclipse.equinox.p2.query.IQueryable;
import org.eclipse.tycho.core.shared.MavenLogger;
import org.eclipse.tycho.repository.p2base.metadata.QueryableCollection;
import org.eclipse.tycho.repository.util.StatusTool;
@SuppressWarnings("restriction")
abstract class AbstractSlicerResolutionStrategy extends AbstractResolutionStrategy {
protected AbstractSlicerResolutionStrategy(MavenLogger logger) {
super(logger);
}
protected final IQueryable<IInstallableUnit> slice(Map<String, String> properties, IProgressMonitor monitor)
throws ResolverException {
return slice(properties, Collections.emptyList(), monitor);
}
protected final IQueryable<IInstallableUnit> slice(Map<String, String> properties,
List<IInstallableUnit> additionalUnits, IProgressMonitor monitor) throws ResolverException {
if (logger.isExtendedDebugEnabled()) {
logger.debug("Properties: " + properties.toString());
logger.debug("Available IUs:\n" + toDebugString(data.getAvailableIUs(), false));
logger.debug("JRE IUs:\n" + toDebugString(data.getEEResolutionHints().getMandatoryUnits(), false));
logger.debug("Root IUs:\n" + toDebugString(data.getRootIUs(), true));
if (data.getAdditionalRequirements() != null && !data.getAdditionalRequirements().isEmpty()) {
StringBuilder sb = new StringBuilder();
for (IRequirement req : data.getAdditionalRequirements()) {
sb.append(" ").append(req.toString()).append("\n");
}
logger.debug("Extra Requirements:\n" + sb.toString());
}
}
Set<IInstallableUnit> availableIUs = new LinkedHashSet<>(data.getAvailableIUs());
availableIUs.addAll(data.getEEResolutionHints().getTemporaryAdditions());
availableIUs.addAll(data.getEEResolutionHints().getMandatoryUnits());
availableIUs.addAll(additionalUnits);
Set<IInstallableUnit> seedIUs = new LinkedHashSet<>(data.getRootIUs());
if (data.getAdditionalRequirements() != null && !data.getAdditionalRequirements().isEmpty()) {
seedIUs.add(createUnitRequiring("tycho-extra", null, data.getAdditionalRequirements()));
}
// make sure profile UIs are part of the slice
seedIUs.addAll(data.getEEResolutionHints().getMandatoryUnits());
if (!data.getEEResolutionHints().getMandatoryRequires().isEmpty()) {
seedIUs.add(createUnitRequiring("tycho-ee", null, data.getEEResolutionHints().getMandatoryRequires()));
}
Slicer slicer = newSlicer(new QueryableCollection(availableIUs), properties);
IQueryable<IInstallableUnit> slice = slicer.slice(seedIUs.toArray(EMPTY_IU_ARRAY), monitor);
MultiStatus slicerStatus = slicer.getStatus();
if (slice == null || isSlicerError(slicerStatus)) {
throw new ResolverException(StatusTool.toLogMessage(slicerStatus), properties.toString(),
StatusTool.findException(slicerStatus));
}
if (logger.isExtendedDebugEnabled()) {
logger.debug("Slice:\n" + ResolverDebugUtils.toDebugString(slice, false, monitor));
}
return slice;
}
protected abstract boolean isSlicerError(MultiStatus slicerStatus);
protected abstract Slicer newSlicer(IQueryable<IInstallableUnit> availableIUs, Map<String, String> properties);
protected static IInstallableUnit createUnitRequiring(String name, Collection<IInstallableUnit> units,
Collection<IRequirement> additionalRequirements) {
InstallableUnitDescription result = new MetadataFactory.InstallableUnitDescription();
String time = Long.toString(System.currentTimeMillis());
result.setId(name + "-" + time);
result.setVersion(Version.createOSGi(0, 0, 0, time));
ArrayList<IRequirement> requirements = new ArrayList<>();
if (units != null) {
for (IInstallableUnit unit : units) {
requirements.add(createStrictRequirementTo(unit));
}
}
if (additionalRequirements != null) {
requirements.addAll(additionalRequirements);
}
result.addRequirements(requirements);
return MetadataFactory.createInstallableUnit(result);
}
protected IInstallableUnit createUnitProviding(String name, Collection<IRequirement> requirements) {
InstallableUnitDescription result = new MetadataFactory.InstallableUnitDescription();
String time = Long.toString(System.currentTimeMillis());
result.setId(name + "-" + time);
result.setVersion(Version.createOSGi(0, 0, 0, time));
for (IRequirement requirement : requirements) {
if (requirement instanceof IRequiredCapability) {
try {
IRequiredCapability capability = (IRequiredCapability) requirement;
String namespace = capability.getNamespace();
IMatchExpression<IInstallableUnit> matches = capability.getMatches();
String extractName = RequiredCapability.extractName(matches);
Version version = RequiredCapability.extractRange(matches).getMinimum();
IProvidedCapability providedCapability = MetadataFactory.createProvidedCapability(namespace,
extractName, version);
result.addProvidedCapabilities(Collections.singleton(providedCapability));
} catch (RuntimeException e) {
logger.debug("can't convert requirement " + requirement + " to capability: " + e.toString(), e);
}
}
}
return MetadataFactory.createInstallableUnit(result);
}
private static IRequirement createStrictRequirementTo(IInstallableUnit unit) {
VersionRange strictRange = new VersionRange(unit.getVersion(), true, unit.getVersion(), true);
int min = 1;
int max = Integer.MAX_VALUE;
boolean greedy = true;
IRequirement requirement = MetadataFactory.createRequirement(IInstallableUnit.NAMESPACE_IU_ID, unit.getId(),
strictRange, unit.getFilter(), min, max, greedy);
return requirement;
}
}