-
Notifications
You must be signed in to change notification settings - Fork 188
/
FeatureXmlTransformer.java
189 lines (169 loc) · 6.66 KB
/
FeatureXmlTransformer.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
/*******************************************************************************
* Copyright (c) 2011, 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 - Issue #845 - Feature restrictions are not taken into account when using emptyVersion
*******************************************************************************/
package org.eclipse.tycho.packaging;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
import org.eclipse.tycho.ArtifactKey;
import org.eclipse.tycho.ArtifactType;
import org.eclipse.tycho.artifacts.IllegalArtifactReferenceException;
import org.eclipse.tycho.artifacts.TargetPlatform;
import org.eclipse.tycho.locking.facade.FileLockService;
import org.eclipse.tycho.locking.facade.FileLocker;
import org.eclipse.tycho.model.Feature;
import org.eclipse.tycho.model.Feature.ImportRef;
import org.eclipse.tycho.model.FeatureRef;
import org.eclipse.tycho.model.PluginRef;
@Component(role = FeatureXmlTransformer.class)
public class FeatureXmlTransformer {
private static final int KBYTE = 1024;
@Requirement
private Logger log;
@Requirement
private FileLockService fileLockService;
public FeatureXmlTransformer() {
}
public FeatureXmlTransformer(Logger log, FileLockService fileLockService) {
this.log = log;
this.fileLockService = fileLockService;
}
/**
* Replaces references in the feature model with versions from the target
* platform.
*
* @param feature The feature model to have plug-in and feature references
* completed.
*/
public Feature expandReferences(Feature feature, TargetPlatform targetPlatform) throws MojoFailureException {
Map<String, ImportRef> featureImports = feature.getRequires().stream().flatMap(req -> req.getImports().stream())
.filter(imp -> Objects.nonNull(imp.getFeature()) && !imp.getFeature().isBlank())
.collect(Collectors.toMap(ImportRef::getFeature, Function.identity()));
Map<String, ImportRef> pluginImports = feature.getRequires().stream().flatMap(req -> req.getImports().stream())
.filter(imp -> Objects.nonNull(imp.getPlugin()) && !imp.getPlugin().isBlank())
.collect(Collectors.toMap(ImportRef::getPlugin, Function.identity()));
for (PluginRef pluginRef : feature.getPlugins()) {
String version = pluginRef.getVersion();
if ("0.0.0".equals(version)) {
ImportRef importRef = pluginImports.get(pluginRef.getId());
if (isVersionedRef(importRef)) {
version = String.format("%s|%s", importRef.getVersion(), importRef.getMatch());
}
}
ArtifactKey plugin = resolvePluginReference(targetPlatform, pluginRef, version);
pluginRef.setVersion(plugin.getVersion());
File location = targetPlatform.getArtifactLocation(plugin);
if (location == null) {
throw new MojoFailureException("location is missing for plugin " + plugin);
}
setDownloadAndInstallSize(pluginRef, location);
}
for (FeatureRef featureRef : feature.getIncludedFeatures()) {
String version = featureRef.getVersion();
if ("0.0.0".equals(version)) {
ImportRef importRef = featureImports.get(featureRef.getId());
if (isVersionedRef(importRef)) {
version = String.format("%s|%s", importRef.getVersion(), importRef.getMatch());
}
}
ArtifactKey includedFeature = resolveFeatureReference(targetPlatform, featureRef, version);
featureRef.setVersion(includedFeature.getVersion());
}
return feature;
}
private boolean isVersionedRef(ImportRef importRef) {
if (importRef == null) {
return false;
}
String version = importRef.getVersion();
if (version == null || version.isEmpty()) {
return false;
}
String match = importRef.getMatch();
if (match == null || match.isEmpty()) {
return false;
}
return true;
}
private ArtifactKey resolvePluginReference(TargetPlatform targetPlatform, PluginRef pluginRef, String version)
throws MojoFailureException {
try {
return targetPlatform.resolveArtifact(ArtifactType.TYPE_ECLIPSE_PLUGIN, pluginRef.getId(), version);
} catch (IllegalArtifactReferenceException e) {
throw new MojoFailureException("Invalid plugin reference with id=" + quote(pluginRef.getId())
+ " and version=" + quote(pluginRef.getVersion()) + ": " + e.getMessage(), e);
}
}
private ArtifactKey resolveFeatureReference(TargetPlatform targetPlatform, FeatureRef featureRef, String version)
throws MojoFailureException {
try {
return targetPlatform.resolveArtifact(ArtifactType.TYPE_ECLIPSE_FEATURE, featureRef.getId(), version);
} catch (IllegalArtifactReferenceException e) {
throw new MojoFailureException("Invalid feature reference with id=" + quote(featureRef.getId())
+ " and version " + quote(featureRef.getVersion()) + ": " + e.getMessage(), e);
}
}
private static String quote(String nullableString) {
if (nullableString == null)
return null;
else
return "\"" + nullableString + "\"";
}
private void setDownloadAndInstallSize(PluginRef pluginRefToEdit, File artifact) {
// TODO 375111 optionally disable this?
long downloadSize = 0;
long installSize = 0;
if (artifact.isFile()) {
installSize = getInstallSize(artifact);
downloadSize = artifact.length();
} else {
log.info("Download/install size is not calculated for directory based bundle " + pluginRefToEdit.getId());
}
pluginRefToEdit.setDownloadSize(downloadSize / KBYTE);
pluginRefToEdit.setInstallSize(installSize / KBYTE);
}
protected long getInstallSize(File location) {
long installSize = 0;
FileLocker locker = fileLockService.getFileLocker(location);
locker.lock();
try {
try {
try (JarFile jar = new JarFile(location)) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
long entrySize = entry.getSize();
if (entrySize > 0) {
installSize += entrySize;
}
}
}
} catch (IOException e) {
throw new RuntimeException("Could not determine installation size of file " + location, e);
}
} finally {
locker.release();
}
return installSize;
}
}