Skip to content

Commit

Permalink
Merge pull request bndtools#5964 from chrisrueger/fix-gav-in-repo-mac…
Browse files Browse the repository at this point in the history
…ro-bndpomrepository

BndPomRepository should support both bsn and gav like MavenBndRepository
  • Loading branch information
pkriens authored Jan 15, 2024
2 parents 276bf9f + 02b1dc3 commit 63837df
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
import java.util.Objects;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import org.osgi.resource.Capability;
import org.osgi.resource.Requirement;
Expand Down Expand Up @@ -302,16 +304,41 @@ public File get(String bsn, Version version, Map<String, String> properties, Dow
}

Archive archive;
ResourceInfo resource = bridge.getInfo(bsn, version);
if (resource == null) {
archive = trySources(bsn, version);
if (archive == null)

if (isMavenGAV(bsn)) {

// Handle the GAV. This "hack" is borrowed from
// aQute.bnd.repository.maven.provider.IndexFile.find(String,
// Version)
// because MavenBndRepository can handle both bsn and GAV too

if (repoImpl instanceof PomRepository pr) {

Archive spec = Archive.valueOf(bsn + ":" + version);

archive = pr.archives.stream()
.filter(a -> matches(a, spec))
.findAny()
.orElse(null);
if (archive == null)
return null;
} else {
return null;
}

} else {

String from = resource.getInfo()
.from();
archive = Archive.valueOf(from);
ResourceInfo resource = bridge.getInfo(bsn, version);
if (resource == null) {
archive = trySources(bsn, version);
if (archive == null)
return null;
} else {

String from = resource.getInfo()
.from();
archive = Archive.valueOf(from);
}
}

Promise<File> p = repoImpl.getMavenRepository()
Expand All @@ -326,6 +353,20 @@ public File get(String bsn, Version version, Map<String, String> properties, Dow
.toLocalFile(archive);
}

private boolean isMavenGAV(String bsn) {
return bsn != null && bsn.indexOf(':') != -1;
}

private boolean matches(Archive archive, Archive spec) {
return archive.revision.program.equals(spec.revision.program) && archive.revision.version.getOSGiVersion()
.equals(spec.revision.version.getOSGiVersion()) && spec.classifier.equals(archive.classifier);
}

private boolean matchesGAV(Archive archive, String groupId, String artifactId) {
return archive.revision.group.equals(groupId) && archive.revision.artifact.equals(artifactId);
}


@Override
public boolean canWrite() {
return false;
Expand All @@ -344,9 +385,31 @@ public SortedSet<Version> versions(String bsn) throws Exception {
if (!init()) {
return Collections.emptySortedSet();
}

if (isMavenGAV(bsn)) {
if (repoImpl instanceof PomRepository pr) {

String[] split = bsn.split(":");
String groupId = split[0];
String artifactId = split[1];


SortedSet<Version> versions = pr.archives.stream()
.filter(a -> matchesGAV(a, groupId, artifactId))
.map(a -> a.revision.version.getOSGiVersion())
.collect(Collectors.toCollection(TreeSet::new));

return versions;

} else {
return Collections.emptySortedSet();
}
}
return bridge.versions(bsn);
}



@Override
public String getName() {
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.osgi.resource.Resource;
Expand Down Expand Up @@ -89,12 +90,21 @@ void readArchives() throws Exception {

void save(Traverser traverser) throws Exception {
Promise<Map<Archive, Resource>> p = traverser.getResources();
Collection<Resource> resources = p.getValue()
.values();

Map<Archive, Resource> map = p.getValue();
setArchives(map.keySet());

Collection<Resource> resources = map.values();
set(resources);

save(getMavenRepository().getName(), resources, getLocation());
}

private void setArchives(Set<Archive> archives) {
this.archives.clear();
this.archives.addAll(archives);
}

void save(String name, Collection<? extends Resource> resources, File location) throws Exception, IOException {
XMLResourceGenerator generator = new XMLResourceGenerator();
generator.resources(resources);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package aQute.bnd.repository.maven.helpers;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.util.SortedSet;

import aQute.bnd.service.RepositoryPlugin;
import aQute.bnd.version.Version;

public class MavenRepoTestHelper {

/**
* This method is called by two maven repo tests to ensure that both repos
* behave the same and can get a resource by bns and gav in case both are
* available.
*
* @param repo
* @throws Exception
*/
public static void assertMavenReposGetViaBSNAndGAV(RepositoryPlugin repo) throws Exception {
assertEquals(1, repo.list("org.apache.commons.cli")
.size());
System.out.println(repo.list("org.apache.commons.cli"));
System.out.println(repo.versions("org.apache.commons.cli"));

File f12maven = repo.get("commons-cli:commons-cli", new Version("1.2.0"), null);
File f12osgi = repo.get("org.apache.commons.cli", new Version("1.2.0"), null);

assertTrue(f12maven.exists());
assertTrue(f12osgi.exists());

assertEquals("commons-cli-1.2.jar", f12maven.getName());
assertEquals(f12maven, f12osgi);

// check if 1.2 instead of 1.2.0 works too
File f12maven2DigitsVer = repo.get("commons-cli:commons-cli", new Version("1.2"), null);
File f12osgi2DigitsVer = repo.get("org.apache.commons.cli", new Version("1.2"), null);

assertTrue(f12maven2DigitsVer.exists());
assertTrue(f12osgi2DigitsVer.exists());

assertEquals("commons-cli-1.2.jar", f12maven2DigitsVer.getName());
assertEquals(f12maven2DigitsVer, f12osgi2DigitsVer);

// version should accept both too
SortedSet<Version> versionsMaven = repo.versions("commons-cli:commons-cli");
SortedSet<Version> versionsOsgi = repo.versions("org.apache.commons.cli");

assertNotNull(versionsMaven);
assertNotNull(versionsOsgi);
assertTrue(versionsMaven.stream()
.anyMatch(v -> "1.2.0".equals(v.toString())));
assertTrue(versionsOsgi.stream()
.anyMatch(v -> "1.2.0".equals(v.toString())));


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.Processor;
import aQute.bnd.osgi.repository.XMLResourceParser;
import aQute.bnd.repository.maven.helpers.MavenRepoTestHelper;
import aQute.bnd.repository.maven.provider.MavenBndRepoTest;
import aQute.bnd.service.RepositoryListenerPlugin;
import aQute.bnd.service.RepositoryPlugin;
import aQute.bnd.test.jupiter.InjectTemporaryDirectory;
Expand Down Expand Up @@ -267,6 +269,31 @@ public void testBndPomRepoFile() throws Exception {
}
}


/**
* Commons CLI 1.2 is in there as GAV & as BSN Note: This test should be
* kept in sync with {@link MavenBndRepoTest#testGetViaBSNAndGAV()}
*/
@Test
public void testGetViaBSNAndGAV() throws Exception {
try (BndPomRepository repo = new BndPomRepository()) {
Workspace w = Workspace.createStandaloneWorkspace(new Processor(), tmp.toURI());
w.setBase(tmp);
repo.setRegistry(w);

Map<String, String> config = new HashMap<>();
config.put("pom", "testdata/pomrepo/simpleGetViaBSNAndGAV.xml");
config.put("transitive", "false");
config.put("snapshotUrl", "https://repo.maven.apache.org/maven2/");
config.put("releaseUrl", "https://repo.maven.apache.org/maven2/");
config.put("name", "test2");
repo.setProperties(config);

MavenRepoTestHelper.assertMavenReposGetViaBSNAndGAV(repo);
}

}

@Test
public void testBndPomRepoFileExistingParent() throws Exception {
try (BndPomRepository bpr = new BndPomRepository()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import aQute.bnd.osgi.Processor;
import aQute.bnd.osgi.resource.ResourceUtils;
import aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability;
import aQute.bnd.repository.maven.helpers.MavenRepoTestHelper;
import aQute.bnd.repository.maven.pom.provider.PomRepositoryTest;
import aQute.bnd.service.RepositoryPlugin.PutOptions;
import aQute.bnd.service.RepositoryPlugin.PutResult;
import aQute.bnd.service.maven.PomOptions;
Expand Down Expand Up @@ -619,24 +621,18 @@ public void testGetFileRepo() throws Exception {
assertTrue(file.isFile());
}

/*
* Commons CLI 1.2 is in there as GAV & as BSN
/**
* Commons CLI 1.2 is in there as GAV & as BSN Note: This test should be
* kept in sync with {@link PomRepositoryTest#testGetViaBSNAndGAV()}
*/
@Test
public void testGetViaBSNAndGAV() throws Exception {
config(null);

assertEquals(1, repo.list("org.apache.commons.cli")
.size());
System.out.println(repo.list("org.apache.commons.cli"));
System.out.println(repo.versions("org.apache.commons.cli"));
File f12maven = repo.get("commons-cli:commons-cli", new Version("1.2.0"), null);
File f12osgi = repo.get("org.apache.commons.cli", new Version("1.2.0"), null);

assertEquals("commons-cli-1.2.jar", f12maven.getName());
assertEquals(f12maven, f12osgi);
MavenRepoTestHelper.assertMavenReposGetViaBSNAndGAV(repo);
}


@Test
public void testList() throws Exception {
config(null);
Expand Down
20 changes: 20 additions & 0 deletions biz.aQute.repository/testdata/pomrepo/simpleGetViaBSNAndGAV.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>


<dependencies>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>

</dependencies>

</project>

0 comments on commit 63837df

Please sign in to comment.