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

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

Introduction

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

Prototype

public void setProperty(String key, Object value) 

Source Link

Document

Sets the value of the specified property.

Usage

From source file:org.lable.oss.dynamicconfig.core.DependencyInjectionIT.java

@Test
public void diModuleTestWithDefaults() {
    System.setProperty(ConfigurationInitializer.LIBRARY_PREFIX + ".type", "classpath");
    System.setProperty(ConfigurationInitializer.LIBRARY_PREFIX + ".classpath.path", "test.yml");
    final HierarchicalConfiguration defaults = new HierarchicalConfiguration();
    defaults.setProperty("type.string", "Not okay");
    defaults.setProperty("only.in.defaults", "XXX");

    final Injector injector = Guice.createInjector(new AbstractModule() {
        @Override/*from   w w w  .  j  av  a 2 s . com*/
        protected void configure() {
            install(new DynamicConfigModule());
            bind(HierarchicalConfiguration.class).annotatedWith(ConfigurationDefaults.class)
                    .toInstance(defaults);
        }
    });

    Configuration configuration = injector.getInstance(Configuration.class);

    // As set in test.yml:
    assertThat(configuration.getString("type.string"), is("Okay"));
    // Overridden in this test:
    assertThat(configuration.getString("only.in.defaults"), is("XXX"));
}

From source file:org.lable.oss.dynamicconfig.provider.zookeeper.ZookeepersAsConfigSource.java

/**
 * Parse the raw configuration data. This logs the data if parsing fails.
 *
 * @param raw Raw byte data./*ww  w  .  j  a  v  a 2s  . com*/
 * @return The configuration tree, or null when parsing fails.
 */
HierarchicalConfiguration parseData(final HierarchicalConfigurationDeserializer deserializer, byte[] raw)
        throws ConfigurationException {
    InputStream bis = new ByteArrayInputStream(raw);
    HierarchicalConfiguration hc;
    try {
        hc = deserializer.deserialize(bis);
    } catch (ConfigurationException e) {
        throw new ConfigurationException("Failed to parse configuration data retrieved from Zookeeper quorum. "
                + "Raw data:\n\n" + new String(raw) + "\n\n" + e.getCause().getMessage());
    }

    // If enabled, copy the Zookeeper quorum to the configuration tree.
    if (copyQuorumTo != null) {
        hc.setProperty(copyQuorumTo, quorum);
    }

    return hc;
}

From source file:org.lable.oss.dynamicconfig.provider.zookeeper.ZookeepersAsConfigSourceIT.java

@Test
public void configurationMonitorTest() throws Exception {
    setData("\n");

    System.setProperty(LIBRARY_PREFIX + ".type", "zookeeper");
    System.setProperty(LIBRARY_PREFIX + ".zookeeper.znode", "/config");
    System.setProperty(LIBRARY_PREFIX + ".zookeeper.quorum", zookeeperHost);
    System.setProperty(LIBRARY_PREFIX + "." + APPNAME_PROPERTY, "test");
    HierarchicalConfiguration defaults = new HierarchicalConfiguration();
    defaults.setProperty("key", "DEFAULT");

    Configuration configuration = ConfigurationInitializer.configureFromProperties(defaults,
            new YamlDeserializer());

    final AtomicInteger count = new AtomicInteger(0);
    Precomputed<String> precomputed = Precomputed.monitorByUpdate(configuration, config -> {
        count.incrementAndGet();//from  ww w.  jav  a  2 s .  com
        return config.getString("key");
    });

    assertThat(precomputed.get(), is("DEFAULT"));
    assertThat(count.get(), is(1));
    assertThat(configuration.getString("key"), is("DEFAULT"));

    TimeUnit.MILLISECONDS.sleep(300);

    assertThat(precomputed.get(), is("DEFAULT"));
    assertThat(count.get(), is(1));

    setData("key: AAA");
    TimeUnit.MILLISECONDS.sleep(300);

    assertThat(count.get(), is(1));
    assertThat(configuration.getString("key"), is("AAA"));
    assertThat(precomputed.get(), is("AAA"));
    assertThat(count.get(), is(2));
    assertThat(precomputed.get(), is("AAA"));
    assertThat(count.get(), is(2));
}

From source file:org.lable.oss.dynamicconfig.provider.zookeeper.ZookeepersAsConfigSourceIT.java

