Search in sources :

Example 61 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project felix by apache.

the class ServiceRegistry method unregisterService.

/**
 * Unregister a service
 * @param bundle The bundle unregistering the service
 * @param reg The service registration
 */
public void unregisterService(final Bundle bundle, final ServiceRegistration<?> reg) {
    // If this is a hook, it should be removed.
    this.hookRegistry.removeHooks(reg.getReference());
    // Now remove the registered service.
    final List<ServiceRegistration<?>> regs = m_regsMap.get(bundle);
    if (regs != null) {
        // this is a per bundle list, therefore synchronizing this should be fine
        synchronized (regs) {
            regs.remove(reg);
        }
    }
    m_regCapSet.removeCapability((BundleCapabilityImpl) reg.getReference());
    // Notify callback objects about unregistering service.
    if (m_callbacks != null) {
        m_callbacks.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, reg.getReference()), null);
    }
    // Now forcibly unget the service object for all stubborn clients.
    final ServiceReference<?> ref = reg.getReference();
    ungetServices(ref);
    // Invalidate registration
    ((ServiceRegistrationImpl) reg).invalidate();
    // Bundles are allowed to get a reference while unregistering
    // get fresh set of bundles (should be empty, but this is a sanity check)
    ungetServices(ref);
    // Now flush all usage counts as the registration is invalid
    for (Bundle usingBundle : m_inUseMap.keySet()) {
        flushUsageCount(usingBundle, ref, null);
    }
}
Also used : ServiceEvent(org.osgi.framework.ServiceEvent) Bundle(org.osgi.framework.Bundle) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 62 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project felix by apache.

the class JaasConfigFactory method updated.

@SuppressWarnings("unchecked")
@Override
public void updated(String pid, Dictionary config) throws ConfigurationException {
    String className = trimToNull(PropertiesUtil.toString(config.get(JAAS_CLASS_NAME), null));
    String flag = trimToNull(PropertiesUtil.toString(config.get(LoginModuleFactory.JAAS_CONTROL_FLAG), "required"));
    int ranking = PropertiesUtil.toInteger(config.get(LoginModuleFactory.JAAS_RANKING), 0);
    // TODO support system property substitution e.g. ${user.home}
    // in property values
    Map options = PropertiesUtil.toMap(config.get(JAAS_OPTIONS), new String[0]);
    String realmName = trimToNull(PropertiesUtil.toString(config.get(LoginModuleFactory.JAAS_REALM_NAME), null));
    if (className == null) {
        log.log(LogService.LOG_WARNING, "Class name for the LoginModule is required. Configuration would be ignored" + config);
        return;
    }
    // Combine the config. As the jaas.options is required for capturing config
    // via felix webconsole. However in normal usage people would like to provide
    // key=value pair directly in config. So merge both to provide a combined
    // view
    Map combinedOptions = convertToMap(config);
    combinedOptions.putAll(options);
    LoginModuleProvider lmf = new ConfigLoginModuleProvider(realmName, className, combinedOptions, ControlFlag.from(flag).flag(), ranking, factory);
    ServiceRegistration reg = context.registerService(LoginModuleFactory.class.getName(), lmf, null);
    ServiceRegistration oldReg = registrations.put(pid, reg);
    // Remove earlier registration if any
    if (oldReg != null) {
        oldReg.unregister();
    }
}
Also used : LoginModuleFactory(org.apache.felix.jaas.LoginModuleFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 63 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project felix by apache.

the class MockBundleContext method registerService.

/* (non-Javadoc)
     * @see org.osgi.framework.BundleContext#registerService(java.lang.String[], java.lang.Object, java.util.Dictionary)
     */
public ServiceRegistration registerService(String[] names, Object service, Dictionary props) {
    props.put(Constants.OBJECTCLASS, names);
    ServiceRegistration sr = new MockServiceRegistration(this, service, names, props);
    for (int i = 0; i < names.length; i++) {
        services.put(names[i], sr);
    }
    fireServiceEvent(sr.getReference(), ServiceEvent.REGISTERED);
    return sr;
}
Also used : ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 64 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project felix by apache.

the class MetaTypeServiceImplTest method testAfterCretionManagedServiceFactory.

public void testAfterCretionManagedServiceFactory() {
    MetaTypeService mts = new MetaTypeServiceImpl(bundleContext);
    MetaTypeInformation mti = mts.getMetaTypeInformation(bundleContext.getBundle());
    // assert still empty
    checkEmpty(mti);
    // register a service with PID
    String factoryPid = "testAfterCreation_factory";
    MockManagedServiceFactory service = new MockManagedServiceFactory();
    Dictionary props = new Hashtable();
    props.put(Constants.SERVICE_PID, factoryPid);
    ServiceRegistration sr = bundleContext.registerService(ManagedServiceFactory.class.getName(), service, props);
    // locales should contain MockMetaTypeProvider.LOCALES
    assertNotNull(mti.getLocales());
    assertTrue(mti.getLocales().length == 1);
    assertEquals(MockMetaTypeProvider.LOCALES[0], mti.getLocales()[0]);
    // pids must be empty
    assertTrue(mti.getPids() == null || mti.getPids().length == 0);
    // pids must contain pid
    assertNotNull(mti.getFactoryPids());
    assertTrue(mti.getFactoryPids().length == 1);
    assertEquals(factoryPid, mti.getFactoryPids()[0]);
    // change the service PID
    String factoryPid2 = "testAfterCreation2";
    Dictionary props2 = new Hashtable();
    props2.put(Constants.SERVICE_PID, factoryPid2);
    sr.setProperties(props2);
    // pids must contain pid2
    assertNotNull(mti.getFactoryPids());
    assertTrue(mti.getFactoryPids().length == 1);
    assertEquals(factoryPid2, mti.getFactoryPids()[0]);
    // unregister the service
    sr.unregister();
    // ensure everything is clear now again
    checkEmpty(mti);
}
Also used : Dictionary(java.util.Dictionary) MetaTypeService(org.osgi.service.metatype.MetaTypeService) Hashtable(java.util.Hashtable) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation) ServiceRegistration(org.osgi.framework.ServiceRegistration) ManagedServiceFactory(org.osgi.service.cm.ManagedServiceFactory)

