[configuration]

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[configuration]

Rainer Hirschmiller
Hi.

I'm trying configurations2 and PropertiesConfiguration. I have created a
helper class doing some error handling and configuration for
ProperiesConfiguration. Especially I load properties from different
property files. I want to do this to define basic configurations in one
file and be able to override these configurations in a second file which
will be loaded later.

Getting the property of such a double defined key which public <T> T
get(final Class<T> cls, final String key) I got the first defined value.
OK, I've overriden the method to get the last defined value.

But I want to know why the behaviour is at it is. Is there a special
reason? I think returning the last defined value would be preferred by
other users allthough. What's do other users think about this behaviour?

Regards
Rainer


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [configuration]

Julio Oliveira
Hi  Rainer

Can you attach the code, to see it.

Regards

Julio M. Oliveira

On Fri, Apr 1, 2016 at 6:52 PM, Rainer Hirschmiller <
[hidden email]> wrote:

> Hi.
>
> I'm trying configurations2 and PropertiesConfiguration. I have created a
> helper class doing some error handling and configuration for
> ProperiesConfiguration. Especially I load properties from different
> property files. I want to do this to define basic configurations in one
> file and be able to override these configurations in a second file which
> will be loaded later.
>
> Getting the property of such a double defined key which public <T> T
> get(final Class<T> cls, final String key) I got the first defined value.
> OK, I've overriden the method to get the last defined value.
>
> But I want to know why the behaviour is at it is. Is there a special
> reason? I think returning the last defined value would be preferred by
> other users allthough. What's do other users think about this behaviour?
>
> Regards
> Rainer
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>


--
Saludos

Julio M. Oliveira - Buenos Aires
http://www.linkedin.com/in/juliomoliveira

Software PYMES - Open Source
http://www.javatango.com/ <http://www.linkedin.com/in/juliomoliveira>
Reply | Threaded
Open this post in threaded view
|

Re: [configuration]

Rainer Hirschmiller
Here a simple example. See see two outputs in the main function.

The first uses the original get-method and returns the first defined
value. The second call(myGet) returns the second defined value.


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;

import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.configuration2.ex.ConversionException;
import org.apache.commons.lang3.ObjectUtils;

public class ConfigurationsHelper extends PropertiesConfiguration {
     public static ConfigurationsHelper INSTANCE = new
ConfigurationsHelper();

     private ConfigurationsHelper() {

         super();
     }

     public static void main(final String[] args) {

         ConfigurationsHelper.read("/one.properties");
         ConfigurationsHelper.read("/two.properties");

System.out.println(ConfigurationsHelper.INSTANCE.get(String.class, "key"));
System.out.println(ConfigurationsHelper.INSTANCE.myGet(String.class,
"key"));
     }

     public static void read(final String filename) {

         final InputStream is =
ConfigurationsHelper.class.getResourceAsStream(filename);

         if (is == null) {
             System.out.println(new FileNotFoundException(filename));
         } else {
             try (Reader reader = new InputStreamReader(is)) {
                 ConfigurationsHelper.INSTANCE.read(reader);
             } catch (final IOException | ConfigurationException
exception) {
                 System.out.println(exception);
             }
         }
     }

     public <T> T myGet(final Class<T> cls, final String key) {
         final List<Object> obj =
ConfigurationsHelper.INSTANCE.getList(key);
         final Object value = obj.get(obj.size() - 1);
         T result = null;

         try {
             result = ObjectUtils.defaultIfNull(
                     this.getConversionHandler().to(value, cls,
this.getInterpolator()),
                     result);
         } catch (final ConversionException cex) {
             System.out.println(cex);

             // improve error message

             final String format = "Key '%s' cannot be converted to
class %s. Value is: '%s'.";             final String msg =
String.format(format, key, cls.getName(), String.valueOf(value));

             throw new ConversionException(msg);
         }

         return result;
     }

}

Am 02.04.2016 um 14:53 schrieb Julio Oliveira:

> Hi  Rainer
>
> Can you attach the code, to see it.
>
> Regards
>
> Julio M. Oliveira
>
> On Fri, Apr 1, 2016 at 6:52 PM, Rainer Hirschmiller <
> [hidden email]> wrote:
>
>> Hi.
>>
>> I'm trying configurations2 and PropertiesConfiguration. I have created a
>> helper class doing some error handling and configuration for
>> ProperiesConfiguration. Especially I load properties from different
>> property files. I want to do this to define basic configurations in one
>> file and be able to override these configurations in a second file which
>> will be loaded later.
>>
>> Getting the property of such a double defined key which public <T> T
>> get(final Class<T> cls, final String key) I got the first defined value.
>> OK, I've overriden the method to get the last defined value.
>>
>> But I want to know why the behaviour is at it is. Is there a special
>> reason? I think returning the last defined value would be preferred by
>> other users allthough. What's do other users think about this behaviour?
>>
>> Regards
>> Rainer
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [configuration]

Oliver Heger-3
In reply to this post by Rainer Hirschmiller
Hi Rainer,

On 01.04.2016 23:52, Rainer Hirschmiller wrote:

> Hi.
>
> I'm trying configurations2 and PropertiesConfiguration. I have created a
> helper class doing some error handling and configuration for
> ProperiesConfiguration. Especially I load properties from different
> property files. I want to do this to define basic configurations in one
> file and be able to override these configurations in a second file which
> will be loaded later.
>
> Getting the property of such a double defined key which public <T> T
> get(final Class<T> cls, final String key) I got the first defined value.
> OK, I've overriden the method to get the last defined value.
>
> But I want to know why the behaviour is at it is. Is there a special
> reason? I think returning the last defined value would be preferred by
> other users allthough. What's do other users think about this behaviour?

when multiple configuration files are loaded into a
PropertiesConfiguration a union of the configuration settings is
created. In your case, you get a property with multiple values, and
querying this property with a get method that returns only a single
value selects the first value per default. With getList() you could
query all values of this property.

IIUC, what you want to achieve is an override semantics. The preferred
way to do this with Commons Configuration is using a
CombinedConfigurationBuilder. This builder reads a definition file that
references multiple configuration sources and constructs a combined
configuration out of it. You can control how this combination is done,
e.g. with union or override semantics.

Please have a look at the user's guide:
http://commons.apache.org/proper/commons-configuration/userguide/howto_combinedbuilder.html

There is a section "Overriding properties" that should describe your use
case.

Oliver

>
> Regards
> Rainer
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]