Search in sources :

Example 1 with Strategy

use of aQute.bnd.service.Strategy 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 2 with Strategy

use of aQute.bnd.service.Strategy in project bnd by bndtools.

the class Project method _repo.

public String _repo(String[] args) throws Exception {
    if (args.length < 2) {
        msgs.RepoTooFewArguments(_repoHelp, args);
        return null;
    }
    String bsns = args[1];
    String version = null;
    Strategy strategy = Strategy.HIGHEST;
    if (args.length > 2) {
        version = args[2];
        if (args.length == 4) {
            if (args[3].equalsIgnoreCase("HIGHEST"))
                strategy = Strategy.HIGHEST;
            else if (args[3].equalsIgnoreCase("LOWEST"))
                strategy = Strategy.LOWEST;
            else if (args[3].equalsIgnoreCase("EXACT"))
                strategy = Strategy.EXACT;
            else
                msgs.InvalidStrategy(_repoHelp, args);
        }
    }
    Collection<String> parts = split(bsns);
    List<String> paths = new ArrayList<String>();
    for (String bsn : parts) {
        Container container = getBundle(bsn, version, strategy, null);
        if (container.getError() != null) {
            error("${repo} macro refers to an artifact %s-%s (%s) that has an error: %s", bsn, version, strategy, container.getError());
        } else
            add(paths, container);
    }
    return join(paths);
}
Also used : ArrayList(java.util.ArrayList) Strategy(aQute.bnd.service.Strategy)

Aggregations

Strategy (aQute.bnd.service.Strategy)2 RepositoryPlugin (aQute.bnd.service.RepositoryPlugin)1 Version (aQute.bnd.version.Version)1 VersionRange (aQute.bnd.version.VersionRange)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1