use of org.apache.felix.cm.impl.helper.ManagedServiceFactoryTracker in project felix by apache.
the class ConfigurationManager method start.
@Override
public void start(BundleContext bundleContext) {
// track the log service using a ServiceTracker
logTracker = new ServiceTracker(bundleContext, LOG_SERVICE_NAME, null);
logTracker.open();
// assign the log level
String logLevelProp = bundleContext.getProperty(CM_LOG_LEVEL);
if (logLevelProp == null) {
logLevel = CM_LOG_LEVEL_DEFAULT;
} else {
try {
logLevel = Integer.parseInt(logLevelProp);
} catch (NumberFormatException nfe) {
logLevel = CM_LOG_LEVEL_DEFAULT;
}
}
// set up some fields
this.bundleContext = bundleContext;
// configurationlistener support
configurationListenerTracker = new ServiceTracker(bundleContext, ConfigurationListener.class.getName(), null);
configurationListenerTracker.open();
syncConfigurationListenerTracker = new ServiceTracker(bundleContext, SynchronousConfigurationListener.class.getName(), null);
syncConfigurationListenerTracker.open();
// initialize the asynchonous updater thread
ThreadGroup tg = new ThreadGroup("Configuration Admin Service");
tg.setDaemon(true);
this.updateThread = new UpdateThread(this, tg, "CM Configuration Updater");
this.eventThread = new UpdateThread(this, tg, "CM Event Dispatcher");
// set up the location (might throw IllegalArgumentException)
try {
FilePersistenceManager fpm = new FilePersistenceManager(bundleContext, bundleContext.getProperty(CM_CONFIG_DIR));
Hashtable props = new Hashtable();
props.put(Constants.SERVICE_PID, fpm.getClass().getName());
props.put(Constants.SERVICE_DESCRIPTION, "Platform Filesystem Persistence Manager");
props.put(Constants.SERVICE_VENDOR, "Apache Software Foundation");
props.put(Constants.SERVICE_RANKING, new Integer(Integer.MIN_VALUE));
filepmRegistration = bundleContext.registerService(PersistenceManager.class.getName(), fpm, props);
// setup dynamic configuration bindings
dynamicBindings = new DynamicBindings(bundleContext, fpm);
} catch (IOException ioe) {
log(LogService.LOG_ERROR, "Failure setting up dynamic configuration bindings", ioe);
} catch (IllegalArgumentException iae) {
log(LogService.LOG_ERROR, "Cannot create the FilePersistenceManager", iae);
}
// register as bundle and service listener
handleBundleEvents = true;
bundleContext.addBundleListener(this);
// get all persistence managers to begin with
// make sure to get the persistence managers at least once
pmtCount = 1;
persistenceManagerTracker = new ServiceTracker(bundleContext, PersistenceManager.class.getName(), null);
persistenceManagerTracker.open();
// consider alive now (before clients use Configuration Admin
// service registered in the next step)
isActive = true;
// create and register configuration admin - start after PM tracker ...
ConfigurationAdminFactory caf = new ConfigurationAdminFactory(this);
Hashtable<String, Object> props = new Hashtable<String, Object>();
props.put(Constants.SERVICE_PID, "org.apache.felix.cm.ConfigurationAdmin");
props.put(Constants.SERVICE_DESCRIPTION, "Configuration Admin Service Specification 1.5 Implementation");
props.put(Constants.SERVICE_VENDOR, "Apache Software Foundation");
configurationAdminRegistration = bundleContext.registerService(ConfigurationAdmin.class.getName(), caf, props);
// start handling ManagedService[Factory] services
managedServiceTracker = new ManagedServiceTracker(this);
managedServiceFactoryTracker = new ManagedServiceFactoryTracker(this);
// start processing the event queues only after registering the service
// see FELIX-2813 for details
this.updateThread.start();
this.eventThread.start();
}
use of org.apache.felix.cm.impl.helper.ManagedServiceFactoryTracker in project felix by apache.
the class ConfigurationManager method start.
public ServiceReference<ConfigurationAdmin> start() {
// configurationlistener support
configurationListenerTracker = new ServiceTracker<>(bundleContext, ConfigurationListener.class, null);
configurationListenerTracker.open();
syncConfigurationListenerTracker = new ServiceTracker<>(bundleContext, SynchronousConfigurationListener.class, null);
syncConfigurationListenerTracker.open();
// initialize the asynchonous updater thread
ThreadGroup tg = new ThreadGroup("Configuration Admin Service");
tg.setDaemon(true);
this.updateThread = new UpdateThread(tg, "CM Configuration Updater");
this.eventThread = new UpdateThread(tg, "CM Event Dispatcher");
// register as bundle and service listener
handleBundleEvents = true;
bundleContext.addBundleListener(this);
// consider alive now (before clients use Configuration Admin
// service registered in the next step)
isActive = true;
// create and register configuration admin - start after PM tracker ...
ConfigurationAdminFactory caf = new ConfigurationAdminFactory(this);
Dictionary<String, Object> props = new Hashtable<>();
props.put(Constants.SERVICE_PID, "org.apache.felix.cm.ConfigurationAdmin");
props.put(Constants.SERVICE_DESCRIPTION, "Configuration Admin Service Specification 1.6 Implementation");
props.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
configurationAdminRegistration = bundleContext.registerService(ConfigurationAdmin.class, caf, props);
// start handling ManagedService[Factory] services
managedServiceTracker = new ManagedServiceTracker(this);
managedServiceFactoryTracker = new ManagedServiceFactoryTracker(this);
// start processing the event queues only after registering the service
// see FELIX-2813 for details
this.updateThread.start();
this.eventThread.start();
return configurationAdminRegistration.getReference();
}
use of org.apache.felix.cm.impl.helper.ManagedServiceFactoryTracker in project felix by apache.
the class ConfigurationManagerTest method test_factoryConfigurationCleanup.
public void test_factoryConfigurationCleanup() throws Exception {
MockNotCachablePersistenceManager pm = new MockNotCachablePersistenceManager();
ConfigurationManager configMgr = new ConfigurationManager(new CachingPersistenceManagerProxy(pm), null);
final Field bcField = configMgr.getClass().getDeclaredField("bundleContext");
bcField.setAccessible(true);
bcField.set(configMgr, new MockBundleContext());
setServiceTrackerField(configMgr, "persistenceManagerTracker");
setServiceTrackerField(configMgr, "logTracker");
setServiceTrackerField(configMgr, "configurationListenerTracker");
setServiceTrackerField(configMgr, "syncConfigurationListenerTracker");
final Field mstField = configMgr.getClass().getDeclaredField("managedServiceFactoryTracker");
mstField.setAccessible(true);
mstField.set(configMgr, new ManagedServiceFactoryTracker(configMgr) {
@Override
public void open() {
}
});
final Field utField = configMgr.getClass().getDeclaredField("updateThread");
utField.setAccessible(true);
utField.set(configMgr, new UpdateThread(null, "Test updater") {
@Override
void schedule(Runnable update) {
update.run();
}
});
final String factoryPid = "my.factory";
final Dictionary<String, Object> props = new Hashtable<>();
props.put("hello", "world");
final ConfigurationImpl c1 = configMgr.createFactoryConfiguration(factoryPid, null);
c1.update(props);
final ConfigurationImpl c2 = configMgr.createFactoryConfiguration(factoryPid, null);
c2.update(props);
final ConfigurationImpl c3 = configMgr.createFactoryConfiguration(factoryPid, null);
c3.update(props);
assertEquals(4, pm.getStored().size());
c1.delete();
assertEquals(3, pm.getStored().size());
c2.delete();
assertEquals(2, pm.getStored().size());
c3.delete();
assertEquals(0, pm.getStored().size());
}
Aggregations