use of org.osgi.framework.wiring.BundleWiring in project bndtools by bndtools.
the class Auxiliary method weave.
@Override
public void weave(WovenClass wovenClass) {
if (delta == null || delta.isEmpty())
return;
BundleWiring wiring = wovenClass.getBundleWiring();
if (wiring == null)
return;
if (wiring.getBundle() != bndlib)
return;
List<String> extra;
synchronized (this) {
extra = delta;
delta = null;
}
if (extra != null)
wovenClass.getDynamicImports().addAll(extra);
}
use of org.osgi.framework.wiring.BundleWiring in project enumerable by hraberg.
the class LambdaWeavingActivator method weave.
public void weave(WovenClass wovenClass) {
BundleWiring wiring = wovenClass.getBundleWiring();
ByteArrayInputStream in = new ByteArrayInputStream(wovenClass.getBytes());
byte[] newBytes = loader.transformClass(wiring.getClassLoader(), wovenClass.getClassName(), in);
if (newBytes != null)
wovenClass.setBytes(newBytes);
}
use of org.osgi.framework.wiring.BundleWiring in project camel by apache.
the class Activator method extenderCapabilityWired.
private boolean extenderCapabilityWired(Bundle bundle) {
BundleWiring wiring = bundle.adapt(BundleWiring.class);
if (wiring == null) {
return true;
}
List<BundleWire> requiredWires = wiring.getRequiredWires(EXTENDER_NAMESPACE);
for (BundleWire requiredWire : requiredWires) {
if (CAMEL_EXTENDER.equals(requiredWire.getCapability().getAttributes().get(EXTENDER_NAMESPACE))) {
if (this.bundleId == requiredWire.getProviderWiring().getBundle().getBundleId()) {
LOG.debug("Camel extender requirement of bundle {} correctly wired to this implementation", bundle.getBundleId());
return true;
} else {
LOG.info("Not processing bundle {} as it requires a camel extender but is not wired to the this implementation", bundle.getBundleId());
return false;
}
}
}
return true;
}
use of org.osgi.framework.wiring.BundleWiring in project karaf by apache.
the class GuardProxyCatalogTest method testProxyCreationThread.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testProxyCreationThread() throws Exception {
ProxyManager proxyManager = getProxyManager();
ConfigurationAdmin ca = EasyMock.createMock(ConfigurationAdmin.class);
EasyMock.expect(ca.listConfigurations(EasyMock.anyObject(String.class))).andReturn(null).anyTimes();
EasyMock.replay(ca);
ServiceReference pmSref = EasyMock.createMock(ServiceReference.class);
EasyMock.replay(pmSref);
ServiceReference pmSref2 = EasyMock.createMock(ServiceReference.class);
EasyMock.replay(pmSref2);
ServiceReference cmSref = EasyMock.createMock(ServiceReference.class);
EasyMock.replay(cmSref);
Bundle b = EasyMock.createMock(Bundle.class);
EasyMock.expect(b.getBundleId()).andReturn(23992734L).anyTimes();
EasyMock.replay(b);
BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
EasyMock.expect(bc.getBundle()).andReturn(b).anyTimes();
EasyMock.expect(bc.createFilter(EasyMock.isA(String.class))).andAnswer(() -> FrameworkUtil.createFilter((String) EasyMock.getCurrentArguments()[0])).anyTimes();
final ServiceListener[] pmListenerHolder = new ServiceListener[1];
String pmFilter = "(&(objectClass=" + ProxyManager.class.getName() + ")" + "(!(" + GuardProxyCatalog.PROXY_SERVICE_KEY + "=*)))";
bc.addServiceListener(EasyMock.isA(ServiceListener.class), EasyMock.eq(pmFilter));
EasyMock.expectLastCall().andAnswer(() -> {
pmListenerHolder[0] = (ServiceListener) EasyMock.getCurrentArguments()[0];
return null;
}).anyTimes();
EasyMock.expect(bc.getServiceReferences(EasyMock.anyObject(String.class), EasyMock.contains(ConfigurationAdmin.class.getName()))).andReturn(new ServiceReference[] { cmSref }).anyTimes();
EasyMock.expect(bc.getService(pmSref)).andReturn(proxyManager).anyTimes();
EasyMock.expect(bc.getService(pmSref2)).andReturn(proxyManager).anyTimes();
EasyMock.expect(bc.getService(cmSref)).andReturn(ca).anyTimes();
EasyMock.replay(bc);
// This should put a ServiceListener in the pmListenerHolder, the ServiceTracker does that
GuardProxyCatalog gpc = new GuardProxyCatalog(bc);
// The service being proxied has these properties
final Hashtable<String, Object> serviceProps = new Hashtable<>();
serviceProps.put(Constants.OBJECTCLASS, new String[] { TestServiceAPI.class.getName() });
serviceProps.put(Constants.SERVICE_ID, 162L);
final Map<ServiceReference<?>, Object> serviceMap = new HashMap<>();
// The mock bundle context for the bundle providing the service is set up here
BundleContext providerBC = EasyMock.createMock(BundleContext.class);
// These are the expected service properties of the proxy registration. Note the proxy marker...
final Hashtable<String, Object> expectedProxyProps = new Hashtable<>(serviceProps);
expectedProxyProps.put(GuardProxyCatalog.PROXY_SERVICE_KEY, Boolean.TRUE);
EasyMock.expect(providerBC.registerService(EasyMock.isA(String[].class), EasyMock.anyObject(), EasyMock.isA(Dictionary.class))).andAnswer((IAnswer) () -> {
Dictionary<String, Object> props = (Dictionary<String, Object>) EasyMock.getCurrentArguments()[2];
ServiceRegistration reg = EasyMock.createMock(ServiceRegistration.class);
ServiceReference sr = mockServiceReference(props);
EasyMock.expect(reg.getReference()).andReturn(sr).anyTimes();
reg.unregister();
EasyMock.expectLastCall().once();
EasyMock.replay(reg);
serviceMap.put(sr, EasyMock.getCurrentArguments()[1]);
return reg;
}).once();
EasyMock.expect(providerBC.getService(EasyMock.isA(ServiceReference.class))).andAnswer(() -> serviceMap.get(EasyMock.getCurrentArguments()[0])).anyTimes();
EasyMock.replay(providerBC);
// In some cases the proxy-creating code is looking for a classloader (e.g. when run through
// a coverage tool such as EclEmma). This will satisfy that.
BundleWiring bw = EasyMock.createMock(BundleWiring.class);
EasyMock.expect(bw.getClassLoader()).andReturn(getClass().getClassLoader()).anyTimes();
EasyMock.replay(bw);
// The mock bundle that provides the original service (and also the proxy is registered with this)
Bundle providerBundle = EasyMock.createNiceMock(Bundle.class);
EasyMock.expect(providerBundle.getBundleContext()).andReturn(providerBC).anyTimes();
EasyMock.expect(providerBundle.adapt(BundleWiring.class)).andReturn(bw).anyTimes();
EasyMock.replay(providerBundle);
ServiceReference sr = mockServiceReference(providerBundle, serviceProps);
assertEquals("Precondition", 0, gpc.proxyMap.size());
assertEquals("Precondition", 0, gpc.createProxyQueue.size());
// Create the proxy for the service
gpc.proxyIfNotAlreadyProxied(sr);
assertEquals(1, gpc.proxyMap.size());
assertEquals(1, gpc.createProxyQueue.size());
// The actual proxy creation is done asynchronously.
GuardProxyCatalog.ServiceRegistrationHolder holder = gpc.proxyMap.get(162L);
assertNull("The registration shouldn't have happened yet", holder.registration);
assertEquals(1, gpc.createProxyQueue.size());
Thread[] tarray = new Thread[Thread.activeCount()];
Thread.enumerate(tarray);
for (Thread t : tarray) {
if (t != null) {
assertTrue(!GuardProxyCatalog.PROXY_CREATOR_THREAD_NAME.equals(t.getName()));
}
}
// make the proxy manager appear
pmListenerHolder[0].serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, pmSref));
// give the system some time to send the events...
Thread.sleep(400);
Thread ourThread = null;
Thread[] tarray2 = new Thread[Thread.activeCount()];
Thread.enumerate(tarray2);
for (Thread t : tarray2) {
if (t != null) {
if (t.getName().equals(GuardProxyCatalog.PROXY_CREATOR_THREAD_NAME)) {
ourThread = t;
}
}
}
assertNotNull(ourThread);
assertTrue(ourThread.isDaemon());
assertTrue(ourThread.isAlive());
assertNotNull(holder.registration);
assertEquals(0, gpc.createProxyQueue.size());
int numProxyThreads = 0;
pmListenerHolder[0].serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, pmSref2));
// give the system some time to send the events...
Thread.sleep(300);
Thread[] tarray3 = new Thread[Thread.activeCount()];
Thread.enumerate(tarray3);
for (Thread t : tarray3) {
if (t != null) {
if (t.getName().equals(GuardProxyCatalog.PROXY_CREATOR_THREAD_NAME)) {
numProxyThreads++;
}
}
}
assertEquals("Maximum 1 proxy thread, even if there is more than 1 proxy service", 1, numProxyThreads);
// Clean up thread
pmListenerHolder[0].serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, pmSref));
// Give the system some time to stop the threads...
Thread.sleep(300);
Thread[] tarray4 = new Thread[Thread.activeCount()];
Thread.enumerate(tarray4);
for (Thread t : tarray4) {
if (t != null) {
assertTrue(!GuardProxyCatalog.PROXY_CREATOR_THREAD_NAME.equals(t.getName()));
}
}
}
use of org.osgi.framework.wiring.BundleWiring in project karaf by apache.
the class GuardProxyCatalogTest method testServiceFactoryBehaviour.
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testServiceFactoryBehaviour() throws Exception {
final Map<ServiceReference, Object> serviceMap = new HashMap<>();
TestServiceAPI testService = new TestService();
BundleContext bc = mockConfigAdminBundleContext();
GuardProxyCatalog gpc = new GuardProxyCatalog(bc);
// The service being proxied has these properties
long serviceID = 117L;
final Hashtable<String, Object> serviceProps = new Hashtable<>();
serviceProps.put(Constants.OBJECTCLASS, new String[] { TestServiceAPI.class.getName() });
serviceProps.put(Constants.SERVICE_ID, serviceID);
serviceProps.put("bar", 42L);
BundleContext providerBC = EasyMock.createMock(BundleContext.class);
EasyMock.expect(providerBC.registerService(EasyMock.isA(String[].class), EasyMock.anyObject(), EasyMock.isA(Dictionary.class))).andAnswer((IAnswer) () -> {
Dictionary<String, Object> props = (Dictionary<String, Object>) EasyMock.getCurrentArguments()[2];
ServiceRegistration reg = EasyMock.createMock(ServiceRegistration.class);
ServiceReference sr = mockServiceReference(props);
EasyMock.expect(reg.getReference()).andReturn(sr).anyTimes();
EasyMock.replay(reg);
serviceMap.put(sr, EasyMock.getCurrentArguments()[1]);
return reg;
}).once();
EasyMock.expect(providerBC.getService(EasyMock.isA(ServiceReference.class))).andAnswer(() -> serviceMap.get(EasyMock.getCurrentArguments()[0])).anyTimes();
EasyMock.replay(providerBC);
// In some cases the proxy-creating code is looking for a classloader (e.g. when run through
// a coverage tool such as EclEmma). This will satisfy that.
BundleWiring bw = EasyMock.createMock(BundleWiring.class);
EasyMock.expect(bw.getClassLoader()).andReturn(getClass().getClassLoader()).anyTimes();
EasyMock.replay(bw);
// The mock bundle that provides the original service (and also the proxy is registered with this)
Bundle providerBundle = EasyMock.createNiceMock(Bundle.class);
EasyMock.expect(providerBundle.getBundleContext()).andReturn(providerBC).anyTimes();
EasyMock.expect(providerBundle.adapt(BundleWiring.class)).andReturn(bw).anyTimes();
EasyMock.replay(providerBundle);
ServiceReference sr = mockServiceReference(providerBundle, serviceProps);
// The mock bundle context for the client bundle
BundleContext clientBC = EasyMock.createMock(BundleContext.class);
EasyMock.expect(clientBC.getService(sr)).andReturn(testService).anyTimes();
EasyMock.replay(clientBC);
// The mock bundle that consumes the service
Bundle clientBundle = EasyMock.createNiceMock(Bundle.class);
EasyMock.expect(clientBundle.getBundleContext()).andReturn(clientBC).anyTimes();
EasyMock.replay(clientBundle);
gpc.proxyIfNotAlreadyProxied(sr);
// The actual proxy creation is done asynchronously.
GuardProxyCatalog.ServiceRegistrationHolder holder = gpc.proxyMap.get(serviceID);
// Mimic the thread that works the queue to create the proxy
GuardProxyCatalog.CreateProxyRunnable runnable = gpc.createProxyQueue.take();
assertEquals(117L, runnable.getOriginalServiceID());
ProxyManager pm = getProxyManager();
runnable.run(pm);
// The runnable should have put the actual registration in the holder
ServiceReference<?> proxySR = holder.registration.getReference();
// Check that the proxy registration was done on the original provider bundle's context
EasyMock.verify(providerBC);
// Test that the actual proxy invokes the original service...
ServiceFactory proxyServiceSF = (ServiceFactory) serviceMap.get(proxySR);
TestServiceAPI proxyService = (TestServiceAPI) proxyServiceSF.getService(clientBundle, null);
assertNotSame("The proxy should not be the same object as the original service", testService, proxyService);
assertEquals("Doing it", proxyService.doit());
EasyMock.reset(clientBC);
EasyMock.expect(clientBC.ungetService(sr)).andReturn(true).once();
EasyMock.replay(clientBC);
proxyServiceSF.ungetService(clientBundle, null, proxyService);
EasyMock.verify(clientBC);
}
Aggregations