Skip to content

Commit

Permalink
improve tag matching
Browse files Browse the repository at this point in the history
- keep backwards compatibility so that Repos without tags return an empty set and empty set means "matches"
- renamed Tags.matchesTags to Tags.isIncluded
- move tags to own package aQute.bnd.service.tags

Signed-off-by: Christoph Rueger <chrisrueger@gmail.com>
  • Loading branch information
chrisrueger committed May 21, 2024
1 parent 7857d51 commit 53cb0dc
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import aQute.bnd.exceptions.Exceptions;
import aQute.bnd.osgi.resource.ResourceUtils;
import aQute.bnd.service.RepositoryPlugin;
import aQute.bnd.service.Tagged;
import aQute.bnd.service.Tags;
import aQute.bnd.service.tags.Tagged;
import aQute.bnd.service.tags.Tags;

/**
* WARNING ! Not tested
Expand Down
9 changes: 5 additions & 4 deletions biz.aQute.bndlib/src/aQute/bnd/service/Registry.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package aQute.bnd.service;

import static aQute.bnd.service.Tagged.matchesTags;

import java.util.List;
import java.util.stream.Collectors;

import aQute.bnd.service.tags.Tagged;

/**
* A registry for objects.
*/
Expand All @@ -13,12 +13,13 @@ public interface Registry {

default <T> List<T> getPlugins(Class<T> c, String... tags) {

if (tags == null || tags.length == 0) {
if (tags.length == 0) {
return getPlugins(c);
}

return getPlugins(c).stream()
.filter(repo -> matchesTags(repo, tags))
.filter(repo -> repo instanceof Tagged taggedRepo && taggedRepo.getTags()
.isIncluded(tags))
.collect(Collectors.toList());
}

Expand Down
7 changes: 3 additions & 4 deletions biz.aQute.bndlib/src/aQute/bnd/service/RepositoryPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Processor;
import aQute.bnd.service.tags.Tagged;
import aQute.bnd.service.tags.Tags;
import aQute.bnd.version.Version;

/**
Expand Down Expand Up @@ -288,8 +290,5 @@ default Promise<Void> sync() throws Exception {
});
}

@Override
default Tags getTags() {
return DEFAULT_REPO_TAGS;
}

}
35 changes: 0 additions & 35 deletions biz.aQute.bndlib/src/aQute/bnd/service/Tagged.java

This file was deleted.

21 changes: 21 additions & 0 deletions biz.aQute.bndlib/src/aQute/bnd/service/tags/Tagged.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package aQute.bnd.service.tags;

/**
* Allows to add tags to implementing classes. Originally intended for tagging
* repositories.
*/
public interface Tagged {

/**
* Dummy placeholder for "empty tags".
*/
String EMPTY_TAGS = "<<EMPTY>>";

/**
* @return a non-null list of tags. Default is empty (meaning 'no tags').
*/
default Tags getTags() {
return Tags.NO_TAGS;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package aQute.bnd.service;
package aQute.bnd.service.tags;

import static java.util.Collections.unmodifiableSortedSet;

Expand All @@ -16,7 +16,7 @@
* categorization and filtering. Typically these entities then implement the
* {@link Tagged} interface.
*/
public final class Tags implements Set<String> {
public class Tags implements Set<String> {
private final SortedSet<String> internalSet;

public final static Tags NO_TAGS = of();
Expand All @@ -29,7 +29,7 @@ private Tags(Collection<? extends String> c) {
this.internalSet = unmodifiableSortedSet(new TreeSet<>(c));
}

static Tags of(String... name) {
public static Tags of(String... name) {
return new Tags(Set.of(name));
}

Expand Down Expand Up @@ -130,6 +130,26 @@ public String toString() {
return internalSet.toString();
}

/**
* @param <T>
* @param tags
* @return <code>true</code> if the passed object matches any of the given
* tags, otherwise returns <code>false</code>
*/
public <T> boolean isIncluded(String... tags) {

if (isEmpty()) {
return true;
}

for (String tag : tags) {
if (contains(tag)) {
return true;
}
}

return false;
}


}
4 changes: 4 additions & 0 deletions biz.aQute.bndlib/src/aQute/bnd/service/tags/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@Version("1.0.0")
package aQute.bnd.service.tags;

import org.osgi.annotation.versioning.Version;
6 changes: 3 additions & 3 deletions biz.aQute.bndlib/src/aQute/lib/deployer/FileRepo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package aQute.lib.deployer;

import static aQute.bnd.service.Tags.parse;
import static aQute.bnd.service.tags.Tags.parse;

import java.io.Closeable;
import java.io.File;
Expand Down Expand Up @@ -36,9 +36,9 @@
import aQute.bnd.service.RegistryPlugin;
import aQute.bnd.service.RepositoryListenerPlugin;
import aQute.bnd.service.RepositoryPlugin;
import aQute.bnd.service.Tagged;
import aQute.bnd.service.Tags;
import aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor;
import aQute.bnd.service.tags.Tagged;
import aQute.bnd.service.tags.Tags;
import aQute.bnd.version.Version;
import aQute.lib.collections.SortedList;
import aQute.lib.hex.Hex;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package aQute.bnd.deployer.repository;

import static aQute.bnd.deployer.repository.RepoConstants.DEFAULT_CACHE_DIR;
import static aQute.bnd.service.Tags.parse;

import java.io.ByteArrayOutputStream;
import java.io.File;
Expand Down Expand Up @@ -41,6 +40,7 @@
import aQute.bnd.service.RepositoryListenerPlugin;
import aQute.bnd.service.ResourceHandle;
import aQute.bnd.service.ResourceHandle.Location;
import aQute.bnd.service.tags.Tags;
import aQute.bnd.version.Version;
import aQute.bnd.version.VersionRange;
import aQute.lib.hex.Hex;
Expand Down Expand Up @@ -135,7 +135,7 @@ public synchronized void setProperties(Map<String, String> map) {
String.format("Cannot create repository cache: '%s' already exists but is not directory.",
cacheDir.getAbsolutePath()));

super.setTags(parse(map.get(PROP_TAGS), DEFAULT_REPO_TAGS));
super.setTags(Tags.parse(map.get(PROP_TAGS), DEFAULT_REPO_TAGS));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package aQute.bnd.repository.maven.pom.provider;

import static aQute.bnd.osgi.Constants.BSN_SOURCE_SUFFIX;
import static aQute.bnd.service.Tags.parse;
import static aQute.bnd.service.tags.Tags.parse;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package aQute.bnd.repository.maven.provider;

import static aQute.bnd.osgi.Constants.BSN_SOURCE_SUFFIX;
import static aQute.bnd.service.Tags.parse;
import static aQute.bnd.service.tags.Tags.parse;

import java.io.Closeable;
import java.io.File;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package aQute.bnd.repository.osgi;

import static aQute.bnd.service.Tags.parse;
import static aQute.bnd.service.tags.Tags.parse;

import java.io.Closeable;
import java.io.File;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package aQute.bnd.repository.p2.provider;

import static aQute.bnd.service.Tags.parse;
import static aQute.bnd.service.tags.Tags.parse;

import java.io.Closeable;
import java.io.File;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import aQute.bnd.build.Workspace;
import aQute.bnd.osgi.Constants;
import aQute.bnd.service.Tagged;
import aQute.bnd.service.tags.Tagged;
import aQute.bnd.test.jupiter.InjectTemporaryDirectory;
import aQute.http.testservers.HttpTestServer.Config;
import aQute.lib.io.IO;
Expand Down Expand Up @@ -66,7 +66,8 @@ public void testEnv() throws Exception {
System.out.println(workspace.getBase());

// check repo tags
// repos should have the 'resolve' tag by default if no tag is specified
// we expect all repos to be returned for the 'resolve' tag by default
// if a repo has no tag specified
List<Repository> repos = workspace.getPlugins(Repository.class);
List<Repository> resolveRepos = workspace.getPlugins(Repository.class, Constants.REPOTAGS_RESOLVE);
assertEquals(repos, resolveRepos);
Expand Down

0 comments on commit 53cb0dc

Please sign in to comment.