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    package org.apache.commons.configuration.reloading;
018    
019    import org.apache.commons.logging.Log;
020    import org.apache.commons.logging.LogFactory;
021    
022    import java.io.File;
023    import java.util.Random;
024    
025    /**
026     * A ReloadingStrategy that randomly returns true or false;
027     */
028    public class FileRandomReloadingStrategy extends FileChangedReloadingStrategy
029    {
030        Random random = new Random();
031    
032          /** The Log to use for diagnostic messages */
033        private Log logger = LogFactory.getLog(FileRandomReloadingStrategy.class);
034    
035        /**
036         * Checks whether a reload is necessary.
037         *
038         * @return a flag whether a reload is required
039         */
040        @Override
041        public boolean reloadingRequired()
042        {
043            boolean result = random.nextBoolean();
044            if (result)
045            {
046                if (logger.isDebugEnabled())
047                {
048                    logger.debug("File change detected: " + getName());
049                }
050            }
051            return result;
052        }
053    
054        /**
055         * Returns the file that is watched by this strategy.
056         *
057         * @return the monitored file
058         */
059        public File getMonitoredFile()
060        {
061            return getFile();
062        }
063    
064        private String getName()
065        {
066            return getName(getFile());
067        }
068    
069        private String getName(File file)
070        {
071            String name = configuration.getURL().toString();
072            if (name == null)
073            {
074                if (file != null)
075                {
076                    name = file.getAbsolutePath();
077                }
078                else
079                {
080                    name = "base: " + configuration.getBasePath()
081                           + "file: " + configuration.getFileName();
082                }
083            }
084            return name;
085        }
086    }