Example 65 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project felix by apache.

the class ConfigurationListenerTest method test_sync_listener.

@Test
public void test_sync_listener() throws IOException {
    final String pid = "test_listener";
    Configuration config = configure(pid, null, false);
    // Synchronous listener expecting synchronous events being
    // registered as a SynchronousConfigurationListener
    final TestListener testListener = new SynchronousTestListener();
    final ServiceRegistration listener = this.bundleContext.registerService(SynchronousConfigurationListener.class.getName(), testListener, null);
    // Synchronous listener expecting asynchronous events being
    // registered as a regular ConfigurationListener
    final TestListener testListenerAsync = new SynchronousTestListener();
    final ServiceRegistration listenerAsync = this.bundleContext.registerService(ConfigurationListener.class.getName(), testListenerAsync, null);
    int eventCount = 0;
    int eventCountAsync = 0;
    try {
        delay();
        testListener.assertNoEvent();
        testListenerAsync.assertNoEvent();
        config.update(new Hashtable<String, Object>() {

            {
                put("x", "x");
            }
        });
        delay();
        testListener.assertEvent(ConfigurationEvent.CM_UPDATED, pid, null, false, ++eventCount);
        testListenerAsync.assertEvent(ConfigurationEvent.CM_UPDATED, pid, null, true, ++eventCountAsync);
        config.update(new Hashtable<String, Object>() {

            {
                put("x", "x");
            }
        });
        delay();
        testListener.assertEvent(ConfigurationEvent.CM_UPDATED, pid, null, false, ++eventCount);
        testListenerAsync.assertEvent(ConfigurationEvent.CM_UPDATED, pid, null, true, ++eventCountAsync);
        config.setBundleLocation("new_Location");
        delay();
        testListener.assertEvent(ConfigurationEvent.CM_LOCATION_CHANGED, pid, null, false, ++eventCount);
        testListenerAsync.assertEvent(ConfigurationEvent.CM_LOCATION_CHANGED, pid, null, true, ++eventCountAsync);
        config.update();
        testListener.assertNoEvent();
        testListenerAsync.assertNoEvent();
        config.delete();
        config = null;
        delay();
        testListener.assertEvent(ConfigurationEvent.CM_DELETED, pid, null, false, ++eventCount);
        testListenerAsync.assertEvent(ConfigurationEvent.CM_DELETED, pid, null, true, ++eventCountAsync);
    } finally {
        if (config != null) {
            try {
                config.delete();
            } catch (IOException ioe) {
            // ignore
            }
        }
        listener.unregister();
        listenerAsync.unregister();
    }
}
Also used : SynchronousConfigurationListener(org.osgi.service.cm.SynchronousConfigurationListener) ConfigurationListener(org.osgi.service.cm.ConfigurationListener) Configuration(org.osgi.service.cm.Configuration) SynchronousConfigurationListener(org.osgi.service.cm.SynchronousConfigurationListener) SynchronousTestListener(org.apache.felix.cm.integration.helper.SynchronousTestListener) TestListener(org.apache.felix.cm.integration.helper.TestListener) SynchronousTestListener(org.apache.felix.cm.integration.helper.SynchronousTestListener) IOException(java.io.IOException) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Aggregations

ServiceRegistration (org.osgi.framework.ServiceRegistration)365 Test (org.junit.Test)106 Hashtable (java.util.Hashtable)94 Bundle (org.osgi.framework.Bundle)64 BundleContext (org.osgi.framework.BundleContext)64 Dictionary (java.util.Dictionary)62 Ensure (org.apache.felix.dm.itest.util.Ensure)42 Properties (java.util.Properties)39 ServiceReference (org.osgi.framework.ServiceReference)38 ArrayList (java.util.ArrayList)34 HashMap (java.util.HashMap)28 ServiceFactory (org.osgi.framework.ServiceFactory)21 IOException (java.io.IOException)16 Map (java.util.Map)15 List (java.util.List)12 ServiceEvent (org.osgi.framework.ServiceEvent)12 HashSet (java.util.HashSet)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)10 Collection (java.util.Collection)9