From 8b0d41727ce4bac2b206aba93f6b1e8cf01fd647 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Thu, 18 Apr 2024 19:38:36 +0200 Subject: [PATCH] [MRESOLVER-540] TransferResource can reveal more about resource (#474) Provide extra information about the resource being transferred. --- https://issues.apache.org/jira/browse/MRESOLVER-540 --- .../aether/transfer/TransferResource.java | 29 ++++++++++++--- .../aether/transfer/TransferEventTest.java | 3 +- .../basic/BasicRepositoryConnector.java | 36 +++++++++++++++---- .../DefaultChecksumPolicyProviderTest.java | 4 +-- .../internal/impl/FailChecksumPolicyTest.java | 4 +-- .../impl/RecordingRepositoryConnector.java | 5 ++- .../internal/impl/WarnChecksumPolicyTest.java | 4 +-- 7 files changed, 59 insertions(+), 26 deletions(-) diff --git a/maven-resolver-api/src/main/java/org/eclipse/aether/transfer/TransferResource.java b/maven-resolver-api/src/main/java/org/eclipse/aether/transfer/TransferResource.java index 824c40af2..8d9fefbfa 100644 --- a/maven-resolver-api/src/main/java/org/eclipse/aether/transfer/TransferResource.java +++ b/maven-resolver-api/src/main/java/org/eclipse/aether/transfer/TransferResource.java @@ -34,6 +34,8 @@ public final class TransferResource { private final String resourceName; + private final Object resource; + private final Path path; private final long startTime; @@ -62,7 +64,7 @@ public final class TransferResource { @Deprecated public TransferResource( String repositoryId, String repositoryUrl, String resourceName, File file, RequestTrace trace) { - this(repositoryId, repositoryUrl, resourceName, file != null ? file.toPath() : null, trace); + this(repositoryId, repositoryUrl, resourceName, file != null ? file.toPath() : null, null, trace); } /** @@ -75,12 +77,18 @@ public TransferResource( * @param resourceName The relative path to the resource within the repository, may be {@code null}. A leading slash * (if any) will be automatically removed. * @param path The source/target file involved in the transfer, may be {@code null}. + * @param resource The representation of this resource, may be {@code null}. * @param trace The trace information, may be {@code null}. * * @since 2.0.0 */ public TransferResource( - String repositoryId, String repositoryUrl, String resourceName, Path path, RequestTrace trace) { + String repositoryId, + String repositoryUrl, + String resourceName, + Path path, + Object resource, + RequestTrace trace) { if (repositoryId == null || repositoryId.isEmpty()) { this.repositoryId = ""; } else { @@ -104,10 +112,9 @@ public TransferResource( } this.path = path; - + this.resource = resource; this.trace = trace; - - startTime = System.currentTimeMillis(); + this.startTime = System.currentTimeMillis(); } /** @@ -140,6 +147,18 @@ public String getResourceName() { return resourceName; } + /** + * The representation of "resource", if any. The content of this field may be + * {@link org.eclipse.aether.artifact.Artifact} or {@link org.eclipse.aether.metadata.Metadata} or {@code null} + * in case of some legacy flow. Preferred way to handle returned value is with {@link instanceof}. + * + * @return The representation of this resource, may be {@code null}. + * @since 2.0.0 + */ + public Object getResource() { + return resource; + } + /** * Gets the local file being uploaded or downloaded. When the repository system merely checks for the existence of a * remote resource, no local file will be involved in the transfer. diff --git a/maven-resolver-api/src/test/java/org/eclipse/aether/transfer/TransferEventTest.java b/maven-resolver-api/src/test/java/org/eclipse/aether/transfer/TransferEventTest.java index 6b4c0fe64..319a5b215 100644 --- a/maven-resolver-api/src/test/java/org/eclipse/aether/transfer/TransferEventTest.java +++ b/maven-resolver-api/src/test/java/org/eclipse/aether/transfer/TransferEventTest.java @@ -19,7 +19,6 @@ package org.eclipse.aether.transfer; import java.nio.ByteBuffer; -import java.nio.file.Path; import org.eclipse.aether.RepositorySystemSession; import org.junit.jupiter.api.Test; @@ -31,7 +30,7 @@ */ public class TransferEventTest { - private static final TransferResource res = new TransferResource("none", "file://nil", "void", (Path) null, null); + private static final TransferResource res = new TransferResource("none", "file://nil", "void", null, null, null); private static final RepositorySystemSession session = mock(RepositorySystemSession.class); diff --git a/maven-resolver-connector-basic/src/main/java/org/eclipse/aether/connector/basic/BasicRepositoryConnector.java b/maven-resolver-connector-basic/src/main/java/org/eclipse/aether/connector/basic/BasicRepositoryConnector.java index 572f2a622..10d93efbe 100644 --- a/maven-resolver-connector-basic/src/main/java/org/eclipse/aether/connector/basic/BasicRepositoryConnector.java +++ b/maven-resolver-connector-basic/src/main/java/org/eclipse/aether/connector/basic/BasicRepositoryConnector.java @@ -31,15 +31,17 @@ import java.util.concurrent.atomic.AtomicBoolean; import org.eclipse.aether.RepositorySystemSession; -import org.eclipse.aether.RequestTrace; import org.eclipse.aether.metadata.Metadata; import org.eclipse.aether.repository.RemoteRepository; import org.eclipse.aether.spi.checksums.ProvidedChecksumsSource; import org.eclipse.aether.spi.connector.ArtifactDownload; +import org.eclipse.aether.spi.connector.ArtifactTransfer; import org.eclipse.aether.spi.connector.ArtifactUpload; import org.eclipse.aether.spi.connector.MetadataDownload; +import org.eclipse.aether.spi.connector.MetadataTransfer; import org.eclipse.aether.spi.connector.MetadataUpload; import org.eclipse.aether.spi.connector.RepositoryConnector; +import org.eclipse.aether.spi.connector.Transfer; import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory; import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmHelper; import org.eclipse.aether.spi.connector.checksum.ChecksumPolicy; @@ -191,7 +193,7 @@ public void get( for (MetadataDownload transfer : safeMetadataDownloads) { URI location = layout.getLocation(transfer.getMetadata(), false); - TransferResource resource = newTransferResource(location, transfer.getPath(), transfer.getTrace()); + TransferResource resource = newTransferResource(location, transfer); TransferEvent.Builder builder = newEventBuilder(resource, false, false); MetadataTransportListener listener = new MetadataTransportListener(transfer, repository, builder); @@ -231,7 +233,7 @@ public void get( URI location = layout.getLocation(transfer.getArtifact(), false); - TransferResource resource = newTransferResource(location, transfer.getPath(), transfer.getTrace()); + TransferResource resource = newTransferResource(location, transfer); TransferEvent.Builder builder = newEventBuilder(resource, false, transfer.isExistenceCheck()); ArtifactTransportListener listener = new ArtifactTransportListener(transfer, repository, builder); @@ -282,7 +284,7 @@ public void put( for (ArtifactUpload transfer : safeArtifactUploads) { URI location = layout.getLocation(transfer.getArtifact(), true); - TransferResource resource = newTransferResource(location, transfer.getPath(), transfer.getTrace()); + TransferResource resource = newTransferResource(location, transfer); TransferEvent.Builder builder = newEventBuilder(resource, true, false); ArtifactTransportListener listener = new ArtifactTransportListener(transfer, repository, builder); @@ -304,7 +306,7 @@ public void put( for (MetadataUpload transfer : transferGroup) { URI location = layout.getLocation(transfer.getMetadata(), true); - TransferResource resource = newTransferResource(location, transfer.getPath(), transfer.getTrace()); + TransferResource resource = newTransferResource(location, transfer); TransferEvent.Builder builder = newEventBuilder(resource, true, false); MetadataTransportListener listener = new MetadataTransportListener(transfer, repository, builder); @@ -368,8 +370,28 @@ private static Collection safe(Collection items) { return (items != null) ? items : Collections.emptyList(); } - private TransferResource newTransferResource(URI path, Path file, RequestTrace trace) { - return new TransferResource(repository.getId(), repository.getUrl(), path.toString(), file, trace); + private TransferResource newTransferResource(URI path, Transfer transfer) { + if (transfer instanceof ArtifactTransfer) { + ArtifactTransfer artifactTransfer = (ArtifactTransfer) transfer; + return new TransferResource( + repository.getId(), + repository.getUrl(), + path.toString(), + artifactTransfer.getPath(), + artifactTransfer.getArtifact(), + artifactTransfer.getTrace()); + } else if (transfer instanceof MetadataTransfer) { + MetadataTransfer metadataTransfer = (MetadataTransfer) transfer; + return new TransferResource( + repository.getId(), + repository.getUrl(), + path.toString(), + metadataTransfer.getPath(), + metadataTransfer.getMetadata(), + metadataTransfer.getTrace()); + } else { + throw new IllegalArgumentException("Accepting only artifact or metadata transfers"); + } } private TransferEvent.Builder newEventBuilder(TransferResource resource, boolean upload, boolean peek) { diff --git a/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/DefaultChecksumPolicyProviderTest.java b/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/DefaultChecksumPolicyProviderTest.java index af434cc4b..e2f7223b7 100644 --- a/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/DefaultChecksumPolicyProviderTest.java +++ b/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/DefaultChecksumPolicyProviderTest.java @@ -18,8 +18,6 @@ */ package org.eclipse.aether.internal.impl; -import java.nio.file.Path; - import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.internal.test.util.TestUtils; import org.eclipse.aether.repository.RemoteRepository; @@ -49,7 +47,7 @@ void setup() { session = TestUtils.newSession(); provider = new DefaultChecksumPolicyProvider(); repository = new RemoteRepository.Builder("test", "default", "file:/void").build(); - resource = new TransferResource(repository.getId(), repository.getUrl(), "file.txt", (Path) null, null); + resource = new TransferResource(repository.getId(), repository.getUrl(), "file.txt", null, null, null); } @AfterEach diff --git a/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/FailChecksumPolicyTest.java b/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/FailChecksumPolicyTest.java index a357ea9b3..3fd522914 100644 --- a/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/FailChecksumPolicyTest.java +++ b/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/FailChecksumPolicyTest.java @@ -18,8 +18,6 @@ */ package org.eclipse.aether.internal.impl; -import java.nio.file.Path; - import org.eclipse.aether.spi.connector.checksum.ChecksumPolicy.ChecksumKind; import org.eclipse.aether.transfer.ChecksumFailureException; import org.eclipse.aether.transfer.TransferResource; @@ -36,7 +34,7 @@ public class FailChecksumPolicyTest { @BeforeEach void setup() { - policy = new FailChecksumPolicy(new TransferResource("null", "file:/dev/null", "file.txt", (Path) null, null)); + policy = new FailChecksumPolicy(new TransferResource("null", "file:/dev/null", "file.txt", null, null, null)); exception = new ChecksumFailureException("test"); } diff --git a/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/RecordingRepositoryConnector.java b/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/RecordingRepositoryConnector.java index 091015ce0..b08bb2ee6 100644 --- a/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/RecordingRepositoryConnector.java +++ b/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/RecordingRepositoryConnector.java @@ -18,7 +18,6 @@ */ package org.eclipse.aether.internal.impl; -import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -165,7 +164,7 @@ private void fireInitiated(Transfer transfer) throws Exception { return; } TransferEvent.Builder event = new TransferEvent.Builder( - session, new TransferResource(null, null, null, (Path) null, transfer.getTrace())); + session, new TransferResource(null, null, null, null, null, transfer.getTrace())); event.setType(TransferEvent.EventType.INITIATED); listener.transferInitiated(event.build()); } @@ -176,7 +175,7 @@ private void fireDone(Transfer transfer) { return; } TransferEvent.Builder event = new TransferEvent.Builder( - session, new TransferResource(null, null, null, (Path) null, transfer.getTrace())); + session, new TransferResource(null, null, null, null, null, transfer.getTrace())); event.setException(transfer.getException()); if (transfer.getException() != null) { listener.transferFailed( diff --git a/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/WarnChecksumPolicyTest.java b/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/WarnChecksumPolicyTest.java index fefea6358..db657b11c 100644 --- a/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/WarnChecksumPolicyTest.java +++ b/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/WarnChecksumPolicyTest.java @@ -18,8 +18,6 @@ */ package org.eclipse.aether.internal.impl; -import java.nio.file.Path; - import org.eclipse.aether.spi.connector.checksum.ChecksumPolicy.ChecksumKind; import org.eclipse.aether.transfer.ChecksumFailureException; import org.eclipse.aether.transfer.TransferResource; @@ -36,7 +34,7 @@ public class WarnChecksumPolicyTest { @BeforeEach void setup() { - policy = new WarnChecksumPolicy(new TransferResource("null", "file:/dev/null", "file.txt", (Path) null, null)); + policy = new WarnChecksumPolicy(new TransferResource("null", "file:/dev/null", "file.txt", null, null, null)); exception = new ChecksumFailureException("test"); }