-
Notifications
You must be signed in to change notification settings - Fork 188
/
MirroringArtifactProvider.java
422 lines (380 loc) · 18.7 KB
/
MirroringArtifactProvider.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*******************************************************************************
* Copyright (c) 2012, 2022 SAP SE 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:
* Tobias Oberlies (SAP SE) - initial API and implementation
* Christoph Läubrich - Issue #658 - Tycho strips p2 artifact properties (eg PGP, maven info...)
* - Issue #692 - Check Hashsums for local cached artifacts
*******************************************************************************/
package org.eclipse.tycho.repository.local;
import static org.eclipse.tycho.repository.util.internal.BundleConstants.BUNDLE_ID;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.TreeSet;
import java.util.concurrent.locks.Lock;
import org.apache.commons.codec.digest.DigestUtils;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.p2.core.ProvisionException;
import org.eclipse.equinox.p2.metadata.IArtifactKey;
import org.eclipse.equinox.p2.query.CompoundQueryable;
import org.eclipse.equinox.p2.query.IQuery;
import org.eclipse.equinox.p2.query.IQueryResult;
import org.eclipse.equinox.p2.query.IQueryable;
import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor;
import org.eclipse.tycho.PackagingType;
import org.eclipse.tycho.TychoConstants;
import org.eclipse.tycho.core.shared.MavenContext;
import org.eclipse.tycho.core.shared.MavenLogger;
import org.eclipse.tycho.core.shared.MultiLineLogger;
import org.eclipse.tycho.repository.p2base.artifact.provider.IRawArtifactFileProvider;
import org.eclipse.tycho.repository.p2base.artifact.provider.IRawArtifactProvider;
import org.eclipse.tycho.repository.p2base.artifact.provider.formats.ArtifactTransferPolicy;
import org.eclipse.tycho.repository.p2base.artifact.provider.streaming.ArtifactSinkException;
import org.eclipse.tycho.repository.p2base.artifact.provider.streaming.IArtifactSink;
import org.eclipse.tycho.repository.p2base.artifact.provider.streaming.IRawArtifactSink;
import org.eclipse.tycho.repository.util.DuplicateFilteringLoggingProgressMonitor;
import org.eclipse.tycho.repository.util.LoggingProgressMonitor;
import org.eclipse.tycho.repository.util.StatusTool;
/**
* {@link IRawArtifactFileProvider} which caches all accessed artifacts in the local Maven
* repository.
*
* <p>
* Note that a <tt>MirroringArtifactProvider</tt> is not a transparent cache of the remote
* providers. The content provided by this instance differs from the remote providers' content in
* the following ways:
* <ul>
* <li>This instance provides all content in the local Maven repository (previously cached or
* installed) in addition to the remote content. This allows lazy access to the remote repositories
* (bug 347477).</li>
* <li>This instance only provides the remote artifacts in certain formats, i.e. only the canonical
* format, or the canonical format and the packed format.</li>
* </ul>
* </p>
*/
public class MirroringArtifactProvider implements IRawArtifactFileProvider {
protected final MavenLogger logger;
protected final MavenLogger splittingLogger;
protected final IRawArtifactProvider remoteProviders;
protected final LocalArtifactRepository localArtifactRepository;
protected final IProgressMonitor monitor;
private MavenContext mavenContext;
/**
* Creates a new {@link MirroringArtifactProvider} instance.
*
* @param localArtifactRepository
* The local Maven repository
* @param remoteProviders
* The provider that will be queried by this instance when it is asked for an
* artifact which is not (yet) available in the local Maven repository. Typically
* this provider is backed by remote p2 repositories.
* @param logger
* a logger for progress output
*/
public static MirroringArtifactProvider createInstance(LocalArtifactRepository localArtifactRepository,
IRawArtifactProvider remoteProviders, MavenContext context) {
return new MirroringArtifactProvider(localArtifactRepository, remoteProviders, context);
}
MirroringArtifactProvider(LocalArtifactRepository localArtifactRepository, IRawArtifactProvider remoteProviders,
MavenContext mavenContext) {
this.remoteProviders = remoteProviders;
this.localArtifactRepository = localArtifactRepository;
this.mavenContext = mavenContext;
this.logger = mavenContext.getLogger();
this.splittingLogger = new MultiLineLogger(this.logger);
this.monitor = new LoggingProgressMonitor(this.logger);
}
// pass through methods
@Override
public final boolean contains(IArtifactKey key) {
if (localArtifactRepository.contains(key)) {
return true;
}
return remoteProviders.contains(key);
}
@Override
@SuppressWarnings({ "restriction", "unchecked" })
public final IQueryResult<IArtifactKey> query(IQuery<IArtifactKey> query, IProgressMonitor monitor) {
IQueryable<IArtifactKey>[] sources = new IQueryable[] { localArtifactRepository, remoteProviders };
return new CompoundQueryable<>(sources).query(query, nonNull(monitor));
}
// mirroring methods
@Override
public final File getArtifactFile(IArtifactKey key) throws MirroringFailedException {
if (makeLocallyAvailable(key)) {
return localArtifactRepository.getArtifactFile(key);
}
return null;
}
@Override
public final File getArtifactFile(IArtifactDescriptor descriptor) throws MirroringFailedException {
if (makeLocallyAvailable(descriptor.getArtifactKey())) {
return localArtifactRepository.getArtifactFile(descriptor);
}
return null;
}
@Override
public final IStatus getArtifact(IArtifactSink sink, IProgressMonitor monitor)
throws ArtifactSinkException, MirroringFailedException {
IArtifactKey requestedKey = sink.getArtifactToBeWritten();
if (makeLocallyAvailable(requestedKey)) {
return localArtifactRepository.getArtifact(sink, monitor);
}
return artifactNotFoundStatus(requestedKey);
}
@Override
public final IStatus getRawArtifact(IRawArtifactSink sink, IProgressMonitor monitor)
throws ArtifactSinkException, MirroringFailedException {
IArtifactKey requestedKey = sink.getArtifactToBeWritten();
if (makeLocallyAvailable(requestedKey)) {
return localArtifactRepository.getRawArtifact(sink, monitor);
}
return artifactNotFoundStatus(requestedKey);
}
@Override
public final IArtifactDescriptor[] getArtifactDescriptors(IArtifactKey key) throws MirroringFailedException {
if (makeLocallyAvailable(key)) {
return localArtifactRepository.getArtifactDescriptors(key);
}
return new IArtifactDescriptor[0];
}
@Override
public final boolean contains(IArtifactDescriptor descriptor) throws MirroringFailedException {
if (makeLocallyAvailable(descriptor.getArtifactKey())) {
return localArtifactRepository.contains(descriptor);
}
return false;
}
/**
* Downloads the artifact from remote if it isn't available locally yet.
*
* @return <code>false</code> if the artifact is neither already cached locally nor available
* remotely.
* @throws MirroringFailedException
* if a fatal error occurred while downloading the artifact.
*/
private boolean makeLocallyAvailable(IArtifactKey key) throws MirroringFailedException {
// TODO 397355 cache artifactDescriptors for the key so that only one synchronization is necessary to figure out if a download is needed
try {
boolean isAvailable = makeOneFormatLocallyAvailable(key);
if (isAvailable) {
/*
* Always execute this step (even when not mirroring packed artifacts) so that a
* local repository that only contains the packed format of an artifact is
* automatically corrected. We need the canonical format in the local repository so
* that {@link IArtifactFileProvider#getArtifactFile(IArtifactKey)} does not return
* null.
*/
ensureArtifactIsPresentInCanonicalFormat(key);
}
return isAvailable;
} catch (ProvisionException e) {
throw new MirroringFailedException(
"Error while mirroring artifact " + key + " to the local Maven repository" + e.getMessage(), e);
} catch (ArtifactSinkException e) {
throw new MirroringFailedException(
"Error while mirroring artifact " + key + " to the local Maven repository" + e.getMessage(), e);
}
}
protected boolean makeOneFormatLocallyAvailable(IArtifactKey key)
throws MirroringFailedException, ProvisionException, ArtifactSinkException {
if (isFileAlreadyAvailable(key)) {
return true;
} else if (remoteProviders.contains(key)) {
Lock downloadLock = localArtifactRepository.getLockForDownload(key);
downloadLock.lock();
try {
if (!isFileAlreadyAvailable(key)) { // check again within lock
File artifactFile;
if (localArtifactRepository.contains(key)) {
artifactFile = localArtifactRepository.getArtifactFile(key);
localArtifactRepository.removeDescriptor(key);
} else {
artifactFile = localArtifactRepository.internalGetArtifactStorageLocation(
localArtifactRepository.createArtifactDescriptor(key));
}
if (artifactFile != null && artifactFile.isFile()) {
//check if only properties has changed...
GAVArtifactDescriptor descriptor = newLocalDescriptor(key);
if (fileMatchesProperties(artifactFile, descriptor.getProperties())) {
localArtifactRepository.internalAddDescriptor(descriptor);
localArtifactRepository.save();
return true;
}
}
downloadArtifact(key);
localArtifactRepository.save();
}
} finally {
downloadLock.unlock();
}
return true;
} else {
return false;
}
}
private boolean fileMatchesProperties(File file, Map<String, String> properties) {
String downloadSize = properties.get("download.size");
if (downloadSize != null && !downloadSize.equals(String.valueOf(file.length()))) {
//early break out...
return false;
}
String sha256 = properties.get("download.checksum.sha-256");
if (sha256 != null) {
try (FileInputStream stream = new FileInputStream(file)) {
String fileSha256 = DigestUtils.sha256Hex(stream);
return fileSha256.equalsIgnoreCase(sha256);
} catch (IOException e) {
return false;
}
}
String md5 = properties.get("download.checksum.md5");
if (md5 != null) {
try (FileInputStream stream = new FileInputStream(file)) {
String fileMd5 = DigestUtils.md5Hex(stream);
return fileMd5.equalsIgnoreCase(md5);
} catch (IOException e) {
return false;
}
}
return false;
}
protected final void downloadArtifact(IArtifactKey key)
throws MirroringFailedException, ProvisionException, ArtifactSinkException {
// logger.info("Downloading " + key.getId() + "_" + key.getVersion() + "..."); // p2 output is enough
IStatus transferStatus = downloadMostSpecificNeededFormatOfArtifact(key);
if (transferStatus.matches(IStatus.ERROR | IStatus.CANCEL)) {
splittingLogger.error(StatusTool.toLogMessage(transferStatus));
throw new MirroringFailedException("Could not mirror artifact " + key + " into the local Maven repository."
+ "See log output for details.", StatusTool.findException(transferStatus));
} else if (transferStatus.matches(IStatus.WARNING)) {
splittingLogger.warn(StatusTool.toLogMessage(transferStatus));
}
}
protected IStatus downloadMostSpecificNeededFormatOfArtifact(IArtifactKey key)
throws ProvisionException, ArtifactSinkException {
// only need canonical format
return downloadCanonicalArtifact(key);
}
protected final IStatus downloadCanonicalArtifact(IArtifactKey key)
throws ProvisionException, ArtifactSinkException {
GAVArtifactDescriptor localDescriptor = newLocalDescriptor(key);
IArtifactSink localSink = localArtifactRepository.newAddingArtifactSink(localDescriptor);
return remoteProviders.getArtifact(localSink, monitorForDownload());
}
protected GAVArtifactDescriptor newLocalDescriptor(IArtifactKey key) {
GAVArtifactDescriptor localDescriptor = localArtifactRepository.createArtifactDescriptor(key);
IArtifactDescriptor remoteDescriptor = findCanonicalDescriptor(remoteProviders.getArtifactDescriptors(key));
Map<String, String> properties = getProperties(remoteDescriptor);
properties.forEach(localDescriptor::setProperty);
return localDescriptor;
}
private void ensureArtifactIsPresentInCanonicalFormat(IArtifactKey key)
throws ProvisionException, ArtifactSinkException {
if (findCanonicalDescriptor(localArtifactRepository.getArtifactDescriptors(key)) == null) {
downloadCanonicalArtifact(key);
}
}
static IArtifactDescriptor findCanonicalDescriptor(IArtifactDescriptor[] descriptors) {
for (IArtifactDescriptor descriptor : descriptors) {
if (ArtifactTransferPolicy.isCanonicalFormat(descriptor)) {
return descriptor;
}
}
return null;
}
private static IStatus artifactNotFoundStatus(IArtifactKey key) {
return new Status(IStatus.ERROR, BUNDLE_ID, ProvisionException.ARTIFACT_NOT_FOUND, "Artifact " + key
+ " is neither available in the local Maven repository nor in the configured remote repositories",
null);
}
/**
* Returns an {@link IProgressMonitor} which translates p2's status updates into a reasonable
* log output.
*/
final IProgressMonitor monitorForDownload() {
// create a new instance for each call - DuplicateFilteringLoggingProgressMonitor is not thread-safe
return new DuplicateFilteringLoggingProgressMonitor(logger);
}
private static IProgressMonitor nonNull(IProgressMonitor monitor) {
if (monitor == null)
return new NullProgressMonitor();
else
return monitor;
}
public static class MirroringFailedException extends RuntimeException {
private static final long serialVersionUID = 1L;
MirroringFailedException(String message, Throwable cause) {
super(message, cause);
}
}
@Override
public boolean isFileAlreadyAvailable(IArtifactKey artifactKey) {
if (localArtifactRepository.contains(artifactKey)) {
if (mavenContext.isOffline()) {
return true;
}
if (remoteProviders.contains(artifactKey)) {
//we must compare remote versus local!
IArtifactDescriptor[] remoteDescriptors = remoteProviders.getArtifactDescriptors(artifactKey);
IArtifactDescriptor remoteDescriptor = findCanonicalDescriptor(remoteDescriptors);
IArtifactDescriptor[] localDescriptors = localArtifactRepository.getArtifactDescriptors(artifactKey);
GAVArtifactDescriptor localDescriptor = (GAVArtifactDescriptor) findCanonicalDescriptor(
localDescriptors);
if (remoteDescriptor != null && localDescriptor != null && localDescriptor.getMavenCoordinates()
.getGroupId().startsWith(TychoConstants.P2_GROUPID_PREFIX)) {
Map<String, String> remoteProperties = getProperties(remoteDescriptor);
Map<String, String> localProperties = getProperties(localDescriptor);
if (!Objects.equals(remoteProperties, localProperties)) {
if (logger.isExtendedDebugEnabled()) {
logger.info("ArtifactKey " + artifactKey + " differs between local and remote:");
TreeSet<String> allKeys = new TreeSet<>();
allKeys.addAll(remoteProperties.keySet());
allKeys.addAll(localProperties.keySet());
for (String key : allKeys) {
String remoteValue = remoteProperties.get(key);
String localValue = localProperties.get(key);
if (Objects.equals(remoteValue, localValue)) {
continue;
}
logger.info(
"\t" + key + " diverged, remote = " + remoteValue + ", local = " + localValue);
}
}
return false;
}
}
}
return true;
}
return false;
}
@SuppressWarnings("deprecation")
private Map<String, String> getProperties(IArtifactDescriptor descriptor) {
if (descriptor == null) {
return Map.of();
}
Map<String, String> map = new LinkedHashMap<>(descriptor.getProperties());
//fix bad metadata in p2...
if (ArtifactTransferPolicy.isCanonicalFormat(descriptor)
&& "pack200".equals(map.get(TychoConstants.PROP_CLASSIFIER))) {
map.remove(TychoConstants.PROP_CLASSIFIER);
map.put(TychoConstants.PROP_EXTENSION, "jar");
map.put(TychoConstants.PROP_TYPE, PackagingType.TYPE_ECLIPSE_PLUGIN);
}
return map;
}
}