@Test
public void viaInitializerTest() throws Exception {
    setData("\n");

    System.setProperty(LIBRARY_PREFIX + ".type", "zookeeper");
    System.setProperty(LIBRARY_PREFIX + ".zookeeper.znode", "/config");
    System.setProperty(LIBRARY_PREFIX + ".zookeeper.quorum", zookeeperHost);
    System.setProperty(LIBRARY_PREFIX + "." + APPNAME_PROPERTY, "test");
    HierarchicalConfiguration defaults = new HierarchicalConfiguration();
    defaults.setProperty("key", "DEFAULT");

    Configuration configuration = ConfigurationInitializer.configureFromProperties(defaults,
            new YamlDeserializer());

    assertThat(configuration.getString("key"), is("DEFAULT"));

    setData("key: AAA");
    TimeUnit.MILLISECONDS.sleep(300);

    assertThat(configuration.getString("key"), is("AAA"));
}

From source file:org.lable.oss.dynamicconfig.serialization.yaml.JsonSerializerTest.java

@Test
public void testSave() throws ConfigurationException, IOException {
    HierarchicalConfigurationSerializer serializer = new JsonSerializer();
    HierarchicalConfiguration configuration = new HierarchicalConfiguration();
    configuration.setProperty("type.unicodeString", "");
    configuration.setProperty("type.booleanFalse", false);
    configuration.setProperty("type.booleanTrue", true);
    configuration.setProperty("type.list", Arrays.asList("1", "2", "3"));

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    serializer.serialize(configuration, output);
    String json = IOUtils.toString(new StringReader(output.toString("UTF-8")));
    JsonNode tree = mapper.readTree(json);

    JsonNode nodeType = tree.get("type");
    assertThat(nodeType.get("unicodeString").textValue(), is(""));
    assertThat(nodeType.get("booleanFalse").booleanValue(), is(false));
    assertThat(nodeType.get("booleanTrue").booleanValue(), is(true));
    ArrayNode listString = (ArrayNode) nodeType.get("list");
    assertThat(listString, instanceOf(ArrayNode.class));
}

From source file:org.midonet.brain.BrainTestUtils.java

/**
 * Fills the configuration with some default values for tests. Allows a
 * zkRoot to be defined by the user.// www  . j  av  a2 s  .  co m
 */
public static void fillTestConfig(HierarchicalConfiguration cfg) {
    cfg.setProperty("midolman.midolman_root_key", zkRoot);
    cfg.setProperty("cassandra.servers", "localhost:9171");
    cfg.addNodes(ZookeeperConfig.GROUP_NAME,
            Arrays.asList(new HierarchicalConfiguration.Node("midolman_root_key", zkRoot)));
}

From source file:org.onosproject.driver.netconf.XmlConfigParser.java

protected static String createControllersConfig(HierarchicalConfiguration cfg,
        HierarchicalConfiguration actualCfg, String target, String netconfOperation, String controllerOperation,
        List<ControllerInfo> controllers) {
    //cfg.getKeys().forEachRemaining(key -> System.out.println(key));
    cfg.setProperty("edit-config.target", target);
    cfg.setProperty("edit-config.default-operation", netconfOperation);
    cfg.setProperty("edit-config.config.capable-switch.id", parseCapableSwitchId(actualCfg));
    cfg.setProperty("edit-config.config.capable-switch." + "logical-switches.switch.id",
            parseSwitchId(actualCfg));/*from w  ww. j a  va 2s. c  o m*/
    List<ConfigurationNode> newControllers = new ArrayList<>();
    for (ControllerInfo ci : controllers) {
        XMLConfiguration controller = new XMLConfiguration();
        controller.setRoot(new HierarchicalConfiguration.Node("controller"));
        String id = ci.type() + ":" + ci.ip() + ":" + ci.port();
        controller.setProperty("id", id);
        controller.setProperty("ip-address", ci.ip());
        controller.setProperty("port", ci.port());
        controller.setProperty("protocol", ci.type());
        newControllers.add(controller.getRootNode());
    }
    cfg.addNodes("edit-config.config.capable-switch.logical-switches." + "switch.controllers", newControllers);
    XMLConfiguration editcfg = (XMLConfiguration) cfg;
    StringWriter stringWriter = new StringWriter();
    try {
        editcfg.save(stringWriter);
    } catch (ConfigurationException e) {
        e.printStackTrace();
    }
    String s = stringWriter.toString().replaceAll("<controller>",
            "<controller nc:operation=\"" + controllerOperation + "\">");
    s = s.replace("<target>" + target + "</target>", "<target><" + target + "/></target>");
    return s;

}

From source file:org.onosproject.driver.XmlConfigParser.java

