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 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 protected long refreshDelay = DEFAULT_REFRESH_DELAY;
68
69
70 private boolean reloading;
71
72
73 private Log log = LogFactory.getLog(getClass());
74
75 public void setConfiguration(FileConfiguration configuration)
76 {
77 this.configuration = configuration;
78 }
79
80 public void init()
81 {
82 if (configuration.getURL() == null && configuration.getFileName() == null)
83 {
84 return;
85 }
86 if (this.configuration == null)
87 {
88 throw new IllegalStateException("No configuration has been set for this strategy");
89 }
90 updateLastModified();
91 }
92
93 public boolean reloadingRequired()
94 {
95 if (!reloading)
96 {
97 long now = System.currentTimeMillis();
98
99 if (now > lastChecked + refreshDelay)
100 {
101 lastChecked = now;
102 if (hasChanged())
103 {
104 reloading = true;
105 }
106 }
107 }
108
109 return reloading;
110 }
111
112 public void reloadingPerformed()
113 {
114 updateLastModified();
115 }
116
117
118
119
120
121
122 public long getRefreshDelay()
123 {
124 return refreshDelay;
125 }
126
127
128
129
130
131
132 public void setRefreshDelay(long refreshDelay)
133 {
134 this.refreshDelay = refreshDelay;
135 }
136
137
138
139
140 protected void updateLastModified()
141 {
142 FileObject file = getFile();
143 if (file != null)
144 {
145 try
146 {
147 lastModified = file.getContent().getLastModifiedTime();
148 }
149 catch (FileSystemException fse)
150 {
151 log.error("Unable to get last modified time for" + file.getName().getURI());
152 }
153 }
154 reloading = false;
155 }
156
157
158
159
160
161
162 protected boolean hasChanged()
163 {
164 FileObject file = getFile();
165 try
166 {
167 if (file == null || !file.exists())
168 {
169 return false;
170 }
171
172 return file.getContent().getLastModifiedTime() > lastModified;
173 }
174 catch (FileSystemException ex)
175 {
176 log.error("Unable to get last modified time for" + file.getName().getURI());
177 return false;
178 }
179 }
180
181
182
183
184
185
186
187 protected FileObject getFile()
188 {
189 try
190 {
191 FileSystemManager fsManager = VFS.getManager();
192 FileSystem fs = ((FileSystemBased) configuration).getFileSystem();
193 String uri = fs.getPath(null, configuration.getURL(), configuration.getBasePath(),
194 configuration.getFileName());
195 if (uri == null)
196 {
197 throw new ConfigurationRuntimeException("Unable to determine file to monitor");
198 }
199 return fsManager.resolveFile(uri);
200 }
201 catch (FileSystemException fse)
202 {
203 String msg = "Unable to monitor " + configuration.getURL().toString();
204 log.error(msg);
205 throw new ConfigurationRuntimeException(msg, fse);
206 }
207 }
208 }