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

allow sub-classes of project to provide repos #6488

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
17 changes: 14 additions & 3 deletions biz.aQute.bndlib/src/aQute/bnd/build/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ public List<Container> getBundlesWildcard(String bsnPattern, String range, Strat

SortedMap<String, Pair<Version, RepositoryPlugin>> providerMap = new TreeMap<>();

List<RepositoryPlugin> plugins = workspace.getRepositories();
List<RepositoryPlugin> plugins = getRepositories();
for (RepositoryPlugin plugin : plugins) {

if (repoFilter != null && !repoFilter.match(plugin))
Expand Down Expand Up @@ -1278,7 +1278,7 @@ public Container getBundle(String bsn, String range, Strategy strategy, Map<Stri
useStrategy = overrideStrategy(attrs, useStrategy);
RepoFilter repoFilter = parseRepoFilter(attrs);

List<RepositoryPlugin> plugins = workspace.getRepositories();
List<RepositoryPlugin> plugins = getRepositories();

if (useStrategy == Strategy.EXACT) {
if (!Verifier.isVersion(range))
Expand Down Expand Up @@ -1386,6 +1386,17 @@ public Container getBundle(String bsn, String range, Strategy strategy, Map<Stri

}

/**
* Provides relevant repositories used by some other methods. By default it
* returns all Repositories of the Workspace. Subclasses can override it to
* provide different or additional repostories.
*
* @return a list of relevant repositories used by various methods.
*/
protected List<RepositoryPlugin> getRepositories() {
return workspace.getRepositories();
}

/**
* @param attrs
* @param useStrategy
Expand Down Expand Up @@ -1502,7 +1513,7 @@ private Container getBundleByHash(String bsn, Map<String, String> attrs) throws
hash = (colonIndex < hashStr.length()) ? hashStr.substring(afterColon) : "";
}

for (RepositoryPlugin plugin : workspace.getRepositories()) {
for (RepositoryPlugin plugin : getRepositories()) {
// The plugin *may* understand version=hash directly
DownloadBlocker blocker = new DownloadBlocker(this);
File result = plugin.get(bsn, Version.LOWEST, Collections.unmodifiableMap(attrs), blocker);
Expand Down
2 changes: 1 addition & 1 deletion biz.aQute.resolve/src/biz/aQute/resolve/package-info.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@Version("9.1.0")
@Version("9.2.0")
package biz.aQute.resolve;

import org.osgi.annotation.versioning.Version;
104 changes: 102 additions & 2 deletions biz.aQute.resolve/test/biz/aQute/resolve/RunResolutionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,41 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

import aQute.bnd.build.model.clauses.HeaderClause;
import aQute.bnd.build.model.conversions.NoopConverter;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.osgi.resource.Resource;
import org.osgi.resource.Wire;

import aQute.bnd.build.Container;
import aQute.bnd.build.Container.TYPE;
import aQute.bnd.build.ProjectLauncher;
import aQute.bnd.build.Workspace;
import aQute.bnd.build.model.clauses.HeaderClause;
import aQute.bnd.build.model.clauses.VersionedClause;
import aQute.bnd.build.model.conversions.NoopConverter;
import aQute.bnd.help.instructions.ResolutionInstructions.Runorder;
import aQute.bnd.osgi.Constants;
import aQute.bnd.result.Result;
import aQute.bnd.service.RepositoryPlugin;
import aQute.bnd.service.Strategy;
import aQute.bnd.test.jupiter.InjectTemporaryDirectory;
import aQute.bnd.version.Version;
import aQute.lib.io.IO;
import biz.aQute.resolve.Bndrun.CacheReason;

Expand Down Expand Up @@ -525,4 +533,96 @@ public void testStartLevelDecoration() throws Exception {
.getAttribs()).containsEntry(Constants.RUNBUNDLES_STARTLEVEL_ATTRIBUTE, "90");
}

@SuppressWarnings("resource")
@Test
public void testBndRunPluginGetBundle() {
try {

Workspace workspace = Workspace.createDefaultWorkspace();

// try with a normal Bndrun, which only considers Workspace bundles
Bndrun bndrun = Bndrun.createBndrun(workspace, null);
bndrun.addBasicPlugin(new MyPlugin());
System.out.println("Bndrun: " + bndrun.getPlugin(MyPlugin.class));
System.out.println("Workspace: " + workspace.getPlugin(MyPlugin.class));
workspace.refresh();
System.out.println("Workspace after refresh: " + workspace.getPlugin(MyPlugin.class));

assertThat(bndrun.getPlugin(MyPlugin.class)).isNotNull();
Container bundle = bndrun.getBundle("testBndRunPluginGetBundle", "[1.0.0,2.0.0)", Strategy.HIGHEST, Map.of());
assertThat(bundle.getType()).isEqualTo(TYPE.ERROR);

// now repeat with a custom MyBndrun class which overrides
// getRepositories()
MyBndrun bndrun2 = new MyBndrun(workspace, null);
bndrun2.addBasicPlugin(new MyPlugin());
assertThat(bndrun2.getPlugin(MyPlugin.class)).isNotNull();
Container bundle2 = bndrun2.getBundle("testBndRunPluginGetBundle", "[1.0.0,2.0.0)", Strategy.HIGHEST,
Map.of());
assertThat(bundle2.getType()).isEqualTo(TYPE.REPO);
assertThat(bundle2.getBundleSymbolicName()).isEqualTo("testBndRunPluginGetBundle");


} catch (Exception e) {
fail();
}
}

private static class MyBndrun extends Bndrun {

public MyBndrun(Workspace workspace, File propertiesFile) throws Exception {
super(workspace, propertiesFile);
}

@Override
protected List<RepositoryPlugin> getRepositories() {
return getPlugins(RepositoryPlugin.class);
}

}

private final class MyPlugin implements RepositoryPlugin {

@Override
public PutResult put(InputStream stream, PutOptions options) throws Exception {
return null;
}

@Override
public File get(String bsn, Version version, Map<String, String> properties, DownloadListener... listeners)
throws Exception {
if("testBndRunPluginGetBundle".equals(bsn)) {
return IO.getFile(RunResolutionTest.this.ws.toFile(),
"testdata/jar/org.apache.felix.http.servlet-api-1.2.0.jar");
}
return null;
}

@Override
public boolean canWrite() {
return false;
}

@Override
public List<String> list(String pattern) throws Exception {
return null;
}

@Override
public SortedSet<Version> versions(String bsn) throws Exception {
return new TreeSet<Version>(Set.of(Version.ONE));
}

@Override
public String getName() {
return null;
}

@Override
public String getLocation() {
return null;
}

}

}