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 |
|
/** |
11 |
|
* Provides a feed to the webpage system. |
12 |
|
* |
13 |
– |
* !! There may also be need to have a Thread to grab any |
14 |
– |
* !! required config (groups, "nice names, etc) and dump |
15 |
– |
* !! that to a file. |
16 |
– |
* |
13 |
|
* @author $Author$ |
14 |
|
* @version $Id$ |
15 |
|
*/ |
32 |
|
*/ |
33 |
|
public final int DEFAULT_AGE = 3600; |
34 |
|
|
35 |
+ |
/** |
36 |
+ |
* The default path seperator, here for convienience |
37 |
+ |
*/ |
38 |
+ |
private final String sep = File.separator; |
39 |
+ |
|
40 |
|
//---STATIC METHODS--- |
41 |
|
|
42 |
|
/** |
43 |
|
* Return a reference to the single class. |
44 |
|
* Construct it if it does not already exist, otherwise just return the reference. |
45 |
|
*/ |
46 |
< |
public static WebFeeder getInstance() { |
46 |
> |
public synchronized static WebFeeder getInstance() { |
47 |
|
if (_instance == null){ |
48 |
|
_instance = new WebFeeder(); |
49 |
|
} |
72 |
|
alertSubDir = cp.getProperty("WebFeeder", "WebFeeder.alertSubDir"); |
73 |
|
File alertsDir = new File(rootPath, alertSubDir); |
74 |
|
if(deleteContents(alertsDir)) { |
75 |
< |
_logger.write(this.toString(), Logger.DEBUG, "Deleted all files and directories from: "+rootPath+"/"+alertSubDir); |
75 |
> |
_logger.write(this.toString(), Logger.DEBUG, "Deleted all files and directories from: "+rootPath+sep+alertSubDir); |
76 |
|
} else { |
77 |
< |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete all files and directories from: "+rootPath+"/"+alertSubDir); |
77 |
> |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete all files and directories from: "+rootPath+sep+alertSubDir); |
78 |
|
} |
79 |
|
// cleanup complete |
80 |
|
} catch (PropertyNotFoundException e) { |
144 |
|
|
145 |
|
// list the files and delete as appropriate |
146 |
|
File alertsDir = new File(rootPath, alertSubDir); |
147 |
< |
File[] contents = alertsDir.listFiles(); |
148 |
< |
for(int i=0; i < contents.length; i++) { |
149 |
< |
File hostdir = contents[i]; |
150 |
< |
if(hostdir.isDirectory()) { |
151 |
< |
File[] hostdirContents = hostdir.listFiles(); |
152 |
< |
for(int j=0; j < hostdirContents.length; j++) { |
153 |
< |
File alertFile = hostdirContents[j]; |
154 |
< |
String filename = alertFile.getName(); |
155 |
< |
if(filename.endsWith(Alert.alertLevels[0]) || |
156 |
< |
filename.endsWith(Alert.alertLevels[Alert.alertLevels.length-1])) { |
157 |
< |
// it ends with either OK or FINAL |
158 |
< |
// ... so we can check it for deletion |
159 |
< |
long lastModified = alertFile.lastModified(); |
160 |
< |
long age = System.currentTimeMillis() - lastModified; |
161 |
< |
if(age > ((long) deleteOlderThan*1000)) { |
162 |
< |
// it's also older than our age to delete older than |
163 |
< |
if(!alertFile.delete()) { |
164 |
< |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'old' alert file: "+alertFile.getPath()); |
147 |
> |
// check it's a directory |
148 |
> |
if(alertsDir.isDirectory()) { |
149 |
> |
// get all the hostnames directories |
150 |
> |
File[] contents = alertsDir.listFiles(); |
151 |
> |
for(int i=0; i < contents.length; i++) { |
152 |
> |
// get a single directory from the array.. |
153 |
> |
File hostdir = contents[i]; |
154 |
> |
// ..and check it's a directory |
155 |
> |
if(hostdir.isDirectory()) { |
156 |
> |
// if this is set, we clean files older than it |
157 |
> |
long deleteFiles = -1; |
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 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 |
> |
// if we're on a final heartbeat, we probably want to |
173 |
> |
// clean up any stale alerts left behind |
174 |
> |
// by setting this flag, we'll clean them up on leaving this loop |
175 |
> |
if(filename.endsWith(".HB."+Alert.alertLevels[Alert.alertLevels.length-1])) { |
176 |
> |
// we do this so that delete files is set to the |
177 |
> |
// latest date of a HB.FINAL. There should only be |
178 |
> |
// one of them though :) |
179 |
> |
if(lastModified > deleteFiles) { |
180 |
> |
deleteFiles = lastModified; |
181 |
> |
} |
182 |
> |
} |
183 |
> |
// it's also older than our age to delete older than |
184 |
> |
if(!alertFile.delete()) { |
185 |
> |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'old' alert file: "+alertFile.getPath()); |
186 |
> |
} |
187 |
|
} |
188 |
|
} |
189 |
|
} |
190 |
+ |
// cleanup stale alerts |
191 |
+ |
if(deleteFiles >= 0) { |
192 |
+ |
File[] remainingHostdirContents = hostdir.listFiles(); |
193 |
+ |
for(int j=0; j < remainingHostdirContents.length; j++) { |
194 |
+ |
File alertFile = remainingHostdirContents[j]; |
195 |
+ |
if(alertFile.lastModified() < deleteFiles) { |
196 |
+ |
// alert file is older than the most recent |
197 |
+ |
// FINAL Heartbeat alert. |
198 |
+ |
if(alertFile.delete()) { |
199 |
+ |
_logger.write(this.toString(), Logger.DEBUG, "Deleted stale alert file: "+alertFile.getPath()); |
200 |
+ |
} |
201 |
+ |
else { |
202 |
+ |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'stale' alert file: "+alertFile.getPath()); |
203 |
+ |
} |
204 |
+ |
} |
205 |
+ |
} |
206 |
+ |
} |
207 |
+ |
// ---- RECAP ---- |
208 |
+ |
// at this point, we have cleaned up any OK or FINAL alerts |
209 |
+ |
// that have passed our age limit. We have then cleaned up |
210 |
+ |
// any alerts older the most recent Heartbeat FINAL alert, |
211 |
+ |
// as these are probably stale. Any files left are valid and |
212 |
+ |
// active alerts. We are now in a position to remove the host |
213 |
+ |
// directory if it's empty. |
214 |
+ |
// --------------- |
215 |
+ |
// do a quick check to see if the directory is now empty |
216 |
+ |
File[] newHostdirContents = hostdir.listFiles(); |
217 |
+ |
if(newHostdirContents.length == 0) { |
218 |
+ |
// it does seem to be, try and delete it |
219 |
+ |
// this will fail anyway if files still remain |
220 |
+ |
if(!hostdir.delete()) { |
221 |
+ |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete the following empty host directory: "+hostdir.getPath()); |
222 |
+ |
} |
223 |
+ |
} |
224 |
|
} |
225 |
|
} |
226 |
|
} |
227 |
+ |
else { |
228 |
+ |
_logger.write(toString(), Logger.WARNING, "IO error reading alerts directory, maybe it doesn't exist? : " +rootPath+sep+alertSubDir); |
229 |
+ |
} |
230 |
|
} |
231 |
|
} |
232 |
|
|
260 |
|
String data = packet.printAll(); |
261 |
|
String hostname = packet.getParam("packet.attributes.machine_name"); |
262 |
|
// set paths |
263 |
< |
File outDir = new File(rootPath, latestSubDir+"/"+hostname); |
264 |
< |
File outFile = new File(rootPath, latestSubDir+"/"+hostname+"/"+latestFileName); |
263 |
> |
File outDir = new File(rootPath, latestSubDir+sep+hostname); |
264 |
> |
File outFile = new File(rootPath, latestSubDir+sep+hostname+sep+latestFileName); |
265 |
|
// write the data out |
266 |
|
writeData(outDir, outFile, data); |
267 |
|
} |
290 |
|
String data = alert.printAll(); |
291 |
|
String hostname = alert.getSource(); |
292 |
|
// set paths |
293 |
< |
File outDir = new File(rootPath, alertSubDir+"/"+hostname); |
294 |
< |
String destFile = alertSubDir+"/"+hostname+"/"+alertFileName+"."+String.valueOf(alert.getInitialAlertTime()); |
293 |
> |
File outDir = new File(rootPath, alertSubDir+sep+hostname); |
294 |
> |
String destFile = alertSubDir+sep+hostname+sep+alertFileName+"."+String.valueOf(alert.getInitialAlertTime()); |
295 |
|
File outFile; |
296 |
|
// check if we're at a special "end case" (OK or FINAL) |
297 |
|
if(alert.getLevel()==0 || alert.getLevel()==Alert.alertLevels.length-1) { |
298 |
+ |
if(alert.getAttributeName().equals("Heartbeat") && alert.getLevel()==Alert.alertLevels.length-1) { |
299 |
+ |
// new file is something like alert.nnnnnnnn.HB.FINAL |
300 |
+ |
outFile = new File(rootPath, destFile+".HB."+Alert.alertLevels[alert.getLevel()]); |
301 |
+ |
} else { |
302 |
+ |
// new file is something like alert.nnnnnnnn.OK |
303 |
+ |
outFile = new File(rootPath, destFile+"."+Alert.alertLevels[alert.getLevel()]); |
304 |
+ |
} |
305 |
|
File oldFile = new File(rootPath, destFile); |
239 |
– |
outFile = new File(rootPath, destFile+"."+Alert.alertLevels[alert.getLevel()]); |
306 |
|
if(!oldFile.renameTo(outFile)) { |
307 |
|
_logger.write(this.toString(), Logger.WARNING, "Failed to rename old file, "+oldFile.getPath()+" to new file, "+outFile.getPath()); |
308 |
|
} |
309 |
|
} else { |
310 |
< |
outFile = new File(destFile); |
310 |
> |
outFile = new File(rootPath, destFile); |
311 |
|
} |
312 |
|
// write the data out |
313 |
|
writeData(outDir, outFile, data); |
317 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
318 |
|
* method to provide clean logging (every class should have this). |
319 |
|
* |
320 |
< |
* This uses the uk.org.iscream.util.FormatName class |
320 |
> |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
321 |
|
* to format the toString() |
322 |
|
* |
323 |
|
* @return the name of this class and its CVS revision |