From 0d71006489c7ca2f91af058b2d390eb6f79fda76 Mon Sep 17 00:00:00 2001 From: Bartosz Spyrko-Smietanko Date: Tue, 23 May 2023 10:24:30 +0100 Subject: [PATCH] Introduce two exceptions to distinquish between resolution failures --- .../channel/ArtifactTransferException.java | 20 +++++++++++++++ .../org/wildfly/channel/ChannelSession.java | 25 +++++++++++-------- .../channel/NoStreamFoundException.java | 20 +++++++++++++++ .../UnresolvedMavenArtifactException.java | 4 +-- .../channel/spi/MavenVersionsResolver.java | 13 +++++----- .../channel/ChannelSessionInitTestCase.java | 2 +- .../channel/maven/VersionResolverFactory.java | 18 +++++++------ 7 files changed, 74 insertions(+), 28 deletions(-) create mode 100644 core/src/main/java/org/wildfly/channel/ArtifactTransferException.java create mode 100644 core/src/main/java/org/wildfly/channel/NoStreamFoundException.java diff --git a/core/src/main/java/org/wildfly/channel/ArtifactTransferException.java b/core/src/main/java/org/wildfly/channel/ArtifactTransferException.java new file mode 100644 index 00000000..5f787c96 --- /dev/null +++ b/core/src/main/java/org/wildfly/channel/ArtifactTransferException.java @@ -0,0 +1,20 @@ +package org.wildfly.channel; + +import java.util.Set; + +/** + * Thrown in case of an error during downloading of one or more of required artifacts. + */ +public class ArtifactTransferException extends UnresolvedMavenArtifactException { + + public ArtifactTransferException(String localizedMessage, + Throwable cause, + Set unresolvedArtifacts, + Set attemptedRepositories) { + super(localizedMessage, cause, unresolvedArtifacts, attemptedRepositories); + } + + public ArtifactTransferException(String message, Set unresolvedArtifacts, Set attemptedRepositories) { + super(message, unresolvedArtifacts, attemptedRepositories); + } +} diff --git a/core/src/main/java/org/wildfly/channel/ChannelSession.java b/core/src/main/java/org/wildfly/channel/ChannelSession.java index 4e7d747e..b03a627b 100644 --- a/core/src/main/java/org/wildfly/channel/ChannelSession.java +++ b/core/src/main/java/org/wildfly/channel/ChannelSession.java @@ -105,10 +105,12 @@ public ChannelSession(List channelDefinitions, MavenVersionsResolver.Fa * @param baseVersion - can be null. The base version is required when the stream for the component specifies multiple versions and needs the base version to * determine the appropriate version to resolve. * @return the Maven Artifact (with a file corresponding to the artifact). - * @throws UnresolvedMavenArtifactException if the latest version can not be resolved or the artifact itself can not be resolved. + * @throws NoStreamFoundException if none of the channels in the {@code ChannelSession} provides the artifact + * @throws ArtifactTransferException if the artifact is provided by the {@code ChannelSession}, but the resolution failed * */ - public MavenArtifact resolveMavenArtifact(String groupId, String artifactId, String extension, String classifier, String baseVersion) throws UnresolvedMavenArtifactException { + public MavenArtifact resolveMavenArtifact(String groupId, String artifactId, String extension, String classifier, String baseVersion) + throws NoStreamFoundException, ArtifactTransferException { requireNonNull(groupId); requireNonNull(artifactId); // baseVersion is not used at the moment but will provide essential to support advanced use cases to determine multiple streams of the same Maven component. @@ -134,7 +136,8 @@ public MavenArtifact resolveMavenArtifact(String groupId, String artifactId, Str * * @param coordinates list of ArtifactCoordinates to resolve * @return a list of resolved MavenArtifacts with resolved versions asnd files - * @throws UnresolvedMavenArtifactException + * @throws NoStreamFoundException if one or more of the artifact is not provided by any of the channels in the {@code ChannelSession} + * @throws ArtifactTransferException if one or more of the artifacts is provided by the {@code ChannelSession}, but the resolution failed */ public List resolveMavenArtifacts(List coordinates) throws UnresolvedMavenArtifactException { requireNonNull(coordinates); @@ -167,9 +170,9 @@ public List resolveMavenArtifacts(List coordi * @param classifier - can be null * @param version - required * @return the Maven Artifact (with a file corresponding to the artifact). - * @throws UnresolvedMavenArtifactException if the artifact can not be resolved + * @throws ArtifactTransferException if the artifact can not be resolved */ - public MavenArtifact resolveDirectMavenArtifact(String groupId, String artifactId, String extension, String classifier, String version) throws UnresolvedMavenArtifactException { + public MavenArtifact resolveDirectMavenArtifact(String groupId, String artifactId, String extension, String classifier, String version) throws ArtifactTransferException { requireNonNull(groupId); requireNonNull(artifactId); requireNonNull(version); @@ -186,9 +189,9 @@ public MavenArtifact resolveDirectMavenArtifact(String groupId, String artifactI * * @param coordinates - list of ArtifactCoordinates to check * @return the Maven Artifact (with a file corresponding to the artifact). - * @throws UnresolvedMavenArtifactException if the artifact can not be resolved + * @throws ArtifactTransferException if the artifact can not be resolved */ - public List resolveDirectMavenArtifacts(List coordinates) throws UnresolvedMavenArtifactException { + public List resolveDirectMavenArtifacts(List coordinates) throws ArtifactTransferException { coordinates.forEach(c -> { requireNonNull(c.getGroupId()); requireNonNull(c.getArtifactId()); @@ -217,9 +220,9 @@ public List resolveDirectMavenArtifacts(List * @param baseVersion - can be null. The base version is required when the stream for the component specifies multiple versions and needs the base version to * determine the appropriate version to resolve. * @return the latest version if a Maven artifact - * @throws UnresolvedMavenArtifactException if the latest version cannot be established + * @throws NoStreamFoundException if the latest version cannot be established */ - public String findLatestMavenArtifactVersion(String groupId, String artifactId, String extension, String classifier, String baseVersion) throws UnresolvedMavenArtifactException { + public String findLatestMavenArtifactVersion(String groupId, String artifactId, String extension, String classifier, String baseVersion) throws NoStreamFoundException { return findChannelWithLatestVersion(groupId, artifactId, extension, classifier, baseVersion).version; } @@ -250,7 +253,7 @@ private void validateNoDuplicatedManifests() { } } - private ChannelImpl.ResolveLatestVersionResult findChannelWithLatestVersion(String groupId, String artifactId, String extension, String classifier, String baseVersion) throws UnresolvedMavenArtifactException { + private ChannelImpl.ResolveLatestVersionResult findChannelWithLatestVersion(String groupId, String artifactId, String extension, String classifier, String baseVersion) throws NoStreamFoundException { requireNonNull(groupId); requireNonNull(artifactId); @@ -270,7 +273,7 @@ private ChannelImpl.ResolveLatestVersionResult findChannelWithLatestVersion(Stri .map(ChannelImpl::getChannelDefinition) .flatMap(d -> d.getRepositories().stream()) .collect(Collectors.toSet()); - throw new UnresolvedMavenArtifactException( + throw new NoStreamFoundException( String.format("Can not resolve latest Maven artifact (no stream found) : %s:%s:%s:%s", groupId, artifactId, extension, classifier), Collections.singleton(coord), repositories); })); diff --git a/core/src/main/java/org/wildfly/channel/NoStreamFoundException.java b/core/src/main/java/org/wildfly/channel/NoStreamFoundException.java new file mode 100644 index 00000000..32dca64e --- /dev/null +++ b/core/src/main/java/org/wildfly/channel/NoStreamFoundException.java @@ -0,0 +1,20 @@ +package org.wildfly.channel; + +import java.util.Set; + +/** + * Thrown if one or more of required artifacts are not found in specified Channels + */ +public class NoStreamFoundException extends UnresolvedMavenArtifactException { + + public NoStreamFoundException(String localizedMessage, + Throwable cause, + Set unresolvedArtifacts, + Set attemptedRepositories) { + super(localizedMessage, cause, unresolvedArtifacts, attemptedRepositories); + } + + public NoStreamFoundException(String message, Set unresolvedArtifacts, Set attemptedRepositories) { + super(message, unresolvedArtifacts, attemptedRepositories); + } +} diff --git a/core/src/main/java/org/wildfly/channel/UnresolvedMavenArtifactException.java b/core/src/main/java/org/wildfly/channel/UnresolvedMavenArtifactException.java index f552df8d..a5365978 100644 --- a/core/src/main/java/org/wildfly/channel/UnresolvedMavenArtifactException.java +++ b/core/src/main/java/org/wildfly/channel/UnresolvedMavenArtifactException.java @@ -18,7 +18,7 @@ import java.util.Set; -public class UnresolvedMavenArtifactException extends RuntimeException { +public abstract class UnresolvedMavenArtifactException extends RuntimeException { private final Set unresolvedArtifacts; private final Set attemptedRepositories; @@ -32,7 +32,7 @@ public UnresolvedMavenArtifactException(String localizedMessage, this.attemptedRepositories = attemptedRepositories; } - public UnresolvedMavenArtifactException(String message, Set unresolvedArtifacts, Set attemptedRepositories) { + public UnresolvedMavenArtifactException(String message, Set unresolvedArtifacts, Set attemptedRepositories) { super(message); this.unresolvedArtifacts = unresolvedArtifacts; this.attemptedRepositories = attemptedRepositories; diff --git a/core/src/main/java/org/wildfly/channel/spi/MavenVersionsResolver.java b/core/src/main/java/org/wildfly/channel/spi/MavenVersionsResolver.java index ab3377c7..b82889b7 100644 --- a/core/src/main/java/org/wildfly/channel/spi/MavenVersionsResolver.java +++ b/core/src/main/java/org/wildfly/channel/spi/MavenVersionsResolver.java @@ -24,6 +24,7 @@ import java.util.Set; import org.wildfly.channel.ArtifactCoordinate; +import org.wildfly.channel.ArtifactTransferException; import org.wildfly.channel.ChannelMetadataCoordinate; import org.wildfly.channel.Repository; import org.wildfly.channel.UnresolvedMavenArtifactException; @@ -57,9 +58,9 @@ public interface MavenVersionsResolver extends Closeable { * * @return a File representing the resolved Maven artifact. * - * @throws UnresolvedMavenArtifactException if the artifact can not be resolved. + * @throws ArtifactTransferException if the artifact can not be resolved. */ - File resolveArtifact(String groupId, String artifactId, String extension, String classifier, String version) throws UnresolvedMavenArtifactException; + File resolveArtifact(String groupId, String artifactId, String extension, String classifier, String version) throws ArtifactTransferException; /** * Resolve a list of maven artifacts based on the full coordinates. @@ -70,9 +71,9 @@ public interface MavenVersionsResolver extends Closeable { * * @return a list of File representing the resolved Maven artifact. * - * @throws UnresolvedMavenArtifactException if any artifacts can not be resolved. + * @throws ArtifactTransferException if any artifacts can not be resolved. */ - List resolveArtifacts(List coordinates) throws UnresolvedMavenArtifactException; + List resolveArtifacts(List coordinates) throws ArtifactTransferException; /** * Resolve a list of channel metadata artifacts based on the coordinates. @@ -88,9 +89,9 @@ public interface MavenVersionsResolver extends Closeable { * * @return a list of URLs to the metadata files * - * @throws UnresolvedMavenArtifactException if any artifacts can not be resolved. + * @throws ArtifactTransferException if any artifacts can not be resolved. */ - List resolveChannelMetadata(List manifestCoords) throws UnresolvedMavenArtifactException; + List resolveChannelMetadata(List manifestCoords) throws ArtifactTransferException; /** * Returns the {@code } version according to the repositories' Maven metadata. If multiple repositories diff --git a/core/src/test/java/org/wildfly/channel/ChannelSessionInitTestCase.java b/core/src/test/java/org/wildfly/channel/ChannelSessionInitTestCase.java index ba4ab8a7..713ef385 100644 --- a/core/src/test/java/org/wildfly/channel/ChannelSessionInitTestCase.java +++ b/core/src/test/java/org/wildfly/channel/ChannelSessionInitTestCase.java @@ -122,7 +122,7 @@ public void throwExceptionRequiredChannelIdNotAvailableAndNotAbleToResolve() thr mockManifest(resolver, baseManifest, "test.channels:base-manifest:1.0.0"); when(resolver.resolveChannelMetadata(List.of(new ChannelManifestCoordinate("test.channels", "i-dont-exist", "1.0.0")))) - .thenThrow(UnresolvedMavenArtifactException.class); + .thenThrow(ArtifactTransferException.class); List channels = List.of(new Channel.Builder() .setName("root level requiring channel") diff --git a/maven-resolver/src/main/java/org/wildfly/channel/maven/VersionResolverFactory.java b/maven-resolver/src/main/java/org/wildfly/channel/maven/VersionResolverFactory.java index a3e16b25..da6ee57b 100644 --- a/maven-resolver/src/main/java/org/wildfly/channel/maven/VersionResolverFactory.java +++ b/maven-resolver/src/main/java/org/wildfly/channel/maven/VersionResolverFactory.java @@ -56,10 +56,12 @@ import org.eclipse.aether.resolution.VersionRangeResult; import org.eclipse.aether.version.Version; import org.wildfly.channel.ArtifactCoordinate; +import org.wildfly.channel.ArtifactTransferException; import org.wildfly.channel.Channel; import org.wildfly.channel.ChannelMapper; import org.wildfly.channel.ChannelMetadataCoordinate; import org.wildfly.channel.Repository; +import org.wildfly.channel.NoStreamFoundException; import org.wildfly.channel.UnresolvedMavenArtifactException; import org.wildfly.channel.spi.MavenVersionsResolver; import org.wildfly.channel.version.VersionMatcher; @@ -150,7 +152,7 @@ public Set getAllVersions(String groupId, String artifactId, String exte } @Override - public File resolveArtifact(String groupId, String artifactId, String extension, String classifier, String version) throws UnresolvedMavenArtifactException { + public File resolveArtifact(String groupId, String artifactId, String extension, String classifier, String version) throws ArtifactTransferException { requireNonNull(groupId); requireNonNull(artifactId); requireNonNull(version); @@ -167,7 +169,7 @@ public File resolveArtifact(String groupId, String artifactId, String extension, try { result = system.resolveArtifact(session, request); } catch (ArtifactResolutionException ex) { - throw new UnresolvedMavenArtifactException(ex.getLocalizedMessage(), ex, + throw new ArtifactTransferException(ex.getLocalizedMessage(), ex, singleton(new ArtifactCoordinate(groupId, artifactId, extension, classifier, version)), attemptedRepositories()); } @@ -203,12 +205,12 @@ public List resolveArtifacts(List coordinates) throws .map(res->res.getRequest().getArtifact()) .map(a->new ArtifactCoordinate(a.getGroupId(), a.getArtifactId(), a.getExtension(), a.getClassifier(), a.getVersion())) .collect(Collectors.toSet()); - throw new UnresolvedMavenArtifactException(ex.getLocalizedMessage(), ex, failed, attemptedRepositories()); + throw new ArtifactTransferException(ex.getLocalizedMessage(), ex, failed, attemptedRepositories()); } } @Override - public List resolveChannelMetadata(List coords) throws UnresolvedMavenArtifactException { + public List resolveChannelMetadata(List coords) throws ArtifactTransferException { requireNonNull(coords); List channels = new ArrayList<>(); @@ -225,7 +227,7 @@ public List resolveChannelMetadata(List versions = getAllVersions(coord.getGroupId(), coord.getArtifactId(), coord.getExtension(), coord.getClassifier()); Optional latestVersion = VersionMatcher.getLatestVersion(versions); version = latestVersion.orElseThrow(() -> - new UnresolvedMavenArtifactException(String.format("Unable to resolve the latest version of channel metadata %s:%s", coord.getGroupId(), coord.getArtifactId()), + new ArtifactTransferException(String.format("Unable to resolve the latest version of channel metadata %s:%s", coord.getGroupId(), coord.getArtifactId()), singleton(new ArtifactCoordinate(coord.getGroupId(), coord.getArtifactId(), coord.getExtension(), coord.getClassifier(), "")), attemptedRepositories())); } @@ -234,7 +236,7 @@ public List resolveChannelMetadata(List metadataResults, return reader.read(new FileReader(f)); } catch (IOException | XmlPullParserException e) { final ArtifactCoordinate requestedArtifact = new ArtifactCoordinate(groupId, artifactId, null, null, "*"); - throw new UnresolvedMavenArtifactException(e.getLocalizedMessage(), e, singleton(requestedArtifact), + throw new ArtifactTransferException(e.getLocalizedMessage(), e, singleton(requestedArtifact), attemptedRepositories()); } }) @@ -284,7 +286,7 @@ private String findLatestMetadataVersion(List metadataResults, .map(getVersion) .filter(s->s!=null&&!s.isEmpty()) .max(COMPARATOR) - .orElseThrow(()-> new UnresolvedMavenArtifactException("No versioning information found in metadata.", + .orElseThrow(()-> new NoStreamFoundException("No versioning information found in metadata.", singleton(new ArtifactCoordinate(groupId, artifactId, null, null, "*")), attemptedRepositories())); }