Search in sources :

Example 11 with VersionRange

use of aQute.bnd.version.VersionRange 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);
}
Also used : RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) VersionRange(aQute.bnd.version.VersionRange) TreeMap(java.util.TreeMap) Version(aQute.bnd.version.Version) Strategy(aQute.bnd.service.Strategy) File(java.io.File)

Example 12 with VersionRange

use of aQute.bnd.version.VersionRange 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;
}
Also used : ArrayList(java.util.ArrayList) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) VersionRange(aQute.bnd.version.VersionRange) TreeMap(java.util.TreeMap) SortedSet(java.util.SortedSet) Version(aQute.bnd.version.Version) File(java.io.File) Pair(aQute.libg.tuple.Pair)

Example 13 with VersionRange

use of aQute.bnd.version.VersionRange in project bnd by bndtools.

the class ResourceBuilder method addRequireBundle.

public void addRequireBundle(String bsn, Attrs attrs) throws Exception {
    CapReqBuilder rbb = new CapReqBuilder(resource, BundleNamespace.BUNDLE_NAMESPACE);
    rbb.addDirectives(attrs);
    StringBuilder filter = new StringBuilder();
    filter.append("(").append(BundleNamespace.BUNDLE_NAMESPACE).append("=").append(bsn).append(")");
    String v = attrs.get(HostNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE);
    if (v != null && VersionRange.isOSGiVersionRange(v)) {
        VersionRange range = VersionRange.parseOSGiVersionRange(v);
        filter.insert(0, "(&");
        filter.append(toBundleVersionFilter(range));
        filter.append(")");
    }
    rbb.addDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE, filter.toString());
    addRequirement(rbb.buildRequirement());
}
Also used : VersionRange(aQute.bnd.version.VersionRange)

Example 14 with VersionRange

use of aQute.bnd.version.VersionRange in project bnd by bndtools.

the class GenericResolveContextResolveTest method testSimpleResolve.

/**
	 * Simple basic resolve. We use a small index with gogo + framework and then
	 * try to see if we can resolve the runtime from the shell requirement.
	 * 
	 * @throws Exception
	 */
public void testSimpleResolve() throws Exception {
    Repository repository = createRepo(IO.getFile("testdata/repo3.index.xml"));
    GenericResolveContext grc = new GenericResolveContext(logger);
    grc.setLevel(2);
    grc.addRepository(repository);
    grc.addFramework("org.apache.felix.framework", null);
    grc.addEE(EE.JavaSE_1_7);
    grc.addRequireBundle("org.apache.felix.gogo.shell", new VersionRange("[0,1]"));
    grc.done();
    Resolver resolver = new BndResolver(new ResolverLogger(4));
    Set<Resource> resources = resolver.resolve(grc).keySet();
    assertNotNull(getResource(resources, "org.apache.felix.gogo.runtime", "0.10"));
}
Also used : Repository(org.osgi.service.repository.Repository) Resolver(org.osgi.service.resolver.Resolver) Resource(org.osgi.resource.Resource) VersionRange(aQute.bnd.version.VersionRange)

Example 15 with VersionRange

use of aQute.bnd.version.VersionRange in project bndtools by bndtools.

the class ResolutionWizard method resourceToRunBundle.

private static VersionedClause resourceToRunBundle(Resource resource) {
    Capability idCap = ResourceUtils.getIdentityCapability(resource);
    String identity = ResourceUtils.getIdentity(idCap);
    // Map version range string, using "latest" for any workspace resources
    Attrs attribs = new Attrs();
    String versionRangeStr;
    if (isWorkspace(resource)) {
        versionRangeStr = VERSION_SNAPSHOT;
    } else {
        Version version = ResourceUtils.getVersion(idCap);
        VersionRange versionRange = createVersionRange(version);
        versionRangeStr = versionRange.toString();
    }
    attribs.put(Constants.VERSION_ATTRIBUTE, versionRangeStr);
    return new VersionedClause(identity, attribs);
}
Also used : Capability(org.osgi.resource.Capability) Version(org.osgi.framework.Version) VersionedClause(aQute.bnd.build.model.clauses.VersionedClause) Attrs(aQute.bnd.header.Attrs) VersionRange(aQute.bnd.version.VersionRange)

Aggregations

VersionRange (aQute.bnd.version.VersionRange)31 Version (aQute.bnd.version.Version)11 File (java.io.File)6 RepositoryPlugin (aQute.bnd.service.RepositoryPlugin)5 Attrs (aQute.bnd.header.Attrs)4 Parameters (aQute.bnd.header.Parameters)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)3 Matcher (java.util.regex.Matcher)3 Resource (org.osgi.resource.Resource)3 Project (aQute.bnd.build.Project)2 VersionedClause (aQute.bnd.build.model.clauses.VersionedClause)2 ResourceDescriptor (aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor)2 SortedList (aQute.lib.collections.SortedList)2 AndFilter (aQute.libg.filters.AndFilter)2 Filter (aQute.libg.filters.Filter)2 NotFilter (aQute.libg.filters.NotFilter)2 SimpleFilter (aQute.libg.filters.SimpleFilter)2 TreeMap (java.util.TreeMap)2