Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Issue #80, OpenShift dry-run mode. Fix channels handling, cleanup default stability #81

Merged
merged 4 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion arquillian-plugin-scanner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.wildfly.glow</groupId>
<artifactId>wildfly-glow-parent</artifactId>
<version>1.0.7.Final-SNAPSHOT</version>
<version>1.1.0.Final-SNAPSHOT</version>
</parent>
<artifactId>wildfly-glow-arquillian-plugin-scanner</artifactId>

Expand Down
2 changes: 1 addition & 1 deletion arquillian-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.wildfly.glow</groupId>
<artifactId>wildfly-glow-parent</artifactId>
<version>1.0.7.Final-SNAPSHOT</version>
<version>1.1.0.Final-SNAPSHOT</version>
</parent>
<artifactId>wildfly-glow-arquillian-plugin</artifactId>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
/*
* Copyright 2024 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wildfly.glow.plugin.arquillian;

import static org.wildfly.channel.maven.VersionResolverFactory.DEFAULT_REPOSITORY_MAPPER;

import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.regex.Pattern;

import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.VersionRangeRequest;
import org.eclipse.aether.resolution.VersionRangeResolutionException;
import org.eclipse.aether.resolution.VersionRangeResult;
import org.jboss.galleon.api.MavenStreamResolver;
import org.jboss.galleon.universe.maven.MavenArtifact;
import org.jboss.galleon.universe.maven.MavenUniverseException;
import org.jboss.galleon.universe.maven.repo.MavenRepoManager;
import org.wildfly.channel.ArtifactTransferException;
import org.wildfly.channel.Channel;
import org.wildfly.channel.ChannelSession;
import org.wildfly.channel.NoStreamFoundException;
import org.wildfly.channel.Repository;
import org.wildfly.channel.UnresolvedMavenArtifactException;
import org.wildfly.channel.VersionResult;
import org.wildfly.channel.maven.VersionResolverFactory;
import org.wildfly.channel.spi.ChannelResolvable;

public class ChannelMavenArtifactRepositoryManager implements MavenRepoManager, ChannelResolvable, MavenStreamResolver {

private final ChannelSession channelSession;
private final RepositorySystem system;
private final DefaultRepositorySystemSession session;
private final List<RemoteRepository> repositories;

public ChannelMavenArtifactRepositoryManager(List<Channel> channels,
RepositorySystem system,
RepositorySystemSession contextSession,
List<RemoteRepository> repositories)
throws Exception {
session = MavenRepositorySystemUtils.newSession();
this.repositories = repositories;
session.setLocalRepositoryManager(contextSession.getLocalRepositoryManager());
Map<String, RemoteRepository> mapping = new HashMap<>();
for (RemoteRepository r : repositories) {
mapping.put(r.getId(), r);
}
Function<Repository, RemoteRepository> mapper = r -> {
RemoteRepository rep = mapping.get(r.getId());
if (rep == null) {
rep = DEFAULT_REPOSITORY_MAPPER.apply(r);
}
return rep;
};
VersionResolverFactory factory = new VersionResolverFactory(system, session, mapper);
channelSession = new ChannelSession(channels, factory);
this.system = system;
}

public ChannelSession getChannelSession() {
return channelSession;
}

@Override
public void resolve(MavenArtifact artifact) throws MavenUniverseException {
try {
resolveFromChannels(artifact);
} catch (ArtifactTransferException ex) {
throw new MavenUniverseException(ex.getLocalizedMessage(), ex);
} catch (NoStreamFoundException ex) {
// unable to resolve the artifact through the channel.
// if the version is defined, let's resolve it directly
if (artifact.getVersion() == null || artifact.getVersion().isEmpty()) {
throw new MavenUniverseException(ex.getLocalizedMessage(), ex);
}
try {
org.wildfly.channel.MavenArtifact mavenArtifact = channelSession.resolveDirectMavenArtifact(
artifact.getGroupId(), artifact.getArtifactId(), artifact.getExtension(), artifact.getClassifier(),
artifact.getVersion());
artifact.setPath(mavenArtifact.getFile().toPath());
} catch (UnresolvedMavenArtifactException e) {
// if the artifact can not be resolved directly either, we abort
throw new MavenUniverseException(e.getLocalizedMessage(), e);
}
}
}

private void resolveFromChannels(MavenArtifact artifact) throws UnresolvedMavenArtifactException {
org.wildfly.channel.MavenArtifact result = channelSession.resolveMavenArtifact(artifact.getGroupId(),
artifact.getArtifactId(), artifact.getExtension(), artifact.getClassifier(), artifact.getVersion());
artifact.setVersion(result.getVersion());
artifact.setPath(result.getFile().toPath());
}

@Override
public void resolveLatestVersion(MavenArtifact artifact) throws MavenUniverseException {
throw new MavenUniverseException("Channel resolution can't be applied to Galleon universe");
}

@Override
public boolean isResolved(MavenArtifact artifact) throws MavenUniverseException {
throw new MavenUniverseException("Channel resolution can't be applied to Galleon universe");
}

@Override
public boolean isLatestVersionResolved(MavenArtifact artifact, String lowestQualifier) throws MavenUniverseException {
throw new MavenUniverseException("Channel resolution can't be applied to Galleon universe");
}

@Override
public void resolveLatestVersion(MavenArtifact artifact, String lowestQualifier, Pattern includeVersion,
Pattern excludeVersion) throws MavenUniverseException {
resolveLatestVersion(artifact, null, false);
}

@Override
public void resolveLatestVersion(MavenArtifact artifact, String lowestQualifier, boolean locallyAvailable)
throws MavenUniverseException {
artifact.setVersion(getLatestVersion(artifact));
resolve(artifact);
}

@Override
public String getLatestVersion(MavenArtifact artifact) throws MavenUniverseException {
return getLatestVersion(artifact, null, null, null);
}

@Override
public String getLatestVersion(MavenArtifact artifact, String lowestQualifier) throws MavenUniverseException {
return getLatestVersion(artifact, lowestQualifier, null, null);
}

@Override
public String getLatestVersion(MavenArtifact artifact, String lowestQualifier, Pattern includeVersion,
Pattern excludeVersion) throws MavenUniverseException {
try {
return channelSession.resolveMavenArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getExtension(),
artifact.getClassifier(), null).getVersion();
} catch (UnresolvedMavenArtifactException e) {
VersionRangeResult res = getVersionRange(new DefaultArtifact(artifact.getGroupId(),
artifact.getArtifactId(), artifact.getExtension(), artifact.getVersionRange()));
return res.getHighestVersion().toString();
}
}

@Override
public List<String> getAllVersions(MavenArtifact artifact) throws MavenUniverseException {
throw new MavenUniverseException("Channel resolution can't be applied to Galleon universe");
}

@Override
public List<String> getAllVersions(MavenArtifact artifact, Pattern includeVersion, Pattern excludeVersion)
throws MavenUniverseException {
throw new MavenUniverseException("Channel resolution can't be applied to Galleon universe");
}

@Override
public void install(MavenArtifact artifact, Path path) throws MavenUniverseException {
throw new MavenUniverseException("Channel resolution can't be applied to Galleon universe");
}

@Override
public String getLatestVersion(String groupId, String artifactId, String extension, String classifier, String baseVersion) {
VersionResult res = channelSession.findLatestMavenArtifactVersion(groupId, artifactId, extension, classifier,
baseVersion);
return res.getVersion();
}

private VersionRangeResult getVersionRange(Artifact artifact) throws MavenUniverseException {
VersionRangeRequest rangeRequest = new VersionRangeRequest();
rangeRequest.setArtifact(artifact);
rangeRequest.setRepositories(repositories);
VersionRangeResult rangeResult;
try {
rangeResult = system.resolveVersionRange(session, rangeRequest);
} catch (VersionRangeResolutionException ex) {
throw new MavenUniverseException(ex.getLocalizedMessage(), ex);
}
return rangeResult;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,27 @@ public ConfiguredChannels(List<ChannelConfiguration> channels,
if (channels.isEmpty()) {
throw new MojoExecutionException("No channel specified.");
}
List<Channel> channelDefinitions = new ArrayList<>();
for (ChannelConfiguration channelConfiguration : channels) {
channelDefinitions.add(channelConfiguration.toChannel(repositories));
}
channelSession = buildChannelSession(system, contextSession, repositories, channelDefinitions);
}

ChannelSession getChannelSession() {
return channelSession;
}

public static ChannelSession buildChannelSession(RepositorySystem system,
RepositorySystemSession contextSession,
List<RemoteRepository> repositories,
List<org.wildfly.channel.Channel> channels) {
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
session.setLocalRepositoryManager(contextSession.getLocalRepositoryManager());
session.setOffline(offline);
Map<String, RemoteRepository> mapping = new HashMap<>();
for (RemoteRepository r : repositories) {
mapping.put(r.getId(), r);
}
List<Channel> channelDefinitions = new ArrayList<>();
for (ChannelConfiguration channelConfiguration : channels) {
channelDefinitions.add(channelConfiguration.toChannel(repositories));
}
Function<Repository, RemoteRepository> mapper = r -> {
RemoteRepository rep = mapping.get(r.getId());
if (rep == null) {
Expand All @@ -68,11 +78,6 @@ public ConfiguredChannels(List<ChannelConfiguration> channels,
return rep;
};
VersionResolverFactory factory = new VersionResolverFactory(system, session, mapper);
channelSession = new ChannelSession(channelDefinitions, factory);
return new ChannelSession(channels, factory);
}

ChannelSession getChannelSession() {
return channelSession;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
Expand All @@ -60,8 +59,6 @@
import java.util.Map;
import java.util.Set;
import org.apache.maven.plugin.logging.Log;
import org.wildfly.channel.UnresolvedMavenArtifactException;
import org.wildfly.channel.VersionResult;
import org.jboss.galleon.ProvisioningException;
import org.jboss.galleon.api.GalleonBuilder;
import org.jboss.galleon.api.GalleonFeaturePack;
Expand All @@ -70,10 +67,9 @@
import org.jboss.galleon.api.config.GalleonConfigurationWithLayersBuilder;
import org.jboss.galleon.api.config.GalleonFeaturePackConfig;
import org.jboss.galleon.api.config.GalleonProvisioningConfig;
import org.jboss.galleon.universe.Channel;
import org.jboss.galleon.universe.FeaturePackLocation;
import org.jboss.galleon.universe.UniverseResolver;
import org.jboss.galleon.universe.maven.MavenChannel;
import org.jboss.galleon.universe.maven.repo.MavenRepoManager;
import org.wildfly.channel.Channel;
import org.wildfly.glow.ScanArguments;
import org.wildfly.glow.error.IdentifiedError;
import static org.wildfly.glow.plugin.arquillian.GlowArquillianDeploymentExporter.TEST_CLASSPATH;
Expand Down Expand Up @@ -322,66 +318,17 @@ public void execute() throws MojoExecutionException, MojoFailureException {
for (String s : project.getTestClasspathElements()) {
paths.add(new File(s).getAbsolutePath());
}
MavenArtifactRepositoryManager artifactResolver = new MavenArtifactRepositoryManager(repoSystem, repoSession, repositories);
MavenRepoManager artifactResolver;
ConfiguredChannels cr = null;
if (channels != null && !channels.isEmpty()) {
getLog().debug("WildFly channel enabled, feature-pack versions are retrieved from channels (if stream known).");
try {
ConfiguredChannels cr = new ConfiguredChannels(channels,
repoSystem, repoSession, repositories,
getLog(), true);
UniverseResolver universeResolver = UniverseResolver.builder().addArtifactResolver(artifactResolver).build();
for (GalleonFeaturePack fp : featurePacks) {
if (fp.getLocation() == null && (fp.getGroupId() == null || fp.getArtifactId() == null)) {
throw new IllegalArgumentException("Feature-pack location or Maven GAV is missing");
}
String groupId;
String artifactId;
String loc = fp.getLocation();
if (loc == null) {
groupId = fp.getGroupId();
artifactId = fp.getArtifactId();
} else {
// Special case for G:A that conflicts with producer:channel that we can't have in the plugin.
if (!FeaturePackLocation.fromString(loc).hasUniverse()) {
long numSeparators = loc.chars().filter(ch -> ch == ':').count();
if (numSeparators <= 1) {
loc += ":";
}
}
FeaturePackLocation location = FeaturePackLocation.fromString(loc);
if (location.isMavenCoordinates()) {
String[] coordinates = loc.split(":");
groupId = coordinates[0];
artifactId = coordinates[1];
} else {
Channel c = universeResolver.getChannel(location);
MavenChannel mc = (MavenChannel) c;
groupId = mc.getFeaturePackGroupId();
artifactId = mc.getFeaturePackArtifactId();
}
}
try {
VersionResult res = cr.getChannelSession().findLatestMavenArtifactVersion(groupId, artifactId,
fp.getExtension(), fp.getClassifier(), null);
getLog().debug(fp.getGroupId() + ":" + fp.getArtifactId() + ", Channel resolved version " + res.getVersion());
if (fp.getLocation() == null) {
fp.setVersion(res.getVersion());
} else {
FeaturePackLocation l = FeaturePackLocation.fromString(loc);
FeaturePackLocation resolved = new FeaturePackLocation(l.getUniverse(),
l.getProducerName(),
l.getChannelName(),
l.getFrequency(),
res.getVersion());
fp.setLocation(resolved.toString());
}
} catch (Exception ex) {
getLog().debug("Got exception trying to resolve " + fp.getGroupId() + ":" + fp.getArtifactId(), ex);
}
}
} catch (MalformedURLException | UnresolvedMavenArtifactException ex) {
throw new MojoExecutionException(ex.getLocalizedMessage(), ex);
getLog().debug("WildFly channel enabled.");
List<Channel> lst = new ArrayList<>();
for (ChannelConfiguration conf : channels) {
lst.add(conf.toChannel(repositories));
}
artifactResolver = new ChannelMavenArtifactRepositoryManager(lst, repoSystem, repoSession, repositories);
} else {
artifactResolver = new MavenArtifactRepositoryManager(repoSystem, repoSession, repositories);
}
Set<String> profiles = new HashSet<>();
if (profile != null) {
Expand All @@ -404,7 +351,6 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}

Arguments arguments = argumentsBuilder.build();

try (ScanResults results = GlowSession.scan(artifactResolver,
arguments, writer)) {
boolean skipTests = Boolean.getBoolean("maven.test.skip") || Boolean.getBoolean("skipTests");
Expand Down Expand Up @@ -626,7 +572,7 @@ private static void collectCpPaths(String javaHome, ClassLoader cl, StringBuilde
}
}

private Path buildInputConfig(Path outputFolder, MavenArtifactRepositoryManager artifactResolver) throws ProvisioningException, IOException, XMLStreamException {
private Path buildInputConfig(Path outputFolder, MavenRepoManager artifactResolver) throws ProvisioningException, IOException, XMLStreamException {
GalleonProvisioningConfig.Builder inBuilder = GalleonProvisioningConfig.builder();
// Build config
for (GalleonFeaturePack fp : featurePacks) {
Expand Down
Loading
Loading