Example usage for org.apache.commons.configuration CompositeConfiguration CompositeConfiguration

List of usage examples for org.apache.commons.configuration CompositeConfiguration CompositeConfiguration

Introduction

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

Prototype

public CompositeConfiguration() 

Source Link

Document

Creates an empty CompositeConfiguration object which can then be added some other Configuration files

Usage

From source file:org.apache.distributedlog.common.config.TestConfigurationSubscription.java

@Test(timeout = 60000)
public void testInitialConfigLoad() throws Exception {
    PropertiesWriter writer = new PropertiesWriter();
    writer.setProperty("prop1", "1");
    writer.setProperty("prop2", "abc");
    writer.setProperty("prop3", "123.0");
    writer.setProperty("prop4", "11132");
    writer.setProperty("prop5", "true");
    writer.save();/*  w w w. ja v a2 s.  com*/

    ScheduledExecutorService mockScheduler = new DeterministicScheduler();
    FileConfigurationBuilder builder = new PropertiesConfigurationBuilder(writer.getFile().toURI().toURL());
    ConcurrentConstConfiguration conf = new ConcurrentConstConfiguration(new CompositeConfiguration());
    List<FileConfigurationBuilder> fileConfigBuilders = Lists.newArrayList(builder);
    ConfigurationSubscription confSub = new ConfigurationSubscription(conf, fileConfigBuilders, mockScheduler,
            100, TimeUnit.MILLISECONDS);
    assertEquals(1, conf.getInt("prop1"));
    assertEquals("abc", conf.getString("prop2"));
    assertEquals(123.0, conf.getFloat("prop3"), 0);
    assertEquals(11132, conf.getInt("prop4"));
    assertEquals(true, conf.getBoolean("prop5"));
}

From source file:org.apache.distributedlog.common.config.TestConfigurationSubscription.java

@Test(timeout = 60000)
public void testExceptionInConfigLoad() throws Exception {
    PropertiesWriter writer = new PropertiesWriter();
    writer.setProperty("prop1", "1");
    writer.save();/*ww w.j av  a2s.  c o m*/

    DeterministicScheduler mockScheduler = new DeterministicScheduler();
    FileConfigurationBuilder builder = new PropertiesConfigurationBuilder(writer.getFile().toURI().toURL());
    ConcurrentConstConfiguration conf = new ConcurrentConstConfiguration(new CompositeConfiguration());
    List<FileConfigurationBuilder> fileConfigBuilders = Lists.newArrayList(builder);
    ConfigurationSubscription confSub = new ConfigurationSubscription(conf, fileConfigBuilders, mockScheduler,
            100, TimeUnit.MILLISECONDS);

    final AtomicInteger count = new AtomicInteger(1);
    conf.addConfigurationListener(new ConfigurationListener() {
        @Override
        public void configurationChanged(ConfigurationEvent event) {
            LOG.info("config changed {}", event);
            // Throw after so we actually see the update anyway.
            if (!event.isBeforeUpdate()) {
                count.getAndIncrement();
                throw new RuntimeException("config listener threw and exception");
            }
        }
    });

    int i = 0;
    int initial = 0;
    while (count.get() == initial) {
        writer.setProperty("prop1", Integer.toString(i++));
        writer.save();
        mockScheduler.tick(100, TimeUnit.MILLISECONDS);
    }

    initial = count.get();
    while (count.get() == initial) {
        writer.setProperty("prop1", Integer.toString(i++));
        writer.save();
        mockScheduler.tick(100, TimeUnit.MILLISECONDS);
    }
}

From source file:org.apache.distributedlog.util.TestConfUtils.java

@Test(timeout = 60000)
public void testLoadConfiguration() {
    Configuration conf1 = new CompositeConfiguration();
    conf1.setProperty("key1", "value1");
    conf1.setProperty("key2", "value2");
    conf1.setProperty("key3", "value3");

    Configuration conf2 = new CompositeConfiguration();
    conf2.setProperty("bkc.key1", "bkc.value1");
    conf2.setProperty("bkc.key4", "bkc.value4");

    assertEquals("value1", conf1.getString("key1"));
    assertEquals("value2", conf1.getString("key2"));
    assertEquals("value3", conf1.getString("key3"));
    assertEquals(null, conf1.getString("key4"));

    ConfUtils.loadConfiguration(conf1, conf2, "bkc.");

    assertEquals("bkc.value1", conf1.getString("key1"));
    assertEquals("value2", conf1.getString("key2"));
    assertEquals("value3", conf1.getString("key3"));
    assertEquals("bkc.value4", conf1.getString("key4"));
    assertEquals(null, conf1.getString("bkc.key1"));
    assertEquals(null, conf1.getString("bkc.key4"));
}

