use of org.osgi.framework.ServiceEvent in project sling by apache.
the class SlingAuthenticatorServiceListenerTest method testAddRemoveRegistrations.
@Test
public void testAddRemoveRegistrations() {
final PathBasedHolderCache<AuthenticationRequirementHolder> cache = new PathBasedHolderCache<AuthenticationRequirementHolder>();
final BundleContext context = mock(BundleContext.class);
final SlingAuthenticatorServiceListener listener = SlingAuthenticatorServiceListener.createListener(context, cache);
final ServiceReference<?> ref1 = createServiceReference(new String[] { "/path1" });
listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref1));
final ServiceReference<?> ref2 = createServiceReference(new String[] { "/path2", "/path3" });
listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref2));
final ServiceReference<?> ref3 = createServiceReference(new String[] { "/path4", "/path5" });
listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref3));
assertPaths(cache, new String[] { "/path1", "/path2", "/path3", "/path4", "/path5" }, new ServiceReference<?>[] { ref1, ref2, ref2, ref3, ref3 });
listener.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, ref2));
assertPaths(cache, new String[] { "/path1", "/path4", "/path5" }, new ServiceReference<?>[] { ref1, ref3, ref3 });
listener.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, ref1));
assertPaths(cache, new String[] { "/path4", "/path5" }, new ServiceReference<?>[] { ref3, ref3 });
listener.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, ref3));
assertTrue(cache.getHolders().isEmpty());
}
use of org.osgi.framework.ServiceEvent in project ddf by codice.
the class RegistryStorePublisher method handleEvent.
@Override
public void handleEvent(Event event) {
if (((ServiceEvent) event.getProperty(EventConstants.EVENT)).getType() != ServiceEvent.MODIFIED) {
return;
}
String pid = event.getProperty(Constants.SERVICE_PID).toString();
Boolean previousAutoPush = registryStoreMap.get(pid);
if (previousAutoPush == null) {
return;
}
BundleContext bundleContext = getBundleContext();
if (bundleContext == null) {
return;
}
RegistryStore registryStore = (RegistryStore) bundleContext.getService(((ServiceEvent) event.getProperty(EventConstants.EVENT)).getServiceReference());
if (!previousAutoPush && registryStore.isAutoPush()) {
registryPublish(registryStore, PUBLISH);
registryStoreMap.put(pid, true);
} else if (previousAutoPush && !registryStore.isAutoPush()) {
registryPublish(registryStore, UNPUBLISH);
registryStoreMap.put(pid, false);
}
}
use of org.osgi.framework.ServiceEvent 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));
}
use of org.osgi.framework.ServiceEvent in project felix by apache.
the class ServiceRegistry method registerService.
// Caller is expected to fire REGISTERED event.
public ServiceRegistration registerService(Bundle bundle, String[] classNames, Object svcObj, Dictionary dict) {
ServiceRegistrationImpl reg = null;
synchronized (this) {
// Create the service registration.
reg = new ServiceRegistrationImpl(this, bundle, classNames, m_currentServiceId++, svcObj, dict);
// Keep track of registered hooks.
addHooks(classNames, svcObj, reg.getReference());
// Get the bundles current registered services.
ServiceRegistration[] regs = (ServiceRegistration[]) m_regsMap.get(bundle);
m_regsMap.put(bundle, addServiceRegistration(regs, reg));
m_regCapSet.addCapability(reg.getReference());
}
// Notify callback objects about registered service.
if (m_callbacks != null) {
m_callbacks.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, reg.getReference()), null);
}
return reg;
}
use of org.osgi.framework.ServiceEvent in project felix by apache.
the class ConnectorServiceTest method registerConnectionFactory.
private ServiceRegistration registerConnectionFactory(ConnectionFactoryMock connFactory, Dictionary props) {
// service reference for ConnectionFactory service
ServiceReference reference = (ServiceReference) Mockito.mock(ServiceReference.class);
Mockito.when(reference.getProperty(ConnectionFactory.IO_SCHEME)).thenReturn(props.get(ConnectionFactory.IO_SCHEME));
Mockito.when(reference.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(++m_serviceId));
Mockito.when(reference.getProperty(Constants.SERVICE_RANKING)).thenReturn(props.get(Constants.SERVICE_RANKING));
// service registration for ConnectionFactory service
ServiceRegistration registration = (ServiceRegistration) Mockito.mock(ServiceRegistration.class);
Mockito.when(registration.getReference()).thenReturn(reference);
// service event
ServiceEvent registeredEvent = (ServiceEvent) Mockito.mock(ServiceEvent.class);
Mockito.when(registeredEvent.getServiceReference()).thenReturn(reference);
Mockito.when(new Integer(registeredEvent.getType())).thenReturn(new Integer(ServiceEvent.REGISTERED));
Mockito.when(m_bundleContext.getService(reference)).thenReturn(connFactory);
// sending registration event
// service tracker for ConnectionFactory service used by ConnectorService
// will be informed about service registration
m_serviceListener.serviceChanged(registeredEvent);
return registration;
}
Aggregations