1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.ac.ukc.iscream.client; |
2 |
> |
package uk.org.iscream.client; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
< |
import uk.ac.ukc.iscream.componentmanager.*; |
6 |
< |
import uk.ac.ukc.iscream.core.*; |
7 |
< |
import uk.ac.ukc.iscream.util.*; |
5 |
> |
import uk.org.iscream.componentmanager.*; |
6 |
> |
import uk.org.iscream.core.*; |
7 |
> |
import uk.org.iscream.util.*; |
8 |
|
import java.io.*; |
9 |
|
|
10 |
|
/** |
11 |
|
* Provides a feed to the webpage system. |
12 |
|
* |
13 |
+ |
* !! still needs a routine to "maintain" alerts !! |
14 |
+ |
* |
15 |
|
* @author $Author$ |
16 |
|
* @version $Id$ |
17 |
|
*/ |
41 |
|
|
42 |
|
private WebFeeder() { |
43 |
|
// do something, or nothing.. but must be private |
44 |
+ |
// don't need to cleanup latest data |
45 |
+ |
|
46 |
+ |
// -- cleanup old alerts |
47 |
+ |
// get config proxy |
48 |
+ |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
49 |
+ |
// get file locations |
50 |
+ |
String rootPath, alertSubDir, alertFileName; |
51 |
+ |
try { |
52 |
+ |
// work out where things are |
53 |
+ |
rootPath = cp.getProperty("WebFeeder", "WebFeeder.rootPath"); |
54 |
+ |
alertSubDir = cp.getProperty("WebFeeder", "WebFeeder.alertSubDir"); |
55 |
+ |
File alertsDir = new File(rootPath+"/"+alertSubDir); |
56 |
+ |
if(deleteContents(alertsDir)) { |
57 |
+ |
_logger.write(this.toString(), Logger.DEBUG, "Deleted all files and directories from: "+rootPath+"/"+alertSubDir); |
58 |
+ |
} else { |
59 |
+ |
_logger.write(this.toString(), Logger.WARNING, "Failed to delete all files and directories from: "+rootPath+"/"+alertSubDir); |
60 |
+ |
} |
61 |
+ |
// cleanup complete |
62 |
+ |
} catch (PropertyNotFoundException e) { |
63 |
+ |
_logger.write(this.toString(), Logger.ERROR, "Failed to cleanup on construction, due to failing to get config for Alert Data: "+e); |
64 |
+ |
// just leave it at that |
65 |
+ |
} |
66 |
|
} |
67 |
|
|
68 |
|
//---PUBLIC METHODS--- |
158 |
|
String hostname = alert.getSource(); |
159 |
|
// set paths |
160 |
|
String destDir = rootPath+"/"+alertSubDir+"/"+hostname; |
137 |
– |
String destFile = destDir+"/"+alertFileName; |
138 |
– |
// try to create directory |
161 |
|
File outDir = new File(destDir); |
162 |
+ |
String destFile = destDir+"/"+alertFileName+"."+String.valueOf(alert.getInitialAlertTime()); |
163 |
+ |
File outFile; |
164 |
+ |
// check if we're at a special "end case" (OK or FINAL) |
165 |
+ |
if(alert.getLevel()==0 || alert.getLevel()==Alert.alertLevels.length-1) { |
166 |
+ |
File oldFile = new File(destFile); |
167 |
+ |
outFile = new File(destFile+"."+Alert.alertLevels[alert.getLevel()]); |
168 |
+ |
if(!oldFile.renameTo(outFile)) { |
169 |
+ |
_logger.write(this.toString(), Logger.WARNING, "Failed to rename old file, "+oldFile.getPath()+" to new file, "+outFile.getPath()); |
170 |
+ |
} |
171 |
+ |
} else { |
172 |
+ |
outFile = new File(destFile); |
173 |
+ |
} |
174 |
+ |
// try to create directory |
175 |
|
if(!outDir.exists()) { |
176 |
|
if(!outDir.mkdirs()) { |
177 |
|
// didn't exist, and we couldn't make it |
181 |
|
} |
182 |
|
} |
183 |
|
// directory has been made, check file exists |
149 |
– |
File outFile = new File(destFile); |
184 |
|
if(!outFile.exists()) { |
185 |
|
try { |
186 |
|
outFile.createNewFile(); |
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.ac.ukc.iscream.util.FormatName class |
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 |
223 |
|
} |
224 |
|
|
225 |
|
//---PRIVATE METHODS--- |
226 |
< |
|
226 |
> |
|
227 |
> |
/** |
228 |
> |
* Iterates through dir (a directory) and deletes |
229 |
> |
* all files and subdirectories. |
230 |
> |
* |
231 |
> |
* @param dir the directory to clear |
232 |
> |
* @return true if it succeeded |
233 |
> |
*/ |
234 |
> |
private boolean deleteContents(File dir) { |
235 |
> |
boolean success = true; |
236 |
> |
if(dir.isDirectory()) { |
237 |
> |
// is a directory |
238 |
> |
File[] contents = dir.listFiles(); |
239 |
> |
for(int i=0; i < contents.length; i++) { |
240 |
> |
File sub = contents[i]; |
241 |
> |
if(sub.isDirectory()) { |
242 |
> |
// lets get recursive |
243 |
> |
success = success & deleteContents(sub); |
244 |
> |
} |
245 |
> |
// it's a file or empty dir |
246 |
> |
success = success & sub.delete(); |
247 |
> |
} |
248 |
> |
} |
249 |
> |
else { |
250 |
> |
// not a directory? |
251 |
> |
success=false; |
252 |
> |
} |
253 |
> |
return success; |
254 |
> |
} |
255 |
> |
|
256 |
|
//---ACCESSOR/MUTATOR METHODS--- |
257 |
|
|
258 |
|
//---ATTRIBUTES--- |