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     * @version $Id: ManagedReloadingStrategy.java 1210646 2011-12-05 21:25:01Z oheger $
030     */
031    public class ManagedReloadingStrategy implements ReloadingStrategy,
032            ManagedReloadingStrategyMBean
033    {
034        /** The logger. */
035        private Log log = LogFactory.getLog(ManagedReloadingStrategy.class);
036    
037        /** Stores a reference to the associated configuration. */
038        private FileConfiguration configuration;
039    
040        /** A flag whether a reload is required. */
041        private boolean reloadingRequired;
042    
043        /**
044         * @see org.apache.commons.configuration.reloading.ReloadingStrategy#init()
045         */
046        public void init()
047        {
048        }
049    
050        /**
051         * @see org.apache.commons.configuration.reloading.ReloadingStrategy#reloadingPerformed()
052         */
053        public void reloadingPerformed()
054        {
055            reloadingRequired = false;
056        }
057    
058        /**
059         * Checks whether reloading is required. This implementation checks whether
060         * the {@code refresh()} method has been invoked.
061         *
062         * @return a flag whether reloading is required
063         * @see org.apache.commons.configuration.reloading.ReloadingStrategy#reloadingRequired()
064         */
065        public boolean reloadingRequired()
066        {
067            return reloadingRequired;
068        }
069    
070        /**
071         * Sets the associated configuration.
072         *
073         * @param configuration the associated configuration
074         */
075        public void setConfiguration(FileConfiguration configuration)
076        {
077            this.configuration = configuration;
078        }
079    
080        /**
081         * Tells this strategy that the monitored configuration file should be
082         * refreshed. This method will typically be called from outside (through an
083         * exposed MBean) on behalf of an administrator.
084         *
085         * @see org.apache.commons.configuration.reloading.ManagedReloadingStrategyMBean#refresh()
086         */
087        public void refresh()
088        {
089            log.info("Reloading configuration.");
090            this.reloadingRequired = true;
091            // force reloading
092            configuration.isEmpty();
093        }
094    }