Search in sources :

Example 1 with ConcurrentCompositeConfiguration

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());
}
Also used : ConcurrentMapConfiguration(com.netflix.config.ConcurrentMapConfiguration) ConcurrentCompositeConfiguration(com.netflix.config.ConcurrentCompositeConfiguration)

Example 2 with ConcurrentCompositeConfiguration

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;
}
Also used : Set(java.util.Set) Configuration(org.apache.commons.configuration.Configuration) ConcurrentMapConfiguration(com.netflix.config.ConcurrentMapConfiguration) AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) ConcurrentCompositeConfiguration(com.netflix.config.ConcurrentCompositeConfiguration) CombinedConfiguration(org.apache.commons.configuration.CombinedConfiguration) ConcurrentMapConfiguration(com.netflix.config.ConcurrentMapConfiguration) IdentityHashMap(java.util.IdentityHashMap) CombinedConfiguration(org.apache.commons.configuration.CombinedConfiguration) ConcurrentCompositeConfiguration(com.netflix.config.ConcurrentCompositeConfiguration)

Example 3 with ConcurrentCompositeConfiguration

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;
}
Also used : Configuration(org.apache.commons.configuration.Configuration) ConcurrentMapConfiguration(com.netflix.config.ConcurrentMapConfiguration) AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) ConcurrentCompositeConfiguration(com.netflix.config.ConcurrentCompositeConfiguration) CombinedConfiguration(org.apache.commons.configuration.CombinedConfiguration) HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) ArrayList(java.util.ArrayList) CombinedConfiguration(org.apache.commons.configuration.CombinedConfiguration) ConcurrentCompositeConfiguration(com.netflix.config.ConcurrentCompositeConfiguration)

Example 4 with ConcurrentCompositeConfiguration

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);
}
Also used : AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) ConcurrentCompositeConfiguration(com.netflix.config.ConcurrentCompositeConfiguration) ExpandedConfigurationListenerAdapter(com.netflix.config.ExpandedConfigurationListenerAdapter) Test(org.junit.Test)

Example 5 with ConcurrentCompositeConfiguration

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);
}
Also used : AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) ConcurrentCompositeConfiguration(com.netflix.config.ConcurrentCompositeConfiguration) ExpandedConfigurationListenerAdapter(com.netflix.config.ExpandedConfigurationListenerAdapter) Test(org.junit.Test)

Aggregations

ConcurrentCompositeConfiguration (com.netflix.config.ConcurrentCompositeConfiguration)21 ConcurrentMapConfiguration (com.netflix.config.ConcurrentMapConfiguration)11 AbstractConfiguration (org.apache.commons.configuration.AbstractConfiguration)8 SystemConfiguration (org.apache.commons.configuration.SystemConfiguration)8 Test (org.junit.Test)7 DynamicConfiguration (com.netflix.config.DynamicConfiguration)5 NeverStartPollingScheduler (io.servicecomb.config.archaius.scheduler.NeverStartPollingScheduler)4 YAMLConfigurationSource (io.servicecomb.config.archaius.sources.YAMLConfigurationSource)4 BootstrapException (com.kixeye.chassis.bootstrap.BootstrapException)3 ExpandedConfigurationListenerAdapter (com.netflix.config.ExpandedConfigurationListenerAdapter)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Configuration (org.apache.commons.configuration.Configuration)3 DynamicWatchedConfiguration (com.netflix.config.DynamicWatchedConfiguration)2 IdentityHashMap (java.util.IdentityHashMap)2 Map (java.util.Map)2 CombinedConfiguration (org.apache.commons.configuration.CombinedConfiguration)2 EnvironmentConfiguration (org.apache.commons.configuration.EnvironmentConfiguration)2 PropertiesConfiguration (org.apache.commons.configuration.PropertiesConfiguration)2 TestingServer (org.apache.curator.test.TestingServer)2