Search in sources :

Example 41 with BundleCapability

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

the class BundleWiresTest method testFilterCandidates.

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

Example 42 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability in project navajo by Dexels.

the class CustomJavaFileFolder method enumerateWiring.

private void enumerateWiring(String packageName, List<JavaFileObject> result, Bundle b) {
    String packageNameDot = packageName.replaceAll("/", ".");
    if (b.getSymbolicName().startsWith("navajo.script")) {
        // ignore script bundles
        return;
    }
    BundleWiring bw = b.adapt(BundleWiring.class);
    if (bw == null) {
        return;
    }
    boolean foundExportedPackage = false;
    List<BundleCapability> sss = bw.getCapabilities("osgi.wiring.package");
    for (BundleCapability bundleWire : sss) {
        String exported = (String) bundleWire.getAttributes().get("osgi.wiring.package");
        if (packageNameDot.equals(exported)) {
            foundExportedPackage = true;
        }
    }
    if (!foundExportedPackage) {
        return;
    }
    Collection<String> cc = bw.listResources(packageName, null, BundleWiring.LISTRESOURCES_LOCAL);
    for (String resource : cc) {
        URL u = b.getResource(resource);
        if (u != null) {
            URI uri = null;
            try {
                uri = u.toURI();
                final CustomJavaFileObject customJavaFileObject = new CustomJavaFileObject(resource, uri, u.toURI(), Kind.CLASS);
                result.add(customJavaFileObject);
                contentMap.put(resource, customJavaFileObject);
            } catch (Exception e1) {
                logger.warn("URI failed for URL: {} ignoring.", u);
            }
        }
    }
}
Also used : BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleCapability(org.osgi.framework.wiring.BundleCapability) URI(java.net.URI) URL(java.net.URL)

Example 43 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability in project tycho by eclipse.

the class DependencyComputer method getRequiredBundlePackages.

private void getRequiredBundlePackages(ModuleWire requiredWire, Set<String> importedPackageNames, Map<String, Set<ModuleCapability>> packages, Map<ModuleWiring, Map<String, Set<ModuleCapability>>> allSources) {
    ModuleWiring providerWiring = requiredWire.getProviderWiring();
    for (ModuleCapability packageCapability : providerWiring.getModuleCapabilities(PackageNamespace.PACKAGE_NAMESPACE)) {
        String packageName = getPackageName(packageCapability);
        // if imported then packages from required bundles do not get added
        if (!importedPackageNames.contains(packageName)) {
            addAggregatePackageSource(packageCapability, packageName, requiredWire, packages, allSources);
        }
    }
    // get the declared packages
    Set<String> declaredPackageNames = new HashSet<>();
    for (BundleCapability declaredPackage : providerWiring.getRevision().getDeclaredCapabilities(PackageNamespace.PACKAGE_NAMESPACE)) {
        declaredPackageNames.add(getPackageName(declaredPackage));
    }
    // and from attached fragments
    for (BundleWire fragmentWire : providerWiring.getProvidedWires(HostNamespace.HOST_NAMESPACE)) {
        for (BundleCapability declaredPackage : fragmentWire.getRequirer().getDeclaredCapabilities(PackageNamespace.PACKAGE_NAMESPACE)) {
            declaredPackageNames.add(getPackageName(declaredPackage));
        }
    }
    for (ModuleWire packageWire : providerWiring.getRequiredModuleWires(PackageNamespace.PACKAGE_NAMESPACE)) {
        String packageName = getPackageName(packageWire.getCapability());
        if (!importedPackageNames.contains(packageName) && declaredPackageNames.contains(packageName)) {
            // if the package is a declared capability AND the wiring imports the package
            // then it is substituted
            addAggregatePackageSource(packageWire.getCapability(), packageName, packageWire, packages, allSources);
        }
    }
}
Also used : ModuleWire(org.eclipse.osgi.container.ModuleWire) ModuleWiring(org.eclipse.osgi.container.ModuleWiring) ModuleCapability(org.eclipse.osgi.container.ModuleCapability) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleWire(org.osgi.framework.wiring.BundleWire) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 44 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability in project ecf by eclipse.

