ReconfigurableClient implements IClient, MessagingClientFactoryListener { public static MessagingClientFactoryRegistry lookupMessagingClientFactoryRegistryService( OsgiHelper osgiHelper ) { MessagingClientFactoryRegistry result = null; final Logger logger = Logger.getLogger( ReconfigurableClient.class.getName()); BundleContext bundleCtx = osgiHelper.findBundleContext(); if( bundleCtx != null ) { logger.info( "The messaging registry is used in an OSGi environment." ); final ServiceReference<MessagingClientFactoryRegistry> reference = bundleCtx.getServiceReference( MessagingClientFactoryRegistry.class ); if( reference != null ) { logger.fine( "The service reference was found." ); result = bundleCtx.getService( reference ); } } else { logger.info( "The messaging registry is NOT used in an OSGi environment." ); } return result; } protected  ReconfigurableClient(); @Override void setDomain( String domain ); @Override String getDomain(); synchronized MessagingClientFactoryRegistry getRegistry(); synchronized void setRegistry( MessagingClientFactoryRegistry registry ); static MessagingClientFactoryRegistry lookupMessagingClientFactoryRegistryService( OsgiHelper osgiHelper ); @Override synchronized String getMessagingType(); void switchMessagingType( String factoryName ); @Override void addMessagingClientFactory( final IMessagingClientFactory factory ); @Override void removeMessagingClientFactory( IMessagingClientFactory factory ); @Override Map<String,String> getConfiguration(); abstract RecipientKind getOwnerKind(); void associateMessageProcessor( AbstractMessageProcessor<T> messageProcessor ); AbstractMessageProcessor<T> getMessageProcessor(); synchronized boolean hasValidClient(); IMessagingClient getMessagingClient();  }
@Test public void testLookupMessagingClientFactoryRegistryService_noReference() { BundleContext bundleCtx = Mockito.mock( BundleContext.class ); OsgiHelper osgiHelper = Mockito.mock( OsgiHelper.class ); Mockito.when( osgiHelper.findBundleContext()).thenReturn( bundleCtx ); MessagingClientFactoryRegistry registry = ReconfigurableClient.lookupMessagingClientFactoryRegistryService( osgiHelper ); Assert.assertNull( registry ); }
@Test @SuppressWarnings( "unchecked" ) public void testLookupMessagingClientFactoryRegistryService_withReference() { BundleContext bundleCtx = Mockito.mock( BundleContext.class ); OsgiHelper osgiHelper = Mockito.mock( OsgiHelper.class ); Mockito.when( osgiHelper.findBundleContext()).thenReturn( bundleCtx ); ServiceReference<MessagingClientFactoryRegistry> reference = Mockito.mock( ServiceReference.class ); Mockito.when( bundleCtx.getServiceReference( MessagingClientFactoryRegistry.class )).thenReturn( reference ); MessagingClientFactoryRegistry registryMock = Mockito.mock( MessagingClientFactoryRegistry.class ); Mockito.when( bundleCtx.getService( reference )).thenReturn( registryMock ); MessagingClientFactoryRegistry registry = ReconfigurableClient.lookupMessagingClientFactoryRegistryService( osgiHelper ); Assert.assertNotNull( registry ); Assert.assertEquals( registryMock, registry ); }