Search in sources :

Example 41 with Version

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);
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) Capability(org.osgi.resource.Capability) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) Requirement(org.osgi.resource.Requirement) Repository(org.osgi.service.repository.Repository) Version(aQute.bnd.version.Version) Collection(java.util.Collection) File(java.io.File)

Example 42 with Version

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;
}
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 43 with Version

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

the class ProjectBuilder method getBaselineJar.

/**
	 * This method attempts to find the baseline jar for the current project. It
	 * reads the -baseline property and treats it as instructions. These
	 * instructions are matched against the bsns of the jars (think sub
	 * builders!). If they match, the sub builder is selected.
	 * <p>
	 * The instruction can then specify the following options:
	 * 
	 * <pre>
	 *  version :
	 * baseline version from repository file : a file path
	 * </pre>
	 * 
	 * If neither is specified, the current version is used to find the highest
	 * version (without qualifier) that is below the current version. If a
	 * version is specified, we take the highest version with the same base
	 * version.
	 * <p>
	 * Since baselining is expensive and easily generates errors you must enable
	 * it. The easiest solution is to {@code -baseline: *}. This will match all
	 * sub builders and will calculate the version.
	 * 
	 * @return a Jar or null
	 */
public Jar getBaselineJar() throws Exception {
    String bl = getProperty(Constants.BASELINE);
    if (bl == null || Constants.NONE.equals(bl))
        return null;
    Instructions baselines = new Instructions(getProperty(Constants.BASELINE));
    if (baselines.isEmpty())
        // no baselining
        return null;
    RepositoryPlugin repo = getBaselineRepo();
    if (repo == null)
        // errors reported already
        return null;
    String bsn = getBsn();
    Version version = new Version(getVersion());
    SortedSet<Version> versions = removeStagedAndFilter(repo.versions(bsn), repo, bsn);
    if (versions.isEmpty()) {
        // We have a repo
        Version v = Version.parseVersion(getVersion()).getWithoutQualifier();
        if (v.compareTo(Version.ONE) > 0) {
            warning("There is no baseline for %s in the baseline repo %s. The build is for version %s, which is higher than 1.0.0 which suggests that there should be a prior version.", getBsn(), repo, v);
        }
        return null;
    }
    for (Entry<Instruction, Attrs> e : baselines.entrySet()) {
        if (e.getKey().matches(bsn)) {
            Attrs attrs = e.getValue();
            Version target;
            if (attrs.containsKey("version")) {
                // Specified version!
                String v = attrs.get("version");
                if (!Verifier.isVersion(v)) {
                    error("Not a valid version in %s %s", Constants.BASELINE, v);
                    return null;
                }
                Version base = new Version(v);
                SortedSet<Version> later = versions.tailSet(base);
                if (later.isEmpty()) {
                    error("For baselineing %s-%s, specified version %s not found", bsn, version, base);
                    return null;
                }
                // First element is equal or next to the base we desire
                target = later.first();
            // Now, we could end up with a higher version than our
            // current
            // project
            } else if (attrs.containsKey("file")) {
                // Can be useful to specify a file
                // for example when copying a bundle with a public api
                File f = getProject().getFile(attrs.get("file"));
                if (f != null && f.isFile()) {
                    Jar jar = new Jar(f);
                    addClose(jar);
                    return jar;
                }
                error("Specified file for baseline but could not find it %s", f);
                return null;
            } else {
                target = versions.last();
            }
            if (target.getWithoutQualifier().compareTo(version.getWithoutQualifier()) > 0) {
                error("The baseline version %s is higher than the current version %s for %s in %s", target, version, bsn, repo);
                return null;
            }
            if (target.getWithoutQualifier().compareTo(version.getWithoutQualifier()) == 0) {
                if (isPedantic()) {
                    warning("Baselining against jar");
                }
            }
            File file = repo.get(bsn, target, attrs);
            if (file == null || !file.isFile()) {
                error("Decided on version %s-%s but cannot get file from repo %s", bsn, version, repo);
                return null;
            }
            Jar jar = new Jar(file);
            addClose(jar);
            return jar;
        }
    }
    // Ignore, nothing matched
    return null;
}
Also used : Version(aQute.bnd.version.Version) Attrs(aQute.bnd.header.Attrs) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) Instructions(aQute.bnd.osgi.Instructions) Jar(aQute.bnd.osgi.Jar) Instruction(aQute.bnd.osgi.Instruction) File(java.io.File)

Example 44 with Version

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

the class PackageInfo method replace.

/*
	 * Replace a version in a file based on a pattern. We search the pattern and
	 * if found we replace group 1 with the new version. If the found version
	 * matches the new version we bail out early.
	 */
private boolean replace(File target, final Version newVersion, Pattern pattern) throws IOException {
    String content = IO.collect(target);
    Matcher m = pattern.matcher(content);
    if (!m.find()) {
        return false;
    }
    Version oldVersion = new Version(m.group(1));
    if (newVersion.compareTo(oldVersion) == 0) {
        return true;
    }
    return replace(newVersion, content, m, target);
}
Also used : Matcher(java.util.regex.Matcher) Version(aQute.bnd.version.Version)

Example 45 with Version

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

the class PackageInfo method getVersion.

/*
	 * Get the version from the file using the pattern
	 */
private Version getVersion(File source, Pattern pattern) throws IOException {
    if (!source.isFile())
        return null;
    String content = IO.collect(source);
    Matcher m = pattern.matcher(content);
    if (!m.find())
        return null;
    return new Version(m.group(1));
}
Also used : Matcher(java.util.regex.Matcher) Version(aQute.bnd.version.Version)

Aggregations

Version (aQute.bnd.version.Version)168 File (java.io.File)64 RepositoryPlugin (aQute.bnd.service.RepositoryPlugin)27 Attrs (aQute.bnd.header.Attrs)19 MavenVersion (aQute.bnd.version.MavenVersion)19 ArrayList (java.util.ArrayList)19 HashMap (java.util.HashMap)18 IOException (java.io.IOException)17 Jar (aQute.bnd.osgi.Jar)16 Workspace (aQute.bnd.build.Workspace)13 Project (aQute.bnd.build.Project)12 RevisionRef (aQute.service.library.Library.RevisionRef)12 Matcher (java.util.regex.Matcher)12 Parameters (aQute.bnd.header.Parameters)11 VersionRange (aQute.bnd.version.VersionRange)11 SortedList (aQute.lib.collections.SortedList)9 Processor (aQute.bnd.osgi.Processor)8 ResourceDescriptor (aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor)8 FileNotFoundException (java.io.FileNotFoundException)8 LinkedHashMap (java.util.LinkedHashMap)8