You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Alec Swan <al...@gmail.com> on 2009/06/16 23:51:55 UTC
Merging overrides with defaults
I have a property file containing default configurations in a key=value
format. I also have another file with the same format which contains
overrides for some of the properties. I need to apply the overrides to the
default configuration while preserving the format and comments. I also need
to write the merged content to a string.
I am using the following code, but the mergedContent contains the default
content and no overrides. I tried reversing the order in which I add
defaultConfig and overridingConfig to CompositeConfiguration, but still no
luck.
PropertiesConfiguration mergedConfig = new
PropertiesConfiguration();
CompositeConfiguration compositeConfig = new
CompositeConfiguration(defaultConfig);
mergedConfig = defaultConfig;
compositeConfig.addConfiguration(overridingConfig);
StringWriter writer = new StringWriter();
mergedConfig.save(writer);
String mergedContent = writer.toString();
What's wrong with the code above?
Thanks.
Re: [configuration] Merging overrides with defaults
Posted by Oliver Heger <ol...@oliver-heger.de>.
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
> <ol...@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
>>> <ol...@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
Re: [configuration] Merging overrides with defaults
Posted by Alec Swan <al...@gmail.com>.
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.
On Wed, Jun 17, 2009 at 2:38 PM, Oliver Heger
<ol...@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
>> <ol...@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
>
>
Re: [configuration] Merging overrides with defaults
Posted by Oliver Heger <ol...@oliver-heger.de>.
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
> <ol...@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
Re: [configuration] Merging overrides with defaults
Posted by Alec Swan <al...@gmail.com>.
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
On Wed, Jun 17, 2009 at 1:54 PM, Oliver Heger
<ol...@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
>
>
Re: [configuration] Merging overrides with defaults
Posted by Oliver Heger <ol...@oliver-heger.de>.
Alec Swan schrieb:
> This is really unfortunate. The merged configuration is required to preserve
> comments from defaults, but not necessarily overrides.
>
> Are there any plans to add this functionality in the near future?
>
> Thanks.
This is a new feature that was not considered before.
I think, it is not trivial to implement in a really generic way. Your
use case requires that one configuration overrides a default
configuration. For another use case a different merge algorithm may be
required. There are some details to think about.
However, you can open an enhancement request in our bug tracking system
[1]. I don't know if somebody finds the time to work on this feature
soon. But if you really need it, you can try to create a patch yourself.
This is open source ;-)
Oliver
[1] http://commons.apache.org/configuration/issue-tracking.html
>
> On Wed, Jun 17, 2009 at 1:54 PM, Oliver Heger
> <ol...@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
Re: [configuration] Merging overrides with defaults
Posted by Alec Swan <al...@gmail.com>.
This is really unfortunate. The merged configuration is required to preserve
comments from defaults, but not necessarily overrides.
Are there any plans to add this functionality in the near future?
Thanks.
On Wed, Jun 17, 2009 at 1:54 PM, Oliver Heger
<ol...@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
>
>
Re: [configuration] Merging overrides with defaults
Posted by Oliver Heger <ol...@oliver-heger.de>.
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 <jo...@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
Re: [configuration] Merging overrides with defaults
Posted by Alec Swan <al...@gmail.com>.
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.
On Wed, Jun 17, 2009 at 11:23 AM, Jörg Schaible <jo...@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
>
>
Re: [configuration] Merging overrides with defaults
Posted by Jörg Schaible <jo...@gmx.de>.
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
Re: [configuration] Merging overrides with defaults
Posted by Alec Swan <al...@gmail.com>.
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?
Thanks.
Alec
On Wed, Jun 17, 2009 at 1:45 AM, Jörg Schaible <jo...@gmx.de>wrote:
> Hi Alec,
>
> as commons contains quite a lot of components, it is best practice to
> include the component's name iln the subject to give a reader the context
> of your question.
>
> Alec Swan wrote at Mittwoch, 17. Juni 2009 00:00:
>
> > I have a property file containing default configurations in a key=value
> > format. I also have another file with the same format which contains
> > overrides for some of the properties. I need to apply the overrides to
> the
> > default configuration while preserving the format and comments. I also
> > need to write the merged content to a string.
> >
> > I am using the following code, but the mergedContent contains the default
> > content and no overrides. I tried reversing the order in which I add
> > defaultConfig and overridingConfig to CompositeConfiguration, but still
> no
> > luck.
> >
> > PropertiesConfiguration mergedConfig = new
> > PropertiesConfiguration();
> > CompositeConfiguration compositeConfig = new
> > CompositeConfiguration(defaultConfig);
> > mergedConfig = defaultConfig;
> >
> > compositeConfig.addConfiguration(overridingConfig);
> >
> > StringWriter writer = new StringWriter();
> > mergedConfig.save(writer);
> > String mergedContent = writer.toString();
> >
> > What's wrong with the code above?
>
> You add the configurations in the wrong sequence. A configuration that
> contains default values should always added last, since the
> CompositeConfiguration will return the first found value for the requested
> key.
>
> - Jörg
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>
Re: [configuration] Merging overrides with defaults
Posted by Jörg Schaible <jo...@gmx.de>.
Hi Alec,
as commons contains quite a lot of components, it is best practice to
include the component's name iln the subject to give a reader the context
of your question.
Alec Swan wrote at Mittwoch, 17. Juni 2009 00:00:
> I have a property file containing default configurations in a key=value
> format. I also have another file with the same format which contains
> overrides for some of the properties. I need to apply the overrides to the
> default configuration while preserving the format and comments. I also
> need to write the merged content to a string.
>
> I am using the following code, but the mergedContent contains the default
> content and no overrides. I tried reversing the order in which I add
> defaultConfig and overridingConfig to CompositeConfiguration, but still no
> luck.
>
> PropertiesConfiguration mergedConfig = new
> PropertiesConfiguration();
> CompositeConfiguration compositeConfig = new
> CompositeConfiguration(defaultConfig);
> mergedConfig = defaultConfig;
>
> compositeConfig.addConfiguration(overridingConfig);
>
> StringWriter writer = new StringWriter();
> mergedConfig.save(writer);
> String mergedContent = writer.toString();
>
> What's wrong with the code above?
You add the configurations in the wrong sequence. A configuration that
contains default values should always added last, since the
CompositeConfiguration will return the first found value for the requested
key.
- Jörg
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Merging overrides with defaults
Posted by Alec Swan <al...@gmail.com>.
I have a property file containing default configurations in a key=value
format. I also have another file with the same format which contains
overrides for some of the properties. I need to apply the overrides to the
default configuration while preserving the format and comments. I also need
to write the merged content to a string.
I am using the following code, but the mergedContent contains the default
content and no overrides. I tried reversing the order in which I add
defaultConfig and overridingConfig to CompositeConfiguration, but still no
luck.
PropertiesConfiguration mergedConfig = new
PropertiesConfiguration();
CompositeConfiguration compositeConfig = new
CompositeConfiguration(defaultConfig);
mergedConfig = defaultConfig;
compositeConfig.addConfiguration(overridingConfig);
StringWriter writer = new StringWriter();
mergedConfig.save(writer);
String mergedContent = writer.toString();
What's wrong with the code above?
Thanks.