Skip to content

Commit

Permalink
Move getOnDownload to MavenExecutionContextView and rename it getOnDo…
Browse files Browse the repository at this point in the history
…wnloaded
  • Loading branch information
bryceatmoderne committed Oct 10, 2024
1 parent 1ed030f commit 66cbbc4
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import org.jspecify.annotations.Nullable;
import org.openrewrite.scheduling.RecipeRunCycle;

import java.net.URI;
import java.time.Duration;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -102,10 +100,6 @@ default void putCurrentRecipe(Recipe recipe) {

BiConsumer<Throwable, ExecutionContext> getOnTimeout();

default @Nullable BiConsumer<URI, Duration> getOnDownload() {
return null;
}

default int getCycle() {
return getCycleDetails().getCycle();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import org.jspecify.annotations.Nullable;

import java.net.URI;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -28,8 +27,6 @@ public class InMemoryExecutionContext implements ExecutionContext {
private final Map<String, Object> messages = new ConcurrentHashMap<>();
private final Consumer<Throwable> onError;
private final BiConsumer<Throwable, ExecutionContext> onTimeout;
@Nullable
private final BiConsumer<URI, Duration> onDownload;

public InMemoryExecutionContext() {
this(
Expand All @@ -47,19 +44,9 @@ public InMemoryExecutionContext(Consumer<Throwable> onError, Duration runTimeout
});
}

public InMemoryExecutionContext(Consumer<Throwable> onError,
Duration runTimeout,
BiConsumer<Throwable, ExecutionContext> onTimeout) {
this(onError, runTimeout, onTimeout, null);
}

public InMemoryExecutionContext(Consumer<Throwable> onError,
Duration runTimeout,
BiConsumer<Throwable, ExecutionContext> onTimeout,
@Nullable BiConsumer<URI, Duration> onDownload) {
public InMemoryExecutionContext(Consumer<Throwable> onError, Duration runTimeout, BiConsumer<Throwable, ExecutionContext> onTimeout) {
this.onError = onError;
this.onTimeout = onTimeout;
this.onDownload = onDownload;
putMessage(ExecutionContext.RUN_TIMEOUT, runTimeout);
}

Expand Down Expand Up @@ -93,10 +80,4 @@ public Consumer<Throwable> getOnError() {
public BiConsumer<Throwable, ExecutionContext> getOnTimeout() {
return onTimeout;
}

@Override
@Nullable
public BiConsumer<URI, Duration> getOnDownload() {
return onDownload;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;

import java.net.URI;
import java.time.Duration;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

Expand Down Expand Up @@ -66,10 +64,4 @@ public Consumer<Throwable> getOnError() {
public BiConsumer<Throwable, ExecutionContext> getOnTimeout() {
return delegate.getOnTimeout();
}

@Override
@Nullable
public BiConsumer<URI, Duration> getOnDownload() {
return delegate.getOnDownload();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import org.openrewrite.maven.internal.MavenParsingException;
import org.openrewrite.maven.tree.*;

import java.net.URI;
import java.time.Duration;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -48,6 +50,9 @@ public class MavenExecutionContextView extends DelegatingExecutionContext {
private static final String MAVEN_RESOLUTION_LISTENER = "org.openrewrite.maven.resolutionListener";
private static final String MAVEN_RESOLUTION_TIME = "org.openrewrite.maven.resolutionTime";

@Nullable
private BiConsumer<URI, Duration> onDownloaded;

public MavenExecutionContextView(ExecutionContext delegate) {
super(delegate);
}
Expand Down Expand Up @@ -238,6 +243,15 @@ public MavenExecutionContextView setMavenSettings(@Nullable MavenSettings settin
return getMessage(MAVEN_SETTINGS, null);
}

@Nullable
public BiConsumer<URI, Duration> getOnDownloaded() {
return onDownloaded;
}

public void setOnDownloaded(BiConsumer<URI, Duration> onDownloaded) {
this.onDownloaded = onDownloaded;
}

private static List<String> mapActiveProfiles(MavenSettings settings, String... activeProfiles) {
if (settings.getActiveProfiles() == null) {
return Arrays.asList(activeProfiles);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,8 @@ public MavenMetadata downloadMetadata(GroupArtifactVersion gav, @Nullable Resolv
}

long nanos = sample.stop(timer.tags("outcome", "success").register(Metrics.globalRegistry));
if (ctx.getOnDownload() != null) {
ctx.getOnDownload().accept(
if (ctx.getOnDownloaded() != null) {
ctx.getOnDownloaded().accept(
URI.create(attemptedUris.get(attemptedUris.size() - 1)),
Duration.ofNanos(nanos));
}
Expand Down Expand Up @@ -609,8 +609,8 @@ public Pom download(GroupArtifactVersion gav,
ctx.getResolutionListener().downloadSuccess(resolvedGav, containingPom);
sample.stop(timer.tags("outcome", "downloaded").register(Metrics.globalRegistry));
long nanos = sample.stop(timer.tags("outcome", "downloaded").register(Metrics.globalRegistry));
if (ctx.getOnDownload() != null) {
ctx.getOnDownload().accept(uri, Duration.ofNanos(nanos));
if (ctx.getOnDownloaded() != null) {
ctx.getOnDownloaded().accept(uri, Duration.ofNanos(nanos));
}
return pom;
} catch (HttpSenderResponseException e) {
Expand Down

0 comments on commit 66cbbc4

Please sign in to comment.