Search in sources :

Example 11 with VersionRange

use of org.osgi.framework.VersionRange in project aries by apache.

the class AbstractClause method parseParameters.

protected static Map<String, Parameter> parseParameters(String clause, boolean replaceVersionWithVersionRange) {
    Map<String, Parameter> parameters = new HashMap<String, Parameter>();
    Matcher matcher = Patterns.PARAMETER.matcher(clause);
    while (matcher.find()) {
        Parameter parameter = ParameterFactory.create(matcher.group());
        if (replaceVersionWithVersionRange && (parameter instanceof VersionAttribute)) {
            parameter = new VersionRangeAttribute(new VersionRange(String.valueOf(parameter.getValue())));
        }
        parameters.put(parameter.getName(), parameter);
    }
    return parameters;
}
Also used : HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) VersionRange(org.osgi.framework.VersionRange)

Example 12 with VersionRange

use of org.osgi.framework.VersionRange in project aries 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.
 */
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<SimpleFilter>();
    for (Entry<String, Object> entry : attrs.entrySet()) {
        if (entry.getValue() instanceof VersionRange) {
            VersionRange vr = (VersionRange) entry.getValue();
            if (vr.getLeftType() == VersionRange.RIGHT_OPEN) {
                filters.add(new SimpleFilter(entry.getKey(), vr.getLeft().toString(), SimpleFilter.GTE));
            } else {
                SimpleFilter not = new SimpleFilter(null, new ArrayList(), SimpleFilter.NOT);
                ((List) not.getValue()).add(new SimpleFilter(entry.getKey(), vr.getLeft().toString(), SimpleFilter.LTE));
                filters.add(not);
            }
            if (vr.getRight() != null) {
                if (vr.getRightType() == VersionRange.RIGHT_OPEN) {
                    filters.add(new SimpleFilter(entry.getKey(), vr.getRight().toString(), SimpleFilter.LTE));
                } else {
                    SimpleFilter not = new SimpleFilter(null, new ArrayList(), SimpleFilter.NOT);
                    ((List) not.getValue()).add(new SimpleFilter(entry.getKey(), vr.getRight().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.osgi.framework.VersionRange) List(java.util.List) ArrayList(java.util.ArrayList)

Example 13 with VersionRange

use of org.osgi.framework.VersionRange in project aries by apache.

the class FragmentHostHeaderTest method testSymbolicName.

@Test
public void testSymbolicName() {
    String headerStr = "org.foo";
    FragmentHostHeader header = new FragmentHostHeader(headerStr);
    assertClauses(header, 1);
    assertSymbolicName(header.getClauses().iterator().next(), headerStr);
    assertBundleVersionAttribute(header.getClauses().iterator().next(), new VersionRange(VersionRange.LEFT_CLOSED, new Version("0"), null, VersionRange.RIGHT_OPEN));
}
Also used : Version(org.osgi.framework.Version) VersionRange(org.osgi.framework.VersionRange) Test(org.junit.Test)

Example 14 with VersionRange

use of org.osgi.framework.VersionRange in project aries by apache.

the class FragmentHostHeaderTest method testBundleVersionSingle.

@Test
public void testBundleVersionSingle() {
    String headerStr = "com.bar.foo;bundle-version=1.0";
    FragmentHostHeader header = new FragmentHostHeader(headerStr);
    assertClauses(header, 1);
    assertSymbolicName(header.getClauses().iterator().next(), "com.bar.foo");
    assertBundleVersionAttribute(header.getClauses().iterator().next(), new VersionRange("1.0"));
}
Also used : VersionRange(org.osgi.framework.VersionRange) Test(org.junit.Test)

Example 15 with VersionRange

use of org.osgi.framework.VersionRange in project rt.equinox.framework by eclipse.

the class PackageAdminImpl method getBundles.

public Bundle[] getBundles(String symbolicName, String versionRange) {
    if (symbolicName == null) {
        throw new IllegalArgumentException();
    }
    if (Constants.SYSTEM_BUNDLE_SYMBOLICNAME.equals(symbolicName)) {
        // need to alias system.bundle to the implementation BSN
        symbolicName = EquinoxContainer.NAME;
    }
    VersionRange range = versionRange == null ? null : new VersionRange(versionRange);
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$
    String filter = (range != null ? "(&" : "") + "(" + IdentityNamespace.IDENTITY_NAMESPACE + "=" + symbolicName + ")" + (range != null ? range.toFilterString(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE) + ")" : "");
    Requirement identityReq = ModuleContainer.createRequirement(IdentityNamespace.IDENTITY_NAMESPACE, Collections.<String, String>singletonMap(Namespace.REQUIREMENT_FILTER_DIRECTIVE, filter), Collections.<String, Object>emptyMap());
    Collection<BundleCapability> identityCaps = container.getFrameworkWiring().findProviders(identityReq);
    if (identityCaps.isEmpty()) {
        return null;
    }
    List<Bundle> sorted = new ArrayList<>(identityCaps.size());
    for (BundleCapability capability : identityCaps) {
        Bundle b = capability.getRevision().getBundle();
        // a sanity check incase this is an old revision
        if (symbolicName.equals(b.getSymbolicName()) && !sorted.contains(b)) {
            sorted.add(b);
        }
    }
    Collections.sort(sorted, new Comparator<Bundle>() {

        @Override
        public int compare(Bundle b1, Bundle b2) {
            return b2.getVersion().compareTo(b1.getVersion());
        }
    });
    if (sorted.isEmpty()) {
        return null;
    }
    return sorted.toArray(new Bundle[sorted.size()]);
}
Also used : Requirement(org.osgi.resource.Requirement) Bundle(org.osgi.framework.Bundle) RequiredBundle(org.osgi.service.packageadmin.RequiredBundle) ArrayList(java.util.ArrayList) VersionRange(org.osgi.framework.VersionRange) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Aggregations

VersionRange (org.osgi.framework.VersionRange)18 Version (org.osgi.framework.Version)7 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 Map (java.util.Map)3 ManifestElement (org.eclipse.osgi.util.ManifestElement)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)2 Requirement (org.osgi.resource.Requirement)2 Artifact (io.fabric8.patch.management.Artifact)1 Artifact.isSameButVersion (io.fabric8.patch.management.Artifact.isSameButVersion)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 Matcher (java.util.regex.Matcher)1 SimpleFilter (org.apache.aries.subsystem.core.capabilityset.SimpleFilter)1 BasicRequirement (org.apache.aries.subsystem.core.internal.BasicRequirement)1 LocationPattern (org.apache.karaf.features.LocationPattern)1 BundleReplacements (org.apache.karaf.features.internal.model.processing.BundleReplacements)1