Skip to content

Commit

Permalink
pass test for GAV
Browse files Browse the repository at this point in the history
1st draft, base for discussion.
I hooked into the indexing of BridgeRepository because commons-cli:commons-cli:1.2 (commons-cli-1.2.jar) is indexed as commons-cli:commons-cli:1.2.0 by bridge repo (notice 1.2 vs. 1.2.0). To satisfy the existing testcase of MavenBndRepo this was the only place where I could get the version as 3 digit semver. To be discussed if it really should be that way

Signed-off-by: Christoph Rueger <chrisrueger@gmail.com>
  • Loading branch information
chrisrueger committed Jan 11, 2024
1 parent 39eca3c commit dabeb36
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public class BridgeRepository {
}

private final Repository repository;
private final Map<String, Map<Version, ResourceInfo>> index = new HashMap<>();
private final Map<String, Map<Version, ResourceInfo>> bsnIndex = new HashMap<>();
private final Map<String, Map<Version, ResourceInfo>> gavIndex = new HashMap<>();

@ProviderType
public interface InfoCapability extends Capability {
Expand Down Expand Up @@ -225,37 +226,50 @@ private void find(Set<Resource> resources, Requirement req) {
}

private void index(Resource r) {
String bsn;
Version version;


if (ResourceUtils.hasType(r, ResourceBuilder.SYNTHETIC))
return;

// index both separately: bsn + gav

IdentityCapability bc = ResourceUtils.getIdentityCapability(r);
if (bc != null) {

String bsn;
Version version;

bsn = bc.osgi_identity();
version = bc.version();

bsnIndex.computeIfAbsent(bsn, k -> new HashMap<>())
.put(version, new ResourceInfo(r));

} else {
logger.debug("No identity for {}, trying info", r);
InfoCapability info = getInfo(r);
if (info == null) {
// No way to index this
logger.debug("Also no info, giving up indexing");
return;
}
}

InfoCapability info = getInfo(r);
if (info != null) {

String bsn;
Version version;

bsn = info.name();
version = info.version();
if (version == null)
version = Version.LOWEST;
}

Map<Version, ResourceInfo> map = index.get(bsn);
if (map == null) {
map = new HashMap<>();
index.put(bsn, map);
gavIndex.computeIfAbsent(bsn, k -> new HashMap<>())
.put(version, new ResourceInfo(r));
}
else {
// No way to index this
logger.debug("No info, giving up indexing");
return;
}
map.put(version, new ResourceInfo(r));


}

public Resource get(String bsn, Version version) throws Exception {
Expand All @@ -267,7 +281,15 @@ public Resource get(String bsn, Version version) throws Exception {
}

public ResourceInfo getInfo(String bsn, Version version) throws Exception {
Map<Version, ResourceInfo> map = index.get(bsn);
Map<Version, ResourceInfo> map = bsnIndex.get(bsn);
if (map == null)
return null;

return map.get(version);
}

public ResourceInfo getInfoByGAV(String gav, Version version) throws Exception {
Map<Version, ResourceInfo> map = gavIndex.get(gav);
if (map == null)
return null;

Expand All @@ -277,15 +299,15 @@ public ResourceInfo getInfo(String bsn, Version version) throws Exception {
public List<String> list(String pattern) throws Exception {
List<String> bsns = new ArrayList<>();
if (pattern == null || pattern.equals("*") || pattern.equals("")) {
bsns.addAll(index.keySet());
bsns.addAll(bsnIndex.keySet());
} else {
String[] split = pattern.split("\\s+");
Glob globs[] = new Glob[split.length];
for (int i = 0; i < split.length; i++) {
globs[i] = new Glob(split[i]);
}

outer: for (String bsn : index.keySet()) {
outer: for (String bsn : bsnIndex.keySet()) {
for (Glob g : globs) {
if (g.matcher(bsn)
.find()) {
Expand All @@ -299,7 +321,7 @@ public List<String> list(String pattern) throws Exception {
}

public SortedSet<Version> versions(String bsn) throws Exception {
Map<Version, ResourceInfo> map = index.get(bsn);
Map<Version, ResourceInfo> map = bsnIndex.get(bsn);
if (map == null || map.isEmpty()) {
return new TreeSet<>();
}
Expand Down Expand Up @@ -358,7 +380,7 @@ public String title(Object... target) throws Exception {
}
if (target.length == 1) {
String bsn = (String) target[0];
Map<Version, ResourceInfo> map = index.get(bsn);
Map<Version, ResourceInfo> map = bsnIndex.get(bsn);
for (ResourceInfo ri : map.values()) {
if (ri.isError())
return bsn + " [!]";
Expand All @@ -382,7 +404,7 @@ public Set<Resource> getResources() {
Pattern.CASE_INSENSITIVE);

public String getStatus() {
return index.entrySet()
return bsnIndex.entrySet()
.stream()
.flatMap(e -> e.getValue()
.values()
Expand All @@ -396,7 +418,7 @@ public String getStatus() {
}

public List<ResourceInfo> getResourceInfos() {
return index.values()
return bsnIndex.values()
.stream()
.flatMap(map -> map.values()
.stream())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,18 +302,51 @@ 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)
return null;


//
// First try if this is a BSN, not a GAV
//

if (bsn.indexOf(':') < 0) {

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);
}

} else {

String from = resource.getInfo()
.from();
archive = Archive.valueOf(from);
//
// 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
// TODO Notice that the GAV can also contain extension +
// classifier
//

ResourceInfo resource = bridge.getInfoByGAV(bsn, version);
if (resource == null) {
archive = trySourcesByGAV(bsn, version);
if (archive == null)
return null;
} else {

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

}


Promise<File> p = repoImpl.getMavenRepository()
.get(archive);

Expand Down Expand Up @@ -444,4 +477,22 @@ private Archive trySources(String bsn, Version version) throws Exception {
.getOther(Archive.JAR_EXTENSION, Archive.SOURCES_CLASSIFIER);
}

/*
* Try to fetch sources by GAV (instead of bsn)
*/
private Archive trySourcesByGAV(String gav, Version version) throws Exception {
if (!gav.endsWith(BSN_SOURCE_SUFFIX))
return null;

String rawBsn = gav.substring(0, gav.length() - BSN_SOURCE_SUFFIX.length());
ResourceInfo resource = bridge.getInfoByGAV(rawBsn, version);
if (resource == null)
return null;

String from = resource.getInfo()
.from();
return Archive.valueOf(from)
.getOther(Archive.JAR_EXTENSION, Archive.SOURCES_CLASSIFIER);
}

}

0 comments on commit dabeb36

Please sign in to comment.