commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ralph Goers <ralph.go...@dslextreme.com>
Subject Re: Commons Configuration: Using Reload Strategy
Date Mon, 16 May 2011 03:47:45 GMT
A static block is only going to be executed once when the class is first accessed.  There is
no way in Java that anything can modify the value of testMe after the class has been loaded.
In fact, as coded, after the static block is executed testMe isn't even available any more.

Ralph


On May 15, 2011, at 3:56 PM, venkat veludandi wrote:

> Good afternoon.
> I am using commons configuration as follows:
>  
> commons_configuration.xml:
>  
> <?xml version="1.0" encoding="UTF-8"?>
> <configuration>
>   <properties fileName="test.properties" config-name="test">
>    <reloadingStrategy refreshDelay="30000"
>      config-class="org.apache.commons.configuration.reloading.FileChangedReloadingStrategy"/>
>   </properties>
> </configuration>
>  
> ===============================================
> CommonsConfiguration.java
> ----------------------------------------
>  
> public class CommonsConfiguration {
>  
>  
>  private static CombinedConfiguration configuration;
>  
>  
>  static {
>   try {
>    DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(new File("/var/commons_configuration.xml"));
>    configuration = builder.getConfiguration(true);
>   } catch (ConfigurationException e) {
>    e.printStackTrace();
>   }
>  }
>  
>  
>  public static CombinedConfiguration getCombinedConfiguration() {
>   return configuration;
>  }
> }
> =================================================================
> When I use commons configuration as follows in any instance/static method:
>  
> String testMe= (String) CommonsConfiguration.getCombinedConfiguration().getConfiguration("test").getProperty("test.me");
>  
> It work fine with reloading strategy. In other words when I change the test.properties
property value, the change gets reflected.
>  
> In some class I was accessing the property in a static block as follows:
> public Sample {
>  static{
>   String testMe = String) CommonsConfiguration.getCombinedConfiguration().getConfiguration("test").getProperty("test.me");
>   //do something
>  }
> }
>  
> If a property is accessed in static block and the property value is changed, the change
does not get reflected.
> Is there any work around to access a property in static block and have the property change
reflected.
>  
> I appreciate and thank you for your feed back.
>  
> Thanks
> Venkat


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


Mime
View raw message