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