Search in sources :

Example 36 with ServiceEvent

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());
}
Also used : ServiceEvent(org.osgi.framework.ServiceEvent) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 37 with ServiceEvent

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);
    }
}
Also used : RegistryStore(org.codice.ddf.registry.api.internal.RegistryStore) ServiceEvent(org.osgi.framework.ServiceEvent) BundleContext(org.osgi.framework.BundleContext)

Example 38 with ServiceEvent

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));
}
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)

Example 39 with ServiceEvent

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;
}
Also used : ServiceEvent(org.osgi.framework.ServiceEvent) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 40 with ServiceEvent

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;
}
Also used : ServiceEvent(org.osgi.framework.ServiceEvent) ServiceReference(org.osgi.framework.ServiceReference) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Aggregations

ServiceEvent (org.osgi.framework.ServiceEvent)57 ServiceReference (org.osgi.framework.ServiceReference)31 Test (org.junit.Test)24 BundleContext (org.osgi.framework.BundleContext)24 ServiceListener (org.osgi.framework.ServiceListener)20 Bundle (org.osgi.framework.Bundle)18 Hashtable (java.util.Hashtable)15 ServiceRegistration (org.osgi.framework.ServiceRegistration)13 Collection (java.util.Collection)6 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)6 IOException (java.io.IOException)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 Dictionary (java.util.Dictionary)4 HashMap (java.util.HashMap)4 ServiceRegistrationHolder (org.apache.karaf.service.guard.impl.GuardProxyCatalog.ServiceRegistrationHolder)4 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3