Example usage for org.apache.commons.configuration AbstractConfiguration addConfigurationListener

List of usage examples for org.apache.commons.configuration AbstractConfiguration addConfigurationListener

Introduction

In this page you can find the example usage for org.apache.commons.configuration AbstractConfiguration addConfigurationListener.

Prototype

public void addConfigurationListener(ConfigurationListener l) 

Source Link

Document

Adds a configuration listener to this object.

Usage

From source file:com.netflix.config.DynamicPropertyUpdaterTest.java

@Test
public void testAddorUpdatePropertyWithColonDelimiter() {
    AbstractConfiguration.setDefaultListDelimiter(':');
    AbstractConfiguration config = new ConcurrentCompositeConfiguration();
    config.addConfigurationListener(new ExpandedConfigurationListenerAdapter(new MyListener()));
    MyListener.resetCount();//from  w ww .j av a 2 s . com
    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"));
    assertEquals(1, MyListener.count); // the config is not set again. when the value is still not changed.
    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);
}

From source file:com.netflix.config.DynamicPropertyUpdaterTest.java

/**
 * Test method for {@link com.charter.aesd.archaius.DynamicPropertyUpdater#updateProperties(com.netflix.config.WatchedUpdateResult, org.apache.commons.configuration.Configuration, boolean)}.
 * @throws InterruptedException /*from w  w w.  j ava 2  s .c o m*/
 */
@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);
}

From source file:com.netflix.config.DynamicPropertyUpdaterTest.java

@Test
public void testAddorChangeProperty() {
    AbstractConfiguration.setDefaultListDelimiter(',');
    AbstractConfiguration config = new ConcurrentCompositeConfiguration();
    config.addConfigurationListener(new ExpandedConfigurationListenerAdapter(new MyListener()));
    MyListener.resetCount();//w ww  .  jav a  2s . co  m
    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"));
    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"));
    assertEquals(1, MyListener.count);
    dynamicPropertyUpdater.addOrChangeProperty("test.host", "test,test1", config);
    assertEquals(2, ((CopyOnWriteArrayList) (config.getProperty("test.host"))).size());
    assertTrue(((CopyOnWriteArrayList) (config.getProperty("test.host"))).contains("test"));
    assertTrue(((CopyOnWriteArrayList) (config.getProperty("test.host"))).contains("test1"));
    assertEquals(2, MyListener.count);

    dynamicPropertyUpdater.addOrChangeProperty("test.host1", "test1,test12", config);
    assertEquals(2, ((CopyOnWriteArrayList) (config.getProperty("test.host1"))).size());
    assertTrue(((CopyOnWriteArrayList) (config.getProperty("test.host1"))).contains("test1"));
    assertTrue(((CopyOnWriteArrayList) (config.getProperty("test.host1"))).contains("test12"));
    assertEquals(3, MyListener.count);

    config.setProperty("test.host1", "test1.test12");
    dynamicPropertyUpdater.addOrChangeProperty("test.host1", "test1.test12", config);
    assertEquals("test1.test12", config.getProperty("test.host1"));
    assertEquals(4, MyListener.count);
}

From source file:com.kixeye.chassis.support.logging.LoggingConfiguration.java

@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));
    }/*from ww  w  . j av  a  2  s.c o m*/

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

From source file:com.netflix.config.ConfigurationManager.java

static synchronized void setDirect(AbstractConfiguration config) {
    if (instance != null) {
        Collection<ConfigurationListener> listeners = instance.getConfigurationListeners();
        // transfer listeners
        // 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);
            }/*from  w w  w .  j  a v a2 s . com*/
        }
        if (listeners != null) {
            for (ConfigurationListener listener : listeners) {
                if (listener instanceof ExpandedConfigurationListenerAdapter
                        && ((ExpandedConfigurationListenerAdapter) listener)
                                .getListener() instanceof DynamicProperty.DynamicPropertyListener) {
                    // no need to transfer the fast property listener as it should be set later
                    // with the new configuration
                    continue;
                }
                config.addConfigurationListener(listener);
            }
        }
    }
    ConfigurationManager.removeDefaultConfiguration();
    ConfigurationManager.instance = config;
    ConfigurationManager.customConfigurationInstalled = true;
    ConfigurationManager.registerConfigBean();
}

From source file:com.netflix.config.ConcurrentCompositeConfiguration.java

/**
 * Add a configuration with a name at a particular index.
 * //from  w  w w. java  2 s . co  m
 * @throws IndexOutOfBoundsException 
 */
public void addConfigurationAtIndex(AbstractConfiguration config, String name, int index)
        throws IndexOutOfBoundsException {
    if (!configList.contains(config)) {
        checkIndex(index);
        configList.add(index, config);
        if (name != null) {
            namedConfigurations.put(name, config);
        }
        config.addConfigurationListener(eventPropagater);
        fireEvent(EVENT_CONFIGURATION_SOURCE_CHANGED, null, null, false);
    } else {
        logger.warn(config + " is not added as it already exits");
    }
}

From source file:org.ssh.test.conf.IConfiguration.java

/**
 * Adds a new configuration to this combined configuration. It is possible
 * (but not mandatory) to give the new configuration a name. This name must
 * be unique, otherwise a {@code ConfigurationRuntimeException} will be
 * thrown. With the optional {@code at} argument you can specify where in
 * the resulting node structure the content of the added configuration
 * should appear. This is a string that uses dots as property delimiters
 * (independent on the current expression engine). For instance if you pass
 * in the string {@code "database.tables"}, all properties of the added
 * configuration will occur in this branch.
 * /*  w w  w.  j  a  v a  2 s  .  co  m*/
 * @param config
 *            the configuration to add (must not be <b>null</b>)
 * @param name
 *            the name of this configuration (can be <b>null</b>)
 * @param at
 *            the position of this configuration in the combined tree (can
 *            be <b>null</b>)
 */
public void addConfiguration(AbstractConfiguration config, String name, String at) {
    if (config == null) {
        throw new IllegalArgumentException("Added configuration must not be null!");
    }
    if (name != null && namedConfigurations.containsKey(name)) {
        throw new ConfigurationRuntimeException(
                "A configuration with the name '" + name + "' already exists in this combined configuration!");
    }

    ConfigData cd = new ConfigData(config, name, at);
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("Adding configuration " + config + " with name " + name);
    }
    configurations.add(cd);
    if (name != null) {
        namedConfigurations.put(name, config);
    }

    config.addConfigurationListener(this);
    invalidate();
}