Search in sources :

Example 31 with BundleRequirement

use of org.osgi.framework.wiring.BundleRequirement in project karaf by apache.

the class BundleWiresTest method packageWire.

private BundleWire packageWire(String packageFilter, BundleCapability bundleRef) {
    BundleWire wire = c.createMock(BundleWire.class);
    BundleRequirement req = packageRequirement(packageFilter);
    expect(wire.getRequirement()).andReturn(req);
    expect(wire.getCapability()).andReturn(bundleRef);
    return wire;
}
Also used : BundleWire(org.osgi.framework.wiring.BundleWire) BundleRequirement(org.osgi.framework.wiring.BundleRequirement)

Example 32 with BundleRequirement

use of org.osgi.framework.wiring.BundleRequirement in project karaf by apache.

the class BundleWiresTest method testFilterMatches.

@Test
public void testFilterMatches() throws IOException {
    BundleWires wires = readFromFile();
    BundleRequirement req = packageRequirement(packageFilter);
    BundleCapability candidate1 = bundleCap(targetBundleId, targetBundleVersion);
    List<BundleCapability> candidates = new ArrayList<>();
    candidates.add(candidate1);
    BundleCapability matchingCandidate = bundleCap(targetBundleId, "1.1.0");
    candidates.add(matchingCandidate);
    c.replay();
    wires.filterMatches(req, candidates);
    assertEquals(1, candidates.size());
    assertEquals(candidate1, candidates.iterator().next());
    c.verify();
}
Also used : ArrayList(java.util.ArrayList) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) Test(org.junit.Test)

Example 33 with BundleRequirement

use of org.osgi.framework.wiring.BundleRequirement in project felix by apache.

the class StatefulResolver method isAllowedDynamicImport.

// This method duplicates a lot of logic from:
// ResolverImpl.getDynamicImportCandidates()
// This is only a rough check since it doesn't include resolver hooks.
boolean isAllowedDynamicImport(BundleRevision revision, String pkgName) {
    // package be dynamically imported.
    if ((revision.getWiring() == null) || pkgName.length() == 0) {
        return false;
    }
    // If the revision doesn't have dynamic imports, then just return
    // immediately.
    List<BundleRequirement> dynamics = Util.getDynamicRequirements(revision.getWiring().getRequirements(null));
    if ((dynamics == null) || dynamics.isEmpty()) {
        return false;
    }
    // attempt to dynamically import it.
    for (BundleCapability cap : revision.getWiring().getCapabilities(null)) {
        if (cap.getNamespace().equals(BundleRevision.PACKAGE_NAMESPACE) && cap.getAttributes().get(BundleRevision.PACKAGE_NAMESPACE).equals(pkgName)) {
            return false;
        }
    }
    // we cannot dynamically import it.
    if (((BundleWiringImpl) revision.getWiring()).hasPackageSource(pkgName)) {
        return false;
    }
    // Loop through the importer's dynamic requirements to determine if
    // there is a matching one for the package from which we want to
    // load a class.
    Map<String, Object> attrs = Collections.singletonMap(BundleRevision.PACKAGE_NAMESPACE, (Object) pkgName);
    BundleRequirementImpl req = new BundleRequirementImpl(revision, BundleRevision.PACKAGE_NAMESPACE, Collections.EMPTY_MAP, attrs);
    List<BundleCapability> candidates = findProviders(req, false);
    // Try to find a dynamic requirement that matches the capabilities.
    BundleRequirementImpl dynReq = null;
    for (int dynIdx = 0; (candidates.size() > 0) && (dynReq == null) && (dynIdx < dynamics.size()); dynIdx++) {
        for (Iterator<BundleCapability> itCand = candidates.iterator(); (dynReq == null) && itCand.hasNext(); ) {
            Capability cap = itCand.next();
            if (CapabilitySet.matches(cap, ((BundleRequirementImpl) dynamics.get(dynIdx)).getFilter())) {
                dynReq = (BundleRequirementImpl) dynamics.get(dynIdx);
            }
        }
    }
    // any candidates that do not match it.
    if (dynReq != null) {
        for (Iterator<BundleCapability> itCand = candidates.iterator(); itCand.hasNext(); ) {
            Capability cap = itCand.next();
            if (!CapabilitySet.matches(cap, dynReq.getFilter())) {
                itCand.remove();
            }
        }
    } else {
        candidates.clear();
    }
    return !candidates.isEmpty();
}
Also used : BundleCapability(org.osgi.framework.wiring.BundleCapability) Capability(org.osgi.resource.Capability) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) BundleRequirementImpl(org.apache.felix.framework.wiring.BundleRequirementImpl)

Example 34 with BundleRequirement

use of org.osgi.framework.wiring.BundleRequirement in project felix by apache.

the class ManifestParserTest method testConvertNativeCode.

public void testConvertNativeCode() throws InvalidSyntaxException {
    List<NativeLibraryClause> nativeLibraryClauses = new ArrayList<NativeLibraryClause>();
    String[] libraryFiles = { "lib/http.dll", "lib/zlib.dll" };
    String[] osNames = { "Windows95", "Windows98", "WindowsNT" };
    String[] processors = { "x86" };
    String[] osVersions = null;
    String[] languages = { "en", "se" };
    String selectionFilter = "(com.acme.windowing=win32)";
    NativeLibraryClause clause = new NativeLibraryClause(libraryFiles, osNames, processors, osVersions, languages, selectionFilter);
    BundleRevision owner = mock(BundleRevision.class);
    nativeLibraryClauses.add(clause);
    List<BundleRequirement> nativeBundleReq = ManifestParser.convertNativeCode(owner, nativeLibraryClauses, false);
    BundleRequirement ir = findRequirement(nativeBundleReq, NativeNamespace.NATIVE_NAMESPACE);
    String filterStr = (String) ir.getDirectives().get(NativeNamespace.REQUIREMENT_FILTER_DIRECTIVE);
    Filter actualFilter = FrameworkUtil.createFilter(filterStr);
    Filter expectedFilter = FrameworkUtil.createFilter("(&(|" + "(osgi.native.osname~=windows95)(osgi.native.osname~=windows98)(osgi.native.osname~=windowsnt)" + ")" + "(osgi.native.processor~=x86)" + "(|(osgi.native.language~=en)" + "(osgi.native.language~=se)" + ")" + "(com.acme.windowing=win32))");
    assertEquals("Filter Should contain native requirements", expectedFilter, actualFilter);
}
Also used : Filter(org.osgi.framework.Filter) ArrayList(java.util.ArrayList) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleRequirement(org.osgi.framework.wiring.BundleRequirement)

Example 35 with BundleRequirement

use of org.osgi.framework.wiring.BundleRequirement in project felix by apache.

the class Inspect method aggregateRequirements.

private static Map<BundleRequirement, List<BundleWire>> aggregateRequirements(List<String> namespace, List<BundleWire> wires) {
    // Aggregate matching capabilities.
    Map<BundleRequirement, List<BundleWire>> map = new HashMap<BundleRequirement, List<BundleWire>>();
    for (BundleWire wire : wires) {
        if (matchNamespace(namespace, wire.getRequirement().getNamespace())) {
            List<BundleWire> providers = map.get(wire.getRequirement());
            if (providers == null) {
                providers = new ArrayList<BundleWire>();
                map.put(wire.getRequirement(), providers);
            }
            providers.add(wire);
        }
    }
    return map;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) BundleWire(org.osgi.framework.wiring.BundleWire) BundleRequirement(org.osgi.framework.wiring.BundleRequirement)

Aggregations

BundleRequirement (org.osgi.framework.wiring.BundleRequirement)55 BundleCapability (org.osgi.framework.wiring.BundleCapability)23 ArrayList (java.util.ArrayList)20 BundleWire (org.osgi.framework.wiring.BundleWire)18 BundleRevision (org.osgi.framework.wiring.BundleRevision)17 HashMap (java.util.HashMap)15 Bundle (org.osgi.framework.Bundle)15 Test (org.junit.Test)14 BundleException (org.osgi.framework.BundleException)11 ResolverHook (org.osgi.framework.hooks.resolver.ResolverHook)10 BundleWiring (org.osgi.framework.wiring.BundleWiring)10 List (java.util.List)9 Collection (java.util.Collection)8 BundleRequirementImpl (org.apache.felix.framework.wiring.BundleRequirementImpl)8 ResolverHookFactory (org.osgi.framework.hooks.resolver.ResolverHookFactory)8 CompositeData (javax.management.openmbean.CompositeData)6 Module (org.eclipse.osgi.container.Module)6 ModuleContainer (org.eclipse.osgi.container.ModuleContainer)6 DummyContainerAdaptor (org.eclipse.osgi.tests.container.dummys.DummyContainerAdaptor)6 SimpleFilter (org.apache.felix.framework.capabilityset.SimpleFilter)5