Search in sources :

Example 1 with AllServiceListener

use of org.osgi.framework.AllServiceListener in project aries by apache.

the class ServiceStateTest method createService.

private void createService(StateConfig stateConfig, final List<Notification> received, final List<AttributeChangeNotification> attributeChanges) throws Exception {
    BundleContext context = mock(BundleContext.class);
    Logger logger = mock(Logger.class);
    ServiceState serviceState = new ServiceState(context, stateConfig, logger);
    ServiceReference reference = mock(ServiceReference.class);
    Bundle b1 = mock(Bundle.class);
    when(b1.getBundleId()).thenReturn(new Long(9));
    when(b1.getSymbolicName()).thenReturn("bundle");
    when(b1.getLocation()).thenReturn("file:/location");
    when(reference.getBundle()).thenReturn(b1);
    when(reference.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(44));
    when(reference.getProperty(Constants.OBJECTCLASS)).thenReturn(new String[] { "org.apache.aries.jmx.Mock" });
    when(context.getAllServiceReferences(null, null)).thenReturn(new ServiceReference[] { reference });
    ServiceEvent registeredEvent = mock(ServiceEvent.class);
    when(registeredEvent.getServiceReference()).thenReturn(reference);
    when(registeredEvent.getType()).thenReturn(ServiceEvent.REGISTERED);
    ServiceEvent modifiedEvent = mock(ServiceEvent.class);
    when(modifiedEvent.getServiceReference()).thenReturn(reference);
    when(modifiedEvent.getType()).thenReturn(ServiceEvent.MODIFIED);
    MBeanServer server = mock(MBeanServer.class);
    //setup for notification
    ObjectName objectName = new ObjectName(OBJECTNAME);
    serviceState.preRegister(server, objectName);
    serviceState.postRegister(true);
    //add NotificationListener to receive the events
    serviceState.addNotificationListener(new NotificationListener() {

        public void handleNotification(Notification notification, Object handback) {
            if (notification instanceof AttributeChangeNotification) {
                attributeChanges.add((AttributeChangeNotification) notification);
            } else {
                received.add(notification);
            }
        }
    }, null, null);
    // capture the ServiceListener registered with BundleContext to issue ServiceEvents
    ArgumentCaptor<AllServiceListener> argument = ArgumentCaptor.forClass(AllServiceListener.class);
    verify(context).addServiceListener(argument.capture());
    //send events
    AllServiceListener serviceListener = argument.getValue();
    serviceListener.serviceChanged(registeredEvent);
    serviceListener.serviceChanged(modifiedEvent);
    //shutdown dispatcher via unregister callback
    serviceState.postDeregister();
    //check the ServiceListener is cleaned up
    verify(context).removeServiceListener(serviceListener);
    ExecutorService dispatcher = serviceState.getEventDispatcher();
    assertTrue(dispatcher.isShutdown());
    dispatcher.awaitTermination(2, TimeUnit.SECONDS);
    assertTrue(dispatcher.isTerminated());
}
Also used : AttributeChangeNotification(javax.management.AttributeChangeNotification) Bundle(org.osgi.framework.Bundle) Logger(org.apache.aries.jmx.Logger) AttributeChangeNotification(javax.management.AttributeChangeNotification) Notification(javax.management.Notification) ServiceReference(org.osgi.framework.ServiceReference) ObjectName(javax.management.ObjectName) AllServiceListener(org.osgi.framework.AllServiceListener) ServiceEvent(org.osgi.framework.ServiceEvent) ExecutorService(java.util.concurrent.ExecutorService) BundleContext(org.osgi.framework.BundleContext) MBeanServer(javax.management.MBeanServer) NotificationListener(javax.management.NotificationListener)

Example 2 with AllServiceListener

use of org.osgi.framework.AllServiceListener in project aries by apache.

the class ServiceStateTest method testLifeCycleOfNotificationSupport.

@Test
public void testLifeCycleOfNotificationSupport() throws Exception {
    BundleContext context = mock(BundleContext.class);
    Logger logger = mock(Logger.class);
    ServiceState serviceState = new ServiceState(context, new StateConfig(), logger);
    MBeanServer server1 = mock(MBeanServer.class);
    MBeanServer server2 = mock(MBeanServer.class);
    ObjectName objectName = new ObjectName(OBJECTNAME);
    serviceState.preRegister(server1, objectName);
    serviceState.postRegister(true);
    // capture the ServiceListener registered with BundleContext to issue ServiceEvents
    ArgumentCaptor<AllServiceListener> argument = ArgumentCaptor.forClass(AllServiceListener.class);
    verify(context).addServiceListener(argument.capture());
    AllServiceListener serviceListener = argument.getValue();
    assertNotNull(serviceListener);
    ExecutorService dispatcher = serviceState.getEventDispatcher();
    //do registration with another server
    serviceState.preRegister(server2, objectName);
    serviceState.postRegister(true);
    // check no more actions on BundleContext
    argument = ArgumentCaptor.forClass(AllServiceListener.class);
    verify(context, atMost(1)).addServiceListener(argument.capture());
    assertEquals(1, argument.getAllValues().size());
    //do one unregister
    serviceState.postDeregister();
    //verify bundleListener not invoked
    verify(context, never()).removeServiceListener(serviceListener);
    assertFalse(dispatcher.isShutdown());
    //do second unregister and check cleanup
    serviceState.postDeregister();
    verify(context).removeServiceListener(serviceListener);
    assertTrue(dispatcher.isShutdown());
    dispatcher.awaitTermination(2, TimeUnit.SECONDS);
    assertTrue(dispatcher.isTerminated());
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Logger(org.apache.aries.jmx.Logger) BundleContext(org.osgi.framework.BundleContext) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) AllServiceListener(org.osgi.framework.AllServiceListener) Test(org.junit.Test)

Aggregations

ExecutorService (java.util.concurrent.ExecutorService)2 MBeanServer (javax.management.MBeanServer)2 ObjectName (javax.management.ObjectName)2 Logger (org.apache.aries.jmx.Logger)2 AllServiceListener (org.osgi.framework.AllServiceListener)2 BundleContext (org.osgi.framework.BundleContext)2 AttributeChangeNotification (javax.management.AttributeChangeNotification)1 Notification (javax.management.Notification)1 NotificationListener (javax.management.NotificationListener)1 Test (org.junit.Test)1 Bundle (org.osgi.framework.Bundle)1 ServiceEvent (org.osgi.framework.ServiceEvent)1 ServiceReference (org.osgi.framework.ServiceReference)1