Search in sources :

Example 6 with ConfigurationListener

use of org.apache.commons.configuration.event.ConfigurationListener in project archaius by Netflix.

the class ConcurrentMapConfigurationTest method testListeners.

@Test
public void testListeners() {
    ConcurrentMapConfiguration conf = new ConcurrentMapConfiguration();
    final AtomicReference<ConfigurationEvent> eventRef = new AtomicReference<ConfigurationEvent>();
    conf.addConfigurationListener(new ConfigurationListener() {

        @Override
        public void configurationChanged(ConfigurationEvent arg0) {
            eventRef.set(arg0);
        }
    });
    conf.addProperty("key", "1");
    assertEquals(1, conf.getInt("key"));
    ConfigurationEvent event = eventRef.get();
    assertEquals("key", event.getPropertyName());
    assertEquals("1", event.getPropertyValue());
    assertTrue(conf == event.getSource());
    assertEquals(AbstractConfiguration.EVENT_ADD_PROPERTY, event.getType());
    conf.setProperty("key", "2");
    event = eventRef.get();
    assertEquals("key", event.getPropertyName());
    assertEquals("2", event.getPropertyValue());
    assertTrue(conf == event.getSource());
    assertEquals(AbstractConfiguration.EVENT_SET_PROPERTY, event.getType());
    conf.clearProperty("key");
    event = eventRef.get();
    assertEquals("key", event.getPropertyName());
    assertNull(event.getPropertyValue());
    assertTrue(conf == event.getSource());
    assertEquals(AbstractConfiguration.EVENT_CLEAR_PROPERTY, event.getType());
    conf.clear();
    assertFalse(conf.getKeys().hasNext());
    event = eventRef.get();
    assertTrue(conf == event.getSource());
    assertEquals(AbstractConfiguration.EVENT_CLEAR, event.getType());
}
Also used : ConfigurationEvent(org.apache.commons.configuration.event.ConfigurationEvent) ConfigurationListener(org.apache.commons.configuration.event.ConfigurationListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 7 with ConfigurationListener

use of org.apache.commons.configuration.event.ConfigurationListener in project chassis by Kixeye.

the class MetricsCloudWatchConfiguration method addConfigurationListener.

private void addConfigurationListener() {
    final MetricsCloudWatchConfiguration springConfig = this;
    ConfigurationManager.getConfigInstance().addConfigurationListener(new ConfigurationListener() {

        @Override
        public synchronized void configurationChanged(ConfigurationEvent event) {
            if (!(event.getType() == AbstractConfiguration.EVENT_SET_PROPERTY || event.getType() == AbstractConfiguration.EVENT_ADD_PROPERTY)) {
                return;
            }
            if (event.isBeforeUpdate()) {
                return;
            }
            String name = event.getPropertyName();
            if (!(name.equals(METRICS_AWS_ENABLED) || name.equals(METRICS_AWS_FILTER) || name.equals(METRICS_AWS_PUBLISH_INTERVAL) || name.equals(METRICS_AWS_PUBLISH_INTERVAL_UNIT))) {
                return;
            }
            springConfig.enabled = name.equals(METRICS_AWS_ENABLED) ? Boolean.parseBoolean(event.getPropertyValue() + "") : springConfig.enabled;
            destroyReporter();
            if (springConfig.enabled) {
                createReporter();
            }
        }
    });
}
Also used : ConfigurationListener(org.apache.commons.configuration.event.ConfigurationListener) ConfigurationEvent(org.apache.commons.configuration.event.ConfigurationEvent)

Aggregations

ConfigurationListener (org.apache.commons.configuration.event.ConfigurationListener)7 ConfigurationEvent (org.apache.commons.configuration.event.ConfigurationEvent)5 Test (org.junit.Test)2 DistributedLogConfiguration (com.twitter.distributedlog.DistributedLogConfiguration)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 PostConstruct (javax.annotation.PostConstruct)1 AbstractConfiguration (org.apache.commons.configuration.AbstractConfiguration)1 DeterministicScheduler (org.jmock.lib.concurrent.DeterministicScheduler)1 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)1