Search in sources :

Example 16 with VersionRange

use of org.apache.felix.utils.version.VersionRange in project karaf by apache.

the class ResourceUtils method addIdentityRequirement.

public static void addIdentityRequirement(ResourceImpl resource, Resource required, boolean mandatory) {
    for (Capability cap : required.getCapabilities(null)) {
        if (cap.getNamespace().equals(IDENTITY_NAMESPACE)) {
            Map<String, Object> attributes = cap.getAttributes();
            Map<String, String> dirs = new HashMap<>();
            dirs.put(REQUIREMENT_RESOLUTION_DIRECTIVE, mandatory ? RESOLUTION_MANDATORY : RESOLUTION_OPTIONAL);
            Map<String, Object> attrs = new HashMap<>();
            attrs.put(IDENTITY_NAMESPACE, attributes.get(IDENTITY_NAMESPACE));
            attrs.put(CAPABILITY_TYPE_ATTRIBUTE, attributes.get(CAPABILITY_TYPE_ATTRIBUTE));
            Version version = (Version) attributes.get(CAPABILITY_VERSION_ATTRIBUTE);
            if (version != null) {
                attrs.put(CAPABILITY_VERSION_ATTRIBUTE, new VersionRange(version, true));
            }
            resource.addRequirement(new RequirementImpl(resource, IDENTITY_NAMESPACE, dirs, attrs));
        }
    }
}
Also used : Capability(org.osgi.resource.Capability) HashMap(java.util.HashMap) Version(org.osgi.framework.Version) VersionRange(org.apache.felix.utils.version.VersionRange)

Example 17 with VersionRange

use of org.apache.felix.utils.version.VersionRange in project karaf by apache.

the class SimpleFilter method convert.

/**
     * Converts a attribute map to a filter. The filter is created by iterating
     * over the map's entry set. If ordering of attributes is important (e.g.,
     * for hitting attribute indices), then the map's entry set should iterate
     * in the desired order. Equality testing is assumed for all attribute types
     * other than version ranges, which are handled appropriated. If the attribute
     * map is empty, then a filter that matches anything is returned.
     *
     * @param attrs Map of attributes to convert to a filter.
     * @return A filter corresponding to the attributes.
     */
@SuppressWarnings("unchecked")
public static SimpleFilter convert(Map<String, Object> attrs) {
    // Rather than building a filter string to be parsed into a SimpleFilter,
    // we will just create the parsed SimpleFilter directly.
    List<SimpleFilter> filters = new ArrayList<>(attrs.size());
    for (Entry<String, Object> entry : attrs.entrySet()) {
        if (entry.getValue() instanceof VersionRange) {
            VersionRange vr = (VersionRange) entry.getValue();
            if (!vr.isOpenFloor()) {
                filters.add(new SimpleFilter(entry.getKey(), vr.getFloor().toString(), SimpleFilter.GTE));
            } else {
                SimpleFilter not = new SimpleFilter(null, new ArrayList(), SimpleFilter.NOT);
                ((List<Object>) not.getValue()).add(new SimpleFilter(entry.getKey(), vr.getFloor().toString(), SimpleFilter.LTE));
                filters.add(not);
            }
            if (vr.getCeiling() != null) {
                if (!vr.isOpenCeiling()) {
                    filters.add(new SimpleFilter(entry.getKey(), vr.getCeiling().toString(), SimpleFilter.LTE));
                } else if (!vr.getCeiling().equals(VersionRange.INFINITE_VERSION)) {
                    SimpleFilter not = new SimpleFilter(null, new ArrayList(), SimpleFilter.NOT);
                    ((List<Object>) not.getValue()).add(new SimpleFilter(entry.getKey(), vr.getCeiling().toString(), SimpleFilter.GTE));
                    filters.add(not);
                }
            }
        } else {
            List<String> values = SimpleFilter.parseSubstring(entry.getValue().toString());
            if (values.size() > 1) {
                filters.add(new SimpleFilter(entry.getKey(), values, SimpleFilter.SUBSTRING));
            } else {
                filters.add(new SimpleFilter(entry.getKey(), values.get(0), SimpleFilter.EQ));
            }
        }
    }
    SimpleFilter sf = null;
    if (filters.size() == 1) {
        sf = filters.get(0);
    } else if (attrs.size() > 1) {
        sf = new SimpleFilter(null, filters, SimpleFilter.AND);
    } else if (filters.isEmpty()) {
        sf = new SimpleFilter(null, null, SimpleFilter.MATCH_ALL);
    }
    return sf;
}
Also used : ArrayList(java.util.ArrayList) VersionRange(org.apache.felix.utils.version.VersionRange) List(java.util.List) LinkedList(java.util.LinkedList) ArrayList(java.util.ArrayList)

