Search in sources :

Example 1 with ConfigurationListener

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

the class ConfigurationManager method setDirect.

static synchronized void setDirect(AbstractConfiguration config) {
    if (instance != null) {
        Collection<ConfigurationListener> listeners = instance.getConfigurationListeners();
        // transfer properties which are not in conflict with new configuration
        for (Iterator<String> i = instance.getKeys(); i.hasNext(); ) {
            String key = i.next();
            Object value = instance.getProperty(key);
            if (value != null && !config.containsKey(key)) {
                config.setProperty(key, value);
            }
        }
        if (listeners != null) {
            for (ConfigurationListener listener : listeners) {
                if (listener instanceof ExpandedConfigurationListenerAdapter && ((ExpandedConfigurationListenerAdapter) listener).getListener() instanceof DynamicProperty.DynamicPropertyListener) {
                    // with the new configuration
                    continue;
                }
                config.addConfigurationListener(listener);
            }
        }
    }
    ConfigurationManager.removeDefaultConfiguration();
    ConfigurationManager.instance = config;
    ConfigurationManager.customConfigurationInstalled = true;
    ConfigurationManager.registerConfigBean();
}
Also used : ConfigurationListener(org.apache.commons.configuration.event.ConfigurationListener)

Example 2 with ConfigurationListener

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

the class ConfigurationManager method removeDefaultConfiguration.

private static synchronized void removeDefaultConfiguration() {
    if (instance == null || customConfigurationInstalled) {
        return;
    }
    ConcurrentCompositeConfiguration defaultConfig = (ConcurrentCompositeConfiguration) instance;
    // stop loading of the configuration
    DynamicURLConfiguration defaultFileConfig = (DynamicURLConfiguration) defaultConfig.getConfiguration(URL_CONFIG_NAME);
    if (defaultFileConfig != null) {
        defaultFileConfig.stopLoading();
    }
    Collection<ConfigurationListener> listeners = defaultConfig.getConfigurationListeners();
    // find the listener and remove it so that DynamicProperty will no longer receives 
    // callback from the default configuration source
    ConfigurationListener dynamicPropertyListener = null;
    for (ConfigurationListener l : listeners) {
        if (l instanceof ExpandedConfigurationListenerAdapter && ((ExpandedConfigurationListenerAdapter) l).getListener() instanceof DynamicProperty.DynamicPropertyListener) {
            dynamicPropertyListener = l;
            break;
        }
    }
    if (dynamicPropertyListener != null) {
        defaultConfig.removeConfigurationListener(dynamicPropertyListener);
    }
    if (configMBean != null) {
        try {
            ConfigJMXManager.unRegisterConfigMBean(defaultConfig, configMBean);
        } catch (Exception e) {
            logger.error("Error unregistering with JMX", e);
        }
    }
    instance = null;
}
Also used : ConfigurationListener(org.apache.commons.configuration.event.ConfigurationListener) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with ConfigurationListener

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

the class LoggingConfiguration method initialize.

@PostConstruct
public void initialize() {
    AbstractConfiguration config = ConfigurationManager.getConfigInstance();
    if (config.containsKey(LOGBACK_CONFIG_NAME)) {
        System.out.println("Loading logging config.");
        reloadLogging(config.getString(LOGBACK_CONFIG_NAME));
    }
    config.addConfigurationListener(new ConfigurationListener() {

        @Override
        public synchronized void configurationChanged(ConfigurationEvent event) {
            if ((event.getType() == AbstractConfiguration.EVENT_ADD_PROPERTY || event.getType() == AbstractConfiguration.EVENT_SET_PROPERTY) && StringUtils.equalsIgnoreCase(LOGBACK_CONFIG_NAME, event.getPropertyName()) && event.getPropertyValue() != null && !event.isBeforeUpdate()) {
                System.out.println("Reloading logging config.");
                reloadLogging((String) event.getPropertyValue());
            }
        }
    });
    ConfigurationListener flumeConfigListener = new ConfigurationListener() {

        private FlumeLoggerLoader loggerLoader = null;

        public synchronized void configurationChanged(ConfigurationEvent event) {
            if (!(event.getType() == AbstractConfiguration.EVENT_SET_PROPERTY || event.getType() == AbstractConfiguration.EVENT_ADD_PROPERTY || event.getType() == AbstractConfiguration.EVENT_CLEAR_PROPERTY)) {
                return;
            }
            if (FlumeLoggerLoader.FLUME_LOGGER_ENABLED_PROPERTY.equals(event.getPropertyName())) {
                if ("true".equals(event.getPropertyValue())) {
                    if (loggerLoader == null) {
                        // construct the bean
                        loggerLoader = (FlumeLoggerLoader) applicationContext.getBean(FlumeLoggerLoader.PROTOTYPE_BEAN_NAME, "chassis");
                    }
                // else we already have one so we're cool
                } else {
                    if (loggerLoader != null) {
                        // delete the bean
                        ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory) applicationContext.getParentBeanFactory();
                        beanFactory.destroyBean(FlumeLoggerLoader.PROTOTYPE_BEAN_NAME, loggerLoader);
                        loggerLoader = null;
                    }
                // else we don't have any so we're cool
                }
            } else if (FlumeLoggerLoader.RELOAD_PROPERTIES.contains(event.getPropertyValue())) {
                // only reload if we're already running - otherwise ignore
                if (loggerLoader != null) {
                    // delete the bean
                    ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory) applicationContext.getParentBeanFactory();
                    beanFactory.destroyBean(FlumeLoggerLoader.PROTOTYPE_BEAN_NAME, loggerLoader);
                    loggerLoader = null;
                    // construct the bean
                    loggerLoader = (FlumeLoggerLoader) applicationContext.getBean(FlumeLoggerLoader.PROTOTYPE_BEAN_NAME, "chassis");
                }
            // else we don't have any so we're cool
            }
        }
    };
    config.addConfigurationListener(flumeConfigListener);
    flumeConfigListener.configurationChanged(new ConfigurationEvent(this, AbstractConfiguration.EVENT_SET_PROPERTY, FlumeLoggerLoader.FLUME_LOGGER_ENABLED_PROPERTY, config.getProperty(FlumeLoggerLoader.FLUME_LOGGER_ENABLED_PROPERTY), false));
}
Also used : AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) ConfigurationListener(org.apache.commons.configuration.event.ConfigurationListener) ConfigurationEvent(org.apache.commons.configuration.event.ConfigurationEvent) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) PostConstruct(javax.annotation.PostConstruct)

Example 4 with ConfigurationListener

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

the class MetricsGraphiteConfiguration method addConfigurationListener.

private void addConfigurationListener() {
    final MetricsGraphiteConfiguration 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_GRAPHITE_ENABLED) || name.equals(METRICS_GRAPHITE_SERVER) || name.equals(METRICS_GRAPHITE_PORT) || name.equals(METRICS_GRAPHITE_FILTER) || name.equals(METRICS_GRAPHITE_PUBLISH_INTERVAL) || name.equals(METRICS_GRAPHITE_PUBLISH_INTERVAL_UNIT))) {
                return;
            }
            springConfig.enabled = name.equals(METRICS_GRAPHITE_ENABLED) ? Boolean.parseBoolean(event.getPropertyValue() + "") : springConfig.enabled;
            destroyReporterLoader();
            if (springConfig.enabled) {
                createReporterLoader();
            }
        }
    });
}
Also used : ConfigurationListener(org.apache.commons.configuration.event.ConfigurationListener) ConfigurationEvent(org.apache.commons.configuration.event.ConfigurationEvent)

Example 5 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)

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