Skip to content

Commit

Permalink
Introduce two exceptions to distinquish between resolution failures
Browse files Browse the repository at this point in the history
  • Loading branch information
spyrkob committed May 23, 2023
1 parent 265990c commit 0d71006
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -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<ArtifactCoordinate> unresolvedArtifacts,
Set<Repository> attemptedRepositories) {
super(localizedMessage, cause, unresolvedArtifacts, attemptedRepositories);
}

public ArtifactTransferException(String message, Set<ArtifactCoordinate> unresolvedArtifacts, Set<Repository> attemptedRepositories) {
super(message, unresolvedArtifacts, attemptedRepositories);
}
}
25 changes: 14 additions & 11 deletions core/src/main/java/org/wildfly/channel/ChannelSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@ public ChannelSession(List<Channel> 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.
Expand All @@ -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<MavenArtifact> resolveMavenArtifacts(List<ArtifactCoordinate> coordinates) throws UnresolvedMavenArtifactException {
requireNonNull(coordinates);
Expand Down Expand Up @@ -167,9 +170,9 @@ public List<MavenArtifact> resolveMavenArtifacts(List<ArtifactCoordinate> 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);
Expand All @@ -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<MavenArtifact> resolveDirectMavenArtifacts(List<ArtifactCoordinate> coordinates) throws UnresolvedMavenArtifactException {
public List<MavenArtifact> resolveDirectMavenArtifacts(List<ArtifactCoordinate> coordinates) throws ArtifactTransferException {
coordinates.forEach(c -> {
requireNonNull(c.getGroupId());
requireNonNull(c.getArtifactId());
Expand Down Expand Up @@ -217,9 +220,9 @@ public List<MavenArtifact> resolveDirectMavenArtifacts(List<ArtifactCoordinate>
* @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;
}

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

Expand All @@ -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);
}));
Expand Down
20 changes: 20 additions & 0 deletions core/src/main/java/org/wildfly/channel/NoStreamFoundException.java
Original file line number Diff line number Diff line change
@@ -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<ArtifactCoordinate> unresolvedArtifacts,
Set<Repository> attemptedRepositories) {
super(localizedMessage, cause, unresolvedArtifacts, attemptedRepositories);
}

public NoStreamFoundException(String message, Set<ArtifactCoordinate> unresolvedArtifacts, Set<Repository> attemptedRepositories) {
super(message, unresolvedArtifacts, attemptedRepositories);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.util.Set;

public class UnresolvedMavenArtifactException extends RuntimeException {
public abstract class UnresolvedMavenArtifactException extends RuntimeException {

private final Set<ArtifactCoordinate> unresolvedArtifacts;
private final Set<Repository> attemptedRepositories;
Expand All @@ -32,7 +32,7 @@ public UnresolvedMavenArtifactException(String localizedMessage,
this.attemptedRepositories = attemptedRepositories;
}

public <E> UnresolvedMavenArtifactException(String message, Set<ArtifactCoordinate> unresolvedArtifacts, Set<Repository> attemptedRepositories) {
public UnresolvedMavenArtifactException(String message, Set<ArtifactCoordinate> unresolvedArtifacts, Set<Repository> attemptedRepositories) {
super(message);
this.unresolvedArtifacts = unresolvedArtifacts;
this.attemptedRepositories = attemptedRepositories;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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<File> resolveArtifacts(List<ArtifactCoordinate> coordinates) throws UnresolvedMavenArtifactException;
List<File> resolveArtifacts(List<ArtifactCoordinate> coordinates) throws ArtifactTransferException;

/**
* Resolve a list of channel metadata artifacts based on the coordinates.
Expand All @@ -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<URL> resolveChannelMetadata(List<? extends ChannelMetadataCoordinate> manifestCoords) throws UnresolvedMavenArtifactException;
List<URL> resolveChannelMetadata(List<? extends ChannelMetadataCoordinate> manifestCoords) throws ArtifactTransferException;

/**
* Returns the {@code <release>} version according to the repositories' Maven metadata. If multiple repositories
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Channel> channels = List.of(new Channel.Builder()
.setName("root level requiring channel")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -150,7 +152,7 @@ public Set<String> 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);
Expand All @@ -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());
}
Expand Down Expand Up @@ -203,12 +205,12 @@ public List<File> resolveArtifacts(List<ArtifactCoordinate> 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<URL> resolveChannelMetadata(List<? extends ChannelMetadataCoordinate> coords) throws UnresolvedMavenArtifactException {
public List<URL> resolveChannelMetadata(List<? extends ChannelMetadataCoordinate> coords) throws ArtifactTransferException {
requireNonNull(coords);

List<URL> channels = new ArrayList<>();
Expand All @@ -225,7 +227,7 @@ public List<URL> resolveChannelMetadata(List<? extends ChannelMetadataCoordinate
Set<String> versions = getAllVersions(coord.getGroupId(), coord.getArtifactId(), coord.getExtension(), coord.getClassifier());
Optional<String> 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()));
}
Expand All @@ -234,7 +236,7 @@ public List<URL> resolveChannelMetadata(List<? extends ChannelMetadataCoordinate
try {
channels.add(channelArtifact.toURI().toURL());
} catch (MalformedURLException e) {
throw new UnresolvedMavenArtifactException(String.format("Unable to resolve the latest version of channel metadata %s:%s", coord.getGroupId(), coord.getArtifactId()), e,
throw new ArtifactTransferException(String.format("Unable to resolve the latest version of channel metadata %s:%s", coord.getGroupId(), coord.getArtifactId()), e,
singleton(new ArtifactCoordinate(coord.getGroupId(), coord.getArtifactId(),
coord.getExtension(), coord.getClassifier(), coord.getVersion())),
attemptedRepositories());
Expand Down Expand Up @@ -276,15 +278,15 @@ private String findLatestMetadataVersion(List<MetadataResult> 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());
}
})
.filter(m->m.getVersioning() != null)
.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()));
}
Expand Down

0 comments on commit 0d71006

Please sign in to comment.