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

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

Introduction

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

Prototype

public void setProperty(String key, Object value) 

Source Link

Usage

From source file:com.netflix.config.samples.SampleApplication.java

/**
 * SampleApplication entrypoint/*w ww  . j a  va2  s  . co m*/
 * @param args
 */
public static void main(String[] args) {
    //@SuppressWarnings(value = {"unused" })
    new SampleApplication();

    // Step 1.
    // Create a Source of Configuration
    // Here we use a simple ConcurrentMapConfiguration
    // You can use your own, or use of the pre built ones including JDBCConfigurationSource
    // which lets you load properties from any RDBMS
    AbstractConfiguration myConfiguration = new ConcurrentMapConfiguration();
    myConfiguration.setProperty("com.netflix.config.samples.sampleapp.prop1", "value1");
    myConfiguration.setProperty("com.netflix.config.samples.SampleApp.SampleBean.name",
            "A Coffee Bean from Gautemala");

    // STEP 2: Optional. Dynamic Runtime property change option
    // We would like to utilize the benefits of obtaining dynamic property
    // changes
    // initialize the DynamicPropertyFactory with our configuration source
    DynamicPropertyFactory.initWithConfigurationSource(myConfiguration);

    // STEP 3: Optional. Option to inspect and modify properties using JConsole
    // We would like to inspect properties via JMX JConsole and potentially
    // update
    // these properties too
    // Register the MBean
    //
    // This can be also achieved automatically by setting "true" to
    // system property "archaius.dynamicPropertyFactory.registerConfigWithJMX"
    configMBean = ConfigJMXManager.registerConfigMbean(myConfiguration);

    // once this application is launched, launch JConsole and navigate to
    // the
    // Config MBean (under the MBeans tab)
    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();
    System.out.println("SampleBean:" + sampleBean);
    System.out.println(sampleBean.getName());

}

From source file:io.servicecomb.springboot.starter.configuration.CseDelegatingProxyUtils.java

public static void addApplicationContext(ConfigurableApplicationContext context) {
    AbstractConfiguration config = ConfigurationManager.getConfigInstance();
    config.clearProperty(APPLICATION_CONTEXT);
    config.setProperty(APPLICATION_CONTEXT, context);
}

From source file:com.netflix.zuul.dependency.ribbon.RibbonConfig.java

private static void setIfNotDefined(String key, String value) {
    final AbstractConfiguration config = ConfigurationManager.getConfigInstance();
    if (config.getString(key) == null) {
        LOG.info("Setting default NIWS Property " + key + "=" + value);
        config.setProperty(key, value);
    }//from   ww w. ja va  2 s.  c  om
}

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);
            }//ww  w  .  j av a 2  s.c  om
        }
        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.spectator.nflx.AutoPluginTest.java

@Before
public void init() {
    AbstractConfiguration cfg = ConfigurationManager.getConfigInstance();
    cfg.setProperty("spectator.nflx.enabled", "false");
}

From source file:com.netflix.genie.server.services.TestFactory.java

/**
 * Test instantiation from a good/existing property.
 *
 * @throws Exception if anything went wrong with the test
 *//*www  .j a va 2  s. c o  m*/
@Test
public void testGoodInstantiation() throws Exception {
    AbstractConfiguration conf = ConfigurationManager.getConfigInstance();
    conf.setProperty("unit.test", "java.lang.String");
    String s = (String) BaseServiceFactory.instantiateFromProperty("unit.test");
    Assert.assertNotNull(s);
}

From source file:com.netflix.genie.server.services.TestFactory.java

/**
 * Test error throwing/handling if a bad/non-existing property is provided.
 *//*  ww  w. j  av a2 s . c om*/
@Test
public void testBadInstantiation() {
    AbstractConfiguration conf = ConfigurationManager.getConfigInstance();
    conf.setProperty("unit.test", "java.lang.NotAString");
    try {
        BaseServiceFactory.instantiateFromProperty("unit.test");
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(true);
    }
}

