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

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

Introduction

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

Prototype

public void setProperty(String key, Object value) 

Source Link

Usage

From source file:org.apache.qpid.systest.rest.VirtualHostRestTest.java

private XMLConfiguration createAndSaveVirtualHostConfiguration(String hostName, File configFile,
        String storeLocation) throws ConfigurationException {
    XMLConfiguration testConfiguration = new XMLConfiguration();
    testConfiguration.setProperty("virtualhosts.virtualhost." + hostName + ".store.class",
            getTestProfileMessageStoreClassName());
    testConfiguration.setProperty("virtualhosts.virtualhost." + hostName + ".store.environment-path",
            storeLocation);//from  ww  w  .j a  v a  2  s.c o m
    testConfiguration.save(configFile);
    return testConfiguration;
}

From source file:org.glite.slcs.acl.impl.ReplaceAccessControlRuleXMLOperation.java

protected void doProcessing(XMLConfiguration config) {

    if (LOG.isDebugEnabled())
        LOG.debug("replacing: " + rule_);

    // find index
    int i = 0;//from   w w w  .jav a 2  s . c om
    String ruleId = null;
    String ruleKey = null;
    while (true) {
        String rulePrefix = "AccessControlRule(" + i + ")";
        i++;
        // read current id
        ruleId = config.getString(rulePrefix + "[@id]");
        if (ruleId == null) {
            // no more rules
            break;
        }
        int id = -1;
        try {
            id = Integer.parseInt(ruleId);
        } catch (Exception e) {
            LOG.error(e);
        }
        if (id == rule_.getId()) {
            ruleKey = rulePrefix;
            // rule found
            break;
        }
    }

    if (ruleKey != null) {
        LOG.debug("replace AccessControlRule[" + ruleId + "]: " + rule_);
        // replace group
        config.setProperty(ruleKey + "[@group]", rule_.getGroupName());
        // clear all existing rule attributes
        config.clearTree(ruleKey + ".Attribute");
        // add new rule attributes
        List ruleAttributes = rule_.getAttributes();
        Iterator attributes = ruleAttributes.iterator();
        while (attributes.hasNext()) {
            Attribute attribute = (Attribute) attributes.next();
            config.addProperty(ruleKey + ".Attribute(-1)", attribute.getValue());
            config.addProperty(ruleKey + ".Attribute[@name]", attribute.getName());
        }
        // save the XML file
        save(config);
        // success
        setStatus(true);

    } else {
        LOG.error("rule to replace: " + rule_ + " not found!");
    }

}

From source file:org.jevis.commons.cli.ConfigurationHelper.java

/**
 * Create an new configuration skeleton// w  w w .  j a va  2 s. c om
 *
 * @param file
 * @throws ConfigurationException
 */
public void createConfigFile(File file) throws ConfigurationException {

    //TODO:  XPath expressions for a better implementation see http://www.code-thrill.com/2012/05/configuration-that-rocks-with-apache.html
    XMLConfiguration config = new XMLConfiguration(file);

    config.setProperty("jevis.datasource.class", "org.jevis.api.sql.JEVisDataSourceSQL");
    config.setProperty("jevis.datasource.host", "openjevis.org");
    config.setProperty("jevis.datasource.schema", "jevis");
    config.setProperty("jevis.datasource.port", "13306");
    config.setProperty("jevis.datasource.username", "jevis");
    config.setProperty("jevis.datasource.password", "jevistest");

    config.save();

}

From source file:org.kepler.configuration.ConfigurationTest.java

public static void changeTest() throws Exception {

    System.out.println("ChangeTest");
    File configDir = Module.make("configuration-manager").getConfigurationsDir();
    File configDirectivesDir = new File(configDir.getParentFile(), "config-directives");
    System.out.println(configDir);
    System.out.println(configDirectivesDir);
    System.out.println();// ww w.j  av  a 2s.  c o m

    File originalXml = new File(configDir, "original.xml");
    File changeXml = new File(configDirectivesDir, "change.xml");
    File changedXml = new File(configDirectivesDir, "changed.xml");

    XMLConfiguration originalXmlConfig = new XMLConfiguration();
    originalXmlConfig.setDelimiterParsingDisabled(true);
    originalXmlConfig.load(originalXml);

    Iterator i;

    i = originalXmlConfig.getKeys();

    System.out.println("Original.xml:");
    while (i.hasNext()) {
        String key = (String) i.next();
        System.out.println(key + " = " + originalXmlConfig.getProperty(key));
    }
    System.out.println();

    XMLConfiguration changeXmlConfig = new XMLConfiguration();
    changeXmlConfig.setDelimiterParsingDisabled(true);
    changeXmlConfig.load(changeXml);

    XMLConfiguration changedXmlConfig = new XMLConfiguration();
    changedXmlConfig.setDelimiterParsingDisabled(true);
    if (changedXml.exists())
        changedXmlConfig.load(changedXml);

    i = changeXmlConfig.getKeys();

    System.out.println("Change.xml:");
    while (i.hasNext()) {
        String key = (String) i.next();
        System.out.println(key + " = " + changeXmlConfig.getProperty(key));
    }
    System.out.println();

    i = changeXmlConfig.getKeys();
    while (i.hasNext()) {
        String key = (String) i.next();
        Object value = changeXmlConfig.getProperty(key);
        Object changed = changedXmlConfig.getProperty(key);
        if (changed == null || !value.equals(changed)) {
            originalXmlConfig.setProperty(key, value);
            changedXmlConfig.setProperty(key, value);
        }
    }

    i = originalXmlConfig.getKeys();

    System.out.println("New Config:");
    while (i.hasNext()) {
        String key = (String) i.next();
        System.out.println(key + " = " + originalXmlConfig.getProperty(key));
    }
    System.out.println();

    i = changedXmlConfig.getKeys();

    System.out.println("Changed.xml");
    while (i.hasNext()) {
        String key = (String) i.next();
        System.out.println(key + " = " + changedXmlConfig.getProperty(key));
    }
    System.out.println();

    changedXmlConfig.save(changedXml);
}

