Search in sources :

Example 1 with EventListenerHook

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();
}
Also used : EventListenerHook(org.osgi.framework.hooks.service.EventListenerHook) FindHook(org.osgi.framework.hooks.service.FindHook) ServiceEvent(org.osgi.framework.ServiceEvent) Collection(java.util.Collection) Map(java.util.Map) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Before(org.junit.Before)

Example 2 with EventListenerHook

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));
}
Also used : EventListenerHook(org.osgi.framework.hooks.service.EventListenerHook) HashMap(java.util.HashMap) EventListenerHook(org.osgi.framework.hooks.service.EventListenerHook) ListenerHook(org.osgi.framework.hooks.service.ListenerHook) ServiceEvent(org.osgi.framework.ServiceEvent) Collection(java.util.Collection) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

Collection (java.util.Collection)2 BundleContext (org.osgi.framework.BundleContext)2 ServiceEvent (org.osgi.framework.ServiceEvent)2 ServiceReference (org.osgi.framework.ServiceReference)2 EventListenerHook (org.osgi.framework.hooks.service.EventListenerHook)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Before (org.junit.Before)1 Test (org.junit.Test)1 FindHook (org.osgi.framework.hooks.service.FindHook)1 ListenerHook (org.osgi.framework.hooks.service.ListenerHook)1