1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.org.iscream.client; |
2 |
> |
package uk.org.iscream.cms.server.client; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
< |
import uk.org.iscream.componentmanager.*; |
6 |
< |
import uk.org.iscream.core.*; |
7 |
< |
import uk.org.iscream.util.*; |
5 |
> |
import uk.org.iscream.cms.server.componentmanager.*; |
6 |
> |
import uk.org.iscream.cms.server.core.*; |
7 |
> |
import uk.org.iscream.cms.server.util.*; |
8 |
|
import java.io.*; |
9 |
|
|
10 |
|
/** |
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 |
+ |
// if this is set, we clean files older than it |
159 |
+ |
long deleteFiles = -1; |
160 |
+ |
// get all the contents of that directory |
161 |
|
File[] hostdirContents = hostdir.listFiles(); |
162 |
|
for(int j=0; j < hostdirContents.length; j++) { |
163 |
|
File alertFile = hostdirContents[j]; |
164 |
+ |
// get the filename.. |
165 |
|
String filename = alertFile.getName(); |
166 |
+ |
// ..and see if it ends with OK or FINAL |
167 |
|
if(filename.endsWith(Alert.alertLevels[0]) || |
168 |
|
filename.endsWith(Alert.alertLevels[Alert.alertLevels.length-1])) { |
169 |
< |
// it ends with either OK or FINAL |
169 |
> |
// it does end with either OK or FINAL |
170 |
|
// ... so we can check it for deletion |
171 |
|
long lastModified = alertFile.lastModified(); |
172 |
|
long age = System.currentTimeMillis() - lastModified; |
173 |
|
if(age > ((long) deleteOlderThan*1000)) { |
174 |
+ |
// if we're on a final heartbeat, we probably want to |
175 |
+ |
// clean up any stale alerts left behind |
176 |
+ |
// by setting this flag, we'll clean them up on leaving this loop |
177 |
+ |
if(filename.endsWith(".HB."+Alert.alertLevels[Alert.alertLevels.length-1])) { |
178 |
+ |
// we do this so that delete files is set to the |
179 |
+ |
// latest date of a HB.FINAL. There should only be |
180 |
+ |
// one of them though :) |
181 |
+ |
if(lastModified > deleteFiles) { |
182 |
+ |
deleteFiles = lastModified; |
183 |
+ |
} |
184 |
+ |
} |
185 |
|
// it's also older than our age to delete older than |
186 |
|
if(!alertFile.delete()) { |
187 |
|
_logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'old' alert file: "+alertFile.getPath()); |
189 |
|
} |
190 |
|
} |
191 |
|
} |
192 |
+ |
// cleanup stale alerts |
193 |
+ |
if(deleteFiles >= 0) { |
194 |
+ |
File[] remainingHostdirContents = hostdir.listFiles(); |
195 |
+ |
for(int j=0; j < remainingHostdirContents.length; j++) { |
196 |
+ |
File alertFile = remainingHostdirContents[j]; |
197 |
+ |
if(alertFile.lastModified() < deleteFiles) { |
198 |
+ |
// alert file is older than the most recent |
199 |
+ |
// FINAL Heartbeat alert. |
200 |
+ |
if(alertFile.delete()) { |
201 |
+ |
_logger.write(this.toString(), Logger.DEBUG, "Deleted stale alert file: "+alertFile.getPath()); |
202 |
+ |
} |
203 |
+ |
else { |
204 |
+ |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'stale' alert file: "+alertFile.getPath()); |
205 |
+ |
} |
206 |
+ |
} |
207 |
+ |
} |
208 |
+ |
} |
209 |
+ |
// ---- RECAP ---- |
210 |
+ |
// at this point, we have cleaned up any OK or FINAL alerts |
211 |
+ |
// that have passed our age limit. We have then cleaned up |
212 |
+ |
// any alerts older the most recent Heartbeat FINAL alert, |
213 |
+ |
// as these are probably stale. Any files left are valid and |
214 |
+ |
// active alerts. We are now in a position to remove the host |
215 |
+ |
// directory if it's empty. |
216 |
+ |
// --------------- |
217 |
+ |
// do a quick check to see if the directory is now empty |
218 |
+ |
File[] newHostdirContents = hostdir.listFiles(); |
219 |
+ |
if(newHostdirContents.length == 0) { |
220 |
+ |
// it does seem to be, try and delete it |
221 |
+ |
// this will fail anyway if files still remain |
222 |
+ |
if(!hostdir.delete()) { |
223 |
+ |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete the following empty host directory: "+hostdir.getPath()); |
224 |
+ |
} |
225 |
+ |
} |
226 |
|
} |
227 |
|
} |
228 |
|
} |
293 |
|
File outFile; |
294 |
|
// check if we're at a special "end case" (OK or FINAL) |
295 |
|
if(alert.getLevel()==0 || alert.getLevel()==Alert.alertLevels.length-1) { |
296 |
+ |
if(alert.getAttributeName().equals("Heartbeat") && alert.getLevel()==Alert.alertLevels.length-1) { |
297 |
+ |
// new file is something like alert.nnnnnnnn.HB.FINAL |
298 |
+ |
outFile = new File(rootPath, destFile+".HB."+Alert.alertLevels[alert.getLevel()]); |
299 |
+ |
} else { |
300 |
+ |
// new file is something like alert.nnnnnnnn.OK |
301 |
+ |
outFile = new File(rootPath, destFile+"."+Alert.alertLevels[alert.getLevel()]); |
302 |
+ |
} |
303 |
|
File oldFile = new File(rootPath, destFile); |
244 |
– |
outFile = new File(rootPath, destFile+"."+Alert.alertLevels[alert.getLevel()]); |
304 |
|
if(!oldFile.renameTo(outFile)) { |
305 |
|
_logger.write(this.toString(), Logger.WARNING, "Failed to rename old file, "+oldFile.getPath()+" to new file, "+outFile.getPath()); |
306 |
|
} |
315 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
316 |
|
* method to provide clean logging (every class should have this). |
317 |
|
* |
318 |
< |
* This uses the uk.org.iscream.util.FormatName class |
318 |
> |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
319 |
|
* to format the toString() |
320 |
|
* |
321 |
|
* @return the name of this class and its CVS revision |