Example 18 with VersionRange

use of org.apache.felix.utils.version.VersionRange in project karaf by apache.

the class Subsystem method addRequirement.

protected void addRequirement(String requirement) throws BundleException {
    for (Requirement req : ResourceBuilder.parseRequirement(this, requirement)) {
        Object range = req.getAttributes().get(CAPABILITY_VERSION_ATTRIBUTE);
        if (range instanceof String) {
            req.getAttributes().put(CAPABILITY_VERSION_ATTRIBUTE, new VersionRange((String) range));
        }
        addRequirement(req);
    }
}
Also used : ResourceUtils.addIdentityRequirement(org.apache.karaf.features.internal.resolver.ResourceUtils.addIdentityRequirement) ResourceUtils.toFeatureRequirement(org.apache.karaf.features.internal.resolver.ResourceUtils.toFeatureRequirement) Requirement(org.osgi.resource.Requirement) VersionRange(org.apache.felix.utils.version.VersionRange)

Example 19 with VersionRange

use of org.apache.felix.utils.version.VersionRange in project karaf by apache.

the class Blacklist method isFeatureBlacklisted.

public static boolean isFeatureBlacklisted(List<String> blacklist, String name, String version) {
    Clause[] clauses = Parser.parseClauses(blacklist.toArray(new String[blacklist.size()]));
    for (Clause clause : clauses) {
        if (clause.getName().equals(name)) {
            // Check feature version
            VersionRange range = VersionRange.ANY_VERSION;
            String vr = clause.getAttribute(BLACKLIST_RANGE);
            if (vr != null) {
                range = new VersionRange(vr, true);
            }
            if (range.contains(VersionTable.getVersion(version))) {
                String type = clause.getAttribute(BLACKLIST_TYPE);
                if (type == null || TYPE_FEATURE.equals(type)) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : VersionRange(org.apache.felix.utils.version.VersionRange) Clause(org.apache.felix.utils.manifest.Clause)

Example 20 with VersionRange

use of org.apache.felix.utils.version.VersionRange in project karaf by apache.

the class Builder method addFeatures.

private void addFeatures(Set<Feature> allFeatures, String feature, Set<Feature> features, boolean mandatory) {
    String name;
    VersionRange range;
    int idx = feature.indexOf('/');
    if (idx > 0) {
        name = feature.substring(0, idx);
        String version = feature.substring(idx + 1);
        version = version.trim();
        if (version.equals(org.apache.karaf.features.internal.model.Feature.DEFAULT_VERSION)) {
            range = new VersionRange(Version.emptyVersion);
        } else {
            range = new VersionRange(version, true, true);
        }
    } else {
        name = feature;
        range = new VersionRange(Version.emptyVersion);
    }
    Set<Feature> set = allFeatures.stream().filter(f -> f.getName().equals(name) && range.contains(VersionTable.getVersion(f.getVersion()))).collect(Collectors.toSet());
    if (mandatory && set.isEmpty()) {
        throw new IllegalStateException("Could not find matching feature for " + feature);
    }
    for (Feature f : set) {
        features.add(f);
        for (Dependency dep : f.getFeature()) {
            addFeatures(allFeatures, dep.toString(), features, !dep.isDependency() && !dep.isPrerequisite());
        }
    }
}
Also used : Manifest(java.util.jar.Manifest) Dependency(org.apache.karaf.features.internal.model.Dependency) Profile(org.apache.karaf.profile.Profile) Arrays(java.util.Arrays) Constants(org.osgi.framework.Constants) KarafPropertiesEditor(org.apache.karaf.tools.utils.KarafPropertiesEditor) LoggerFactory(org.slf4j.LoggerFactory) ConfigFile(org.apache.karaf.features.internal.model.ConfigFile) FeaturesService(org.apache.karaf.features.FeaturesService) java.nio.file(java.nio.file) DownloadManagerHelper(org.apache.karaf.features.internal.download.impl.DownloadManagerHelper) Clause(org.apache.felix.utils.manifest.Clause) ByteArrayInputStream(java.io.ByteArrayInputStream) JaxbUtil(org.apache.karaf.features.internal.model.JaxbUtil) Map(java.util.Map) URI(java.net.URI) VersionTable(org.apache.felix.utils.version.VersionTable) ZipEntry(java.util.zip.ZipEntry) BundleRevision(org.osgi.framework.wiring.BundleRevision) EnumSet(java.util.EnumSet) MavenResolver(org.ops4j.pax.url.mvn.MavenResolver) Blacklist(org.apache.karaf.features.internal.service.Blacklist) Deployer(org.apache.karaf.features.internal.service.Deployer) MapUtils(org.apache.karaf.features.internal.util.MapUtils) Collection(java.util.Collection) Set(java.util.Set) MANIFEST_NAME(java.util.jar.JarFile.MANIFEST_NAME) UUID(java.util.UUID) Library(org.apache.karaf.features.Library) Version(org.osgi.framework.Version) Attributes(java.util.jar.Attributes) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) VersionRange(org.apache.felix.utils.version.VersionRange) List(java.util.List) PropertiesLoader(org.apache.karaf.util.config.PropertiesLoader) Properties(org.apache.felix.utils.properties.Properties) Pattern(java.util.regex.Pattern) DownloadManager(org.apache.karaf.features.internal.download.DownloadManager) Features(org.apache.karaf.features.internal.model.Features) Dictionary(java.util.Dictionary) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DownloadCallback(org.apache.karaf.features.internal.download.DownloadCallback) HashMap(java.util.HashMap) ResourceBuilder(org.apache.karaf.features.internal.resolver.ResourceBuilder) Kar(org.apache.karaf.kar.internal.Kar) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) StreamProvider(org.apache.karaf.features.internal.download.StreamProvider) ProfileBuilder(org.apache.karaf.profile.ProfileBuilder) Parser(org.apache.karaf.util.maven.Parser) BaseRepository(org.apache.karaf.features.internal.repository.BaseRepository) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Bundle(org.apache.karaf.features.internal.model.Bundle) MavenResolvers(org.ops4j.pax.url.mvn.MavenResolvers) VersionUtils(org.apache.karaf.profile.versioning.VersionUtils) Downloader(org.apache.karaf.features.internal.download.Downloader) TYPE_REPOSITORY(org.apache.karaf.features.internal.service.Blacklist.TYPE_REPOSITORY) Content(org.apache.karaf.features.internal.model.Content) Hashtable(java.util.Hashtable) LinkedHashSet(java.util.LinkedHashSet) Logger(org.slf4j.Logger) Profiles(org.apache.karaf.profile.impl.Profiles) MalformedURLException(java.net.MalformedURLException) Conditional(org.apache.karaf.features.internal.model.Conditional) Resource(org.osgi.resource.Resource) IOException(java.io.IOException) ResolverImpl(org.apache.felix.resolver.ResolverImpl) Resolver(org.osgi.service.resolver.Resolver) KarafPropertyEdits(org.apache.karaf.tools.utils.model.KarafPropertyEdits) File(java.io.File) TreeMap(java.util.TreeMap) Feature(org.apache.karaf.features.internal.model.Feature) Config(org.apache.karaf.features.internal.model.Config) Startup(org.apache.karaf.profile.assembly.Builder.Stage.Startup) Collections(java.util.Collections) InputStream(java.io.InputStream) VersionRange(org.apache.felix.utils.version.VersionRange) Dependency(org.apache.karaf.features.internal.model.Dependency) Feature(org.apache.karaf.features.internal.model.Feature)

Aggregations

VersionRange (org.apache.felix.utils.version.VersionRange)20 Version (org.osgi.framework.Version)8 Bundle (org.osgi.framework.Bundle)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Feature (org.apache.karaf.features.Feature)5 InputStream (java.io.InputStream)4 EnumSet (java.util.EnumSet)4 HashSet (java.util.HashSet)4 List (java.util.List)4 Set (java.util.Set)4 TreeSet (java.util.TreeSet)4 IOException (java.io.IOException)3 URL (java.net.URL)3 Collection (java.util.Collection)3 Collections (java.util.Collections)3 Hashtable (java.util.Hashtable)3 Map (java.util.Map)3 Manifest (java.util.jar.Manifest)3 Clause (org.apache.felix.utils.manifest.Clause)3