Skip to content

Commit

Permalink
[MRESOLVER-540] TransferResource can reveal more about resource (#474)
Browse files Browse the repository at this point in the history
Provide extra information about the resource being transferred.

---

https://issues.apache.org/jira/browse/MRESOLVER-540
  • Loading branch information
cstamas authored Apr 18, 2024
1 parent 99c9c06 commit 8b0d417
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public final class TransferResource {

private final String resourceName;

private final Object resource;

private final Path path;

private final long startTime;
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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 {
Expand All @@ -104,10 +112,9 @@ public TransferResource(
}

this.path = path;

this.resource = resource;
this.trace = trace;

startTime = System.currentTimeMillis();
this.startTime = System.currentTimeMillis();
}

/**
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -368,8 +370,28 @@ private static <T> Collection<T> safe(Collection<T> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
}

Expand Down

0 comments on commit 8b0d417

Please sign in to comment.