From source file:org.apache.falcon.regression.core.util.Config.java

private void initConfig(String propFileName) throws ConfigurationException {
    CompositeConfiguration compositeConfiguration = new CompositeConfiguration();
    LOGGER.info("Going to add properties from system properties.");
    compositeConfiguration.addConfiguration(new SystemConfiguration());

    LOGGER.info("Going to read properties from: " + propFileName);
    final PropertiesConfiguration merlinConfig = new PropertiesConfiguration(
            Config.class.getResource("/" + propFileName));
    //if changed configuration will be reloaded within 2 min
    final FileChangedReloadingStrategy reloadingStrategy = new FileChangedReloadingStrategy();
    reloadingStrategy.setRefreshDelay(2 * 60 * 1000);
    merlinConfig.setReloadingStrategy(reloadingStrategy);
    compositeConfiguration.addConfiguration(merlinConfig);
    this.confObj = compositeConfiguration;
}

From source file:org.apache.fluo.api.config.SimpleConfiguration.java

public SimpleConfiguration() {
    CompositeConfiguration compositeConfig = new CompositeConfiguration();
    compositeConfig.setThrowExceptionOnMissing(true);
    compositeConfig.setDelimiterParsingDisabled(true);
    internalConfig = compositeConfig;/*w  w  w  . j  a  va 2s  .  c  o m*/
}

From source file:org.apache.hedwig.conf.AbstractConfiguration.java

protected AbstractConfiguration() {
    conf = new CompositeConfiguration();
}

From source file:org.apache.isis.objectstore.nosql.db.file.server.FileServer.java

public FileServer() {
    org.apache.log4j.PropertyConfigurator.configure("config/logging.properties");

    try {/* w ww . j  a  va 2s .co m*/
        config = new CompositeConfiguration();
        config.addConfiguration(new SystemConfiguration());
        config.addConfiguration(new PropertiesConfiguration("config/server.properties"));

        final String data = config.getString("fileserver.data");
        final String services = config.getString("fileserver.services");
        final String logs = config.getString("fileserver.logs");
        final String archive = config.getString("fileserver.archive");

        Util.setDirectory(data, services, logs, archive);
        server = new FileServerProcessor();
    } catch (final ConfigurationException e) {
        LOG.error("configuration failure", e);
        System.out.println(e.getMessage());
        System.exit(0);
    }
}

From source file:org.apache.isis.runtimes.dflt.objectstores.nosql.db.file.server.FileServer.java

public FileServer() {
    PropertyConfigurator.configure("config/logging.properties");

    try {//from   w  ww  .j  a  va 2  s.c  o  m
        config = new CompositeConfiguration();
        config.addConfiguration(new SystemConfiguration());
        config.addConfiguration(new PropertiesConfiguration("config/server.properties"));

        final String data = config.getString("fileserver.data");
        final String services = config.getString("fileserver.services");
        final String logs = config.getString("fileserver.logs");
        final String archive = config.getString("fileserver.archive");

        Util.setDirectory(data, services, logs, archive);
        server = new FileServerProcessor();
    } catch (final ConfigurationException e) {
        LOG.error("configuration failure", e);
        System.out.println(e.getMessage());
        System.exit(0);
    }
}

From source file:org.apache.juddi.config.AppConfig.java

/**
 * Does the actual work of reading the configuration from System
 * Properties and/or juddiv3.properties file. When the juddiv3.properties
 * file is updated the file will be reloaded. By default the reloadDelay is
 * set to 1 second to prevent excessive date stamp checking.
 *///  ww  w  .  ja  v  a 2s  .  c o m
