Skip to content

Commit

Permalink
[bpt] Prepare for using new Transport#downloadArtifact
Browse files Browse the repository at this point in the history
P2 has a new way to download an actual P2 artifact by passing the
IArtifactDescriptor to the transport so it can use additional
information for caching.

This simply prepares the TychoRepositoryTransport for usages by
overriding the new method and delegate calls from the old one.
  • Loading branch information
laeubi committed Dec 27, 2024
1 parent 19e9370 commit 3a087b4
Showing 1 changed file with 31 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.eclipse.equinox.internal.provisional.p2.repository.IStateful;
import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.equinox.p2.core.spi.IAgentServiceFactory;
import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor;

@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 @@ -85,49 +86,32 @@ public TychoRepositoryTransport() {
}

@Override
public IStatus download(URI toDownload, OutputStream target, long startPos, IProgressMonitor monitor) {
if (startPos > 0) {
return new Status(IStatus.ERROR, TychoRepositoryTransport.class.getName(),
"range downloads are not implemented");
}
return download(toDownload, target, monitor);
}

@Override
public IStatus download(URI toDownload, OutputStream target, IProgressMonitor monitor) {
public IStatus downloadArtifact(URI source, OutputStream target, IArtifactDescriptor descriptor,
IProgressMonitor monitor) {
String id = "p2"; // TODO we might compute the id from the IRepositoryIdManager based on the URI?
if (cacheConfig.isInteractive()) {
logger.info("Downloading from " + id + ": " + toDownload);
logger.info("Downloading from " + id + ": " + source);
}
try {
DownloadStatusOutputStream statusOutputStream = new DownloadStatusOutputStream(target,
"Download of " + toDownload);
stream(toDownload, monitor).transferTo(statusOutputStream);
"Download of " + source);
stream(source, monitor).transferTo(statusOutputStream);
DownloadStatus downloadStatus = statusOutputStream.getStatus();
if (cacheConfig.isInteractive()) {
logger.info("Downloaded from " + id + ": " + toDownload + " ("
logger.info("Downloaded from " + id + ": " + source + " ("
+ FileUtils.byteCountToDisplaySize(downloadStatus.getFileSize()) + " at "
+ FileUtils.byteCountToDisplaySize(downloadStatus.getTransferRate()) + "/s)");
}
return reportStatus(downloadStatus, target);
} catch (AuthenticationFailedException e) {
return new Status(IStatus.ERROR, TychoRepositoryTransport.class.getName(),
"authentication failed for " + toDownload, e);
return Status.error("authentication failed for " + source, e);
} catch (IOException e) {
return reportStatus(new Status(IStatus.ERROR, TychoRepositoryTransport.class.getName(),
"download from " + toDownload + " failed", e), target);
return reportStatus(Status.error("download from " + source + " failed", e), target);
} catch (CoreException e) {
return reportStatus(e.getStatus(), target);
}
}

private IStatus reportStatus(IStatus status, OutputStream target) {
if (target instanceof IStateful stateful) {
stateful.setStatus(status);
}
return status;
}

@Override
public InputStream stream(URI toDownload, IProgressMonitor monitor)
throws FileNotFoundException, CoreException, AuthenticationFailedException {
Expand Down Expand Up @@ -162,8 +146,7 @@ public InputStream stream(URI toDownload, IProgressMonitor monitor)
if (DEBUG_REQUESTS) {
logger.debug(" --> generic error: " + e);
}
throw new CoreException(new Status(IStatus.ERROR, TychoRepositoryTransport.class.getName(),
"download from " + toDownload + " failed", e));
throw new CoreException(Status.error("download from " + toDownload + " failed", e));
} finally {
if (DEBUG_REQUESTS) {
logger.debug("Total number of requests: " + requests.longValue() + " (" + indexRequests.longValue()
Expand All @@ -172,6 +155,19 @@ public InputStream stream(URI toDownload, IProgressMonitor monitor)
}
}

@Override
public IStatus download(URI toDownload, OutputStream target, IProgressMonitor monitor) {
return downloadArtifact(toDownload, target, null, monitor);
}

@Override
public IStatus download(URI toDownload, OutputStream target, long startPos, IProgressMonitor monitor) {
if (startPos > 0) {
return Status.error("range downloads are not implemented");
}
return downloadArtifact(toDownload, target, null, monitor);
}

TransportProtocolHandler getHandler(URI uri) {
String scheme = uri.getScheme();
if (scheme != null) {
Expand Down Expand Up @@ -199,8 +195,7 @@ public long getLastModified(URI toDownload, IProgressMonitor monitor)
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
throw new CoreException(new Status(IStatus.ERROR, TychoRepositoryTransport.class.getName(),
"download from " + toDownload + " failed", e));
throw new CoreException(Status.error("download from " + toDownload + " failed", e));
}
}

Expand Down Expand Up @@ -235,4 +230,11 @@ TransportCacheConfig getCacheConfig() {
return cacheConfig;
}

private static IStatus reportStatus(IStatus status, OutputStream target) {
if (target instanceof IStateful stateful) {
stateful.setStatus(status);
}
return status;
}

}

0 comments on commit 3a087b4

Please sign in to comment.