-
Notifications
You must be signed in to change notification settings - Fork 188
/
InstallableUnitGenerator.java
283 lines (263 loc) · 10.7 KB
/
InstallableUnitGenerator.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*******************************************************************************
* Copyright (c) 2022 Christoph Läubrich and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.tycho.p2maven;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.logging.Logger;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.internal.p2.publisher.eclipse.FeatureParser;
import org.eclipse.equinox.internal.p2.publisher.eclipse.IProductDescriptor;
import org.eclipse.equinox.internal.p2.updatesite.CategoryParser;
import org.eclipse.equinox.internal.p2.updatesite.SiteModel;
import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.publisher.IPublisherAction;
import org.eclipse.equinox.p2.publisher.eclipse.BundlesAction;
import org.eclipse.equinox.p2.publisher.eclipse.Feature;
import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager;
import org.eclipse.tycho.PackagingType;
import org.eclipse.tycho.p2maven.actions.AuthoredIUAction;
import org.eclipse.tycho.p2maven.actions.CategoryDependenciesAction;
import org.eclipse.tycho.p2maven.actions.FeatureDependenciesAction;
import org.eclipse.tycho.p2maven.actions.ProductDependenciesAction;
import org.eclipse.tycho.p2maven.actions.ProductFile2;
import org.eclipse.tycho.p2maven.helper.PluginRealmHelper;
import org.eclipse.tycho.p2maven.io.MetadataIO;
import org.xml.sax.SAXException;
/**
* Component used to generate {@link IInstallableUnit}s from other artifacts
*
*/
@Component(role = InstallableUnitGenerator.class)
public class InstallableUnitGenerator {
private static final boolean DUMP_DATA = Boolean.getBoolean("tycho.p2.dump")
|| Boolean.getBoolean("tycho.p2.dump.units");
@Requirement
private Logger log;
private static final String KEY_UNITS = "InstallableUnitGenerator.units";
@Requirement
private IProvisioningAgent provisioningAgent;
@Requirement(role = InstallableUnitProvider.class)
private Map<String, InstallableUnitProvider> additionalUnitProviders;
@Requirement
private PluginRealmHelper pluginRealmHelper;
@Requirement
private InstallableUnitPublisher publisher;
@Requirement
private PlexusContainer plexus;
/**
* Computes the {@link IInstallableUnit}s for a collection of projects.
*
* @param projects the projects to compute InstallableUnits for
* @return a map from the passed project to the InstallebalUnits
* @throws CoreException if computation for any project failed
*/
public Map<MavenProject, Collection<IInstallableUnit>> getInstallableUnits(Collection<MavenProject> projects,
MavenSession session)
throws CoreException {
init();
Objects.requireNonNull(session);
List<CoreException> errors = new CopyOnWriteArrayList<CoreException>();
Map<MavenProject, Collection<IInstallableUnit>> result = new ConcurrentHashMap<MavenProject, Collection<IInstallableUnit>>();
projects.parallelStream().unordered().takeWhile(nil -> errors.isEmpty()).forEach(project -> {
try {
result.put(project, getInstallableUnits(project, session, false));
} catch (CoreException e) {
errors.add(e);
}
});
if (errors.isEmpty()) {
return result;
}
if (errors.size() == 1) {
throw errors.get(0);
}
MultiStatus multiStatus = new MultiStatus(InstallableUnitGenerator.class, IStatus.ERROR,
"computing installable unit units failed");
errors.forEach(e -> multiStatus.add(e.getStatus()));
throw new CoreException(multiStatus);
}
private void init() {
// this requirement is here to bootstrap P2 service access
// see https://github.com/eclipse-equinox/p2/issues/100
// then this would not be required anymore
provisioningAgent.getService(IArtifactRepositoryManager.class);
}
/**
* Computes the {@link IInstallableUnit}s for the given project, the computation
* is cached unless forceUpdate is <code>true</code> meaning data is always
* regenerated from scratch.
*
* @param project the project to examine
* @param session
* @param forceUpdate if cached data is fine
* @return a (possibly empty) collection of {@link IInstallableUnit}s for the
* given {@link MavenProject}
* @throws CoreException if anything goes wrong
*/
@SuppressWarnings("unchecked")
public Collection<IInstallableUnit> getInstallableUnits(MavenProject project, MavenSession session,
boolean forceUpdate)
throws CoreException {
init();
Objects.requireNonNull(session);
log.debug("Computing installable units for " + project + ", force update = " + forceUpdate);
synchronized (project) {
if (!forceUpdate) {
Object contextValue = project.getContextValue(KEY_UNITS);
if (contextValue instanceof Collection<?>) {
Collection<IInstallableUnit> collection = (Collection<IInstallableUnit>) contextValue;
if (isCompatible(collection)) {
log.debug("Using cached value for " + project);
return collection;
} else {
log.debug("Can't use cached value for " + project
+ " because of incompatible classloaders, update is forced!");
}
}
}
List<IPublisherAction> actions = new ArrayList<>();
File basedir = project.getBasedir();
if (basedir == null || !basedir.isDirectory()) {
log.warn("No valid basedir for " + project + "!");
return Collections.emptyList();
}
String packaging = project.getPackaging();
switch (packaging) {
case PackagingType.TYPE_ECLIPSE_TEST_PLUGIN:
case PackagingType.TYPE_ECLIPSE_PLUGIN: {
actions.add(new BundlesAction(new File[] { basedir }));
break;
}
case PackagingType.TYPE_ECLIPSE_FEATURE: {
FeatureParser parser = new FeatureParser();
Feature feature = parser.parse(basedir);
feature.setLocation(basedir.getAbsolutePath());
FeatureDependenciesAction action = new FeatureDependenciesAction(feature);
actions.add(action);
break;
}
case PackagingType.TYPE_ECLIPSE_REPOSITORY: {
File categoryFile = new File(basedir, "category.xml");
if (categoryFile.exists()) {
try (InputStream stream = new FileInputStream(categoryFile)) {
SiteModel siteModel = new CategoryParser(null).parse(stream);
actions.add(new CategoryDependenciesAction(siteModel, project.getArtifactId(),
project.getVersion()));
} catch (IOException | SAXException e) {
throw new CoreException(Status.error("Error reading " + categoryFile.getAbsolutePath()));
}
}
for (File f : basedir.listFiles(File::isFile)) {
if (f.getName().endsWith(".product") && !f.getName().startsWith(".polyglot")) {
try {
IProductDescriptor productDescriptor = new ProductFile2(f.getAbsolutePath());
actions.add(new ProductDependenciesAction(productDescriptor));
} catch (CoreException e) {
throw e;
} catch (Exception e) {
throw new CoreException(Status.error("Error reading " + f.getAbsolutePath() + ": " + e, e));
}
}
}
break;
}
case PackagingType.TYPE_P2_IU: {
actions.add(new AuthoredIUAction(basedir));
break;
}
default:
}
Collection<IInstallableUnit> publishedUnits = publisher.publishMetadata(actions);
for (InstallableUnitProvider unitProvider : getProvider(project, session)) {
log.debug("Asking: " + unitProvider + " for additional units for " + project + "...");
Collection<IInstallableUnit> installableUnits = unitProvider.getInstallableUnits(project, session);
log.debug("Provider " + unitProvider + " generated " + installableUnits.size() + " (" + installableUnits
+ ") units for " + project);
publishedUnits.addAll(installableUnits);
}
Collection<IInstallableUnit> result = Collections.unmodifiableCollection(publishedUnits);
if (DUMP_DATA) {
File file = new File(project.getBasedir(), "project-units.xml");
try {
new MetadataIO().writeXML(result, file);
} catch (IOException e) {
}
}
if (result.isEmpty()) {
log.debug("Can't generate any InstallableUnit for packaging type '" + packaging + "' for " + project);
}
project.setContextValue(KEY_UNITS, result);
return result;
}
}
private Collection<InstallableUnitProvider> getProvider(MavenProject project, MavenSession mavenSession)
throws CoreException {
Set<InstallableUnitProvider> unitProviders = new HashSet<InstallableUnitProvider>(
additionalUnitProviders.values());
try {
pluginRealmHelper.execute(mavenSession, project, () -> {
try {
for (InstallableUnitProvider provider : plexus.lookupList(InstallableUnitProvider.class)) {
unitProviders.add(provider);
}
} catch (ComponentLookupException e) {
// ignore, nothing was found...
}
}, InstallableUnitGenerator::hasPluginDependency);
} catch (Exception e) {
throw new CoreException(Status.error("Can't lookup InstallableUnitProviders", e));
}
return unitProviders;
}
private static boolean isCompatible(Collection<?> collection) {
// TODO currently causes errors if called from different classloaders!
// Check how we properly export p2 artifacts to the build!
if (collection.isEmpty()) {
return true;
}
for (Object unit : collection) {
if (!IInstallableUnit.class.isInstance(unit)) {
return false;
}
}
return true;
}
private static boolean hasPluginDependency(PluginDescriptor pluginDescriptor) {
if (pluginDescriptor.getArtifactMap().containsKey(P2Plugin.KEY)) {
return true;
}
return pluginDescriptor.getDependencies().stream().filter(dep -> P2Plugin.GROUP_ID.equals(dep.getGroupId()))
.filter(dep -> P2Plugin.ARTIFACT_ID.equals(dep.getArtifactId())).findAny().isPresent();
}
}