From source file:org.kepler.configuration.ConfigurationTest.java

public static void removeTest() throws Exception {

    System.out.println("RemoveTest");
    File configDir = Module.make("configuration-manager").getConfigurationsDir();
    File configDirectivesDir = new File(configDir.getParentFile(), "config-directives");
    System.out.println(configDir);
    System.out.println(configDirectivesDir);
    System.out.println();/*from w w  w  . j a  v  a  2s .c  o m*/

    File originalXml = new File(configDir, "original.xml");
    File removeXml = new File(configDirectivesDir, "remove.xml");
    File removedXml = new File(configDirectivesDir, "removed.xml");
    File newXml = new File(configDirectivesDir, "new.xml");

    XMLConfiguration originalXmlConfig = new XMLConfiguration();
    originalXmlConfig.setDelimiterParsingDisabled(true);
    originalXmlConfig.load(originalXml);

    Iterator i;

    XMLConfiguration removeXmlConfig = new XMLConfiguration();
    removeXmlConfig.setDelimiterParsingDisabled(true);
    removeXmlConfig.load(removeXml);

    XMLConfiguration removedXmlConfig = new XMLConfiguration();
    removedXmlConfig.setDelimiterParsingDisabled(true);
    if (removedXml.exists())
        removedXmlConfig.load(removedXml);

    i = removeXmlConfig.getKeys();
    while (i.hasNext()) {
        String key = (String) i.next();
        Object value = removeXmlConfig.getProperty(key);
        Object removed = removedXmlConfig.getProperty(key);
        if (removed == null || !value.equals(removed)) {
            originalXmlConfig.clearProperty(key);
            removedXmlConfig.setProperty(key, value);
        }
    }

    removedXmlConfig.save(removedXml);
    originalXmlConfig.save(newXml);
}

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));/*w  w  w .j  ava2s . co  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));//  w w  w.j a  va 2s .com
    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/*from www  .j  ava2s .  co m*/
 */
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

/**
 * Retrieves a valid XML configuration for a specific XML path for a single
 * instance of the Map specified key-value pairs.
 *
 * @param file   path of the file to be used.
 * @param values map of key and values to set under the generic path.
 * @return Hierarchical configuration containing XML with values.
 *//*from w w  w . ja  va  2 s. c  o  m*/
public XMLConfiguration getXmlConfiguration(String file, Map<String, String> values) {
    InputStream stream = getCfgInputStream(file);
    XMLConfiguration cfg = loadXml(stream);
    XMLConfiguration complete = new XMLConfiguration();
    List<String> paths = new ArrayList<>();
    Map<String, String> valuesWithKey = new HashMap<>();
    values.keySet().forEach(path -> {
        List<String> allPaths = findPaths(cfg, path);
        String key = nullIsNotFound(allPaths.isEmpty() ? null : allPaths.get(0),
                "Yang model does not contain desired path");
        paths.add(key);
        valuesWithKey.put(key, values.get(path));
    });
    Collections.sort(paths, new StringLengthComparator());
    paths.forEach(key -> complete.setProperty(key, valuesWithKey.get(key)));
    addProperties(cfg, complete);
    return complete;
}

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

/**
 * Retrieves a valid XML configuration for a specific XML path for multiple
 * instance of YangElements objects.//  ww  w. j  a v a  2  s. co m
 *
 * @param file     path of the file to be used.
 * @param elements List of YangElements that are to be set.
 * @return Hierachical configuration containing XML with values.
 */
public XMLConfiguration getXmlConfiguration(String file, List<YangElement> elements) {
    InputStream stream = getCfgInputStream(file);
    HierarchicalConfiguration cfg = loadXml(stream);
    XMLConfiguration complete = new XMLConfiguration();
    Multimap<String, YangElement> commonElements = ArrayListMultimap.create();

    //saves the elements in a Multimap based on the computed key.
    elements.forEach(element -> {
        String completeKey = nullIsNotFound(findPath(cfg, element.getBaseKey()),
                "Yang model does not contain desired path");
        commonElements.put(completeKey, element);
    });

    //iterates over the elements and constructs the configuration
    commonElements.keySet().forEach(key -> {
        // if there is more than one element for a given path
        if (commonElements.get(key).size() > 1) {
            //creates a list of nodes that have to be added for that specific path
            ArrayList<ConfigurationNode> nodes = new ArrayList<>();
            //creates the nodes
            commonElements.get(key).forEach(element -> nodes.add(getInnerNode(element).getRootNode()));
            //computes the parent path
            String parentPath = key.substring(0, key.lastIndexOf("."));
            //adds the nodes to the complete configuration
            complete.addNodes(parentPath, nodes);
        } else {
            //since there is only a single element we can assume it's the first one.
            Map<String, String> keysAndValues = commonElements.get(key).stream().findFirst().get()
                    .getKeysAndValues();
            keysAndValues.forEach((k, v) -> complete.setProperty(key + "." + k, v));
        }
    });
    addProperties(cfg, complete);
    return complete;
}