Search in sources :

Example 1 with EventHook

use of org.osgi.framework.hooks.service.EventHook in project felix by apache.

the class EventDispatcherTest method testFireServiceEvent.

public void testFireServiceEvent() {
    final Bundle b1 = getMockBundle();
    final Bundle b2 = getMockBundle();
    final Bundle b3 = getMockBundle();
    final Bundle b4 = getMockBundle();
    final Set calledHooks = new HashSet();
    final EventHook eh1 = new EventHook() {

        public void event(ServiceEvent event, Collection contexts) {
            calledHooks.add(this);
        }
    };
    final EventHook eh2 = new EventHook() {

        public void event(ServiceEvent event, Collection contexts) {
            calledHooks.add(this);
            for (Iterator it = contexts.iterator(); it.hasNext(); ) {
                BundleContext bc = (BundleContext) it.next();
                if (bc.getBundle() == b1) {
                    it.remove();
                }
                if (bc.getBundle() == b2) {
                    it.remove();
                }
            }
        }
    };
    Logger logger = new Logger();
    ServiceRegistry registry = new ServiceRegistry(logger, null);
    registry.registerService(b4, new String[] { EventHook.class.getName() }, eh1, new Hashtable());
    registry.registerService(b4, new String[] { EventHook.class.getName() }, eh2, new Hashtable());
    // -- Set up event dispatcher
    EventDispatcher ed = new EventDispatcher(logger, registry);
    // -- Register some listeners
    final List fired = Collections.synchronizedList(new ArrayList());
    ServiceListener sl1 = new ServiceListener() {

        public void serviceChanged(ServiceEvent arg0) {
            fired.add(this);
        }
    };
    ed.addListener(b1.getBundleContext(), ServiceListener.class, sl1, null);
    ServiceListener sl2 = new ServiceListener() {

        public void serviceChanged(ServiceEvent arg0) {
            fired.add(this);
        }
    };
    ed.addListener(b2.getBundleContext(), ServiceListener.class, sl2, null);
    ServiceListener sl3 = new ServiceListener() {

        public void serviceChanged(ServiceEvent arg0) {
            fired.add(this);
        }
    };
    ed.addListener(b3.getBundleContext(), ServiceListener.class, sl3, null);
    // --- make the invocation
    ServiceReference sr = EasyMock.createNiceMock(ServiceReference.class);
    EasyMock.expect(sr.getProperty(Constants.OBJECTCLASS)).andReturn(new String[] { "java.lang.String" }).anyTimes();
    sr.isAssignableTo(b1, String.class.getName());
    EasyMock.expectLastCall().andReturn(Boolean.TRUE).anyTimes();
    sr.isAssignableTo(b2, String.class.getName());
    EasyMock.expectLastCall().andReturn(Boolean.TRUE).anyTimes();
    sr.isAssignableTo(b3, String.class.getName());
    EasyMock.expectLastCall().andReturn(Boolean.TRUE).anyTimes();
    EasyMock.replay(new Object[] { sr });
    ServiceEvent event = new ServiceEvent(ServiceEvent.REGISTERED, sr);
    assertEquals("Precondition failed", 0, fired.size());
    Felix framework = new Felix(new HashMap());
    ed.fireServiceEvent(event, null, framework);
    assertEquals(1, fired.size());
    assertSame(sl3, fired.iterator().next());
    assertEquals(2, calledHooks.size());
    assertTrue(calledHooks.contains(eh1));
    assertTrue(calledHooks.contains(eh2));
}
Also used : ServiceListener(org.osgi.framework.ServiceListener) Bundle(org.osgi.framework.Bundle) ServiceReference(org.osgi.framework.ServiceReference) EventHook(org.osgi.framework.hooks.service.EventHook) ServiceEvent(org.osgi.framework.ServiceEvent) BundleContext(org.osgi.framework.BundleContext)

Example 2 with EventHook

use of org.osgi.framework.hooks.service.EventHook in project felix by apache.

the class ServiceRegistryTest method testRegisterEventHookService.

