Hello,

On 17.06.2010, at 11:21, Bengt Rodehav wrote:

> Hello!
> 
> I use iPOJO (1.4.0) and File Install (3.0.0) deployed on Karaf (1.6.0).
> 
> I use the service factory pattern in OSGi to instantiate services from
> configuration files picked up by File Install. Howrver, there seems to be a
> startup problem. When the service is originally started, not all properties
> (in my newly instantiated service have received their values from the
> configuration file). Using Felix web console (the Configuration tab) the
> property values are correct. However, when I look under the iPOJO tab on the
> created instances the values are wrong. If I do any change in my
> configuration file (e g add some whitspace) and save it, File Install picks
> up the change and the iPOJO instance is now configured with the correct
> value.
> 
> I've seen a pattern regarding this. It seems like properties that are not
> given a default value in my code (initialised to null) will get their
> configuration set properly but the properties that are initialised to
> another value than null will not get their proper value until I later
> re-save my configuration file (which I guess is the next time File Install
> will propagate configuration changes).
> 
> Given the following code in my iPOJO service:
> 
> ...
>  @Property(name = "toUri", mandatory = false)
>  private String mToUri;
> 
>  @Property(name = "delay", mandatory = false)
>  private long mDelay = 1000;
> ...
> 
> The "toUri" property will be initialised properly when the service is
> started but the "delay" property will be initialised to 1000 regardless of
> what value I put in the configuration file. When I later re-save the
> configuration file, the proper value of the "delay" property will be
> propagated.

Your instance receives the configuration from the config admin before that an 
'object' is created. So iPOJO associates mDelay to the correct value (from the 
configuration). However, then, an object is created (and so the constructor is 
called), and it executes an implicit constructor doing:
mDelay = 1000;

This overrides the value received by the configuration. If the configuration is 
updated, the instance receives the new value overriding the 1000.

So to avoid such behavior, just do:
@Property(name="delay", mandatory=false, value="1000")
private long mDelay; // No value here

This will inject 1000 if the property is not set in the configuration, or 
inject the value from the configuration.

Regards,

Clement




> 
> How can I have a default value but still have it overridden properly?
> 
> /Bengt


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@felix.apache.org
For additional commands, e-mail: users-h...@felix.apache.org

Reply via email to