Search in sources :

Example 1 with SimpleFilter

use of org.apache.aries.subsystem.core.capabilityset.SimpleFilter in project aries by apache.

the class AbstractClauseBasedHeader method parseVersionRange.

protected static VersionRange parseVersionRange(List<SimpleFilter> filters) {
    SimpleFilter floor = null;
    SimpleFilter ceiling = null;
    for (SimpleFilter filter : filters) {
        switch(filter.getOperation()) {
            case SimpleFilter.EQ:
            case SimpleFilter.GTE:
                floor = filter;
                break;
            case SimpleFilter.LTE:
                ceiling = filter;
                break;
            case SimpleFilter.NOT:
                SimpleFilter negated = ((List<SimpleFilter>) filter.getValue()).get(0);
                switch(negated.getOperation()) {
                    case SimpleFilter.EQ:
                    case SimpleFilter.GTE:
                        ceiling = filter;
                        break;
                    case SimpleFilter.LTE:
                        floor = filter;
                        break;
                    default:
                        throw new IllegalArgumentException("Invalid filter: " + filter);
                }
                break;
            case SimpleFilter.PRESENT:
                /* This can happen with version ranges of the form 
					 * (1.5.0,2.0.0). The filter form will be
					 * (&(version=*)(!(version<=1.5.0))(!(version>=2.0.0)). The
					 * presence operator is required because an absent version
					 * attribute would otherwise match. These should simply be
					 * ignored for the purposes of converting back into a
					 * version range.
					 */
                break;
            default:
                throw new IllegalArgumentException("Invalid filter: " + filter);
        }
    }
    if (ceiling == null) {
        return new VersionRange(String.valueOf(floor.getValue()));
    }
    String range = new StringBuilder().append(floor.getOperation() == SimpleFilter.NOT ? '(' : '[').append(floor.getOperation() == SimpleFilter.NOT ? ((List<SimpleFilter>) floor.getValue()).get(0).getValue() : floor.getValue()).append(',').append(ceiling.getOperation() == SimpleFilter.NOT ? ((List<SimpleFilter>) ceiling.getValue()).get(0).getValue() : ceiling.getValue()).append(ceiling.getOperation() == SimpleFilter.NOT ? ')' : ']').toString();
    return new VersionRange(range);
}
Also used : SimpleFilter(org.apache.aries.subsystem.core.capabilityset.SimpleFilter) List(java.util.List) VersionRange(org.osgi.framework.VersionRange)

Example 2 with SimpleFilter

use of org.apache.aries.subsystem.core.capabilityset.SimpleFilter in project aries by apache.

the class Aries1453Test method shouldResolveFragmentHostWithVersionRangeAndMatchWithBundlesInThatRange.

@Test
public void shouldResolveFragmentHostWithVersionRangeAndMatchWithBundlesInThatRange() {
    FragmentHostHeader header = new FragmentHostHeader("host-bundle;bundle-version=\"[9.6.0,10)\"");
    FragmentHostRequirement requirement = new FragmentHostRequirement(header.getClauses().iterator().next(), null);
    FragmentHostCapability capability = new FragmentHostCapability(new BundleSymbolicNameHeader("host-bundle"), new BundleVersionHeader("9.6.1"), new Resource() {

        @Override
        public List<Capability> getCapabilities(String namespace) {
            return null;
        }

        @Override
        public List<Requirement> getRequirements(String namespace) {
            return null;
        }
    });
    String filterDirective = requirement.getDirectives().get(Namespace.REQUIREMENT_FILTER_DIRECTIVE);
    SimpleFilter simpleFilter = SimpleFilter.parse(filterDirective);
    CapabilitySet capabilitySet = new CapabilitySet(Arrays.asList(HostNamespace.HOST_NAMESPACE), true);
    capabilitySet.addCapability(capability);
    Set<Capability> capabilities = capabilitySet.match(simpleFilter, true);
    Assert.assertTrue(capabilities.size() == 1);
    Assert.assertSame(capabilities.iterator().next(), capability);
}
Also used : Capability(org.osgi.resource.Capability) Resource(org.osgi.resource.Resource) SimpleFilter(org.apache.aries.subsystem.core.capabilityset.SimpleFilter) CapabilitySet(org.apache.aries.subsystem.core.capabilityset.CapabilitySet) List(java.util.List) Test(org.junit.Test)

Aggregations

List (java.util.List)2 SimpleFilter (org.apache.aries.subsystem.core.capabilityset.SimpleFilter)2 CapabilitySet (org.apache.aries.subsystem.core.capabilityset.CapabilitySet)1 Test (org.junit.Test)1 VersionRange (org.osgi.framework.VersionRange)1 Capability (org.osgi.resource.Capability)1 Resource (org.osgi.resource.Resource)1