Search in sources :

Example 1 with ManagedServiceFactoryTracker

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();
}
Also used : ManagedServiceFactoryTracker(org.apache.felix.cm.impl.helper.ManagedServiceFactoryTracker) ManagedServiceTracker(org.apache.felix.cm.impl.helper.ManagedServiceTracker) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Hashtable(java.util.Hashtable) IOException(java.io.IOException) FilePersistenceManager(org.apache.felix.cm.file.FilePersistenceManager) ManagedServiceTracker(org.apache.felix.cm.impl.helper.ManagedServiceTracker)

Example 2 with ManagedServiceFactoryTracker

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();
}
Also used : ManagedServiceFactoryTracker(org.apache.felix.cm.impl.helper.ManagedServiceFactoryTracker) Hashtable(java.util.Hashtable) SynchronousConfigurationListener(org.osgi.service.cm.SynchronousConfigurationListener) SynchronousConfigurationListener(org.osgi.service.cm.SynchronousConfigurationListener) ConfigurationListener(org.osgi.service.cm.ConfigurationListener) ManagedServiceTracker(org.apache.felix.cm.impl.helper.ManagedServiceTracker) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 3 with ManagedServiceFactoryTracker

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());
}
Also used : ManagedServiceFactoryTracker(org.apache.felix.cm.impl.helper.ManagedServiceFactoryTracker) CachingPersistenceManagerProxy(org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy) Hashtable(java.util.Hashtable) Field(java.lang.reflect.Field) MockBundleContext(org.apache.felix.cm.MockBundleContext) MockNotCachablePersistenceManager(org.apache.felix.cm.MockNotCachablePersistenceManager)

Aggregations

Hashtable (java.util.Hashtable)3 ManagedServiceFactoryTracker (org.apache.felix.cm.impl.helper.ManagedServiceFactoryTracker)3 ManagedServiceTracker (org.apache.felix.cm.impl.helper.ManagedServiceTracker)2 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 MockBundleContext (org.apache.felix.cm.MockBundleContext)1 MockNotCachablePersistenceManager (org.apache.felix.cm.MockNotCachablePersistenceManager)1 FilePersistenceManager (org.apache.felix.cm.file.FilePersistenceManager)1 CachingPersistenceManagerProxy (org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy)1 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)1 ConfigurationListener (org.osgi.service.cm.ConfigurationListener)1 SynchronousConfigurationListener (org.osgi.service.cm.SynchronousConfigurationListener)1 ServiceTracker (org.osgi.util.tracker.ServiceTracker)1