Author: oheger Date: Tue Feb 13 12:54:15 2007 New Revision: 507212 URL: http://svn.apache.org/viewvc?view=rev&rev=507212 Log: CONFIGURATION-237: Added ManagedReloadingStrategy. Thanks to Nicolas de Loof
Added: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/reloading/ManagedReloadingStrategy.java (with props) jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/reloading/ManagedReloadingStrategyMBean.java (with props) jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/reloading/TestManagedReloadingStrategy.java (with props) Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml jakarta/commons/proper/configuration/trunk/xdocs/howto_filebased.xml Added: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/reloading/ManagedReloadingStrategy.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/reloading/ManagedReloadingStrategy.java?view=auto&rev=507212 ============================================================================== --- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/reloading/ManagedReloadingStrategy.java (added) +++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/reloading/ManagedReloadingStrategy.java Tue Feb 13 12:54:15 2007 @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.configuration.reloading; + +import org.apache.commons.configuration.FileConfiguration; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * A strategy to reload configuration based on management requests. Designed for + * JMX management. + * + * @author Nicolas De loof + */ +public class ManagedReloadingStrategy implements ReloadingStrategy, + ManagedReloadingStrategyMBean +{ + /** The logger. */ + private Log log = LogFactory.getLog(ManagedReloadingStrategy.class); + + /** Stores a reference to the associated configuration. */ + private FileConfiguration configuration; + + /** A flag whether a reload is required. */ + private boolean reloadingRequired; + + /** + * @see org.apache.commons.configuration.reloading.ReloadingStrategy#init() + */ + public void init() + { + return; + } + + /** + * @see org.apache.commons.configuration.reloading.ReloadingStrategy#reloadingPerformed() + */ + public void reloadingPerformed() + { + reloadingRequired = false; + } + + /** + * Checks whether reloading is required. This implementation checks whether + * the <code>refresh()</code> method has been invokded. + * + * @return a flag whether reloading is required + * @see org.apache.commons.configuration.reloading.ReloadingStrategy#reloadingRequired() + */ + public boolean reloadingRequired() + { + return reloadingRequired; + } + + /** + * Sets the associated configuration. + * + * @param configuration the associated configuration + */ + public void setConfiguration(FileConfiguration configuration) + { + this.configuration = configuration; + } + + /** + * Tells this strategy that the monitored configuration file should be + * refreshed. This method will typically be called from outside (through an + * exposed MBean) on behalf of an administrator. + * + * @see org.apache.commons.configuration.reloading.ManagedReloadingStrategyMBean#refresh() + */ + public void refresh() + { + log.info("Reloading configuration."); + this.reloadingRequired = true; + // force reloading + configuration.isEmpty(); + } +} Propchange: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/reloading/ManagedReloadingStrategy.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/reloading/ManagedReloadingStrategy.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/reloading/ManagedReloadingStrategy.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/reloading/ManagedReloadingStrategyMBean.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/reloading/ManagedReloadingStrategyMBean.java?view=auto&rev=507212 ============================================================================== --- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/reloading/ManagedReloadingStrategyMBean.java (added) +++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/reloading/ManagedReloadingStrategyMBean.java Tue Feb 13 12:54:15 2007 @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.configuration.reloading; + +/** + * MBean definition for managing configuration reload. + * + * @author Nicolas De Loof + */ +public interface ManagedReloadingStrategyMBean +{ + /** + * Management method to force configuration reload. + */ + void refresh(); +} Propchange: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/reloading/ManagedReloadingStrategyMBean.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/reloading/ManagedReloadingStrategyMBean.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/reloading/ManagedReloadingStrategyMBean.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/reloading/TestManagedReloadingStrategy.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/reloading/TestManagedReloadingStrategy.java?view=auto&rev=507212 ============================================================================== --- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/reloading/TestManagedReloadingStrategy.java (added) +++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/reloading/TestManagedReloadingStrategy.java Tue Feb 13 12:54:15 2007 @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.configuration.reloading; + +import java.io.File; +import java.io.FileWriter; + +import org.apache.commons.configuration.PropertiesConfiguration; + +import junit.framework.TestCase; + +/** + * Test case for the ManagedReloadingStrategy class. + * + * @author Nicolas De loof + */ +public class TestManagedReloadingStrategy extends TestCase +{ + + public void testManagedRefresh() throws Exception + { + File file = new File("target/testReload.properties"); + if (file.exists()) + { + file.delete(); + } + // create the configuration file + FileWriter out = new FileWriter(file); + out.write("string=value1"); + out.flush(); + out.close(); + + // load the configuration + PropertiesConfiguration config = new PropertiesConfiguration("target/testReload.properties"); + ManagedReloadingStrategy strategy = new ManagedReloadingStrategy(); + config.setReloadingStrategy(strategy); + assertEquals("Initial value", "value1", config.getString("string")); + + // change the file + out = new FileWriter(file); + out.write("string=value2"); + out.flush(); + out.close(); + + // test the automatic reloading + assertEquals("No automatic reloading", "value1", config.getString("string")); + strategy.refresh(); + assertEquals("Modified value with enabled reloading", "value2", config.getString("string")); + } + +} Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/reloading/TestManagedReloadingStrategy.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/reloading/TestManagedReloadingStrategy.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/reloading/TestManagedReloadingStrategy.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?view=diff&rev=507212&r1=507211&r2=507212 ============================================================================== --- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original) +++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Tue Feb 13 12:54:15 2007 @@ -22,7 +22,11 @@ </properties> <body> - <release version="1.4" date="2007-02-06"> + <release version="1.4-SNAPSHOT" date="in SVN"> + <action dev="oheger" type="add" issue="CONFIGURATION-237" due-to="Nicolas de Loof"> + With ManagedReloadingStrategy a new reloading strategy for file-based + configurations was added that can be triggered through JMX. + </action> <action dev="oheger" type="update"> The dependencies to Commons Collections and Commons Digester are updated to use the recent available version. However older versions Modified: jakarta/commons/proper/configuration/trunk/xdocs/howto_filebased.xml URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/xdocs/howto_filebased.xml?view=diff&rev=507212&r1=507211&r2=507212 ============================================================================== --- jakarta/commons/proper/configuration/trunk/xdocs/howto_filebased.xml (original) +++ jakarta/commons/proper/configuration/trunk/xdocs/howto_filebased.xml Tue Feb 13 12:54:15 2007 @@ -24,15 +24,15 @@ </properties> <body> - <section name="File-based Configurations"> - <p> - Often configuration properties are stored in files on the user's hard + <section name="File-based Configurations"> + <p> + Often configuration properties are stored in files on the user's hard disk, e.g. in .properties files or as XML documents. Configuration classes that deal with such properties need to provide typical operations like loading or saving files. The files to be processed can be specified in several different flavors like <code>java.io.File</code> objects, relative or absolute path names, or URLs. - </p> + </p> <p> To provide a consistent way of dealing with configuration files in Commons Configuration the <code><a href="apidocs/org/apache/commons/configuration/FileConfiguration.html">FileConfiguration</a></code> @@ -45,7 +45,7 @@ In the following sections we take a closer look at the methods of the <code>FileConfiguration</code> interface and how they are used. </p> - + <subsection name="Specifying the file"> <p> The <code>FileConfiguration</code> interface contains several @@ -85,7 +85,7 @@ be thrown. </p> </subsection> - + <subsection name="Loading"> <p> After the file name has been defined using one of the methods mentioned @@ -122,7 +122,7 @@ content can be done in a single step. </p> </subsection> - + <subsection name="Saving"> <p> Saving is implemented analogously to loading: There is a no argument @@ -150,7 +150,7 @@ config.save("usergui.backup.properties); </source> </subsection> - + <subsection name="Automatic Saving"> <p> If you want to ensure that every modification of a configuration @@ -168,7 +168,7 @@ configuration. This will lead to many I/O operations, too. </p> </subsection> - + <subsection name="Automatic Reloading"> <p> A common issue with file-based configurations is to handle the @@ -196,6 +196,45 @@ the reloading strategy. This is a time in milli seconds with the meaning that the reloading strategy will only once check the file's last modification time in the period specified here. + </p> + </subsection> + + <subsection name="Managed Reloading"> + <p> + <code>ManagedReloadingStrategy</code> is an alternative to automatic + reloading. It allows to hot-reload properties on a running application + but only when requested by admin. The <code>refresh()</code> method + will force a reload of the configuration source. + </p> + <p> + A typical use of this feature is to setup ManagedReloadingStrategy as + a JMX MBean. The following code sample uses Springframework + MBeanExporter to expose the ManagedReloadingStrategy to the JMX + console : +<source> +<![CDATA[ +<!-- A file based configuration bean --> +<bean id="configuration" class="(...).PropertiesConfiguration"> + <constructor-arg type="java.net.URL" value="file:${user.home}/custom.properties"/> + <property name="reloadingStrategy" ref="reloadingStrategy"/> +</bean> + +<!-- The managed reloading strategy for the configuration bean --> +<bean id="reloadingStrategy" class="...ManagedReloadingStrategy"/> + +<!-- The MBeanExporter that exposes reloadingStrategy to the JMX console --> +<bean id="mbeanMetadataExporter" class="org.springframework.jmx.export.MBeanExporter"> + <property name="server" ref="mbeanServer"/> + <property name="beans"> + <map> + <entry key="myApp:bean=configuration" value-ref="reloadingStrategy"/> + </map> + </property> +</bean> +]]> +</source> + With this configuration, the JMX console will expose the + "myApp:bean=configuration" MBean and it's refresh operation. </p> </subsection> </section> --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]