Commons Configuration
  1. Commons Configuration
  2. CONFIGURATION-119

[configuration] ConfigurationFactory auto save overwrites properties file

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.1
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:

      Operating System: other
      Platform: Other

      Description

      Setting a PropertiesConfiguration autoSave=true via a ConfigurationFactory can
      cause the underlying properties file to be completely overwritten.

      Example:

      config.xml:
      <?xml version="1.0" encoding="ISO-8859-1" ?>
      <configuration>
      <properties fileName="application.properties" />
      <properties fileName="ui.properties" autoSave="true"/>
      </configuration>

      application.properties:
      application.name=Test App
      application.version = 0.01
      application.title = $

      {application.name} V${application.version}

      ui.properties:
      ui.test="stuff here"

      Execute the code:
      ConfigurationFactory factory = new ConfigurationFactory();
      URL configURL = getClass().getResource("/config.xml");
      factory.setConfigurationURL(configURL);
      try { config = factory.getConfiguration(); System.out.println(config.getString("application.title")); System.out.println(config.getString("ui.test")); config.setProperty("ui.test", "will this get saved ?"); System.out.println(config.getString("ui.test")); } catch (ConfigurationException e) { e.printStackTrace(); }

      Program output:
      12/04/2005 10:24:00
      org.apache.commons.configuration.ConfigurationFactory$FileConfigurationFactory
      createObject
      INFO: Trying to load configuration application.properties
      12/04/2005 10:24:00
      org.apache.commons.configuration.ConfigurationFactory$FileConfigurationFactory
      createObject
      INFO: Trying to load configuration ui.properties
      Test App V0.01
      "stuff here"
      will this get saved ?

      After execution
      ---------------
      application.properties:
      application.name=Test App
      application.version = 0.01
      application.title = ${application.name}

      V$

      {application.version}

      ui.properties:

      1. written by PropertiesConfiguration
      2. Tue Apr 12 10:24:00 EST 2005

      NOTE ui.test doesn't appear with either the old value or the modified one.

        Activity

        Hide
        Oliver Heger added a comment -

        Created an attachment (id=14961)
        Unit test for loading a PropertiesConfiguration with enabled auto save

        The attached unit test shows the heart of the bug: the combination of enabled
        auto save flag and the load() method of PropertiesConfiguration.

        On my system (Win XP) this test causes an endless loop, but this may depend on
        the platform and factors like default sizes of the used buffered streams. Fact
        is that the properties file is overwritten while it is being loaded, which will
        cause unpredictable behavior.

        Show
        Oliver Heger added a comment - Created an attachment (id=14961) Unit test for loading a PropertiesConfiguration with enabled auto save The attached unit test shows the heart of the bug: the combination of enabled auto save flag and the load() method of PropertiesConfiguration. On my system (Win XP) this test causes an endless loop, but this may depend on the platform and factors like default sizes of the used buffered streams. Fact is that the properties file is overwritten while it is being loaded, which will cause unpredictable behavior.
        Hide
        Oliver Heger added a comment -

        The following seems to happen:

        PropertiesConfiguration.load() loads the specified properties file line by line
        and calls addProperty() for each detected property. In
        AbstractFileConfiguration.addProperty() the new property value is stored and
        then possiblySave() is called. If the auto save flag is set, this method will
        trigger the save() method, which in turn will immediately overwrite the current
        configuration file.

        As a solution I suggest to store the old value of the auto save flag at the
        beginning of PropertiesConfiguration.load() and set it always to false. At the
        end of the method (in a finally block) the flag's old value can be reset.

        autoSave really should not be enabled during execution of a load method. So it
        might be better to implement such a mechanism already in
        AbstractFileConfiguration (but where?). Any thoughts or comments?

        Show
        Oliver Heger added a comment - The following seems to happen: PropertiesConfiguration.load() loads the specified properties file line by line and calls addProperty() for each detected property. In AbstractFileConfiguration.addProperty() the new property value is stored and then possiblySave() is called. If the auto save flag is set, this method will trigger the save() method, which in turn will immediately overwrite the current configuration file. As a solution I suggest to store the old value of the auto save flag at the beginning of PropertiesConfiguration.load() and set it always to false. At the end of the method (in a finally block) the flag's old value can be reset. autoSave really should not be enabled during execution of a load method. So it might be better to implement such a mechanism already in AbstractFileConfiguration (but where?). Any thoughts or comments?
        Hide
        Oliver Heger added a comment -

        Committed a patch, should be fixed in the next nightly builds.

        Show
        Oliver Heger added a comment - Committed a patch, should be fixed in the next nightly builds.

          People

          • Assignee:
            Unassigned
            Reporter:
            Pete Cain
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development