public static String createControllersConfig(HierarchicalConfiguration cfg, HierarchicalConfiguration actualCfg,
        String target, String netconfOperation, String controllerOperation, List<ControllerInfo> controllers) {
    //cfg.getKeys().forEachRemaining(key -> System.out.println(key));
    cfg.setProperty("edit-config.target", target);
    cfg.setProperty("edit-config.default-operation", netconfOperation);
    cfg.setProperty("edit-config.config.capable-switch.id", parseCapableSwitchId(actualCfg));
    cfg.setProperty("edit-config.config.capable-switch." + "logical-switches.switch.id",
            parseSwitchId(actualCfg));// ww  w  . ja v a2 s. c  om
    List<ConfigurationNode> newControllers = new ArrayList<>();
    for (ControllerInfo ci : controllers) {
        XMLConfiguration controller = new XMLConfiguration();
        controller.setRoot(new HierarchicalConfiguration.Node("controller"));
        String id = ci.type() + ":" + ci.ip() + ":" + ci.port();
        controller.setProperty("id", id);
        controller.setProperty("ip-address", ci.ip());
        controller.setProperty("port", ci.port());
        controller.setProperty("protocol", ci.type());
        newControllers.add(controller.getRootNode());
    }
    cfg.addNodes("edit-config.config.capable-switch.logical-switches." + "switch.controllers", newControllers);
    XMLConfiguration editcfg = (XMLConfiguration) cfg;
    StringWriter stringWriter = new StringWriter();
    try {
        editcfg.save(stringWriter);
    } catch (ConfigurationException e) {
        log.error("createControllersConfig()", e);
    }
    String s = stringWriter.toString().replaceAll("<controller>",
            "<controller nc:operation=\"" + controllerOperation + "\">");
    s = s.replace("<target>" + target + "</target>", "<target><" + target + "/></target>");
    return s;

}

From source file:org.onosproject.drivers.fujitsu.FujitsuVoltControllerConfig.java

/**
 * Forms XML string to change controller information.
 *
 * @param cfg a hierarchical configuration
 * @param target the type of configuration
 * @param netconfOperation operation type
 * @param controllers list of controllers
 * @return XML string/*w  ww .  j a  va2 s  .c om*/
 */
private String createVoltControllersConfig(HierarchicalConfiguration cfg, String target,
        String netconfOperation, List<ControllerInfo> controllers) {
    XMLConfiguration editcfg = null;

    cfg.setProperty(EDIT_CONFIG_TG, target);
    cfg.setProperty(EDIT_CONFIG_DO, netconfOperation);

    List<ConfigurationNode> newControllers = new ArrayList<>();
    for (ControllerInfo ci : controllers) {
        XMLConfiguration controller = new XMLConfiguration();
        controller.setRoot(new HierarchicalConfiguration.Node(OF_CONTROLLER));
        controller.setProperty(OFCONFIG_ID, ci.annotations().value(OFCONFIG_ID));
        controller.setProperty(CONTROLLER_INFO_ID, ci.annotations().value(OFCONFIG_ID));
        controller.setProperty(CONTROLLER_INFO_IP, ci.ip());
        controller.setProperty(CONTROLLER_INFO_PORT, ci.port());
        controller.setProperty(CONTROLLER_INFO_PROTOCOL, ci.type());
        newControllers.add(controller.getRootNode());
    }
    cfg.addNodes(VOLT_EDITCONFIG, newControllers);

    try {
        editcfg = (XMLConfiguration) cfg;
    } catch (ClassCastException e) {
        e.printStackTrace();
    }
    StringWriter stringWriter = new StringWriter();
    try {
        editcfg.save(stringWriter);
    } catch (ConfigurationException e) {
        e.printStackTrace();
    }
    String s = stringWriter.toString();
    String fromStr = buildStartTag(TARGET, false) + target + buildEndTag(TARGET, false);
    String toStr = buildStartTag(TARGET, false) + buildEmptyTag(target, false) + buildEndTag(TARGET, false);
    s = s.replace(fromStr, toStr);
    return s;
}

From source file:org.onosproject.drivers.utilities.YangXmlUtils.java

private void addProperties(HierarchicalConfiguration cfg, HierarchicalConfiguration complete) {
    cfg.getKeys().forEachRemaining(key -> {
        String property = (String) cfg.getProperty(key);
        if (!property.equals("")) {
            complete.setProperty(key, property);
        }/*from  w  w w  .  j a va2  s  .c om*/
    });
}