Skip to content

Commit

Permalink
Add support to plug-in additional ArtifactDownloadProvider
Browse files Browse the repository at this point in the history
The transport now support to supply an artifact descriptor that then can
be used to look-up the artifact in several other places to prevent
downloading it several times.

This now adds an abstraction for components to supply an
ArtifactDownloadProvider component to implement such strategies
independent from the actual transport implementation.

In addition to this, also the interface for TransportProtocolHandler is
moved to the SPI.
  • Loading branch information
laeubi committed Dec 28, 2024
1 parent 87a514c commit 0b38562
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.net.URI;

import org.codehaus.plexus.component.annotations.Component;
import org.eclipse.tycho.transport.TransportProtocolHandler;

@Component(role = TransportProtocolHandler.class, hint = "file")
public class FileTransportProtocolHandler implements TransportProtocolHandler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable;
import org.eclipse.tycho.MavenRepositorySettings.Credentials;
import org.eclipse.tycho.transport.TransportProtocolHandler;

/**
* Handles files discovery over the FTP protocol.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
import org.eclipse.tycho.transport.TransportProtocolHandler;

@Component(role = TransportProtocolHandler.class, hint = "http")
public class HttpTransportProtocolHandler implements TransportProtocolHandler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.eclipse.tycho.p2maven.transport;

import org.codehaus.plexus.component.annotations.Component;
import org.eclipse.tycho.transport.TransportProtocolHandler;

@Component(role = TransportProtocolHandler.class, hint = "https")
public class HttpsTransportProtocolHandler extends HttpTransportProtocolHandler {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.NumberFormat;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
Expand All @@ -44,6 +47,8 @@
import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.equinox.p2.core.spi.IAgentServiceFactory;
import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor;
import org.eclipse.tycho.transport.ArtifactDownloadProvider;
import org.eclipse.tycho.transport.TransportProtocolHandler;

@Component(role = org.eclipse.equinox.internal.p2.repository.Transport.class, hint = "tycho")
public class TychoRepositoryTransport extends org.eclipse.equinox.internal.p2.repository.Transport
Expand Down Expand Up @@ -78,6 +83,9 @@ public Thread newThread(Runnable r) {
@Requirement(role = TransportProtocolHandler.class)
Map<String, TransportProtocolHandler> transportProtocolHandlers;

@Requirement // TODO @Inject results in a list with multiple items of the same provider!
List<ArtifactDownloadProvider> artifactDownloadProvider;

private LongAdder requests = new LongAdder();
private LongAdder indexRequests = new LongAdder();

Expand All @@ -88,6 +96,17 @@ public TychoRepositoryTransport() {
@Override
public IStatus downloadArtifact(URI source, OutputStream target, IArtifactDescriptor descriptor,
IProgressMonitor monitor) {
if (descriptor != null) {
Iterator<ArtifactDownloadProvider> iterator = artifactDownloadProvider.stream().distinct()
.sorted(Comparator.comparingInt(ArtifactDownloadProvider::getPriority).reversed()).iterator();
while (iterator.hasNext()) {
ArtifactDownloadProvider provider = iterator.next();
IStatus status = provider.downloadArtifact(source, target, descriptor);
if (!status.matches(IStatus.CANCEL)) {
return reportStatus(status, target);
}
}
}
String id = "p2"; // TODO we might compute the id from the IRepositoryIdManager based on the URI?
if (cacheConfig.isInteractive()) {
logger.info("Downloading from " + id + ": " + source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.core.runtime.URIUtil;
import org.eclipse.equinox.internal.p2.repository.CacheManager;
import org.eclipse.equinox.p2.core.ProvisionException;
import org.eclipse.tycho.transport.TransportProtocolHandler;

public class TychoRepositoryTransportCacheManager extends CacheManager {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*******************************************************************************
* Copyright (c) 2024 Christoph Läubrich 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:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.tycho.transport;

import java.io.OutputStream;
import java.net.URI;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.equinox.internal.p2.repository.DownloadStatus;
import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor;

/**
* An {@link ArtifactDownloadProvider} can supply an alternative caching strategy for artifacts
* fetched from P2
*/
public interface ArtifactDownloadProvider {

/**
* Ask the provider to download the given artifact and transfer it to the given target
*
* @param source
* the source URI, might be a mirror URL
* @param target
* the target where the result should be transfered to
* @param descriptor
* the artifact descriptor to be queried
* @return a status of type cancel if this provider can't supply the artifact, or a
* {@link DownloadStatus} in case of success to supply additional information to P2, or
* an error if the download failed even though it should not.
*/
public IStatus downloadArtifact(URI source, OutputStream target, IArtifactDescriptor descriptor);

/**
* @return the priority, higher values are considered first
*/
int getPriority();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*******************************************************************************
* Copyright (c) 2022 Christoph Läubrich 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:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.tycho.transport;

import java.io.File;
import java.io.IOException;
import java.net.URI;

public interface TransportProtocolHandler {

long getLastModified(URI uri) throws IOException;

File getFile(URI remoteFile) throws IOException;

}

0 comments on commit 0b38562

Please sign in to comment.