Skip to content

Commit

Permalink
Refactor RR backends (#57)
Browse files Browse the repository at this point in the history
To allow overriding them explicitly, also introduce Mojo configurations (and project properties support) for them. Also, from now on libYear will not "swallow" IAEx if no RR could be selected, but will error, forcing user to set one.

Tested with:
* mojo config `repositoryVendor` - OK
* project property `toolbox.search.backend.type` - OK
* CLI `-Dtoolbox.search.backend.type=foo` - OK
* project property `toolbox.search.backend.type` is overridden by CLI property `toolbox.search.backend.type` - OK
  • Loading branch information
cstamas committed Jun 21, 2024
1 parent c2448a5 commit f2458ed
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,19 @@ boolean exists(
boolean javadoc,
boolean signature,
boolean allRequired,
String repositoryVendor,
Output output)
throws IOException;

boolean identify(RemoteRepository remoteRepository, String target, Output output) throws IOException;

boolean list(RemoteRepository remoteRepository, String gavoid, Output output) throws IOException;
boolean list(RemoteRepository remoteRepository, String gavoid, String repositoryVendor, Output output)
throws IOException;

boolean search(RemoteRepository remoteRepository, String expression, Output output) throws IOException;

boolean verify(RemoteRepository remoteRepository, String gav, String sha1, Output output) throws IOException;
boolean verify(RemoteRepository remoteRepository, String gav, String sha1, String repositoryVendor, Output output)
throws IOException;

// Various

Expand All @@ -221,6 +224,7 @@ boolean libYear(
boolean allowSnapshots,
boolean upToDate,
ArtifactVersionSelector artifactVersionSelector,
String repositoryVendor,
Output output)
throws Exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.apache.maven.search.api.SearchResponse;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.deployment.DeploymentException;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.VersionRangeResolutionException;
import org.eclipse.aether.util.artifact.ArtifactIdUtils;
import org.eclipse.aether.version.Version;
Expand Down Expand Up @@ -72,9 +71,18 @@ public static LibYearSink libYear(
boolean quiet,
boolean allowSnapshots,
boolean upToDate,
BiFunction<Artifact, List<Version>, String> versionSelector) {
BiFunction<Artifact, List<Version>, String> versionSelector,
List<SearchBackend> searchBackends) {
return new LibYearSink(
output, context, toolboxResolver, toolboxSearchApi, quiet, allowSnapshots, upToDate, versionSelector);
output,
context,
toolboxResolver,
toolboxSearchApi,
quiet,
allowSnapshots,
upToDate,
versionSelector,
searchBackends);
}

