Search in sources :

Example 1 with CachingPersistenceManagerProxy

use of org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy in project felix by apache.

the class ConfigurationManagerTest method createConfigurationManagerAndLog.

private static ConfigurationManager createConfigurationManagerAndLog(final LogService logService) throws IOException {
    final PersistenceManager pm = Mockito.mock(PersistenceManager.class);
    ConfigurationManager configMgr = new ConfigurationManager(new CachingPersistenceManagerProxy(pm), null);
    try {
        Field field = Log.class.getDeclaredField("logTracker");
        field.setAccessible(true);
        field.set(Log.logger, new ServiceTracker(new MockBundleContext(), "", null) {

            @Override
            public Object getService() {
                return logService;
            }
        });
    } catch (Throwable ignore) {
        throw (IllegalArgumentException) new IllegalArgumentException("Cannot set logTracker field value").initCause(ignore);
    }
    return configMgr;
}
Also used : Field(java.lang.reflect.Field) PersistenceManager(org.apache.felix.cm.PersistenceManager) MockNotCachablePersistenceManager(org.apache.felix.cm.MockNotCachablePersistenceManager) MockPersistenceManager(org.apache.felix.cm.MockPersistenceManager) ServiceTracker(org.osgi.util.tracker.ServiceTracker) MockBundleContext(org.apache.felix.cm.MockBundleContext) CachingPersistenceManagerProxy(org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy)

Example 2 with CachingPersistenceManagerProxy

use of org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy in project felix by apache.

the class ConfigurationManager method listConfigurations.

ConfigurationImpl[] listConfigurations(ConfigurationAdminImpl configurationAdmin, String filterString) throws IOException, InvalidSyntaxException {
    SimpleFilter filter = null;
    if (filterString != null) {
        filter = SimpleFilter.parse(filterString);
    }
    Log.logger.log(LogService.LOG_DEBUG, "Listing configurations matching {0}", new Object[] { filterString });
    List<ConfigurationImpl> configList = new ArrayList<>();
    Collection<Dictionary> configs = this.persistenceManager.getDictionaries(filter);
    for (final Dictionary config : configs) {
        // ignore non-Configuration dictionaries
        final String pid = (String) config.get(Constants.SERVICE_PID);
        if (pid == null) {
            continue;
        }
        // CM 1.4 / 104.13.2.3 Permission required
        if (!configurationAdmin.hasPermission(this, (String) config.get(ConfigurationAdmin.SERVICE_BUNDLELOCATION))) {
            Log.logger.log(LogService.LOG_DEBUG, "Omitting configuration {0}: No permission for bundle {1} on configuration bound to {2}", new Object[] { pid, configurationAdmin.getBundle().getLocation(), config.get(ConfigurationAdmin.SERVICE_BUNDLELOCATION) });
            continue;
        }
        // ensure the service.pid and returned a cached config if available
        ConfigurationImpl cfg = null;
        if (this.persistenceManager instanceof CachingPersistenceManagerProxy) {
            cfg = getCachedConfiguration(pid);
            if (cfg == null) {
                cfg = new ConfigurationImpl(this, this.persistenceManager, config);
                // add the to configurations cache if it wasn't in the cache
                cacheConfiguration(cfg);
            }
        } else {
            cfg = new ConfigurationImpl(this, this.persistenceManager, config);
        }
        // FELIX-611: Ignore configuration objects without props
        if (!cfg.isNew()) {
            Log.logger.log(LogService.LOG_DEBUG, "Adding configuration {0}", new Object[] { pid });
            configList.add(cfg);
        } else {
            Log.logger.log(LogService.LOG_DEBUG, "Omitting configuration {0}: Is new", new Object[] { pid });
        }
    }
    if (configList.size() == 0) {
        return null;
    }
    return configList.toArray(new ConfigurationImpl[configList.size()]);
}
Also used : Dictionary(java.util.Dictionary) CachingPersistenceManagerProxy(org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy) ArrayList(java.util.ArrayList)

Example 3 with CachingPersistenceManagerProxy

use of org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy in project felix by apache.

the class ConfigurationManagerTest method test_listConfigurations_cached.

@Test
public void test_listConfigurations_cached() throws Exception {
    String pid = "testDefaultPersistenceManager";
    PersistenceManager pm = new MockPersistenceManager();
    Dictionary<String, Object> dictionary = new Hashtable<>();
    dictionary.put("property1", "value1");
    dictionary.put(Constants.SERVICE_PID, pid);
    pm.store(pid, dictionary);
    ConfigurationManager configMgr = new ConfigurationManager(new CachingPersistenceManagerProxy(pm), null);
    ConfigurationImpl[] conf = configMgr.listConfigurations(new ConfigurationAdminImpl(configMgr, null), null);
    assertEquals(1, conf.length);
    assertEquals(2, conf[0].getProperties(true).size());
    dictionary = new Hashtable<>();
    dictionary.put("property1", "value2");
    pid = "testDefaultPersistenceManager";
    dictionary.put(Constants.SERVICE_PID, pid);
    pm.store(pid, dictionary);
    conf = configMgr.listConfigurations(new ConfigurationAdminImpl(configMgr, null), null);
    assertEquals(1, conf.length);
    assertEquals(2, conf[0].getProperties(true).size());
    // verify that the property in the configurations cache was used
    assertEquals("value1", conf[0].getProperties(true).get("property1"));
}
Also used : PersistenceManager(org.apache.felix.cm.PersistenceManager) MockNotCachablePersistenceManager(org.apache.felix.cm.MockNotCachablePersistenceManager) MockPersistenceManager(org.apache.felix.cm.MockPersistenceManager) Hashtable(java.util.Hashtable) CachingPersistenceManagerProxy(org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy) MockPersistenceManager(org.apache.felix.cm.MockPersistenceManager) Test(org.junit.Test)

Example 4 with CachingPersistenceManagerProxy

use of org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy 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

CachingPersistenceManagerProxy (org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy)4 MockNotCachablePersistenceManager (org.apache.felix.cm.MockNotCachablePersistenceManager)3 Field (java.lang.reflect.Field)2 Hashtable (java.util.Hashtable)2 MockBundleContext (org.apache.felix.cm.MockBundleContext)2 MockPersistenceManager (org.apache.felix.cm.MockPersistenceManager)2 PersistenceManager (org.apache.felix.cm.PersistenceManager)2 ArrayList (java.util.ArrayList)1 Dictionary (java.util.Dictionary)1 ManagedServiceFactoryTracker (org.apache.felix.cm.impl.helper.ManagedServiceFactoryTracker)1 Test (org.junit.Test)1 ServiceTracker (org.osgi.util.tracker.ServiceTracker)1