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 org.apache.commons.configuration.ConfigurationRuntimeException; |
21 | |
import org.apache.commons.configuration.FileConfiguration; |
22 | |
import org.apache.commons.configuration.FileSystem; |
23 | |
import org.apache.commons.configuration.FileSystemBased; |
24 | |
import org.apache.commons.logging.Log; |
25 | |
import org.apache.commons.logging.LogFactory; |
26 | |
import org.apache.commons.vfs2.FileObject; |
27 | |
import org.apache.commons.vfs2.FileSystemException; |
28 | |
import org.apache.commons.vfs2.FileSystemManager; |
29 | |
import org.apache.commons.vfs2.VFS; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 20 | public class VFSFileChangedReloadingStrategy implements ReloadingStrategy |
53 | |
{ |
54 | |
|
55 | |
private static final int DEFAULT_REFRESH_DELAY = 5000; |
56 | |
|
57 | |
|
58 | |
protected FileConfiguration configuration; |
59 | |
|
60 | |
|
61 | |
protected long lastModified; |
62 | |
|
63 | |
|
64 | |
protected long lastChecked; |
65 | |
|
66 | |
|
67 | 20 | protected long refreshDelay = DEFAULT_REFRESH_DELAY; |
68 | |
|
69 | |
|
70 | |
private boolean reloading; |
71 | |
|
72 | |
|
73 | 20 | private Log log = LogFactory.getLog(getClass()); |
74 | |
|
75 | |
public void setConfiguration(FileConfiguration configuration) |
76 | |
{ |
77 | 20 | this.configuration = configuration; |
78 | 20 | } |
79 | |
|
80 | |
public void init() |
81 | |
{ |
82 | 14 | if (configuration.getURL() == null && configuration.getFileName() == null) |
83 | |
{ |
84 | 0 | return; |
85 | |
} |
86 | 14 | if (this.configuration == null) |
87 | |
{ |
88 | 0 | throw new IllegalStateException("No configuration has been set for this strategy"); |
89 | |
} |
90 | 14 | updateLastModified(); |
91 | 14 | } |
92 | |
|
93 | |
public boolean reloadingRequired() |
94 | |
{ |
95 | 64 | if (!reloading) |
96 | |
{ |
97 | 63 | long now = System.currentTimeMillis(); |
98 | |
|
99 | 63 | if (now > lastChecked + refreshDelay) |
100 | |
{ |
101 | 37 | lastChecked = now; |
102 | 37 | if (hasChanged()) |
103 | |
{ |
104 | 9 | reloading = true; |
105 | |
} |
106 | |
} |
107 | |
} |
108 | |
|
109 | 64 | return reloading; |
110 | |
} |
111 | |
|
112 | |
public void reloadingPerformed() |
113 | |
{ |
114 | 8 | updateLastModified(); |
115 | 8 | } |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public long getRefreshDelay() |
123 | |
{ |
124 | 7 | return refreshDelay; |
125 | |
} |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
public void setRefreshDelay(long refreshDelay) |
133 | |
{ |
134 | 20 | this.refreshDelay = refreshDelay; |
135 | 20 | } |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
protected void updateLastModified() |
141 | |
{ |
142 | 22 | FileObject file = getFile(); |
143 | 22 | if (file != null) |
144 | |
{ |
145 | |
try |
146 | |
{ |
147 | 22 | lastModified = file.getContent().getLastModifiedTime(); |
148 | |
} |
149 | 3 | catch (FileSystemException fse) |
150 | |
{ |
151 | 3 | log.error("Unable to get last modified time for" + file.getName().getURI()); |
152 | 19 | } |
153 | |
} |
154 | 22 | reloading = false; |
155 | 22 | } |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
protected boolean hasChanged() |
163 | |
{ |
164 | 36 | FileObject file = getFile(); |
165 | |
try |
166 | |
{ |
167 | 36 | if (file == null || !file.exists()) |
168 | |
{ |
169 | 5 | return false; |
170 | |
} |
171 | |
|
172 | 31 | return file.getContent().getLastModifiedTime() > lastModified; |
173 | |
} |
174 | 0 | catch (FileSystemException ex) |
175 | |
{ |
176 | 0 | log.error("Unable to get last modified time for" + file.getName().getURI()); |
177 | 0 | return false; |
178 | |
} |
179 | |
} |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
protected FileObject getFile() |
188 | |
{ |
189 | |
try |
190 | |
{ |
191 | 58 | FileSystemManager fsManager = VFS.getManager(); |
192 | 58 | FileSystem fs = ((FileSystemBased) configuration).getFileSystem(); |
193 | 58 | String uri = fs.getPath(null, configuration.getURL(), configuration.getBasePath(), |
194 | |
configuration.getFileName()); |
195 | 58 | if (uri == null) |
196 | |
{ |
197 | 0 | throw new ConfigurationRuntimeException("Unable to determine file to monitor"); |
198 | |
} |
199 | 58 | return fsManager.resolveFile(uri); |
200 | |
} |
201 | 0 | catch (FileSystemException fse) |
202 | |
{ |
203 | 0 | String msg = "Unable to monitor " + configuration.getURL().toString(); |
204 | 0 | log.error(msg); |
205 | 0 | throw new ConfigurationRuntimeException(msg, fse); |
206 | |
} |
207 | |
} |
208 | |
} |