use of com.netflix.config.ConcurrentCompositeConfiguration in project archaius by Netflix.
the class SampleApplicationWithDefaultConfiguration method main.
public static void main(String[] args) {
new SampleApplication();
ConcurrentCompositeConfiguration myConfiguration = (ConcurrentCompositeConfiguration) DynamicPropertyFactory.getInstance().getBackingConfigurationSource();
ConcurrentMapConfiguration subConfig = new ConcurrentMapConfiguration();
subConfig.setProperty("com.netflix.config.samples.SampleApp.SampleBean.name", "A Coffee Bean from Cuba");
myConfiguration.setProperty("com.netflix.config.samples.sampleapp.prop1", "value1");
myConfiguration.addConfiguration(subConfig);
System.out.println("Started SampleApplication. Launch JConsole to inspect and update properties.");
System.out.println("To see how callback work, update property com.netflix.config.samples.SampleApp.SampleBean.sensitiveBeanData from BaseConfigBean in JConsole");
SampleBean sampleBean = new SampleBean();
// this should show the bean taking properties from two different sources
// property "com.netflix.config.samples.SampleApp.SampleBean.numSeeds" is from sampleapp.properites
// property "com.netflix.config.samples.SampleApp.SampleBean.name" is from subConfig added above
System.out.println("SampleBean:" + sampleBean);
System.out.println(sampleBean.getName());
}
use of com.netflix.config.ConcurrentCompositeConfiguration in project archaius by Netflix.
the class OverridingPropertiesConfiguration method convertToConcurrentCompositeConfiguration.
/**
* Convert CombinedConfiguration into {@link ConcurrentCompositeConfiguration} as the later has better performance
* and thread safety.
*
* @param config Configuration to be converted
*/
public static ConcurrentCompositeConfiguration convertToConcurrentCompositeConfiguration(CombinedConfiguration config) {
ConcurrentCompositeConfiguration root = new ConcurrentCompositeConfiguration();
IdentityHashMap<Configuration, String> reverseMap = new IdentityHashMap<Configuration, String>();
for (String name : (Set<String>) config.getConfigurationNames()) {
Configuration child = config.getConfiguration(name);
reverseMap.put(child, name);
}
for (int i = 0; i < config.getNumberOfConfigurations(); i++) {
Configuration child = config.getConfiguration(i);
String name = reverseMap.get(child);
if (child instanceof CombinedConfiguration) {
CombinedConfiguration combinedConf = (CombinedConfiguration) child;
ConcurrentCompositeConfiguration newConf = convertToConcurrentCompositeConfiguration(combinedConf);
root.addConfiguration(newConf, name);
} else {
Configuration conf = new ConcurrentMapConfiguration(child);
root.addConfiguration((AbstractConfiguration) conf, name);
}
}
return root;
}
use of com.netflix.config.ConcurrentCompositeConfiguration in project archaius by Netflix.
the class OverridingPropertiesConfiguration method getAllNamedConfiguration.
/**
* Gets all named sub-configuration from a configuration in a map. This method examines each sub-configuration
* which is an instance of {@link ConcurrentCompositeConfiguration} or CombinedConfiguration and extract the
* named configurations out of them.
*
* @param conf Configuration to get all the named sub-configurations
* @return map where key is the name of the sub-configuration and value is the sub-configuration
*/
public static Map<String, Configuration> getAllNamedConfiguration(Configuration conf) {
List<Configuration> toProcess = new ArrayList<Configuration>();
Map<String, Configuration> map = new HashMap<String, Configuration>();
toProcess.add(conf);
while (!toProcess.isEmpty()) {
Configuration current = toProcess.remove(0);
if (current instanceof ConcurrentCompositeConfiguration) {
ConcurrentCompositeConfiguration composite = (ConcurrentCompositeConfiguration) current;
for (String name : composite.getConfigurationNames()) {
map.put(name, composite.getConfiguration(name));
}
for (Configuration c : composite.getConfigurations()) {
toProcess.add(c);
}
} else if (current instanceof CombinedConfiguration) {
CombinedConfiguration combined = (CombinedConfiguration) current;
for (String name : (Set<String>) combined.getConfigurationNames()) {
map.put(name, combined.getConfiguration(name));
}
for (int i = 0; i < combined.getNumberOfConfigurations(); i++) {
toProcess.add(combined.getConfiguration(i));
}
}
}
return map;
}
use of com.netflix.config.ConcurrentCompositeConfiguration in project archaius by Netflix.
the class DynamicPropertyUpdaterTest method testAddorUpdatePropertyWithColonDelimiter.
@Test
public void testAddorUpdatePropertyWithColonDelimiter() {
AbstractConfiguration.setDefaultListDelimiter(':');
AbstractConfiguration config = new ConcurrentCompositeConfiguration();
config.addConfigurationListener(new ExpandedConfigurationListenerAdapter(new MyListener()));
MyListener.resetCount();
config.setProperty("test.host", "test:test1:test2");
assertEquals(1, MyListener.count);
dynamicPropertyUpdater.addOrChangeProperty("test.host", "test:test1:test2", config);
assertEquals(3, ((CopyOnWriteArrayList) (config.getProperty("test.host"))).size());
assertTrue(((CopyOnWriteArrayList) (config.getProperty("test.host"))).contains("test"));
assertTrue(((CopyOnWriteArrayList) (config.getProperty("test.host"))).contains("test1"));
assertTrue(((CopyOnWriteArrayList) (config.getProperty("test.host"))).contains("test2"));
// the config is not set again. when the value is still not changed.
assertEquals(1, MyListener.count);
config.setProperty("test.host1", "test1:test12");
// changing the new object value , the config.setProperty should be called again.
dynamicPropertyUpdater.addOrChangeProperty("test.host1", "test1.test12", config);
assertEquals("test1.test12", config.getProperty("test.host1"));
assertEquals(3, MyListener.count);
}
use of com.netflix.config.ConcurrentCompositeConfiguration in project archaius by Netflix.
the class DynamicPropertyUpdaterTest method testUpdateProperties.
/**
* Test method for {@link com.charter.aesd.archaius.DynamicPropertyUpdater#updateProperties(com.netflix.config.WatchedUpdateResult, org.apache.commons.configuration.Configuration, boolean)}.
* @throws InterruptedException
*/
@Test
public void testUpdateProperties() throws InterruptedException {
AbstractConfiguration.setDefaultListDelimiter(',');
AbstractConfiguration config = new ConcurrentCompositeConfiguration();
config.addConfigurationListener(new ExpandedConfigurationListenerAdapter(new MyListener()));
MyListener.resetCount();
config.setProperty("test", "host,host1,host2");
config.setProperty("test12", "host12");
Map<String, Object> added = Maps.newHashMap();
added.put("test.host", "test,test1");
Map<String, Object> changed = Maps.newHashMap();
changed.put("test", "host,host1");
changed.put("test.host", "");
dynamicPropertyUpdater.updateProperties(WatchedUpdateResult.createIncremental(added, changed, null), config, false);
assertEquals("", config.getProperty("test.host"));
assertEquals(2, ((CopyOnWriteArrayList) (config.getProperty("test"))).size());
assertTrue(((CopyOnWriteArrayList) (config.getProperty("test"))).contains("host"));
assertTrue(((CopyOnWriteArrayList) (config.getProperty("test"))).contains("host1"));
assertEquals(5, MyListener.count);
}
Aggregations