10 |
|
/** |
11 |
|
* Provides a feed to the webpage system. |
12 |
|
* |
13 |
< |
* !! still needs a routine to "maintain" alerts !! |
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 |
|
* |
17 |
|
* @author $Author$ |
18 |
|
* @version $Id$ |
19 |
|
*/ |
20 |
< |
public class WebFeeder { |
20 |
> |
public class WebFeeder extends Thread { |
21 |
|
|
22 |
|
//---FINAL ATTRIBUTES--- |
23 |
|
|
26 |
|
*/ |
27 |
|
public static final String REVISION = "$Revision$"; |
28 |
|
|
29 |
+ |
/** |
30 |
+ |
* Default check period in seconds (30 minutes) |
31 |
+ |
*/ |
32 |
+ |
public final int DEFAULT_CHECK_PERIOD = 1800; |
33 |
+ |
|
34 |
+ |
/** |
35 |
+ |
* Delete alerts older than this in seconds, default. |
36 |
+ |
*/ |
37 |
+ |
public final int DEFAULT_AGE = 3600; |
38 |
+ |
|
39 |
+ |
/** |
40 |
+ |
* The default path seperator, here for convienience |
41 |
+ |
*/ |
42 |
+ |
private final String sep = File.separator; |
43 |
+ |
|
44 |
|
//---STATIC METHODS--- |
45 |
|
|
46 |
|
/** |
55 |
|
} |
56 |
|
|
57 |
|
//---CONSTRUCTORS--- |
58 |
< |
|
58 |
> |
|
59 |
> |
/** |
60 |
> |
* Construct a new WebFeeder. This will also wipe out any |
61 |
> |
* old Alerts, as these can't be carried from one session |
62 |
> |
* until the next. |
63 |
> |
*/ |
64 |
|
private WebFeeder() { |
65 |
|
// do something, or nothing.. but must be private |
66 |
|
// don't need to cleanup latest data |
74 |
|
// work out where things are |
75 |
|
rootPath = cp.getProperty("WebFeeder", "WebFeeder.rootPath"); |
76 |
|
alertSubDir = cp.getProperty("WebFeeder", "WebFeeder.alertSubDir"); |
77 |
< |
File alerts = new File(rootPath+"/"+alertSubDir); |
78 |
< |
File[] contents = alerts.listFiles(); |
79 |
< |
for(int i=0; i < contents.length; i++) { |
80 |
< |
contents[i].delete(); |
77 |
> |
File alertsDir = new File(rootPath, alertSubDir); |
78 |
> |
if(deleteContents(alertsDir)) { |
79 |
> |
_logger.write(this.toString(), Logger.DEBUG, "Deleted all files and directories from: "+rootPath+sep+alertSubDir); |
80 |
> |
} else { |
81 |
> |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete all files and directories from: "+rootPath+sep+alertSubDir); |
82 |
|
} |
83 |
|
// cleanup complete |
61 |
– |
_logger.write(this.toString(), Logger.DEBUG, "Deleted "+contents.length+" files or directories from: "+rootPath+"/"+alertSubDir); |
84 |
|
} catch (PropertyNotFoundException e) { |
85 |
|
_logger.write(this.toString(), Logger.ERROR, "Failed to cleanup on construction, due to failing to get config for Alert Data: "+e); |
86 |
|
// just leave it at that |
87 |
|
} |
88 |
+ |
|
89 |
+ |
// set our name and startup |
90 |
+ |
setName("client.WebFeeder"); |
91 |
+ |
start(); |
92 |
|
} |
93 |
|
|
94 |
|
//---PUBLIC METHODS--- |
95 |
|
|
96 |
< |
// TO BE COMPLETED |
97 |
< |
// The following two methods should process and save |
98 |
< |
// XMLPackets and Alerts, in a directory structure that |
99 |
< |
// has been defined by the web page designer. |
96 |
> |
/** |
97 |
> |
* Thread loop, will check at intervals for any files |
98 |
> |
* that need to be "cleaned up". This will normally |
99 |
> |
* be OK and FINAL alerts that have been around for longer |
100 |
> |
* than a specified period of time. |
101 |
> |
*/ |
102 |
> |
public void run() { |
103 |
> |
boolean running = true; |
104 |
> |
// get config proxy |
105 |
> |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
106 |
> |
// loop round |
107 |
> |
while(running) { |
108 |
> |
// get our check period |
109 |
> |
int checkPeriod = 0; |
110 |
> |
try { |
111 |
> |
checkPeriod = Integer.parseInt(cp.getProperty("WebFeeder", "WebFeeder.checkPeriod")); |
112 |
> |
} catch (NumberFormatException e) { |
113 |
> |
checkPeriod = DEFAULT_CHECK_PERIOD; |
114 |
> |
_logger.write(toString(), Logger.WARNING, "Erronous WebFeeder.checkPeriod value in configuration using default of " + checkPeriod + " seconds"); |
115 |
> |
} catch (PropertyNotFoundException e) { |
116 |
> |
checkPeriod = DEFAULT_CHECK_PERIOD; |
117 |
> |
_logger.write(toString(), Logger.WARNING, "WebFeeder.checkPeriod value unavailable using default of " + checkPeriod + " seconds"); |
118 |
> |
} |
119 |
> |
// wait for the check period |
120 |
> |
try { |
121 |
> |
Thread.sleep(checkPeriod * 1000); |
122 |
> |
} catch (InterruptedException e) { |
123 |
> |
} |
124 |
> |
|
125 |
> |
// get alerts directory |
126 |
> |
String rootPath, alertSubDir, alertFileName; |
127 |
> |
try { |
128 |
> |
rootPath = cp.getProperty("WebFeeder", "WebFeeder.rootPath"); |
129 |
> |
alertSubDir = cp.getProperty("WebFeeder", "WebFeeder.alertSubDir"); |
130 |
> |
alertFileName = cp.getProperty("WebFeeder", "WebFeeder.alertFileName"); |
131 |
> |
} catch (PropertyNotFoundException e) { |
132 |
> |
_logger.write(this.toString(), Logger.ERROR, "Failed to get config for Alert Data: "+e); |
133 |
> |
// bail out |
134 |
> |
continue; |
135 |
> |
} |
136 |
> |
|
137 |
> |
// get the "age" barrier |
138 |
> |
int deleteOlderThan = 0; |
139 |
> |
try { |
140 |
> |
deleteOlderThan = Integer.parseInt(cp.getProperty("WebFeeder", "WebFeeder.alertDeleteOlderThan")); |
141 |
> |
} catch (NumberFormatException e) { |
142 |
> |
deleteOlderThan = DEFAULT_AGE; |
143 |
> |
_logger.write(toString(), Logger.WARNING, "Erronous WebFeeder.alertDeleteOlderThan value in configuration using default of " + deleteOlderThan + " seconds"); |
144 |
> |
} catch (PropertyNotFoundException e) { |
145 |
> |
deleteOlderThan = DEFAULT_AGE; |
146 |
> |
_logger.write(toString(), Logger.WARNING, "WebFeeder.alertDeleteOlderThan value unavailable using default of " + deleteOlderThan + " seconds"); |
147 |
> |
} |
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 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()) { |
174 |
> |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'old' alert file: "+alertFile.getPath()); |
175 |
> |
} |
176 |
> |
} |
177 |
> |
} |
178 |
> |
} |
179 |
> |
} |
180 |
> |
} |
181 |
> |
} |
182 |
> |
} |
183 |
|
|
184 |
< |
// There may also be need to have a Thread to grab any |
185 |
< |
// required config (groups, "nice names, etc) and dump |
186 |
< |
// that to a file. |
187 |
< |
|
184 |
> |
/** |
185 |
> |
* Handles an XMLPacket. This will write it out to disk |
186 |
> |
* in an appropriate manner. |
187 |
> |
* |
188 |
> |
* @param packet the XMLPacket to write |
189 |
> |
*/ |
190 |
|
public void receiveXMLPacket(XMLPacket packet) { |
191 |
|
String packetType = packet.getParam("packet.attributes.type"); |
192 |
|
if(packetType == null || !packetType.equals("data")) { |
211 |
|
String data = packet.printAll(); |
212 |
|
String hostname = packet.getParam("packet.attributes.machine_name"); |
213 |
|
// set paths |
214 |
< |
String destDir = rootPath+"/"+latestSubDir+"/"+hostname; |
215 |
< |
String destFile = destDir+"/"+latestFileName; |
216 |
< |
// try to create directory |
217 |
< |
File outDir = new File(destDir); |
107 |
< |
if(!outDir.exists()) { |
108 |
< |
if(!outDir.mkdirs()) { |
109 |
< |
// didn't exist, and we couldn't make it |
110 |
< |
_logger.write(this.toString(), Logger.ERROR, "Failed to create directory: "+outDir.getPath()); |
111 |
< |
// bail out |
112 |
< |
return; |
113 |
< |
} |
114 |
< |
} |
115 |
< |
// directory has been made, check file exists |
116 |
< |
File outFile = new File(destFile); |
117 |
< |
if(!outFile.exists()) { |
118 |
< |
try { |
119 |
< |
outFile.createNewFile(); |
120 |
< |
} catch (IOException e) { |
121 |
< |
_logger.write(this.toString(), Logger.ERROR, "Failed to create file: "+e); |
122 |
< |
// bail out |
123 |
< |
return; |
124 |
< |
} |
125 |
< |
} |
126 |
< |
// file should now exist |
127 |
< |
if(outFile.canWrite()) { |
128 |
< |
PrintWriter out; |
129 |
< |
try { |
130 |
< |
out = new PrintWriter(new FileWriter(outFile)); |
131 |
< |
out.println(data); |
132 |
< |
out.close(); |
133 |
< |
} catch (IOException e) { |
134 |
< |
_logger.write(this.toString(), Logger.ERROR, "Failed to write file: "+e); |
135 |
< |
} |
136 |
< |
} |
137 |
< |
else { |
138 |
< |
_logger.write(this.toString(), Logger.ERROR, "File not writeable: "+outFile.getPath()); |
139 |
< |
} |
214 |
> |
File outDir = new File(rootPath, latestSubDir+sep+hostname); |
215 |
> |
File outFile = new File(rootPath, latestSubDir+sep+hostname+sep+latestFileName); |
216 |
> |
// write the data out |
217 |
> |
writeData(outDir, outFile, data); |
218 |
|
} |
219 |
|
|
220 |
+ |
/** |
221 |
+ |
* Handles an Alert. This will write it out to disk |
222 |
+ |
* in an appropriate manner. |
223 |
+ |
* |
224 |
+ |
* @param alert the Alert object to write |
225 |
+ |
*/ |
226 |
|
public void receiveAlert(Alert alert) { |
227 |
|
// get config proxy |
228 |
|
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
241 |
|
String data = alert.printAll(); |
242 |
|
String hostname = alert.getSource(); |
243 |
|
// set paths |
244 |
< |
String destDir = rootPath+"/"+alertSubDir+"/"+hostname; |
245 |
< |
File outDir = new File(destDir); |
162 |
< |
String destFile = destDir+"/"+alertFileName+"."+String.valueOf(alert.getInitialAlertTime()); |
244 |
> |
File outDir = new File(rootPath, alertSubDir+sep+hostname); |
245 |
> |
String destFile = alertSubDir+sep+hostname+sep+alertFileName+"."+String.valueOf(alert.getInitialAlertTime()); |
246 |
|
File outFile; |
247 |
|
// check if we're at a special "end case" (OK or FINAL) |
248 |
|
if(alert.getLevel()==0 || alert.getLevel()==Alert.alertLevels.length-1) { |
249 |
< |
File oldFile = new File(destFile); |
250 |
< |
outFile = new File(destFile+Alert.alertLevels[alert.getLevel()]); |
249 |
> |
File oldFile = new File(rootPath, destFile); |
250 |
> |
outFile = new File(rootPath, destFile+"."+Alert.alertLevels[alert.getLevel()]); |
251 |
|
if(!oldFile.renameTo(outFile)) { |
252 |
|
_logger.write(this.toString(), Logger.WARNING, "Failed to rename old file, "+oldFile.getPath()+" to new file, "+outFile.getPath()); |
253 |
|
} |
254 |
|
} else { |
255 |
< |
outFile = new File(destFile); |
255 |
> |
outFile = new File(rootPath, destFile); |
256 |
|
} |
257 |
+ |
// write the data out |
258 |
+ |
writeData(outDir, outFile, data); |
259 |
+ |
} |
260 |
+ |
|
261 |
+ |
/** |
262 |
+ |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
263 |
+ |
* method to provide clean logging (every class should have this). |
264 |
+ |
* |
265 |
+ |
* This uses the uk.org.iscream.util.FormatName class |
266 |
+ |
* to format the toString() |
267 |
+ |
* |
268 |
+ |
* @return the name of this class and its CVS revision |
269 |
+ |
*/ |
270 |
+ |
public String toString() { |
271 |
+ |
return FormatName.getName( |
272 |
+ |
_name, |
273 |
+ |
getClass().getName(), |
274 |
+ |
REVISION); |
275 |
+ |
} |
276 |
+ |
|
277 |
+ |
//---PRIVATE METHODS--- |
278 |
+ |
|
279 |
+ |
/** |
280 |
+ |
* Attempts to write "data" to "outFile" in "outDir". |
281 |
+ |
* |
282 |
+ |
* Performs checks to create the directories, and the file. |
283 |
+ |
* Does not "return" anything to indicate failure or success. |
284 |
+ |
* |
285 |
+ |
* @param outDir the directory to put the file in |
286 |
+ |
* @param outFile the filename to put the data in |
287 |
+ |
* @param data the String of data to write |
288 |
+ |
*/ |
289 |
+ |
private void writeData(File outDir, File outFile, String data) { |
290 |
|
// try to create directory |
291 |
|
if(!outDir.exists()) { |
292 |
|
if(!outDir.mkdirs()) { |
323 |
|
} |
324 |
|
|
325 |
|
/** |
326 |
< |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
327 |
< |
* method to provide clean logging (every class should have this). |
326 |
> |
* Iterates through dir (a directory) and deletes |
327 |
> |
* all files and subdirectories. |
328 |
|
* |
329 |
< |
* This uses the uk.org.iscream.util.FormatName class |
330 |
< |
* to format the toString() |
215 |
< |
* |
216 |
< |
* @return the name of this class and its CVS revision |
329 |
> |
* @param dir the directory to clear |
330 |
> |
* @return true if it succeeded |
331 |
|
*/ |
332 |
< |
public String toString() { |
333 |
< |
return FormatName.getName( |
334 |
< |
_name, |
335 |
< |
getClass().getName(), |
336 |
< |
REVISION); |
332 |
> |
private boolean deleteContents(File dir) { |
333 |
> |
boolean success = true; |
334 |
> |
if(dir.isDirectory()) { |
335 |
> |
// is a directory |
336 |
> |
File[] contents = dir.listFiles(); |
337 |
> |
for(int i=0; i < contents.length; i++) { |
338 |
> |
File sub = contents[i]; |
339 |
> |
if(sub.isDirectory()) { |
340 |
> |
// lets get recursive |
341 |
> |
success = success & deleteContents(sub); |
342 |
> |
} |
343 |
> |
// it's a file or empty dir |
344 |
> |
success = success & sub.delete(); |
345 |
> |
} |
346 |
> |
} |
347 |
> |
else { |
348 |
> |
// not a directory? |
349 |
> |
success=false; |
350 |
> |
} |
351 |
> |
return success; |
352 |
|
} |
353 |
< |
|
225 |
< |
//---PRIVATE METHODS--- |
226 |
< |
|
353 |
> |
|
354 |
|
//---ACCESSOR/MUTATOR METHODS--- |
355 |
|
|
356 |
|
//---ATTRIBUTES--- |