-
Notifications
You must be signed in to change notification settings - Fork 38
/
ResolvedTargetPlatform.java
238 lines (206 loc) · 7.57 KB
/
ResolvedTargetPlatform.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
* Copyright (c) 2012-2014 Obeo 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:
* Mikael Barbero (Obeo) - initial API and implementation
* Hannes Niederhausen (itemis AG) - added maven related code
*/
package org.eclipse.cbi.targetplatform.resolved;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.EnumSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.eclipse.cbi.targetplatform.model.IU;
import org.eclipse.cbi.targetplatform.model.Location;
import org.eclipse.cbi.targetplatform.model.Option;
import org.eclipse.cbi.targetplatform.model.TargetPlatform;
import org.eclipse.cbi.targetplatform.util.LocationIndexBuilder;
import org.eclipse.cbi.targetplatform.util.MavenLocationIndexBuilder;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.EList;
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
import org.eclipse.jdt.launching.JavaRuntime;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
public class ResolvedTargetPlatform {
private final List<ResolvedLocation> locations;
private final List<ResolvedMavenLocation> mavenLocations;
private final String name;
private final EnumSet<Option> options;
private final Environment environment;
public ResolvedTargetPlatform(String name, List<ResolvedLocation> locations, List<ResolvedMavenLocation> mavenLocations, EnumSet<Option> options, Environment environment) {
this.name = name;
this.locations = locations;
this.options = options;
this.environment = environment;
this.mavenLocations=mavenLocations;
}
public List<ResolvedLocation> getLocations() {
if (locations==null) {
return Collections.emptyList();
}
return locations;
}
public String getName() {
return name;
}
public List<ResolvedMavenLocation> getMavenLocations() {
if (mavenLocations==null) {
return Collections.emptyList();
}
return mavenLocations;
}
public EnumSet<Option> getOptions() {
return options;
}
public Environment getEnvironment() {
return environment;
}
public Diagnostic resolve(IMetadataRepositoryManager metadataRepositoryManager, IProgressMonitor monitor) {
BasicDiagnostic ret = new BasicDiagnostic();
int numOfLocations = getLocations().size()+getMavenLocations().size();
SubMonitor subMonitor = SubMonitor.convert(monitor, numOfLocations);
for (ResolvedLocation location : getLocations()) {
if(subMonitor.isCanceled()) {
ret.merge(BasicDiagnostic.CANCEL_INSTANCE);
break;
}
Diagnostic diagnostic = location.resolve(metadataRepositoryManager, subMonitor);
if (diagnostic.getSeverity() >= Diagnostic.WARNING) {
ret.merge(diagnostic);
}
subMonitor.worked(1);
}
for (ResolvedMavenLocation location : getMavenLocations()) {
if(subMonitor.isCanceled()) {
ret.merge(BasicDiagnostic.CANCEL_INSTANCE);
break;
}
Diagnostic diagnostic = location.resolve(subMonitor);
if (diagnostic.getSeverity() >= Diagnostic.WARNING) {
ret.merge(diagnostic);
}
subMonitor.worked(1);
}
return ret;
}
public static ResolvedTargetPlatform create(TargetPlatform targetPlatform, LocationIndexBuilder indexBuilder, MavenLocationIndexBuilder mavenIndexBuilder) throws URISyntaxException {
Preconditions.checkArgument(targetPlatform != null);
LinkedList<ResolvedLocation> locations = Lists.newLinkedList();
ListMultimap<String, Location> locationIndex = indexBuilder.getLocationIndex(targetPlatform);
for (String locationUri : locationIndex.keySet()) {
ResolvedLocation resolvedLocation = resolveLocation(locationIndex, locationUri);
locations.addFirst(resolvedLocation);
}
List<ResolvedMavenLocation> mavenLocations = mavenIndexBuilder.getLocationIndex(targetPlatform).values().stream() //
.map(ResolvedMavenLocation::new)
.collect(Collectors.toList());
final EnumSet<Option> options = getOptionSet(targetPlatform.getOptions());
return new ResolvedTargetPlatform(targetPlatform.getName(), locations, mavenLocations, options, Environment.create(targetPlatform));
}
private static ResolvedLocation resolveLocation(ListMultimap<String, Location> locationIndex, String locationUri)
throws URISyntaxException {
List<UnresolvedIU> ius = Lists.newArrayList();
Set<String> ids = Sets.newHashSet();
List<Location> list = locationIndex.get(locationUri);
for (Location location : list) { // in reverse order of declaration: overrides seen first
EList<IU> iuList = location.getIus();
for (IU iu : iuList) { // in order of declaration
String idv = iu.getID() + ":" + Strings.nullToEmpty(iu.getVersion());
if (!ids.contains(idv)) { // neither duplicates (same location), nor redundant overrides (across locations)
if (iu.getVersion() != null || !ids.contains(iu.getID())) { // can override the unversioned only
ids.add(idv);
ids.add(iu.getID());
ius.add(new UnresolvedIU(iu.getID(), Strings.emptyToNull(iu.getVersion())));
}
}
}
}
Location firstLocation = locationIndex.get(locationUri).get(0);
ResolvedLocation resolvedLocation = new ResolvedLocation(firstLocation.getID(), firstLocation.getUri(), ius, getOptionSet(firstLocation.getOptions()));
return resolvedLocation;
}
public static EnumSet<Option> getOptionSet(EList<Option> optionsList) {
final EnumSet<Option> optionSet;
if (optionsList.isEmpty()) {
optionSet = EnumSet.noneOf(Option.class);
} else {
optionSet = EnumSet.copyOf(optionsList);
}
return optionSet;
}
public static class Environment {
private final String os;
private final String ws;
private final String arch;
private final String nl;
private final String targetJRE;
public Environment(String os, String ws, String arch, String nl, String targetJRE) {
this.os = os;
this.ws = ws;
this.arch = arch;
this.nl = nl;
this.targetJRE = targetJRE;
}
public static Environment create(TargetPlatform targetPlatform) {
final org.eclipse.cbi.targetplatform.model.Environment env = targetPlatform.getEnvironment();
if (env != null) {
final String os = Strings.emptyToNull(env.getOperatingSystem());
final String ws = Strings.emptyToNull(env.getWindowingSystem());
final String arch = Strings.emptyToNull(env.getArchitecture());
final String jrePath;
final String nl;
if (env.getLocalization() != null) {
nl = env.getLocalization().toString();
} else {
nl = null;
}
if (env.getExecutionEnvironment() != null) {
IPath path = JavaRuntime.newJREContainerPath(env.getExecutionEnvironment());
if (path != null) {
jrePath = path.toString();
} else {
jrePath = null;
}
} else {
jrePath = null;
}
return new Environment(os, ws, arch, nl, jrePath);
} else {
return null;
}
}
public String getOs() {
return os;
}
public String getWs() {
return ws;
}
public String getArch() {
return arch;
}
public String getNl() {
return nl;
}
public String getTargetJRE() {
return targetJRE;
}
}
}