2

I use a org.apache.commons.configuration.CombinedConfiguration containing two org.apache.commons.configuration.XMLConfiguration instances. One is used as default configuration and is deployed with my JAR the other one is the user configuration that can overwrite the default values.

Here is some code that loads the instances:

    XMLConfiguration defaultConfig = new XMLConfiguration(defaultConfigFileURL);
    XMLConfiguration userConfig = new XMLConfiguration(extConfigFilePath);
    userConfig.setAutoSave(AUTO_SAVE);
    FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
    strategy.setRefreshDelay(FILE_REFRESH_RATE);
    userConfig.setReloadingStrategy(strategy);
    userConfig.setExpressionEngine(new XPathExpressionEngine());
    defaultConfig.setExpressionEngine(new XPathExpressionEngine());
    this.config = new CombinedConfiguration();
    this.config.setExpressionEngine(new XPathExpressionEngine());
    OverrideCombiner oc = new OverrideCombiner();
    this.config.setNodeCombiner(oc);
    this.config.setThrowExceptionOnMissing(true);
    this.config.addConfiguration(userConfig, "USER");
    this.config.addConfiguration(defaultConfig, "DEFAULT");

With AUTO_SAVE = true the userConfig instance automatically saves changes to the file which is working so far. My problem is, that it adds the paramters multiple times so the config file looks chaotic:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<fst_configuration>
<parameters>
        <WS_PASS_TO_SAP>true</WS_PASS_TO_SAP>
<PATH_CESA_DIR>C:\FST-RELEASE\FST\build\web\WEB-INF\cesa</PATH_CESA_DIR>
<PATH_ERIC_WORK_DIR>C:\FST-RELEASE\FST\build\web\WEB-INF\eric</PATH_ERIC_WORK_DIR>
<PATH_HSQL_DB>C:\FST-RELEASE\FST\build\web\WEB-INF\db</PATH_HSQL_DB>
<PATH_HSQL_DB>C:\FST-RELEASE\FST\build\web\WEB-INF\db</PATH_HSQL_DB>
<CESA_CMD_VERSION>17566520</CESA_CMD_VERSION>
<PATH_HSQL_DB>C:\FST-RELEASE\FST\build\web\WEB-INF\db</PATH_HSQL_DB>
<CESA_CMD_VERSION>17566520</CESA_CMD_VERSION>
</parameters>
(...)

For example, I change the parameter PATH_HSQL_DB at startup. After 3 startups the paramter is stored 3 times in the file assigned to userConfig.

This is the code that adds a paramter to the userConfig instance:

(...) // value is a String
String name = "PATH_HSQL_DB";
String keyString = String.format("/parameters/%s", name);
userConfig.addProperty(keyString, value);

What can I do to prevent this?

1 Answers1

0

You are adding a property rather than setting one. Use

String name = "PATH_HSQL_DB";
String keyString = String.format("/parameters/%s", name);
userConfig.setProperty(keyString, value);

Adding a property doesn't remove existing ones, setting them does.

Holloway
  • 6,412
  • 1
  • 26
  • 33