View Javadoc

1   /*
2    * Copyright 2001-2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.configuration.reloading;
18  
19  import org.apache.commons.configuration.FileConfiguration;
20  
21  /***
22   * A reloading strategy that will reload the configuration every time its
23   * underlying file is changed. The file is not reloaded more than once
24   * every 5 seconds by default, this time can be changed by setting the refresh
25   * delay. This strategy only works with FileConfiguration instances.
26   *
27   * @author Emmanuel Bourg
28   * @version $Revision: 155945 $, $Date: 2005-03-02 20:29:39 +0100 (Mi, 02 Mrz 2005) $
29   * @since 1.1
30   */
31  public class FileChangedReloadingStrategy implements ReloadingStrategy
32  {
33      protected FileConfiguration configuration;
34  
35      /*** The last time the configuration file was modified. */
36      protected long lastModified;
37  
38      /*** The last time the file was checked for changes. */
39      protected long lastChecked;
40  
41      /*** The minimum delay in milliseconds between checks. */
42      protected long refreshDelay = 5000;
43  
44      public void setConfiguration(FileConfiguration configuration)
45      {
46          this.configuration = configuration;
47      }
48  
49      public void init()
50      {
51          updateLastModified();
52      }
53  
54      public boolean reloadingRequired()
55      {
56          boolean reloading = false;
57  
58          long now = System.currentTimeMillis();
59  
60          if ((now > lastChecked + refreshDelay) && hasChanged())
61          {
62              lastChecked = now;
63              reloading = true;
64          }
65  
66          return reloading;
67      }
68  
69      public void reloadingPerformed()
70      {
71          updateLastModified();
72      }
73  
74      /***
75       * Return the minimal time in milliseconds between two reloadings.
76       */
77      public long getRefreshDelay()
78      {
79          return refreshDelay;
80      }
81  
82      /***
83       * Set the minimal time between two reloadings.
84       *
85       * @param refreshDelay refresh delay in milliseconds
86       */
87      public void setRefreshDelay(long refreshDelay)
88      {
89          this.refreshDelay = refreshDelay;
90      }
91  
92      /***
93       * Update the last modified time.
94       */
95      protected void updateLastModified()
96      {
97          lastModified = configuration.getFile().lastModified();
98      }
99  
100     /***
101      * Check if the configuration has changed since the last time it was loaded.
102      */
103     protected boolean hasChanged()
104     {
105         if (!configuration.getFile().exists())
106         {
107             return false;
108         }
109 
110         return (configuration.getFile().lastModified() > lastModified);
111     }
112 
113 }