001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.configuration.reloading;
019    
020    import org.apache.commons.configuration.FileConfiguration;
021    import org.apache.commons.logging.Log;
022    import org.apache.commons.logging.LogFactory;
023    
024    /**
025     * A strategy to reload configuration based on management requests. Designed for
026     * JMX management.
027     *
028     * @author Nicolas De loof
029     */
030    public class ManagedReloadingStrategy implements ReloadingStrategy,
031            ManagedReloadingStrategyMBean
032    {
033        /** The logger. */
034        private Log log = LogFactory.getLog(ManagedReloadingStrategy.class);
035    
036        /** Stores a reference to the associated configuration. */
037        private FileConfiguration configuration;
038    
039        /** A flag whether a reload is required. */
040        private boolean reloadingRequired;
041    
042        /**
043         * @see org.apache.commons.configuration.reloading.ReloadingStrategy#init()
044         */
045        public void init()
046        {
047        }
048    
049        /**
050         * @see org.apache.commons.configuration.reloading.ReloadingStrategy#reloadingPerformed()
051         */
052        public void reloadingPerformed()
053        {
054            reloadingRequired = false;
055        }
056    
057        /**
058         * Checks whether reloading is required. This implementation checks whether
059         * the <code>refresh()</code> method has been invokded.
060         *
061         * @return a flag whether reloading is required
062         * @see org.apache.commons.configuration.reloading.ReloadingStrategy#reloadingRequired()
063         */
064        public boolean reloadingRequired()
065        {
066            return reloadingRequired;
067        }
068    
069        /**
070         * Sets the associated configuration.
071         *
072         * @param configuration the associated configuration
073         */
074        public void setConfiguration(FileConfiguration configuration)
075        {
076            this.configuration = configuration;
077        }
078    
079        /**
080         * Tells this strategy that the monitored configuration file should be
081         * refreshed. This method will typically be called from outside (through an
082         * exposed MBean) on behalf of an administrator.
083         *
084         * @see org.apache.commons.configuration.reloading.ManagedReloadingStrategyMBean#refresh()
085         */
086        public void refresh()
087        {
088            log.info("Reloading configuration.");
089            this.reloadingRequired = true;
090            // force reloading
091            configuration.isEmpty();
092        }
093    }