public void testRegisterEventHookService() {
    MockControl control = MockControl.createNiceControl(Bundle.class);
    Bundle b = (Bundle) control.getMock();
    control.replay();
    MockControl controlContext = MockControl.createNiceControl(BundleContext.class);
    BundleContext c = (BundleContext) controlContext.getMock();
    controlContext.expectAndReturn(c.getBundle(), b);
    controlContext.replay();
    ServiceRegistry sr = new ServiceRegistry(new Logger(), null);
    EventHook hook = new EventHook() {

        @Override
        public void event(ServiceEvent event, Collection contexts) {
        }
    };
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
    ServiceRegistration reg = sr.registerService(c.getBundle(), new String[] { EventHook.class.getName() }, hook, new Hashtable());
    assertEquals(1, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertTrue(sr.getHookRegistry().getHooks(EventHook.class).iterator().next() instanceof ServiceReference);
    assertSame(reg.getReference(), sr.getHookRegistry().getHooks(EventHook.class).iterator().next());
    assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
    assertEquals("Postcondition failed", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Postcondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
    sr.unregisterService(b, reg);
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
}
Also used : EventHook(org.osgi.framework.hooks.service.EventHook) MockControl(org.easymock.MockControl) Bundle(org.osgi.framework.Bundle) ServiceEvent(org.osgi.framework.ServiceEvent) Hashtable(java.util.Hashtable) Collection(java.util.Collection) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) ServiceReference(org.osgi.framework.ServiceReference)

Example 3 with EventHook

use of org.osgi.framework.hooks.service.EventHook in project felix by apache.

the class ServiceRegistryTest method testRegisterCombinedService.

public void testRegisterCombinedService() {
    MockControl control = MockControl.createNiceControl(Bundle.class);
    Bundle b = (Bundle) control.getMock();
    control.replay();
    MockControl controlContext = MockControl.createNiceControl(BundleContext.class);
    BundleContext c = (BundleContext) controlContext.getMock();
    controlContext.expectAndReturn(c.getBundle(), b);
    controlContext.replay();
    ServiceRegistry sr = new ServiceRegistry(new Logger(), null);
    class CombinedService implements ListenerHook, FindHook, EventHook, Runnable {

        @Override
        public void added(Collection listeners) {
        }

        @Override
        public void removed(Collection listener) {
        }

        @Override
        public void find(BundleContext context, String name, String filter, boolean allServices, Collection references) {
        }

        @Override
        public void event(ServiceEvent event, Collection contexts) {
        }

        @Override
        public void run() {
        }
    }
    CombinedService hook = new CombinedService();
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
    ServiceRegistration reg = sr.registerService(c.getBundle(), new String[] { Runnable.class.getName(), ListenerHook.class.getName(), FindHook.class.getName(), EventHook.class.getName() }, hook, new Hashtable());
    assertEquals(1, sr.getHookRegistry().getHooks(ListenerHook.class).size());
    assertSame(reg.getReference(), sr.getHookRegistry().getHooks(ListenerHook.class).iterator().next());
    assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
    assertEquals(1, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertSame(reg.getReference(), sr.getHookRegistry().getHooks(EventHook.class).iterator().next());
    assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
    assertEquals(1, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertSame(reg.getReference(), sr.getHookRegistry().getHooks(FindHook.class).iterator().next());
    assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
    sr.unregisterService(b, reg);
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
}
Also used : FindHook(org.osgi.framework.hooks.service.FindHook) Bundle(org.osgi.framework.Bundle) ListenerHook(org.osgi.framework.hooks.service.ListenerHook) Hashtable(java.util.Hashtable) EventHook(org.osgi.framework.hooks.service.EventHook) MockControl(org.easymock.MockControl) ServiceEvent(org.osgi.framework.ServiceEvent) Collection(java.util.Collection) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Aggregations

Bundle (org.osgi.framework.Bundle)3 BundleContext (org.osgi.framework.BundleContext)3 ServiceEvent (org.osgi.framework.ServiceEvent)3 EventHook (org.osgi.framework.hooks.service.EventHook)3 Collection (java.util.Collection)2 Hashtable (java.util.Hashtable)2 MockControl (org.easymock.MockControl)2 ServiceReference (org.osgi.framework.ServiceReference)2 ServiceRegistration (org.osgi.framework.ServiceRegistration)2 ServiceListener (org.osgi.framework.ServiceListener)1 FindHook (org.osgi.framework.hooks.service.FindHook)1 ListenerHook (org.osgi.framework.hooks.service.ListenerHook)1