use of org.osgi.framework.wiring.BundleWire in project aries by apache.
the class ClientWeavingHookGenericCapabilityTest method testHeadersFromFragment.
@Test
public void testHeadersFromFragment() throws Exception {
// Register the bundle that provides the SPI implementation.
Bundle providerBundle = mockProviderBundle("impl1", 1);
activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle, new HashMap<String, Object>());
Dictionary<String, String> fragmentConsumerHeaders = new Hashtable<String, String>();
fragmentConsumerHeaders.put(SpiFlyConstants.REQUIRE_CAPABILITY, SpiFlyConstants.CLIENT_REQUIREMENT);
Bundle fragment = EasyMock.createMock(Bundle.class);
EasyMock.expect(fragment.getHeaders()).andReturn(fragmentConsumerHeaders).anyTimes();
EasyMock.replay(fragment);
BundleRevision frev = EasyMock.createMock(BundleRevision.class);
EasyMock.expect(frev.getBundle()).andReturn(fragment).anyTimes();
EasyMock.replay(frev);
BundleRequirement req = EasyMock.createMock(BundleRequirement.class);
EasyMock.expect(req.getRevision()).andReturn(frev).anyTimes();
EasyMock.replay(req);
BundleWire wire = EasyMock.createMock(BundleWire.class);
EasyMock.expect(wire.getRequirement()).andReturn(req).anyTimes();
EasyMock.replay(wire);
List<BundleWire> wires = Collections.singletonList(wire);
BundleWiring wiring = EasyMock.createMock(BundleWiring.class);
EasyMock.expect(wiring.getProvidedWires("osgi.wiring.host")).andReturn(wires).anyTimes();
EasyMock.replay(wiring);
BundleRevision rev = EasyMock.createMock(BundleRevision.class);
EasyMock.expect(rev.getWiring()).andReturn(wiring).anyTimes();
EasyMock.replay(rev);
Bundle consumerBundle = mockConsumerBundle(new Hashtable<String, String>(), rev, providerBundle);
activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.REQUIRE_CAPABILITY);
Bundle spiFlyBundle = mockSpiFlyBundle("spifly", Version.parseVersion("1.9.4"), consumerBundle, providerBundle);
WeavingHook wh = new ClientWeavingHook(spiFlyBundle.getBundleContext(), activator);
// Weave the TestClient class.
URL clsUrl = getClass().getResource("TestClient.class");
Assert.assertNotNull("Precondition", clsUrl);
String clientClassName = "org.apache.aries.spifly.dynamic.TestClient";
WovenClass wc = new MyWovenClass(clsUrl, clientClassName, consumerBundle);
Assert.assertEquals("Precondition", 0, wc.getDynamicImports().size());
wh.weave(wc);
Assert.assertEquals(1, wc.getDynamicImports().size());
String di1 = "org.apache.aries.spifly;bundle-symbolic-name=spifly;bundle-version=1.9.4";
String di2 = "org.apache.aries.spifly;bundle-version=1.9.4;bundle-symbolic-name=spifly";
String di = wc.getDynamicImports().get(0);
Assert.assertTrue("Weaving should have added a dynamic import", di1.equals(di) || di2.equals(di));
// Invoke the woven class and check that it properly sets the TCCL so that the
// META-INF/services/org.apache.aries.mytest.MySPI file from impl1 is visible.
Class<?> cls = wc.getDefinedClass();
Method method = cls.getMethod("test", new Class[] { String.class });
Object result = method.invoke(cls.newInstance(), "hello");
Assert.assertEquals(Collections.singleton("olleh"), result);
}
use of org.osgi.framework.wiring.BundleWire in project aries by apache.
the class BundleWiringState method getRevisionWiring.
private CompositeData getRevisionWiring(BundleRevision revision, int revisionID, String namespace, Map<BundleRevision, Integer> revisionIDMap) {
BundleWiring wiring = revision.getWiring();
List<BundleCapability> capabilities = wiring.getCapabilities(namespace);
List<BundleRequirement> requirements = wiring.getRequirements(namespace);
List<BundleWire> providedWires = wiring.getProvidedWires(namespace);
List<BundleWire> requiredWires = wiring.getRequiredWires(namespace);
BundleWiringData data = new BundleWiringData(wiring.getBundle().getBundleId(), revisionID, capabilities, requirements, providedWires, requiredWires, revisionIDMap);
return data.toCompositeData();
}
use of org.osgi.framework.wiring.BundleWire in project karaf by apache.
the class Requirements method printMatchingRequirements.
private static boolean printMatchingRequirements(BundleWiring wiring, Pattern namespace) {
List<BundleWire> wires = wiring.getRequiredWires(null);
Map<BundleRequirement, List<BundleWire>> aggregateReqs = aggregateRequirements(namespace, wires);
List<BundleRequirement> allReqs = wiring.getRequirements(null);
boolean matches = false;
for (BundleRequirement req : allReqs) {
if (matchNamespace(namespace, req.getNamespace())) {
matches = true;
List<BundleWire> providers = aggregateReqs.get(req);
if (providers != null) {
System.out.println(req.getNamespace() + "; " + req.getDirectives().get(Constants.FILTER_DIRECTIVE) + " resolved by:");
for (BundleWire wire : providers) {
String msg;
Object keyAttr = wire.getCapability().getAttributes().get(wire.getCapability().getNamespace());
if (keyAttr != null) {
msg = wire.getCapability().getNamespace() + "; " + keyAttr + " " + getVersionFromCapability(wire.getCapability());
} else {
msg = wire.getCapability().toString();
}
msg = " " + msg + " from " + wire.getProviderWiring().getBundle();
System.out.println(msg);
}
} else {
System.out.println(req.getNamespace() + "; " + req.getDirectives().get(Constants.FILTER_DIRECTIVE) + " " + UNRESOLVED_MESSAGE);
}
}
}
return matches;
}
use of org.osgi.framework.wiring.BundleWire in project karaf by apache.
the class ShowBundleTree method printDuplicatePackages.
/*
* Check for bundles in the tree exporting the same package
* as a possible cause for 'Unresolved constraint...' on a uses-conflict
*/
private void printDuplicatePackages(Tree<Bundle> tree) {
Set<Bundle> bundles = tree.flatten();
Map<String, Set<Bundle>> exports = new HashMap<>();
for (Bundle bundle : bundles) {
for (BundleRevision revision : bundle.adapt(BundleRevisions.class).getRevisions()) {
BundleWiring wiring = revision.getWiring();
if (wiring != null) {
List<BundleWire> wires = wiring.getProvidedWires(BundleRevision.PACKAGE_NAMESPACE);
if (wires != null) {
for (BundleWire wire : wires) {
String name = wire.getCapability().getAttributes().get(BundleRevision.PACKAGE_NAMESPACE).toString();
exports.computeIfAbsent(name, k -> new HashSet<>()).add(bundle);
}
}
}
}
}
for (String pkg : exports.keySet()) {
if (exports.get(pkg).size() > 1) {
System.out.printf("%n");
System.out.printf("WARNING: multiple bundles are exporting package %s%n", pkg);
for (Bundle bundle : exports.get(pkg)) {
System.out.printf("- %s%n", bundle);
}
}
}
}
use of org.osgi.framework.wiring.BundleWire in project karaf by apache.
the class Capabilities method printMatchingCapabilities.
private static boolean printMatchingCapabilities(BundleWiring wiring, Pattern namespace) {
List<BundleWire> wires = wiring.getProvidedWires(null);
Map<BundleCapability, List<BundleWire>> aggregateCaps = aggregateCapabilities(namespace, wires);
List<BundleCapability> allCaps = wiring.getCapabilities(null);
boolean matches = false;
for (BundleCapability cap : allCaps) {
if (matchNamespace(namespace, cap.getNamespace())) {
matches = true;
List<BundleWire> dependents = aggregateCaps.get(cap);
Object keyAttr = cap.getAttributes().get(cap.getNamespace());
if (dependents != null) {
String msg;
if (keyAttr != null) {
msg = cap.getNamespace() + "; " + keyAttr + " " + getVersionFromCapability(cap);
} else {
msg = cap.toString();
}
msg = msg + " required by:";
System.out.println(msg);
for (BundleWire wire : dependents) {
System.out.println(" " + wire.getRequirerWiring().getBundle());
}
} else if (keyAttr != null) {
System.out.println(cap.getNamespace() + "; " + cap.getAttributes().get(cap.getNamespace()) + " " + getVersionFromCapability(cap) + " " + UNUSED_MESSAGE);
} else {
System.out.println(cap + " " + UNUSED_MESSAGE);
}
}
}
return matches;
}
Aggregations