use of org.osgi.framework.hooks.service.EventListenerHook in project aries by apache.
the class ManagedServiceFactoryUseSystemBundleTest method regiserHook.
@Before
public void regiserHook() throws BundleException {
context().getBundleByName(CM_BUNDLE).stop();
final BundleContext systemContext = context().getBundle(Constants.SYSTEM_BUNDLE_LOCATION).getBundleContext();
eventHook = context().registerService(EventListenerHook.class, new EventListenerHook() {
public void event(ServiceEvent event, Map contexts) {
if (CM_BUNDLE.equals(event.getServiceReference().getBundle().getSymbolicName())) {
// hide from everything but the system bundle
// TODO on R6 we should be able to even try hiding from the system bundle
// R5 it was not clear if hooks could hide from the system bundle
// equinox R5 does allow hiding from system bundle
contexts.keySet().retainAll(Collections.singleton(systemContext));
}
}
}, null);
findHook = context().registerService(FindHook.class, new FindHook() {
public void find(BundleContext context, String arg1, String arg2, boolean arg3, Collection references) {
// equinox R5 does allow hiding from system bundle
if (!context.equals(systemContext)) {
for (Iterator<ServiceReference> iReferences = references.iterator(); iReferences.hasNext(); ) {
if (CM_BUNDLE.equals(iReferences.next().getBundle().getSymbolicName())) {
iReferences.remove();
}
}
}
}
}, null);
context().getBundleByName(CM_BUNDLE).start();
}
use of org.osgi.framework.hooks.service.EventListenerHook in project sling by apache.
the class ServiceUserMappedBundleFilterTest method testEvent.
@Test
public void testEvent() {
Map<BundleContext, Collection<ListenerHook.ListenerInfo>> map = new HashMap<BundleContext, Collection<ListenerHook.ListenerInfo>>();
map.put(bundleContext1, new ArrayList<ListenerHook.ListenerInfo>());
map.put(bundleContext2, new ArrayList<ListenerHook.ListenerInfo>());
ServiceEvent serviceEvent = mock(ServiceEvent.class);
ServiceReference serviceReference = mock(ServiceReference.class);
when(serviceEvent.getServiceReference()).thenReturn(serviceReference);
when(serviceReference.getProperty(Constants.OBJECTCLASS)).thenReturn(new String[] { ServiceUserMappedImpl.SERVICEUSERMAPPED });
when(serviceReference.getProperty(Mapping.SERVICENAME)).thenReturn(BUNDLE1);
EventListenerHook eventListenerHook = new ServiceUserMappedBundleFilter();
eventListenerHook.event(serviceEvent, map);
TestCase.assertEquals(1, map.size());
TestCase.assertTrue(map.containsKey(bundleContext1));
}
Aggregations