From source file:com.netflix.zuul.dependency.cassandra.CassandraHelper.java

private void setAstynaxConfiguration(AbstractConfiguration configuration) {
    configuration.setProperty(DEFAULT_NFASTYANAX_READCONSISTENCY, DynamicPropertyFactory.getInstance()
            .getStringProperty(DEFAULT_NFASTYANAX_READCONSISTENCY, "CL_ONE").get());
    configuration.setProperty(DEFAULT_NFASTYANAX_WRITECONSISTENCY, DynamicPropertyFactory.getInstance()
            .getStringProperty("zuul.cassandra.default.nfastyanax.writeConsistency", "CL_ONE").get());
    configuration.setProperty(DEFAULT_NFASTYANAX_SOCKETTIMEOUT, DynamicPropertyFactory.getInstance()
            .getStringProperty("zuul.cassandra.default.nfastyanax.socketTimeout", "2000").get());
    configuration.setProperty(DEFAULT_NFASTYANAX_MAXCONNSPERHOST, DynamicPropertyFactory.getInstance()
            .getStringProperty("zuul.cassandra.default.nfastyanax.maxConnsPerHost", "3").get());
    configuration.setProperty(DEFAULT_NFASTYANAX_MAXTIMEOUTWHENEXHAUSTED, DynamicPropertyFactory.getInstance()
            .getStringProperty("zuul.cassandra.default.nfastyanax.maxTimeoutWhenExhausted", "2000").get());
    configuration.setProperty(DEFAULT_NFASTYANAX_MAXFAILOVERCOUNT, DynamicPropertyFactory.getInstance()
            .getStringProperty("zuul.cassandra.default.nfastyanax.maxFailoverCount", "1").get());
    configuration.setProperty(DEFAULT_NFASTYANAX_FAILOVERWAITTIME, DynamicPropertyFactory.getInstance()
            .getStringProperty("zuul.cassandra.default.nfastyanax.failoverWaitTime", "0").get());
}

From source file:com.hazelcast.eurekast.one.SelfRegistrationTestCase.java

@Test
public void testSimpleDiscovery() throws Exception {
    AbstractConfiguration configInstance = ConfigurationManager.getConfigInstance();

    String serviceUrl = "http://localhost:" + serverResource.getPort()
            + MockRemoteEurekaServer.EUREKA_API_BASE_PATH;
    configInstance.setProperty("eureka.serviceUrl.defaultZone", serviceUrl);
    configInstance.setProperty("eureka.name", "hazelcast-test");

    try {/*from  www  . j  av  a  2  s . co  m*/
        TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
        HazelcastInstance hz1 = factory.newHazelcastInstance();
        HazelcastInstance hz2 = factory.newHazelcastInstance();

        assertClusterSizeEventually(2, hz1);
        assertClusterSizeEventually(2, hz2);

    } finally {
        Hazelcast.shutdownAll();
    }
}

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

@Test
public void testSwitchingToConfiguration() throws Exception {
    longProp = propertyFactory.getLongProperty("dprops1", Long.MAX_VALUE, new Runnable() {
        public void run() {
            propertyChanged = true;//from w ww.  j  ava 2s . c o m
        }
    });
    AbstractConfiguration newConfig = new ConcurrentMapConfiguration();
    DynamicStringProperty prop = propertyFactory.getStringProperty("abc", "default");
    newConfig.setProperty("abc", "nondefault");
    newConfig.setProperty("dprops1", "0");
    DynamicPropertyFactory.initWithConfigurationSource(newConfig);
    Thread.sleep(2000);
    assertEquals("nondefault", prop.get());
    assertEquals(0, longProp.get());
    assertTrue(newConfig == ConfigurationManager.getConfigInstance());
    assertTrue(ConfigurationManager.isConfigurationInstalled());
}