1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }