Search in sources :

Example 1 with PersistenceManager

use of org.apache.felix.cm.PersistenceManager in project felix by apache.

the class ConfigurationImpl method update.

/**
 * @see org.osgi.service.cm.Configuration#update(java.util.Dictionary)
 */
public void update(Dictionary<String, ?> properties) throws IOException {
    PersistenceManager localPersistenceManager = getPersistenceManager();
    if (localPersistenceManager != null) {
        CaseInsensitiveDictionary newProperties = new CaseInsensitiveDictionary(properties);
        getConfigurationManager().log(LogService.LOG_DEBUG, "Updating config {0} with {1}", new Object[] { getPidString(), newProperties });
        setAutoProperties(newProperties, true);
        // persist new configuration
        localPersistenceManager.store(getPidString(), newProperties);
        // finally assign the configuration for use
        configure(newProperties);
        // update the service and fire an CM_UPDATED event
        getConfigurationManager().updated(this, true);
    }
}
Also used : PersistenceManager(org.apache.felix.cm.PersistenceManager)

Example 2 with PersistenceManager

use of org.apache.felix.cm.PersistenceManager in project felix by apache.

the class ConfigurationManagerTest method test_listConfigurations_notcached.

@Test
public void test_listConfigurations_notcached() throws Exception {
    String pid = "testDefaultPersistenceManager";
    PersistenceManager pm = new MockNotCachablePersistenceManager();
    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 PersistenceManagerProxy(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", "valueNotCached");
    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 value returned was not the one from the cache
    assertEquals("valueNotCached", 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) PersistenceManagerProxy(org.apache.felix.cm.impl.persistence.PersistenceManagerProxy) CachingPersistenceManagerProxy(org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy) MockNotCachablePersistenceManager(org.apache.felix.cm.MockNotCachablePersistenceManager) Test(org.junit.Test)

Example 3 with PersistenceManager

use of org.apache.felix.cm.PersistenceManager 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 4 with PersistenceManager

use of org.apache.felix.cm.PersistenceManager in project fabric8 by jboss-fuse.

the class EncryptedPropertyResolver method inject.

/**
 * Replaces original Felix' PersistenceManager with our version
 * @param configAdmin
 * @param pm
 * @return
 */
private PersistenceManager inject(ConfigurationAdmin configAdmin, PersistenceManager pm) {
    try {
        Field configurationManager = configAdmin.getClass().getDeclaredField("configurationManager");
        configurationManager.setAccessible(true);
        Object configurationManagerValue = configurationManager.get(configAdmin);
        Field persistenceManagers = configurationManagerValue.getClass().getDeclaredField("persistenceManagers");
        persistenceManagers.setAccessible(true);
        Object[] persistenceManagersValue = (Object[]) persistenceManagers.get(configurationManagerValue);
        if (persistenceManagersValue != null && persistenceManagersValue.length == 1) {
            // replace org.apache.felix.cm.impl.CachingPersistenceManagerProxy.pm
            Field pmField = persistenceManagersValue[0].getClass().getDeclaredField("pm");
            pmField.setAccessible(true);
            PersistenceManager originalPM = (PersistenceManager) pmField.get(persistenceManagersValue[0]);
            pmField.set(persistenceManagersValue[0], pm);
            // decrypt org.apache.felix.cm.impl.CachingPersistenceManagerProxy.cache
            Field cacheField = persistenceManagersValue[0].getClass().getDeclaredField("cache");
            cacheField.setAccessible(true);
            Hashtable<String, Dictionary> hashMap = (Hashtable<String, Dictionary>) cacheField.get(persistenceManagersValue[0]);
            for (Dictionary<String, String> storedProps : hashMap.values()) {
                String encryptedValuesList = storedProps.get("fabric.zookeeper.encrypted.values");
                if (encryptedValuesList == null) {
                    continue;
                }
                String[] encryptedValues = encryptedValuesList.split("\\s*,\\s");
                for (String encryptedValue : encryptedValues) {
                    String value = storedProps.get(encryptedValue);
                    if (value != null && value.startsWith("crypt:")) {
                        storedProps.put(encryptedValue + ".encrypted", value);
                        try {
                            storedProps.put(encryptedValue, encryptor.decrypt(value.substring("crypt:".length())));
                        } catch (EncryptionOperationNotPossibleException e) {
                            LOG.error(e.getMessage(), e);
                        }
                    }
                }
            }
            return originalPM;
        }
    } catch (Exception e) {
        LOG.warn(e.getMessage());
    }
    return null;
}
Also used : Field(java.lang.reflect.Field) Dictionary(java.util.Dictionary) EncryptingPersistenceManager(org.apache.felix.cm.file.EncryptingPersistenceManager) PersistenceManager(org.apache.felix.cm.PersistenceManager) Hashtable(java.util.Hashtable) EncryptionOperationNotPossibleException(org.jasypt.exceptions.EncryptionOperationNotPossibleException) FabricException(io.fabric8.api.FabricException) IOException(java.io.IOException) EncryptionOperationNotPossibleException(org.jasypt.exceptions.EncryptionOperationNotPossibleException)

Example 5 with PersistenceManager

use of org.apache.felix.cm.PersistenceManager in project felix by apache.

the class ConfigurationImpl method update.

/* (non-Javadoc)
     * @see org.osgi.service.cm.Configuration#update()
     */
public void update() throws IOException {
    PersistenceManager localPersistenceManager = getPersistenceManager();
    if (localPersistenceManager != null) {
        // read configuration from persistence (again)
        if (localPersistenceManager.exists(getPidString())) {
            Dictionary<String, Object> properties = localPersistenceManager.load(getPidString());
            // ensure serviceReference pid
            String servicePid = (String) properties.get(Constants.SERVICE_PID);
            if (servicePid != null && !getPidString().equals(servicePid)) {
                throw new IOException("PID of configuration file does match requested PID; expected " + getPidString() + ", got " + servicePid);
            }
            configureFromPersistence(properties);
        }
        // update the service but do not fire an CM_UPDATED event
        getConfigurationManager().updated(this, false);
    }
}
Also used : PersistenceManager(org.apache.felix.cm.PersistenceManager) IOException(java.io.IOException)

Aggregations

PersistenceManager (org.apache.felix.cm.PersistenceManager)15 Hashtable (java.util.Hashtable)8 MockPersistenceManager (org.apache.felix.cm.MockPersistenceManager)7 MockNotCachablePersistenceManager (org.apache.felix.cm.MockNotCachablePersistenceManager)6 Dictionary (java.util.Dictionary)5 List (java.util.List)3 FilePersistenceManager (org.apache.felix.cm.file.FilePersistenceManager)3 CachingPersistenceManagerProxy (org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2 Enumeration (java.util.Enumeration)2 SimpleFilter (org.apache.felix.cm.impl.SimpleFilter)2 FabricException (io.fabric8.api.FabricException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 MockBundleContext (org.apache.felix.cm.MockBundleContext)1 NotCachablePersistenceManager (org.apache.felix.cm.NotCachablePersistenceManager)1 EncryptingPersistenceManager (org.apache.felix.cm.file.EncryptingPersistenceManager)1 PersistenceManagerProxy (org.apache.felix.cm.impl.persistence.PersistenceManagerProxy)1