47 |
|
* Return a reference to the single class. |
48 |
|
* Construct it if it does not already exist, otherwise just return the reference. |
49 |
|
*/ |
50 |
< |
public static WebFeeder getInstance() { |
50 |
> |
public synchronized static WebFeeder getInstance() { |
51 |
|
if (_instance == null){ |
52 |
|
_instance = new WebFeeder(); |
53 |
|
} |
148 |
|
|
149 |
|
// list the files and delete as appropriate |
150 |
|
File alertsDir = new File(rootPath, alertSubDir); |
151 |
+ |
// get all the hostnames directories |
152 |
|
File[] contents = alertsDir.listFiles(); |
153 |
|
for(int i=0; i < contents.length; i++) { |
154 |
+ |
// get a single directory from the array.. |
155 |
|
File hostdir = contents[i]; |
156 |
+ |
// ..and check it's a directory |
157 |
|
if(hostdir.isDirectory()) { |
158 |
+ |
// get all the contents of that directory |
159 |
|
File[] hostdirContents = hostdir.listFiles(); |
160 |
|
for(int j=0; j < hostdirContents.length; j++) { |
161 |
|
File alertFile = hostdirContents[j]; |
162 |
+ |
// get the filename.. |
163 |
|
String filename = alertFile.getName(); |
164 |
+ |
// ..and see if it ends with OK or FINAL |
165 |
|
if(filename.endsWith(Alert.alertLevels[0]) || |
166 |
|
filename.endsWith(Alert.alertLevels[Alert.alertLevels.length-1])) { |
167 |
< |
// it ends with either OK or FINAL |
167 |
> |
// it does end with either OK or FINAL |
168 |
|
// ... so we can check it for deletion |
169 |
|
long lastModified = alertFile.lastModified(); |
170 |
|
long age = System.currentTimeMillis() - lastModified; |
171 |
|
if(age > ((long) deleteOlderThan*1000)) { |
172 |
|
// it's also older than our age to delete older than |
173 |
< |
if(!alertFile.delete()) { |
173 |
> |
if(alertFile.delete()) { |
174 |
> |
// do a quick check to see if the directory is now empty |
175 |
> |
File[] newHostdirContents = hostdir.listFiles(); |
176 |
> |
if(newHostdirContents.length == 0) { |
177 |
> |
// it does seem to be, try and delete it |
178 |
> |
// this will fail anyway if files still remain |
179 |
> |
if(!hostdir.delete()) { |
180 |
> |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete the following empty host directory: "+hostdir.getPath()); |
181 |
> |
} |
182 |
> |
} |
183 |
> |
} |
184 |
> |
else { |
185 |
|
_logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'old' alert file: "+alertFile.getPath()); |
186 |
|
} |
187 |
|
} |