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 |
|
//---STATIC METHODS--- |
40 |
|
|
41 |
|
/** |
50 |
|
} |
51 |
|
|
52 |
|
//---CONSTRUCTORS--- |
53 |
< |
|
53 |
> |
|
54 |
> |
/** |
55 |
> |
* Construct a new WebFeeder. This will also wipe out any |
56 |
> |
* old Alerts, as these can't be carried from one session |
57 |
> |
* until the next. |
58 |
> |
*/ |
59 |
|
private WebFeeder() { |
60 |
|
// do something, or nothing.. but must be private |
61 |
|
// don't need to cleanup latest data |
80 |
|
_logger.write(this.toString(), Logger.ERROR, "Failed to cleanup on construction, due to failing to get config for Alert Data: "+e); |
81 |
|
// just leave it at that |
82 |
|
} |
83 |
+ |
|
84 |
+ |
// set our name and startup |
85 |
+ |
setName("client.WebFeeder"); |
86 |
+ |
start(); |
87 |
|
} |
88 |
|
|
89 |
|
//---PUBLIC METHODS--- |
90 |
|
|
91 |
< |
// TO BE COMPLETED |
92 |
< |
// The following two methods should process and save |
93 |
< |
// XMLPackets and Alerts, in a directory structure that |
94 |
< |
// has been defined by the web page designer. |
91 |
> |
/** |
92 |
> |
* Thread loop, will check at intervals for any files |
93 |
> |
* that need to be "cleaned up". This will normally |
94 |
> |
* be OK and FINAL alerts that have been around for longer |
95 |
> |
* than a specified period of time. |
96 |
> |
*/ |
97 |
> |
public void run() { |
98 |
> |
boolean running = true; |
99 |
> |
// get config proxy |
100 |
> |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
101 |
> |
// loop round |
102 |
> |
while(running) { |
103 |
> |
// get our check period |
104 |
> |
int checkPeriod = 0; |
105 |
> |
try { |
106 |
> |
checkPeriod = Integer.parseInt(cp.getProperty("WebFeeder", "WebFeeder.checkPeriod")); |
107 |
> |
} catch (NumberFormatException e) { |
108 |
> |
checkPeriod = DEFAULT_CHECK_PERIOD; |
109 |
> |
_logger.write(toString(), Logger.WARNING, "Erronous WebFeeder.checkPeriod value in configuration using default of " + checkPeriod + " seconds"); |
110 |
> |
} catch (PropertyNotFoundException e) { |
111 |
> |
checkPeriod = DEFAULT_CHECK_PERIOD; |
112 |
> |
_logger.write(toString(), Logger.WARNING, "WebFeeder.checkPeriod value unavailable using default of " + checkPeriod + " seconds"); |
113 |
> |
} |
114 |
> |
// wait for the check period |
115 |
> |
try { |
116 |
> |
Thread.sleep(checkPeriod * 1000); |
117 |
> |
} catch (InterruptedException e) { |
118 |
> |
} |
119 |
> |
|
120 |
> |
// get alerts directory |
121 |
> |
String rootPath, alertSubDir, alertFileName; |
122 |
> |
try { |
123 |
> |
rootPath = cp.getProperty("WebFeeder", "WebFeeder.rootPath"); |
124 |
> |
alertSubDir = cp.getProperty("WebFeeder", "WebFeeder.alertSubDir"); |
125 |
> |
alertFileName = cp.getProperty("WebFeeder", "WebFeeder.alertFileName"); |
126 |
> |
} catch (PropertyNotFoundException e) { |
127 |
> |
_logger.write(this.toString(), Logger.ERROR, "Failed to get config for Alert Data: "+e); |
128 |
> |
// bail out |
129 |
> |
continue; |
130 |
> |
} |
131 |
> |
|
132 |
> |
// get the "age" barrier |
133 |
> |
int deleteOlderThan = 0; |
134 |
> |
try { |
135 |
> |
deleteOlderThan = Integer.parseInt(cp.getProperty("WebFeeder", "WebFeeder.alertDeleteOlderThan")); |
136 |
> |
} catch (NumberFormatException e) { |
137 |
> |
deleteOlderThan = DEFAULT_AGE; |
138 |
> |
_logger.write(toString(), Logger.WARNING, "Erronous WebFeeder.alertDeleteOlderThan value in configuration using default of " + deleteOlderThan + " seconds"); |
139 |
> |
} catch (PropertyNotFoundException e) { |
140 |
> |
deleteOlderThan = DEFAULT_AGE; |
141 |
> |
_logger.write(toString(), Logger.WARNING, "WebFeeder.alertDeleteOlderThan value unavailable using default of " + deleteOlderThan + " seconds"); |
142 |
> |
} |
143 |
> |
|
144 |
> |
// list the files |
145 |
> |
String alertsPath = rootPath+"/"+alertSubDir; |
146 |
> |
File alertsDir = new File(alertsPath); |
147 |
> |
File[] contents = alertsDir.listFiles(); |
148 |
> |
for(int i=0; i < contents.length; i++) { |
149 |
> |
File hostdir = contents[i]; |
150 |
> |
File[] hostdirContents = hostdir.listFiles(); |
151 |
> |
for(int j=0; j < hostdirContents.length; j++) { |
152 |
> |
File alertFile = hostdirContents[j]; |
153 |
> |
String filename = alertFile.getName(); |
154 |
> |
if(filename.endsWith(Alert.alertLevels[0]) || |
155 |
> |
filename.endsWith(Alert.alertLevels[Alert.alertLevels.length-1])) { |
156 |
> |
// it ends with either OK or FINAL |
157 |
> |
// ... so we can check it for deletion |
158 |
> |
long lastModified = alertFile.lastModified(); |
159 |
> |
long age = System.currentTimeMillis() - lastModified; |
160 |
> |
if(age > ((long) deleteOlderThan*1000)) { |
161 |
> |
// it's also older than our age to delete older than |
162 |
> |
if(!alertFile.delete()) { |
163 |
> |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'old' alert file: "+alertFile.getPath()); |
164 |
> |
} |
165 |
> |
} |
166 |
> |
} |
167 |
> |
} |
168 |
> |
} |
169 |
> |
// check through, deleting those passed the age barrier |
170 |
> |
} |
171 |
> |
} |
172 |
|
|
173 |
< |
// There may also be need to have a Thread to grab any |
174 |
< |
// required config (groups, "nice names, etc) and dump |
175 |
< |
// that to a file. |
176 |
< |
|
173 |
> |
/** |
174 |
> |
* Handles an XMLPacket. This will write it out to disk |
175 |
> |
* in an appropriate manner. |
176 |
> |
* |
177 |
> |
* @param packet the XMLPacket to write |
178 |
> |
*/ |
179 |
|
public void receiveXMLPacket(XMLPacket packet) { |
180 |
|
String packetType = packet.getParam("packet.attributes.type"); |
181 |
|
if(packetType == null || !packetType.equals("data")) { |
201 |
|
String hostname = packet.getParam("packet.attributes.machine_name"); |
202 |
|
// set paths |
203 |
|
String destDir = rootPath+"/"+latestSubDir+"/"+hostname; |
104 |
– |
String destFile = destDir+"/"+latestFileName; |
105 |
– |
// try to create directory |
204 |
|
File outDir = new File(destDir); |
205 |
< |
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 |
205 |
> |
String destFile = destDir+"/"+latestFileName; |
206 |
|
File outFile = new File(destFile); |
207 |
< |
if(!outFile.exists()) { |
208 |
< |
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 |
< |
} |
207 |
> |
// write the data out |
208 |
> |
writeData(outDir, outFile, data); |
209 |
|
} |
210 |
|
|
211 |
+ |
/** |
212 |
+ |
* Handles an Alert. This will write it out to disk |
213 |
+ |
* in an appropriate manner. |
214 |
+ |
* |
215 |
+ |
* @param alert the Alert object to write |
216 |
+ |
*/ |
217 |
|
public void receiveAlert(Alert alert) { |
218 |
|
// get config proxy |
219 |
|
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
246 |
|
} else { |
247 |
|
outFile = new File(destFile); |
248 |
|
} |
249 |
+ |
// write the data out |
250 |
+ |
writeData(outDir, outFile, data); |
251 |
+ |
} |
252 |
+ |
|
253 |
+ |
/** |
254 |
+ |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
255 |
+ |
* method to provide clean logging (every class should have this). |
256 |
+ |
* |
257 |
+ |
* This uses the uk.org.iscream.util.FormatName class |
258 |
+ |
* to format the toString() |
259 |
+ |
* |
260 |
+ |
* @return the name of this class and its CVS revision |
261 |
+ |
*/ |
262 |
+ |
public String toString() { |
263 |
+ |
return FormatName.getName( |
264 |
+ |
_name, |
265 |
+ |
getClass().getName(), |
266 |
+ |
REVISION); |
267 |
+ |
} |
268 |
+ |
|
269 |
+ |
//---PRIVATE METHODS--- |
270 |
+ |
|
271 |
+ |
/** |
272 |
+ |
* Attempts to write "data" to "outFile" in "outDir". |
273 |
+ |
* |
274 |
+ |
* Performs checks to create the directories, and the file. |
275 |
+ |
* Does not "return" anything to indicate failure or success. |
276 |
+ |
* |
277 |
+ |
* @param outDir the directory to put the file in |
278 |
+ |
* @param outFile the filename to put the data in |
279 |
+ |
* @param data the String of data to write |
280 |
+ |
*/ |
281 |
+ |
private void writeData(File outDir, File outFile, String data) { |
282 |
|
// try to create directory |
283 |
|
if(!outDir.exists()) { |
284 |
|
if(!outDir.mkdirs()) { |
313 |
|
_logger.write(this.toString(), Logger.ERROR, "File not writeable: "+outFile.getPath()); |
314 |
|
} |
315 |
|
} |
208 |
– |
|
209 |
– |
/** |
210 |
– |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
211 |
– |
* method to provide clean logging (every class should have this). |
212 |
– |
* |
213 |
– |
* This uses the uk.org.iscream.util.FormatName class |
214 |
– |
* to format the toString() |
215 |
– |
* |
216 |
– |
* @return the name of this class and its CVS revision |
217 |
– |
*/ |
218 |
– |
public String toString() { |
219 |
– |
return FormatName.getName( |
220 |
– |
_name, |
221 |
– |
getClass().getName(), |
222 |
– |
REVISION); |
223 |
– |
} |
224 |
– |
|
225 |
– |
//---PRIVATE METHODS--- |
316 |
|
|
317 |
|
/** |
318 |
|
* Iterates through dir (a directory) and deletes |