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