Search in sources :

Example 71 with BundleWiring

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

the class Felix method addUninstalled.

private Set<Bundle> addUninstalled(Bundle bundle, Set<Bundle> refreshCandidates) {
    // Add this bundle first, so that it gets refreshed first
    refreshCandidates.add(bundle);
    BundleRevisions bundleRevisions = bundle.adapt(BundleRevisions.class);
    if (bundleRevisions != null) {
        for (BundleRevision br : bundleRevisions.getRevisions()) {
            BundleWiring bw = br.getWiring();
            if (bw != null) {
                for (BundleWire wire : bw.getRequiredWires(null)) {
                    Bundle b = wire.getProvider().getBundle();
                    if (b.getState() == Bundle.UNINSTALLED && !refreshCandidates.contains(b))
                        refreshCandidates = addUninstalled(b, refreshCandidates);
                }
            }
        }
    }
    Set<Bundle> dependent = populateDependentGraph(bundle, new HashSet<Bundle>());
    for (Bundle b : dependent) {
        if (b.getState() == Bundle.UNINSTALLED && !refreshCandidates.contains(b)) {
            refreshCandidates = addUninstalled(b, refreshCandidates);
        }
    }
    return refreshCandidates;
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleWire(org.osgi.framework.wiring.BundleWire) BundleRevisions(org.osgi.framework.wiring.BundleRevisions)

Example 72 with BundleWiring

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

the class RequirementsCapabilitiesTest method testIdentityCapabilityBundleFragment.

public void testIdentityCapabilityBundleFragment() throws Exception {
    String bmf = "Bundle-SymbolicName: cap.bundle\n" + "Bundle-Version: 1.2.3.Blah\n" + "Bundle-ManifestVersion: 2\n" + "Import-Package: org.osgi.framework\n";
    File bundleFile = createBundle(bmf);
    String fmf = "Bundle-SymbolicName: cap.frag\n" + "Bundle-Version: 1.0.0\n" + "Fragment-Host: cap.bundle\n" + "Bundle-ManifestVersion: 2\n" + "Export-Package: org.foo.bar;version=\"2.0.0\"\n" + "Import-Package: org.osgi.util.tracker\n";
    File fragFile = createBundle(fmf);
    Bundle b = felix.getBundleContext().installBundle(bundleFile.toURI().toASCIIString());
    Bundle f = felix.getBundleContext().installBundle(fragFile.toURI().toASCIIString());
    // Check the bundle capabilities.
    // First check the capabilities on the Bundle Revision, which is available on installed bundles
    BundleRevision bbr = b.adapt(BundleRevision.class);
    List<Capability> bwbCaps = bbr.getCapabilities("osgi.wiring.bundle");
    assertEquals(1, bwbCaps.size());
    Map<String, Object> expectedBWBAttrs = new HashMap<String, Object>();
    expectedBWBAttrs.put("osgi.wiring.bundle", "cap.bundle");
    expectedBWBAttrs.put("bundle-version", Version.parseVersion("1.2.3.Blah"));
    Capability expectedBWBCap = new TestCapability("osgi.wiring.bundle", expectedBWBAttrs, Collections.<String, String>emptyMap());
    assertCapsEquals(expectedBWBCap, bwbCaps.get(0));
    List<Capability> bwhCaps = bbr.getCapabilities("osgi.wiring.host");
    assertEquals(1, bwhCaps.size());
    Map<String, Object> expectedBWHAttrs = new HashMap<String, Object>();
    expectedBWHAttrs.put("osgi.wiring.host", "cap.bundle");
    expectedBWHAttrs.put("bundle-version", Version.parseVersion("1.2.3.Blah"));
    Capability expectedBWHCap = new TestCapability("osgi.wiring.host", expectedBWHAttrs, Collections.<String, String>emptyMap());
    assertCapsEquals(expectedBWHCap, bwhCaps.get(0));
    List<Capability> bwiCaps = bbr.getCapabilities("osgi.identity");
    assertEquals(1, bwiCaps.size());
    Map<String, Object> expectedBWIAttrs = new HashMap<String, Object>();
    expectedBWIAttrs.put("osgi.identity", "cap.bundle");
    expectedBWIAttrs.put("type", "osgi.bundle");
    expectedBWIAttrs.put("version", Version.parseVersion("1.2.3.Blah"));
    Capability expectedBWICap = new TestCapability("osgi.identity", expectedBWIAttrs, Collections.<String, String>emptyMap());
    assertCapsEquals(expectedBWICap, bwiCaps.get(0));
    assertEquals("The Bundle should not directly expose osgi.wiring.package", 0, bbr.getCapabilities("osgi.wiring.package").size());
    // Check the fragment's capabilities.
    // First check the capabilities on the Bundle Revision, which is available on installed fragments
    BundleRevision fbr = f.adapt(BundleRevision.class);
    List<Capability> fwpCaps = fbr.getCapabilities("osgi.wiring.package");
    assertEquals(1, fwpCaps.size());
    Map<String, Object> expectedFWAttrs = new HashMap<String, Object>();
    expectedFWAttrs.put("osgi.wiring.package", "org.foo.bar");
    expectedFWAttrs.put("version", Version.parseVersion("2"));
    expectedFWAttrs.put("bundle-symbolic-name", "cap.frag");
    expectedFWAttrs.put("bundle-version", Version.parseVersion("1.0.0"));
    Capability expectedFWCap = new TestCapability("osgi.wiring.package", expectedFWAttrs, Collections.<String, String>emptyMap());
    assertCapsEquals(expectedFWCap, fwpCaps.get(0));
    List<Capability> fiCaps = fbr.getCapabilities("osgi.identity");
    assertEquals(1, fiCaps.size());
    Map<String, Object> expectedFIAttrs = new HashMap<String, Object>();
    expectedFIAttrs.put("osgi.identity", "cap.frag");
    expectedFIAttrs.put("type", "osgi.fragment");
    expectedFIAttrs.put("version", Version.parseVersion("1.0.0"));
    Capability expectedFICap = new TestCapability("osgi.identity", expectedFIAttrs, Collections.<String, String>emptyMap());
    assertCapsEquals(expectedFICap, fiCaps.get(0));
    // Start the bundle. This will make the BundleWiring available on both the bundle and the fragment
    b.start();
    // Check the Bundle Wiring on the fragment. It should only contain the osgi.identity capability
    // All the other capabilities should have migrated to the bundle's BundleWiring.
    BundleWiring fbw = f.adapt(BundleWiring.class);
    List<BundleCapability> fbwCaps = fbw.getCapabilities(null);
    assertEquals("Fragment should only have 1 capability: it's osgi.identity", 1, fbwCaps.size());
    assertCapsEquals(expectedFICap, fbwCaps.get(0));
    // Check the Bundle Wiring on the bundle. It should contain all the capabilities originally on the
    // bundle and also contain the osgi.wiring.package capability from the fragment.
    BundleWiring bbw = b.adapt(BundleWiring.class);
    List<BundleCapability> bwbCaps2 = bbw.getCapabilities("osgi.wiring.bundle");
    assertEquals(1, bwbCaps2.size());
    assertCapsEquals(expectedBWBCap, bwbCaps2.get(0));
    List<BundleCapability> bwhCaps2 = bbw.getCapabilities("osgi.wiring.host");
    assertEquals(1, bwhCaps2.size());
    assertCapsEquals(expectedBWHCap, bwhCaps2.get(0));
    List<BundleCapability> bwiCaps2 = bbw.getCapabilities("osgi.identity");
    assertEquals(1, bwiCaps2.size());
    assertCapsEquals(expectedBWICap, bwiCaps2.get(0));
    List<BundleCapability> bwpCaps2 = bbw.getCapabilities("osgi.wiring.package");
    assertEquals("Bundle should have inherited the osgi.wiring.package capability from the fragment", 1, bwpCaps2.size());
    assertCapsEquals(expectedFWCap, bwpCaps2.get(0));
}
Also used : BundleCapability(org.osgi.framework.wiring.BundleCapability) Capability(org.osgi.resource.Capability) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleCapability(org.osgi.framework.wiring.BundleCapability) File(java.io.File)

Example 73 with BundleWiring

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

the class Activator method loadComponents.

/**
 * Loads the components of the given bundle. If the bundle has no
 * <i>Service-Component</i> header, this method has no effect. The
 * fragments of a bundle are not checked for the header (112.4.1).
 * <p>
 * This method calls the {@link Bundle#getBundleContext()} method to find
 * the <code>BundleContext</code> of the bundle. If the context cannot be
 * found, this method does not load components for the bundle.
 */
private void loadComponents(Bundle bundle) {
    if (bundle.getHeaders("").get(ComponentConstants.SERVICE_COMPONENT) == null) {
        // no components in the bundle, abandon
        return;
    }
    // there should be components, load them with a bundle context
    BundleContext context = bundle.getBundleContext();
    if (context == null) {
        log(LogService.LOG_DEBUG, m_bundle, "Cannot get BundleContext of bundle {0}/{1}", new Object[] { bundle.getSymbolicName(), bundle.getBundleId() }, null);
        return;
    }
    // Examine bundle for extender requirement; if present check if bundle is wired to us.
    BundleWiring wiring = bundle.adapt(BundleWiring.class);
    List<BundleWire> extenderWires = wiring.getRequiredWires(ExtenderNamespace.EXTENDER_NAMESPACE);
    try {
        for (BundleWire wire : extenderWires) {
            if (ComponentConstants.COMPONENT_CAPABILITY_NAME.equals(wire.getCapability().getAttributes().get(ExtenderNamespace.EXTENDER_NAMESPACE))) {
                if (!m_bundle.adapt(BundleRevision.class).equals(wire.getProvider())) {
                    log(LogService.LOG_DEBUG, m_bundle, "Bundle {0}/{1} wired to a different extender: {2}", new Object[] { bundle.getSymbolicName(), bundle.getBundleId(), wire.getProvider().getSymbolicName() }, null);
                    return;
                }
                break;
            }
        }
    } catch (NoSuchMethodError e) {
        log(LogService.LOG_DEBUG, m_bundle, "Cannot determine bundle wiring on pre R6 framework", null, null);
    }
    // FELIX-1666 method is called for the LAZY_ACTIVATION event and
    // the started event. Both events cause this method to be called;
    // so we have to make sure to not load components twice
    // FELIX-2231 Mark bundle loaded early to prevent concurrent loading
    // if LAZY_ACTIVATION and STARTED event are fired at the same time
    final boolean loaded;
    final Long bundleId = bundle.getBundleId();
    synchronized (m_componentBundles) {
        if (m_componentBundles.containsKey(bundleId)) {
            loaded = true;
        } else {
            m_componentBundles.put(bundleId, null);
            loaded = false;
        }
    }
    // terminate if already loaded (or currently being loaded)
    if (loaded) {
        log(LogService.LOG_DEBUG, m_bundle, "Components for bundle {0}/{1} already loaded. Nothing to do.", new Object[] { bundle.getSymbolicName(), bundle.getBundleId() }, null);
        return;
    }
    try {
        BundleComponentActivator ga = new BundleComponentActivator(this, m_componentRegistry, m_componentActor, context, m_configuration);
        ga.initialEnable();
        // replace bundle activator in the map
        synchronized (m_componentBundles) {
            m_componentBundles.put(bundleId, ga);
        }
    } catch (Exception e) {
        // not marked as being loaded
        synchronized (m_componentBundles) {
            m_componentBundles.remove(bundleId);
        }
        if (e instanceof IllegalStateException && bundle.getState() != Bundle.ACTIVE) {
            log(LogService.LOG_DEBUG, m_bundle, "Bundle {0}/{1} has been stopped while trying to activate its components. Trying again when the bundles gets started again.", new Object[] { bundle.getSymbolicName(), bundle.getBundleId() }, e);
        } else {
            log(LogService.LOG_ERROR, m_bundle, "Error while loading components of bundle {0}/{1}", new Object[] { bundle.getSymbolicName(), bundle.getBundleId() }, e);
        }
    }
}
Also used : BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleWire(org.osgi.framework.wiring.BundleWire) BundleContext(org.osgi.framework.BundleContext)

Example 74 with BundleWiring

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

the class BundleRevisionDependencies method getDependentBundles.

public synchronized Set<Bundle> getDependentBundles(Bundle bundle) {
    Set<Bundle> result = new HashSet<Bundle>();
    List<BundleRevision> revisions = bundle.adapt(BundleRevisions.class).getRevisions();
    for (BundleRevision revision : revisions) {
        // since their dependents are their hosts.
        if (Util.isFragment(revision)) {
            BundleWiring wiring = revision.getWiring();
            if (wiring != null) {
                for (BundleWire bw : wiring.getRequiredWires(null)) {
                    if (HostNamespace.HOST_NAMESPACE.equals(bw.getCapability().getNamespace())) {
                        result.add(bw.getProvider().getBundle());
                    }
                }
            }
        } else {
            Map<BundleCapability, Set<BundleWire>> caps = m_dependentsMap.get(revision);
            if (caps != null) {
                for (Entry<BundleCapability, Set<BundleWire>> entry : caps.entrySet()) {
                    for (BundleWire dependentWire : entry.getValue()) {
                        result.add(dependentWire.getRequirer().getBundle());
                    }
                }
            }
        }
    }
    return result;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleWire(org.osgi.framework.wiring.BundleWire) HashSet(java.util.HashSet) BundleRevisions(org.osgi.framework.wiring.BundleRevisions)

Example 75 with BundleWiring

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

the class DTOFactory method createBundleWiringDTOArray.

private static BundleWiringDTO[] createBundleWiringDTOArray(Bundle bundle) {
    BundleRevisions brs = bundle.adapt(BundleRevisions.class);
    if (brs == null || brs.getRevisions() == null)
        return null;
    List<BundleRevision> revisions = brs.getRevisions();
    BundleWiringDTO[] dtos = new BundleWiringDTO[revisions.size()];
    for (int i = 0; i < revisions.size(); i++) {
        BundleWiring wiring = revisions.get(i).getWiring();
        dtos[i] = createBundleWiringDTO(wiring);
    }
    return dtos;
}
Also used : BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleWiringDTO(org.osgi.framework.wiring.dto.BundleWiringDTO) BundleRevisions(org.osgi.framework.wiring.BundleRevisions)

Aggregations

BundleWiring (org.osgi.framework.wiring.BundleWiring)110 Bundle (org.osgi.framework.Bundle)61 BundleWire (org.osgi.framework.wiring.BundleWire)39 ArrayList (java.util.ArrayList)23 BundleRevision (org.osgi.framework.wiring.BundleRevision)23 BundleCapability (org.osgi.framework.wiring.BundleCapability)19 Test (org.junit.Test)15 HashMap (java.util.HashMap)13 Hashtable (java.util.Hashtable)12 List (java.util.List)12 BundleContext (org.osgi.framework.BundleContext)12 URL (java.net.URL)10 Dictionary (java.util.Dictionary)10 HashSet (java.util.HashSet)9 LinkedHashMap (java.util.LinkedHashMap)8 Version (org.osgi.framework.Version)7 BundleRevisions (org.osgi.framework.wiring.BundleRevisions)7 AbstractIntegrationTest (org.apache.aries.jmx.AbstractIntegrationTest)6 IAnswer (org.easymock.IAnswer)6 ServiceReference (org.osgi.framework.ServiceReference)6