private void loadConfiguration() throws ConfigurationException {
    //Properties from system properties
    CompositeConfiguration compositeConfig = new CompositeConfiguration();
    compositeConfig.addConfiguration(new SystemConfiguration());
    //Properties from file
    //changed 7-19-2013 AO for JUDDI-627
    XMLConfiguration propConfig = null;
    final String filename = System.getProperty(JUDDI_CONFIGURATION_FILE_SYSTEM_PROPERTY);
    if (filename != null) {
        propConfig = new XMLConfiguration(filename);
        try {
            loadedFrom = new File(filename).toURI().toURL();
            //   propConfig = new PropertiesConfiguration(filename);
        } catch (MalformedURLException ex) {
            try {
                loadedFrom = new URL("file://" + filename);
            } catch (MalformedURLException ex1) {
                log.warn("unable to get an absolute path to " + filename
                        + ". This may be ignorable if everything works properly.", ex1);
            }
        }
    } else {
        //propConfig = new PropertiesConfiguration(JUDDI_PROPERTIES);
        propConfig = new XMLConfiguration(JUDDI_PROPERTIES);
        loadedFrom = ClassUtil.getResource(JUDDI_PROPERTIES, this.getClass());
    }
    //Hey! this may break things
    propConfig.setAutoSave(true);

    log.info("Reading from properties file:  " + loadedFrom);
    long refreshDelay = propConfig.getLong(Property.JUDDI_CONFIGURATION_RELOAD_DELAY, 1000l);
    log.debug("Setting refreshDelay to " + refreshDelay);
    FileChangedReloadingStrategy fileChangedReloadingStrategy = new FileChangedReloadingStrategy();
    fileChangedReloadingStrategy.setRefreshDelay(refreshDelay);
    propConfig.setReloadingStrategy(fileChangedReloadingStrategy);
    compositeConfig.addConfiguration(propConfig);

    Properties properties = new Properties();
    if ("Hibernate".equals(propConfig.getString(Property.PERSISTENCE_PROVIDER))) {
        if (propConfig.containsKey(Property.DATASOURCE))
            properties.put("hibernate.connection.datasource", propConfig.getString(Property.DATASOURCE));
        if (propConfig.containsKey(Property.HBM_DDL_AUTO))
            properties.put("hibernate.hbm2ddl.auto", propConfig.getString(Property.HBM_DDL_AUTO));
        if (propConfig.containsKey(Property.DEFAULT_SCHEMA))
            properties.put("hibernate.default_schema", propConfig.getString(Property.DEFAULT_SCHEMA));
        if (propConfig.containsKey(Property.HIBERNATE_DIALECT))
            properties.put("hibernate.dialect", propConfig.getString(Property.HIBERNATE_DIALECT));
    }
    // initialize the entityManagerFactory.
    PersistenceManager.initializeEntityManagerFactory(propConfig.getString(Property.JUDDI_PERSISTENCEUNIT_NAME),
            properties);
    // Properties from the persistence layer 
    MapConfiguration persistentConfig = new MapConfiguration(getPersistentConfiguration(compositeConfig));

    compositeConfig.addConfiguration(persistentConfig);
    //Making the new configuration globally accessible.
    config = compositeConfig;
}

From source file:org.apache.juddi.v3.client.config.ClientConfig.java

/**
 * Does the actual work of reading the configuration from System
 * Properties and/or uddi.xml file. When the uddi.xml
 * file is updated the file will be reloaded. By default the reloadDelay is
 * set to 1 second to prevent excessive date stamp checking.
 *//*from www . java 2  s. c  o m*/
private void loadConfiguration(String configurationFile, Properties properties) throws ConfigurationException {
    //Properties from system properties
    CompositeConfiguration compositeConfig = new CompositeConfiguration();
    compositeConfig.addConfiguration(new SystemConfiguration());
    //Properties from XML file
    if (System.getProperty(UDDI_CONFIG_FILENAME_PROPERTY) != null) {
        log.info("Using system property config override");
        configurationFile = System.getProperty(UDDI_CONFIG_FILENAME_PROPERTY);
    }
    XMLConfiguration xmlConfig = null;
    if (configurationFile != null) {
        xmlConfig = new XMLConfiguration(configurationFile);
    } else {
        final String filename = System.getProperty(UDDI_CONFIG_FILENAME_PROPERTY);
        if (filename != null) {
            xmlConfig = new XMLConfiguration(filename);
        } else {
            xmlConfig = new XMLConfiguration(DEFAULT_UDDI_CONFIG);
        }
    }
    log.info("Reading UDDI Client properties file " + xmlConfig.getBasePath() + " use -D"
            + UDDI_CONFIG_FILENAME_PROPERTY + " to override");
    this.configurationFile = xmlConfig.getBasePath();
    long refreshDelay = xmlConfig.getLong(Property.UDDI_RELOAD_DELAY, 1000l);
    log.debug("Setting refreshDelay to " + refreshDelay);
    FileChangedReloadingStrategy fileChangedReloadingStrategy = new FileChangedReloadingStrategy();
    fileChangedReloadingStrategy.setRefreshDelay(refreshDelay);
    xmlConfig.setReloadingStrategy(fileChangedReloadingStrategy);
    compositeConfig.addConfiguration(xmlConfig);
    //Making the new configuration globally accessible.
    config = compositeConfig;
    readConfig(properties);
}