1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.configuration.reloading;
19
20 import java.io.File;
21 import java.net.MalformedURLException;
22 import java.net.URL;
23
24 import org.apache.commons.configuration.ConfigurationUtils;
25 import org.apache.commons.configuration.FileConfiguration;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class FileChangedReloadingStrategy implements ReloadingStrategy
47 {
48
49 private static final String JAR_PROTOCOL = "jar";
50
51
52 private static final int DEFAULT_REFRESH_DELAY = 5000;
53
54
55 protected FileConfiguration configuration;
56
57
58 protected long lastModified;
59
60
61 protected long lastChecked;
62
63
64 protected long refreshDelay = DEFAULT_REFRESH_DELAY;
65
66
67 private boolean reloading;
68
69
70 private Log logger = LogFactory.getLog(FileChangedReloadingStrategy.class);
71
72 public void setConfiguration(FileConfiguration configuration)
73 {
74 this.configuration = configuration;
75 }
76
77 public void init()
78 {
79 updateLastModified();
80 }
81
82 public boolean reloadingRequired()
83 {
84 if (!reloading)
85 {
86 long now = System.currentTimeMillis();
87
88 if (now > lastChecked + refreshDelay)
89 {
90 lastChecked = now;
91 if (hasChanged())
92 {
93 if (logger.isDebugEnabled())
94 {
95 logger.debug("File change detected: " + getName());
96 }
97 reloading = true;
98 }
99 }
100 }
101
102 return reloading;
103 }
104
105 public void reloadingPerformed()
106 {
107 updateLastModified();
108 }
109
110
111
112
113
114
115 public long getRefreshDelay()
116 {
117 return refreshDelay;
118 }
119
120
121
122
123
124
125 public void setRefreshDelay(long refreshDelay)
126 {
127 this.refreshDelay = refreshDelay;
128 }
129
130
131
132
133 protected void updateLastModified()
134 {
135 File file = getFile();
136 if (file != null)
137 {
138 lastModified = file.lastModified();
139 }
140 reloading = false;
141 }
142
143
144
145
146
147
148 protected boolean hasChanged()
149 {
150 File file = getFile();
151 if (file == null || !file.exists())
152 {
153 if (logger.isWarnEnabled() && lastModified != 0)
154 {
155 logger.warn("File was deleted: " + getName(file));
156 lastModified = 0;
157 }
158 return false;
159 }
160
161 return file.lastModified() > lastModified;
162 }
163
164
165
166
167
168
169
170 protected File getFile()
171 {
172 return (configuration.getURL() != null) ? fileFromURL(configuration
173 .getURL()) : configuration.getFile();
174 }
175
176
177
178
179
180
181
182
183 private File fileFromURL(URL url)
184 {
185 if (JAR_PROTOCOL.equals(url.getProtocol()))
186 {
187 String path = url.getPath();
188 try
189 {
190 return ConfigurationUtils.fileFromURL(new URL(path.substring(0,
191 path.indexOf('!'))));
192 }
193 catch (MalformedURLException mex)
194 {
195 return null;
196 }
197 }
198 else
199 {
200 return ConfigurationUtils.fileFromURL(url);
201 }
202 }
203
204 private String getName()
205 {
206 return getName(getFile());
207 }
208
209 private String getName(File file)
210 {
211 String name = configuration.getURL().toString();
212 if (name == null)
213 {
214 if (file != null)
215 {
216 name = file.getAbsolutePath();
217 }
218 else
219 {
220 name = "base: " + configuration.getBasePath()
221 + "file: " + configuration.getFileName();
222 }
223 }
224 return name;
225 }
226 }