the class RemoteServiceAdmin method getVersionForPackage.

private Version getVersionForPackage(final Bundle providingBundle, String packageName) {
    Version result = null;
    BundleRevision providingBundleRevision = AccessController.doPrivileged(new PrivilegedAction<BundleRevision>() {

        public BundleRevision run() {
            return providingBundle.adapt(BundleRevision.class);
        }
    });
    if (providingBundleRevision == null)
        return null;
    List<BundleCapability> providerCapabilities = providingBundleRevision.getDeclaredCapabilities(BundleRevision.PACKAGE_NAMESPACE);
    for (BundleCapability c : providerCapabilities) {
        result = getVersionForMatchingCapability(packageName, c);
        if (result != null)
            return result;
    }
    return result;
}
Also used : Version(org.osgi.framework.Version) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 45 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability in project ecf by eclipse.

the class RemoteServiceAdmin method getPackageVersionViaRequestingBundle.

private Version getPackageVersionViaRequestingBundle(String packageName, final Bundle requestingBundle, Version remoteVersion) {
    Version result = null;
    // First check the requesting bundle for the desired export package
    // capability
    BundleRevision requestingBundleRevision = AccessController.doPrivileged(new PrivilegedAction<BundleRevision>() {

        public BundleRevision run() {
            return requestingBundle.adapt(BundleRevision.class);
        }
    });
    if (requestingBundleRevision != null) {
        List<BundleCapability> requestingBundleCapabilities = requestingBundleRevision.getDeclaredCapabilities(BundleRevision.PACKAGE_NAMESPACE);
        for (BundleCapability requestingBundleCapability : requestingBundleCapabilities) {
            Version candidate = getVersionForMatchingCapability(packageName, requestingBundleCapability);
            // If found, set our result
            if (candidate != null) {
                if (remoteVersion != null && candidate.equals(remoteVersion))
                    return candidate;
                result = candidate;
            }
        }
    }
    // look in exported package that are wired to the requesting bundle
    if (result == null) {
        // look for wired exported packages
        BundleWiring requestingBundleWiring = requestingBundle.adapt(BundleWiring.class);
        if (requestingBundleWiring != null) {
            result = getPackageVersionForMatchingWire(packageName, requestingBundleWiring.getRequiredWires(BundleRevision.PACKAGE_NAMESPACE), BundleRevision.PACKAGE_NAMESPACE);
            // in wired require bundles
            if (result == null)
                result = getPackageVersionForMatchingWire(packageName, requestingBundleWiring.getRequiredWires(BundleRevision.BUNDLE_NAMESPACE), BundleRevision.BUNDLE_NAMESPACE);
        }
    }
    return result;
}
Also used : Version(org.osgi.framework.Version) BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Aggregations

BundleCapability (org.osgi.framework.wiring.BundleCapability)164 ArrayList (java.util.ArrayList)65 Bundle (org.osgi.framework.Bundle)57 BundleRevision (org.osgi.framework.wiring.BundleRevision)52 HashMap (java.util.HashMap)48 BundleWire (org.osgi.framework.wiring.BundleWire)47 BundleRequirement (org.osgi.framework.wiring.BundleRequirement)44 BundleWiring (org.osgi.framework.wiring.BundleWiring)44 List (java.util.List)41 Test (org.junit.Test)28 HashSet (java.util.HashSet)27 BundleException (org.osgi.framework.BundleException)25 ResolverHook (org.osgi.framework.hooks.resolver.ResolverHook)25 Collection (java.util.Collection)24 Set (java.util.Set)24 Version (org.osgi.framework.Version)22 Map (java.util.Map)18 ResolverHookFactory (org.osgi.framework.hooks.resolver.ResolverHookFactory)18 File (java.io.File)16 Module (org.eclipse.osgi.container.Module)14