commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oliver Heger <oliver.he...@oliver-heger.de>
Subject Re: [configuration] Merging overrides with defaults
Date Thu, 18 Jun 2009 06:00:52 GMT
Alec Swan schrieb:
> Thanks, this approach worked.
> 
> One minor problem I ran into was that the merged configuration always has a
> new line at the end even if the default configuration does not have a new
> line at the end of the file. Is there a way to control if the new line
> should be added or not?
> 
> Thanks.

The PropertiesConfigurationLayout class allows you to configure the 
number of empty lines to be output before a property, but after a 
property always a line separator is written.

Since the last release there was some work of making the output of 
PropertiesConfiguration more customizable ([1]). However, I am not sure 
whether this gives you enough control for your purpose. And this code 
has not yet been released.

Oliver

[1] http://issues.apache.org/jira/browse/CONFIGURATION-370

> 
> On Wed, Jun 17, 2009 at 2:38 PM, Oliver Heger
> <oliver.heger@oliver-heger.de>wrote:
> 
>> Alec Swan schrieb:
>>
>>> Can the following be done with Commons Configurations?
>>>
>>> 1. Load defaults into a PropertyConfiguration object.
>>> 2. Load overrides into a Java Properties object. (Note that I do not need
>>> to
>>> preserve the formatting of overrides.)
>>> 3. Iterate over the overrides properties and set values on corresponding
>>> default properties by calling PropertyConfiguration.setXXX methods.
>>> 4. Save the resulting PropertyConfiguration object.
>>>
>>> How  can I make sure that the resulting PropertyConfiguration object
>>> preserves comments from the original defaults?
>>>
>>> Thanks.
>>>
>>> Alec
>>>
>> From looking at the code I assume that setting the value of a property
>> removes the old comment, but I did not test this.
>>
>> To avoid this, you can work with the PropertiesConfigurationLayout object
>> directly that is associated with the PropertiesConfiguration. This could
>> look something like the following:
>>
>> PropertiesConfiguration config =
>>  new PropertiesConfiguration(defaultsFile);
>> PropertiesConfigurationLayout layout = config.getLayout();
>>
>> Now iterate over the properties in the overrides config and for each key k
>> do the following:
>>
>> String comment = layout.getComment(k);
>> config.setProperty(k, <new value from override config>);
>> layout.setComment(k, comment);
>>
>> I think, this should work.
>>
>>
>> Oliver
>>
>>
>>> On Wed, Jun 17, 2009 at 1:54 PM, Oliver Heger
>>> <oliver.heger@oliver-heger.de>wrote:
>>>
>>>  Alec Swan schrieb:
>>>>  I wrote the code following the example on
>>>>>
>>>>> http://commons.apache.org/configuration/userguide-1.2/howto_compositeconfiguration.html
>>>>>
>>>>> There is no save() method in CompositeConfiguration. How can I save it?
>>>>>
>>>>> Thanks.
>>>>>
>>>>>  The merged configuration produced by CompositeConfiguration is only
>>>> virtual, i.e. there is no physical PropertiesConfiguration containing the
>>>> merged properties. It lives only in memory and cannot be saved.
>>>>
>>>> I am afraid, Commons Configuration does not provide an easy solution for
>>>> your problem. You can copy the content of the composite configuration
>>>> into a
>>>> properties configuration, e.g.:
>>>>
>>>> PropertiesConfiguration propConfig = new PropertiesConfiguration();
>>>> propConfig.copy(compositeConfig);
>>>> propConfig.save(someFile);
>>>>
>>>> But this will copy only the properties and not the comments or the
>>>> overall
>>>> layout.
>>>>
>>>> Oliver
>>>>
>>>>
>>>>
>>>>  On Wed, Jun 17, 2009 at 11:23 AM, Jörg Schaible <joerg.schaible@gmx.de
>>>>>> wrote:
>>>>>>
>>>>>  Alec Swan wrote at Mittwoch, 17. Juni 2009 18:39:
>>>>>
>>>>>>  Thank you for updating the subject, Jörg.
>>>>>>
>>>>>>> I changed the order in which I add defaults and overrides. The
>>>>>>> following
>>>>>>> is the new code:
>>>>>>>
>>>>>>>                   // merge overrides with defaults
>>>>>>>                   CompositeConfiguration compositeConfig = new
>>>>>>> CompositeConfiguration(overridingConfig);
>>>>>>>                   compositeConfig.addConfiguration(defaultConfig);
>>>>>>>
>>>>>>>                   // convert merged properties to string
>>>>>>>                   StringWriter writer = new StringWriter();
>>>>>>>                   overridingConfig.save(writer);
>>>>>>>
>>>>>>> defaultConfig contains the following properties:
>>>>>>> # A property
>>>>>>> A = 1
>>>>>>> # B property
>>>>>>> B = 2
>>>>>>>
>>>>>>> overridingConfig contains the following properties:
>>>>>>> B = 3
>>>>>>>
>>>>>>> I expected the merged content written to the writer to contain
these
>>>>>>> properties:
>>>>>>> # A property
>>>>>>> A = 1
>>>>>>> # B property
>>>>>>> B = 3
>>>>>>>
>>>>>>> Instead, the merged content written to the writer is the same
as the
>>>>>>> overridingConfig:
>>>>>>> B = 3
>>>>>>>
>>>>>>> Could anybody tell me what's wrong with my code?
>>>>>>>
>>>>>>>  You saved the overridingConfiguration and not the compositeConfig.
>>>>>> - Jörg
>>>>>>
>>>>>>
>>>>>> ---------------------------------------------------------------------
>>>>>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>>>>>> For additional commands, e-mail: dev-help@commons.apache.org
>>>>>>
>>>>>>
>>>>>>
>>>>>>  ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>>>> For additional commands, e-mail: dev-help@commons.apache.org
>>>>
>>>>
>>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>> For additional commands, e-mail: dev-help@commons.apache.org
>>
>>
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message