View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.configuration.reloading;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  import java.io.File;
23  import java.util.Random;
24  
25  /**
26   * A ReloadingStrategy that randomly returns true or false;
27   */
28  public class FileRandomReloadingStrategy extends FileChangedReloadingStrategy
29  {
30      Random random = new Random();
31  
32        /** The Log to use for diagnostic messages */
33      private Log logger = LogFactory.getLog(FileRandomReloadingStrategy.class);
34  
35      /**
36       * Checks whether a reload is necessary.
37       *
38       * @return a flag whether a reload is required
39       */
40      @Override
41      public boolean reloadingRequired()
42      {
43          boolean result = random.nextBoolean();
44          if (result)
45          {
46              if (logger.isDebugEnabled())
47              {
48                  logger.debug("File change detected: " + getName());
49              }
50          }
51          return result;
52      }
53  
54      /**
55       * Returns the file that is watched by this strategy.
56       *
57       * @return the monitored file
58       */
59      public File getMonitoredFile()
60      {
61          return getFile();
62      }
63  
64      private String getName()
65      {
66          return getName(getFile());
67      }
68  
69      private String getName(File file)
70      {
71          String name = configuration.getURL().toString();
72          if (name == null)
73          {
74              if (file != null)
75              {
76                  name = file.getAbsolutePath();
77              }
78              else
79              {
80                  name = "base: " + configuration.getBasePath()
81                         + "file: " + configuration.getFileName();
82              }
83          }
84          return name;
85      }
86  }