private final Output output;
Expand All @@ -98,7 +106,8 @@ private LibYearSink(
boolean quiet,
boolean allowSnapshots,
boolean upToDate,
BiFunction<Artifact, List<Version>, String> versionSelector) {
BiFunction<Artifact, List<Version>, String> versionSelector,
List<SearchBackend> searchBackends) {
this.output = requireNonNull(output, "output");
this.context = requireNonNull(context, "context");
this.toolboxResolver = requireNonNull(toolboxResolver, "toolboxResolver");
Expand All @@ -109,16 +118,7 @@ private LibYearSink(
this.versionSelector = requireNonNull(versionSelector);
this.now = Instant.now().atZone(ZoneId.systemDefault()).toLocalDate();
this.artifacts = new CopyOnWriteArraySet<>();

this.searchBackends = new ArrayList<>();
for (RemoteRepository remoteRepository : context.remoteRepositories()) {
try {
this.searchBackends.add(toolboxSearchApi.getRemoteRepositoryBackend(
context.repositorySystemSession(), remoteRepository));
} catch (IllegalArgumentException e) {
// most likely cannot figure out what extractor to use; ignore
}
}
this.searchBackends = requireNonNull(searchBackends);
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,12 +682,13 @@ public boolean exists(
boolean javadoc,
boolean signature,
boolean allRequired,
String repositoryVendor,
Output output)
throws IOException {
ArrayList<Artifact> missingOnes = new ArrayList<>();
ArrayList<Artifact> existingOnes = new ArrayList<>();
try (SearchBackend backend =
toolboxSearchApi.getRemoteRepositoryBackend(context.repositorySystemSession(), remoteRepository)) {
try (SearchBackend backend = toolboxSearchApi.getRemoteRepositoryBackend(
context.repositorySystemSession(), remoteRepository, repositoryVendor)) {
Artifact artifact = new DefaultArtifact(gav);
boolean exists = toolboxSearchApi.exists(backend, artifact);
if (!exists) {
Expand Down Expand Up @@ -786,9 +787,10 @@ public boolean identify(RemoteRepository remoteRepository, String target, Output
}

@Override
public boolean list(RemoteRepository remoteRepository, String gavoid, Output output) throws IOException {
try (SearchBackend backend =
toolboxSearchApi.getRemoteRepositoryBackend(context.repositorySystemSession(), remoteRepository)) {
public boolean list(RemoteRepository remoteRepository, String gavoid, String repositoryVendor, Output output)
throws IOException {
try (SearchBackend backend = toolboxSearchApi.getRemoteRepositoryBackend(
context.repositorySystemSession(), remoteRepository, repositoryVendor)) {
String[] elements = gavoid.split(":");
if (elements.length < 1 || elements.length > 3) {
throw new IllegalArgumentException("Invalid gavoid");
Expand Down Expand Up @@ -852,10 +854,11 @@ public boolean search(RemoteRepository remoteRepository, String expression, Outp
}

@Override
public boolean verify(RemoteRepository remoteRepository, String gav, String sha1, Output output)
public boolean verify(
RemoteRepository remoteRepository, String gav, String sha1, String repositoryVendor, Output output)
throws IOException {
try (SearchBackend backend =
toolboxSearchApi.getRemoteRepositoryBackend(context.repositorySystemSession(), remoteRepository)) {
try (SearchBackend backend = toolboxSearchApi.getRemoteRepositoryBackend(
context.repositorySystemSession(), remoteRepository, repositoryVendor)) {
Artifact artifact = new DefaultArtifact(gav);
boolean verified = toolboxSearchApi.verify(backend, new DefaultArtifact(gav), sha1);
output.normal("Artifact SHA1({})={}: {}", artifact, sha1, verified ? "MATCHED" : "NOT MATCHED");
Expand All @@ -874,8 +877,15 @@ public boolean libYear(
boolean allowSnapshots,
boolean upToDate,
ArtifactVersionSelector artifactVersionSelector,
String repositoryVendor,
Output output)
throws Exception {
ArrayList<SearchBackend> searchBackends = new ArrayList<>();
for (RemoteRepository remoteRepository : context.remoteRepositories()) {
searchBackends.add(toolboxSearchApi.getRemoteRepositoryBackend(
context.repositorySystemSession(), remoteRepository, repositoryVendor));
}

try (ArtifactSink sink = LibYearSink.libYear(
output,
context,
Expand All @@ -884,7 +894,8 @@ public boolean libYear(
quiet,
allowSnapshots,
upToDate,
artifactVersionSelector)) {
artifactVersionSelector,
searchBackends)) {
for (ResolutionRoot resolutionRoot : resolutionRoots) {
try {
ResolutionRoot root = toolboxResolver.loadRoot(resolutionRoot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,39 +40,58 @@ public class ToolboxSearchApiImpl {

public ToolboxSearchApiImpl() {}

/**
* Creates RR search backend: it may be explicitly selected by using parameter {@code repositoryVendor}. The logic:
* <ul>
* <li>if {@code repositoryVendor} is non-{@code null}, use it</li>
* <li>check {@code toolbox.search.backend.type} session config property</li>
* <li>check {@link RemoteRepository#getContentType()}</li>
* <li>finally, if none above, try some "heuristics"</li>
* </ul>
* This is all about the Search API RR backend extractor selection. Note: in some use cases "extractor" is not
* used, so forcing any value in those cases is perfectly fine.
*
* @param session The session, must not be {@code null}.
* @param remoteRepository The repository to create RR backend for, must not be {@code null}.
* @param repositoryVendor The "override" type, or may be {@code null}, in which case logic above will be applied.
* Basically determines extractor to be used with it.
*/
public SearchBackend getRemoteRepositoryBackend(
RepositorySystemSession session, RemoteRepository remoteRepository) {
String type = (String) session.getConfigProperties().get("toolbox.search.backend.type");
if (type == null) {
if ("central".equals(remoteRepository.getContentType())) {
type = "central";
} else if ("nx2".equals(remoteRepository.getContentType())) {
type = "nx2";
} else {
// Some heuristics trying to figure out (probably not ideal)
if (ContextOverrides.CENTRAL.getId().equals(remoteRepository.getId())
&& ContextOverrides.CENTRAL.getUrl().equals(remoteRepository.getUrl())) {
type = "central";
} else if (remoteRepository.getUrl().startsWith("https://repo.maven.apache.org/maven2")
|| remoteRepository.getUrl().startsWith("https://repo1.maven.org/maven2/")) {
type = "central";
} else if (remoteRepository.getUrl().startsWith("https://repository.apache.org/")
|| remoteRepository.getUrl().startsWith("https://oss.sonatype.org/")
|| remoteRepository.getUrl().startsWith("https://s01.oss.sonatype.org/")) {
type = "nx2";
} else if (remoteRepository.getUrl().contains("/content/groups/")
|| remoteRepository.getUrl().contains("/content/repositories/")) {
type = "nx2";
RepositorySystemSession session, RemoteRepository remoteRepository, String repositoryVendor) {
if (repositoryVendor == null) {
repositoryVendor = (String) session.getConfigProperties().get("toolbox.search.backend.type");
if (repositoryVendor == null) {
if ("central".equals(remoteRepository.getContentType())) {
repositoryVendor = "central";
} else if ("nx2".equals(remoteRepository.getContentType())) {
repositoryVendor = "nx2";
} else {
// Some heuristics trying to figure out (probably not ideal)
if (ContextOverrides.CENTRAL.getId().equals(remoteRepository.getId())
&& ContextOverrides.CENTRAL.getUrl().equals(remoteRepository.getUrl())) {
repositoryVendor = "central";
} else if (remoteRepository.getUrl().startsWith("https://repo.maven.apache.org/maven2")
|| remoteRepository.getUrl().startsWith("https://repo1.maven.org/maven2/")) {
repositoryVendor = "central";
} else if (remoteRepository.getUrl().startsWith("https://repository.apache.org/")
|| remoteRepository.getUrl().startsWith("https://oss.sonatype.org/")
|| remoteRepository.getUrl().startsWith("https://s01.oss.sonatype.org/")) {
repositoryVendor = "nx2";
} else if (remoteRepository.getUrl().contains("/content/groups/")
|| remoteRepository.getUrl().contains("/content/repositories/")) {
repositoryVendor = "nx2";
}
}
}
}
final ResponseExtractor extractor;
if ("central".equalsIgnoreCase(type)) {
if ("central".equalsIgnoreCase(repositoryVendor)) {
extractor = new MavenCentralResponseExtractor();
} else if ("nx2".equalsIgnoreCase(type)) {
} else if ("nx2".equalsIgnoreCase(repositoryVendor)) {
extractor = new Nx2ResponseExtractor();
} else {
throw new IllegalArgumentException("Unsupported extractor");
throw new IllegalArgumentException(
"Unsupported Search RR extractor: '" + repositoryVendor + "'; (supported are 'central', 'nx2')");
}
return RemoteRepositorySearchBackendFactory.create(
remoteRepository.getId() + "-rr",
Expand All @@ -84,6 +103,9 @@ public SearchBackend getRemoteRepositoryBackend(
extractor);
}

/**
* Creates SMO search backend: it works only for Maven Central, obviously.
*/
public SearchBackend getSmoBackend(RepositorySystemSession session, RemoteRepository remoteRepository) {
if (!ContextOverrides.CENTRAL.getId().equals(remoteRepository.getId())) {
throw new IllegalArgumentException("The SMO service is offered for Central only");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public abstract class GavSearchMojoSupport extends GavMojoSupport {
names = {"--repositoryVendor"},
description = "The vendor of the remote repository")
@Parameter(property = "repositoryVendor")
private String repositoryVendor;
protected String repositoryVendor;

protected RemoteRepository getRemoteRepository(ToolboxCommando toolboxCommando) {
RemoteRepository remoteRepository =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.eclipse.aether.artifact.ArtifactTypeRegistry;
import org.eclipse.aether.artifact.DefaultArtifact;
Expand All @@ -35,6 +36,20 @@ public abstract class MPMojoSupport extends MojoSupport {
@Component
protected ArtifactHandlerManager artifactHandlerManager;

/**
* The repository vendor to use for Search RR backend ("central", "nx2" or any other extractor). If empty,
* heuristics will be applied to figure out.
*/
@Parameter(property = "toolbox.search.backend.type")
private String repositoryVendor;

protected String getRepositoryVendor() {
if (repositoryVendor != null) {
return repositoryVendor;
}
return mavenProject.getProperties().getProperty("toolbox.search.backend.type");
}

protected List<Dependency> toDependencies(List<org.apache.maven.model.Dependency> dependencies) {
ArtifactTypeRegistry artifactTypeRegistry =
mavenSession.getRepositorySession().getArtifactTypeRegistry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.eclipse.aether.resolution.ArtifactDescriptorException;

/**
* Support class for "project aware" Mojos.
* Support class for "project aware" Mojos dealing with plugins.
*/
public abstract class MPPluginMojoSupport extends MPMojoSupport {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ public class GavExistsMojo extends GavSearchMojoSupport {
@Override
protected boolean doExecute(Output output, ToolboxCommando toolboxCommando) throws IOException {
return toolboxCommando.exists(
getRemoteRepository(toolboxCommando), gav, pom, sources, javadoc, signature, allRequired, output);
getRemoteRepository(toolboxCommando), gav, pom, sources, javadoc, signature, allRequired, null, output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ protected boolean doExecute(Output output, ToolboxCommando toolboxCommando) thro
allowSnapshots,
upToDate,
toolboxCommando.parseArtifactVersionSelectorSpec(artifactVersionSelectorSpec),
repositoryVendor,
output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public class GavListMojo extends GavSearchMojoSupport {

@Override
protected boolean doExecute(Output output, ToolboxCommando toolboxCommando) throws Exception {
return toolboxCommando.list(getRemoteRepository(toolboxCommando), gavoid, output);
return toolboxCommando.list(getRemoteRepository(toolboxCommando), gavoid, null, output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public class GavVerifyMojo extends GavSearchMojoSupport {

@Override
protected boolean doExecute(Output output, ToolboxCommando toolboxCommando) throws IOException {
return toolboxCommando.verify(getRemoteRepository(toolboxCommando), gav, sha1, output);
return toolboxCommando.verify(getRemoteRepository(toolboxCommando), gav, sha1, null, output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ protected boolean doExecute(Output output, ToolboxCommando toolboxCommando) thro
allowSnapshots,
upToDate,
toolboxCommando.parseArtifactVersionSelectorSpec(artifactVersionSelectorSpec),
getRepositoryVendor(),
output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ protected boolean doExecute(Output output, ToolboxCommando toolboxCommando) thro
allowSnapshots,
upToDate,
toolboxCommando.parseArtifactVersionSelectorSpec(artifactVersionSelectorSpec),
getRepositoryVendor(),
output);
}
}

0 comments on commit f2458ed

Please sign in to comment.