use of aQute.bnd.version.Version in project bnd by bndtools.
the class ProjectTest method testPackageInfoType.
/*
* Verify that that -versionannotations works. We can be osgi, bnd,
* packageinfo, or an annotation. When not set, we are packageinfo
*/
public void testPackageInfoType() throws Exception {
Workspace ws = getWorkspace("testresources/ws");
Project project = ws.getProject("p5");
project.setTrace(true);
Version newVersion = new Version(2, 0, 0);
project.setProperty(Constants.PACKAGEINFOTYPE, "bnd");
// Package with no package info
project.setPackageInfo("pkg1", newVersion);
Version version = project.getPackageInfo("pkg1");
assertEquals(newVersion, version);
checkPackageInfoFiles(project, "pkg1", false, true);
String content = IO.collect(project.getFile("src/pkg1/package-info.java"));
assertTrue(content.contains("import aQute.bnd.annotation.Version"));
// Package with package-info.java containing @Version("1.0.0")
project.setPackageInfo("pkg2", newVersion);
version = project.getPackageInfo("pkg2");
assertEquals(newVersion, version);
checkPackageInfoFiles(project, "pkg2", false, true);
// new packageinfo must now contain osgi ann.
project.setProperty(Constants.PACKAGEINFOTYPE, "osgi");
// Package with package-info.java containing version + packageinfo
project.setPackageInfo("pkg5", newVersion);
version = project.getPackageInfo("pkg5");
assertEquals(newVersion, version);
checkPackageInfoFiles(project, "pkg5", true, true);
content = IO.collect(project.getFile("src/pkg5/package-info.java"));
assertTrue(content.contains("import aQute.bnd.annotation.Version"));
// Package with package-info.java NOT containing version +
// packageinfo
project.setPackageInfo("pkg6", newVersion);
version = project.getPackageInfo("pkg6");
assertEquals(newVersion, version);
checkPackageInfoFiles(project, "pkg6", true, true);
content = IO.collect(project.getFile("src/pkg6/package-info.java"));
assertTrue(content.contains("import org.osgi.annotation.versioning.Version"));
// Package with package-info.java NOT containing version
project.setPackageInfo("pkg7", newVersion);
version = project.getPackageInfo("pkg7");
assertEquals(newVersion, version);
checkPackageInfoFiles(project, "pkg7", false, true);
newVersion = new Version(2, 2, 0);
// Update packageinfo file
project.setPackageInfo("pkg1", newVersion);
version = project.getPackageInfo("pkg1");
assertEquals(newVersion, version);
checkPackageInfoFiles(project, "pkg1", false, true);
}
use of aQute.bnd.version.Version in project bnd by bndtools.
the class ProjectTest method testSetPackageVersion.
public void testSetPackageVersion() throws Exception {
Workspace ws = getWorkspace("testresources/ws");
Project project = ws.getProject("p5");
project.setTrace(true);
Version newVersion = new Version(2, 0, 0);
// Package with no package info
project.setPackageInfo("pkg1", newVersion);
Version version = project.getPackageInfo("pkg1");
assertEquals(newVersion, version);
checkPackageInfoFiles(project, "pkg1", true, false);
// Package with package-info.java containing @Version("1.0.0")
project.setPackageInfo("pkg2", newVersion);
version = project.getPackageInfo("pkg2");
assertEquals(newVersion, version);
checkPackageInfoFiles(project, "pkg2", false, true);
// Package with package-info.java containing
// @aQute.bnd.annotations.Version("1.0.0")
project.setPackageInfo("pkg3", newVersion);
version = project.getPackageInfo("pkg3");
assertEquals(newVersion, version);
checkPackageInfoFiles(project, "pkg3", false, true);
// Package with package-info.java containing
// @aQute.bnd.annotations.Version(value="1.0.0")
project.setPackageInfo("pkg4", newVersion);
version = project.getPackageInfo("pkg4");
assertEquals(newVersion, version);
checkPackageInfoFiles(project, "pkg4", false, true);
// Package with package-info.java containing version + packageinfo
project.setPackageInfo("pkg5", newVersion);
version = project.getPackageInfo("pkg5");
assertEquals(newVersion, version);
checkPackageInfoFiles(project, "pkg5", true, true);
// Package with package-info.java NOT containing version +
// packageinfo
project.setPackageInfo("pkg6", newVersion);
version = project.getPackageInfo("pkg6");
assertEquals(newVersion, version);
checkPackageInfoFiles(project, "pkg6", true, true);
// Package with package-info.java NOT containing version
project.setPackageInfo("pkg7", newVersion);
version = project.getPackageInfo("pkg7");
assertEquals(newVersion, version);
checkPackageInfoFiles(project, "pkg7", true, true);
newVersion = new Version(2, 2, 0);
// Update packageinfo file
project.setPackageInfo("pkg1", newVersion);
version = project.getPackageInfo("pkg1");
assertEquals(newVersion, version);
checkPackageInfoFiles(project, "pkg1", true, false);
}
use of aQute.bnd.version.Version in project bnd by bndtools.
the class Project method getBundle.
/**
* Get a bundle from one of the plugin repositories. If an exact version is
* required we just return the first repository found (in declaration order
* in the build.bnd file).
*
* @param bsn The bundle symbolic name
* @param range The version range
* @param strategy set to LOWEST or HIGHEST
* @return the file object that points to the bundle or null if not found
* @throws Exception when something goes wrong
*/
public Container getBundle(String bsn, String range, Strategy strategy, Map<String, String> attrs) throws Exception {
if (range == null)
range = "0";
if (VERSION_ATTR_SNAPSHOT.equals(range) || VERSION_ATTR_PROJECT.equals(range)) {
return getBundleFromProject(bsn, attrs);
} else if (VERSION_ATTR_HASH.equals(range)) {
return getBundleByHash(bsn, attrs);
}
Strategy useStrategy = strategy;
if (VERSION_ATTR_LATEST.equals(range)) {
Container c = getBundleFromProject(bsn, attrs);
if (c != null)
return c;
useStrategy = Strategy.HIGHEST;
}
useStrategy = overrideStrategy(attrs, useStrategy);
RepoFilter repoFilter = parseRepoFilter(attrs);
List<RepositoryPlugin> plugins = workspace.getRepositories();
if (useStrategy == Strategy.EXACT) {
if (!Verifier.isVersion(range))
return new Container(this, bsn, range, Container.TYPE.ERROR, null, bsn + ";version=" + range + " Invalid version", null, null);
// For an exact range we just iterate over the repos
// and return the first we find.
Version version = new Version(range);
for (RepositoryPlugin plugin : plugins) {
DownloadBlocker blocker = new DownloadBlocker(this);
File result = plugin.get(bsn, version, attrs, blocker);
if (result != null)
return toContainer(bsn, range, attrs, result, blocker);
}
} else {
VersionRange versionRange = VERSION_ATTR_LATEST.equals(range) ? new VersionRange("0") : new VersionRange(range);
// We have a range search. Gather all the versions in all the repos
// and make a decision on that choice. If the same version is found
// in
// multiple repos we take the first
SortedMap<Version, RepositoryPlugin> versions = new TreeMap<Version, RepositoryPlugin>();
for (RepositoryPlugin plugin : plugins) {
if (repoFilter != null && !repoFilter.match(plugin))
continue;
try {
SortedSet<Version> vs = plugin.versions(bsn);
if (vs != null) {
for (Version v : vs) {
if (!versions.containsKey(v) && versionRange.includes(v))
versions.put(v, plugin);
}
}
} catch (UnsupportedOperationException ose) {
// To query, we must have a real version
if (!versions.isEmpty() && Verifier.isVersion(range)) {
Version version = new Version(range);
DownloadBlocker blocker = new DownloadBlocker(this);
File file = plugin.get(bsn, version, attrs, blocker);
// if it does, return this as a result
if (file != null)
return toContainer(bsn, range, attrs, file, blocker);
}
}
}
//
// We have to augment the list of returned versions
// with info from the workspace. We use null as a marker
// to indicate that it is a workspace project
//
SortedSet<Version> localVersions = getWorkspace().getWorkspaceRepository().versions(bsn);
for (Version v : localVersions) {
if (!versions.containsKey(v) && versionRange.includes(v))
versions.put(v, null);
}
if (!versions.isEmpty()) {
Version provider = null;
switch(useStrategy) {
case HIGHEST:
provider = versions.lastKey();
break;
case LOWEST:
provider = versions.firstKey();
break;
case EXACT:
// TODO need to handle exact better
break;
}
if (provider != null) {
RepositoryPlugin repo = versions.get(provider);
if (repo == null) {
// project
return getBundleFromProject(bsn, attrs);
}
String version = provider.toString();
DownloadBlocker blocker = new DownloadBlocker(this);
File result = repo.get(bsn, provider, attrs, blocker);
if (result != null)
return toContainer(bsn, version, attrs, result, blocker);
} else {
msgs.FoundVersions_ForStrategy_ButNoProvider(versions, useStrategy);
}
}
}
return new Container(this, bsn, range, Container.TYPE.ERROR, null, bsn + ";version=" + range + " Not found in " + plugins, null, null);
}
use of aQute.bnd.version.Version in project bnd by bndtools.
the class Project method getBundleByHash.
private Container getBundleByHash(String bsn, Map<String, String> attrs) throws Exception {
String hashStr = attrs.get("hash");
String algo = SHA_256;
String hash = hashStr;
int colonIndex = hashStr.indexOf(':');
if (colonIndex > -1) {
algo = hashStr.substring(0, colonIndex);
int afterColon = colonIndex + 1;
hash = (colonIndex < hashStr.length()) ? hashStr.substring(afterColon) : "";
}
for (RepositoryPlugin plugin : workspace.getRepositories()) {
// The plugin *may* understand version=hash directly
DownloadBlocker blocker = new DownloadBlocker(this);
File result = plugin.get(bsn, Version.LOWEST, Collections.unmodifiableMap(attrs), blocker);
// Service, use a capability search on the osgi.content namespace.
if (result == null && plugin instanceof Repository) {
Repository repo = (Repository) plugin;
if (!SHA_256.equals(algo))
// R5 repos only support SHA-256
continue;
Requirement contentReq = new CapReqBuilder(ContentNamespace.CONTENT_NAMESPACE).filter(String.format("(%s=%s)", ContentNamespace.CONTENT_NAMESPACE, hash)).buildSyntheticRequirement();
Set<Requirement> reqs = Collections.singleton(contentReq);
Map<Requirement, Collection<Capability>> providers = repo.findProviders(reqs);
Collection<Capability> caps = providers != null ? providers.get(contentReq) : null;
if (caps != null && !caps.isEmpty()) {
Capability cap = caps.iterator().next();
IdentityCapability idCap = ResourceUtils.getIdentityCapability(cap.getResource());
Map<String, Object> idAttrs = idCap.getAttributes();
String id = (String) idAttrs.get(IdentityNamespace.IDENTITY_NAMESPACE);
Object version = idAttrs.get(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE);
Version bndVersion = version != null ? Version.parseVersion(version.toString()) : Version.LOWEST;
if (!bsn.equals(id)) {
String error = String.format("Resource with requested hash does not match ID '%s' [hash: %s]", bsn, hashStr);
return new Container(this, bsn, "hash", Container.TYPE.ERROR, null, error, null, null);
}
result = plugin.get(id, bndVersion, null, blocker);
}
}
if (result != null)
return toContainer(bsn, "hash", attrs, result, blocker);
}
// If we reach this far, none of the repos found the resource.
return new Container(this, bsn, "hash", Container.TYPE.ERROR, null, "Could not find resource by content hash " + hashStr, null, null);
}
use of aQute.bnd.version.Version in project bnd by bndtools.
the class Project method getBundlesWildcard.
/**
* Get all bundles matching a wildcard expression.
*
* @param bsnPattern A bsn wildcard, e.g. "osgi*" or just "*".
* @param range A range to narrow the versions of bundles found, or null to
* return any version.
* @param strategyx The version selection strategy, which may be 'HIGHEST'
* or 'LOWEST' only -- 'EXACT' is not permitted.
* @param attrs Additional search attributes.
* @throws Exception
*/
public List<Container> getBundlesWildcard(String bsnPattern, String range, Strategy strategyx, Map<String, String> attrs) throws Exception {
if (VERSION_ATTR_SNAPSHOT.equals(range) || VERSION_ATTR_PROJECT.equals(range))
return Collections.singletonList(new Container(this, bsnPattern, range, TYPE.ERROR, null, "Cannot use snapshot or project version with wildcard matches", null, null));
if (strategyx == Strategy.EXACT)
return Collections.singletonList(new Container(this, bsnPattern, range, TYPE.ERROR, null, "Cannot use exact version strategy with wildcard matches", null, null));
VersionRange versionRange;
if (range == null || VERSION_ATTR_LATEST.equals(range))
versionRange = new VersionRange("0");
else
versionRange = new VersionRange(range);
RepoFilter repoFilter = parseRepoFilter(attrs);
if (bsnPattern != null) {
bsnPattern = bsnPattern.trim();
if (bsnPattern.length() == 0 || bsnPattern.equals("*"))
bsnPattern = null;
}
SortedMap<String, Pair<Version, RepositoryPlugin>> providerMap = new TreeMap<String, Pair<Version, RepositoryPlugin>>();
List<RepositoryPlugin> plugins = workspace.getRepositories();
for (RepositoryPlugin plugin : plugins) {
if (repoFilter != null && !repoFilter.match(plugin))
continue;
List<String> bsns = plugin.list(bsnPattern);
if (bsns != null)
for (String bsn : bsns) {
SortedSet<Version> versions = plugin.versions(bsn);
if (versions != null && !versions.isEmpty()) {
Pair<Version, RepositoryPlugin> currentProvider = providerMap.get(bsn);
Version candidate;
switch(strategyx) {
case HIGHEST:
candidate = versions.last();
if (currentProvider == null || candidate.compareTo(currentProvider.getFirst()) > 0) {
providerMap.put(bsn, new Pair<Version, RepositoryPlugin>(candidate, plugin));
}
break;
case LOWEST:
candidate = versions.first();
if (currentProvider == null || candidate.compareTo(currentProvider.getFirst()) < 0) {
providerMap.put(bsn, new Pair<Version, RepositoryPlugin>(candidate, plugin));
}
break;
default:
// we shouldn't have reached this point!
throw new IllegalStateException("Cannot use exact version strategy with wildcard matches");
}
}
}
}
List<Container> containers = new ArrayList<Container>(providerMap.size());
for (Entry<String, Pair<Version, RepositoryPlugin>> entry : providerMap.entrySet()) {
String bsn = entry.getKey();
Version version = entry.getValue().getFirst();
RepositoryPlugin repo = entry.getValue().getSecond();
DownloadBlocker downloadBlocker = new DownloadBlocker(this);
File bundle = repo.get(bsn, version, attrs, downloadBlocker);
if (bundle != null && !bundle.getName().endsWith(".lib")) {
containers.add(new Container(this, bsn, range, Container.TYPE.REPO, bundle, null, attrs, downloadBlocker));
}
}
return containers;
}
Aggregations