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;
}
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));
}
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);
}
}
}
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;
}
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;
}
Aggregations