1 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
tdb |
1.7 |
package uk.org.iscream.client; |
3 |
tdb |
1.1 |
|
4 |
|
|
//---IMPORTS--- |
5 |
tdb |
1.7 |
import uk.org.iscream.componentmanager.*; |
6 |
|
|
import uk.org.iscream.core.*; |
7 |
|
|
import uk.org.iscream.util.*; |
8 |
tdb |
1.2 |
import java.io.*; |
9 |
tdb |
1.1 |
|
10 |
|
|
/** |
11 |
|
|
* Provides a feed to the webpage system. |
12 |
|
|
* |
13 |
tdb |
1.2 |
* @author $Author: tdb1 $ |
14 |
tdb |
1.8 |
* @version $Id: WebFeeder.java,v 1.7 2001/03/14 23:25:29 tdb1 Exp $ |
15 |
tdb |
1.1 |
*/ |
16 |
|
|
public class WebFeeder { |
17 |
|
|
|
18 |
|
|
//---FINAL ATTRIBUTES--- |
19 |
|
|
|
20 |
|
|
/** |
21 |
|
|
* The current CVS revision of this class |
22 |
|
|
*/ |
23 |
tdb |
1.8 |
public static final String REVISION = "$Revision: 1.7 $"; |
24 |
tdb |
1.1 |
|
25 |
|
|
//---STATIC METHODS--- |
26 |
|
|
|
27 |
|
|
/** |
28 |
|
|
* Return a reference to the single class. |
29 |
|
|
* Construct it if it does not already exist, otherwise just return the reference. |
30 |
|
|
*/ |
31 |
|
|
public static WebFeeder getInstance() { |
32 |
|
|
if (_instance == null){ |
33 |
|
|
_instance = new WebFeeder(); |
34 |
|
|
} |
35 |
|
|
return _instance; |
36 |
|
|
} |
37 |
|
|
|
38 |
|
|
//---CONSTRUCTORS--- |
39 |
|
|
|
40 |
|
|
private WebFeeder() { |
41 |
|
|
// do something, or nothing.. but must be private |
42 |
tdb |
1.8 |
// need to clean up all the alerts |
43 |
|
|
// don't need to cleanup latest data |
44 |
tdb |
1.1 |
} |
45 |
|
|
|
46 |
|
|
//---PUBLIC METHODS--- |
47 |
|
|
|
48 |
|
|
// TO BE COMPLETED |
49 |
|
|
// The following two methods should process and save |
50 |
|
|
// XMLPackets and Alerts, in a directory structure that |
51 |
|
|
// has been defined by the web page designer. |
52 |
|
|
|
53 |
|
|
// There may also be need to have a Thread to grab any |
54 |
|
|
// required config (groups, "nice names, etc) and dump |
55 |
|
|
// that to a file. |
56 |
|
|
|
57 |
|
|
public void receiveXMLPacket(XMLPacket packet) { |
58 |
tdb |
1.4 |
String packetType = packet.getParam("packet.attributes.type"); |
59 |
|
|
if(packetType == null || !packetType.equals("data")) { |
60 |
|
|
// bail out, without warning |
61 |
|
|
// this is probably a heartbeat or similar |
62 |
|
|
return; |
63 |
|
|
} |
64 |
tdb |
1.2 |
// get config proxy |
65 |
|
|
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
66 |
|
|
// get file locations |
67 |
|
|
String rootPath, latestSubDir, latestFileName; |
68 |
|
|
try { |
69 |
|
|
rootPath = cp.getProperty("WebFeeder", "WebFeeder.rootPath"); |
70 |
|
|
latestSubDir = cp.getProperty("WebFeeder", "WebFeeder.latestSubDir"); |
71 |
|
|
latestFileName = cp.getProperty("WebFeeder", "WebFeeder.latestFileName"); |
72 |
|
|
} catch (PropertyNotFoundException e) { |
73 |
|
|
_logger.write(this.toString(), Logger.ERROR, "Failed to get config for Latest Data: "+e); |
74 |
|
|
// bail out |
75 |
|
|
return; |
76 |
|
|
} |
77 |
|
|
// get raw data |
78 |
|
|
String data = packet.printAll(); |
79 |
tdb |
1.3 |
String hostname = packet.getParam("packet.attributes.machine_name"); |
80 |
tdb |
1.2 |
// set paths |
81 |
|
|
String destDir = rootPath+"/"+latestSubDir+"/"+hostname; |
82 |
|
|
String destFile = destDir+"/"+latestFileName; |
83 |
tdb |
1.3 |
// try to create directory |
84 |
tdb |
1.2 |
File outDir = new File(destDir); |
85 |
tdb |
1.3 |
if(!outDir.exists()) { |
86 |
|
|
if(!outDir.mkdirs()) { |
87 |
|
|
// didn't exist, and we couldn't make it |
88 |
|
|
_logger.write(this.toString(), Logger.ERROR, "Failed to create directory: "+outDir.getPath()); |
89 |
|
|
// bail out |
90 |
|
|
return; |
91 |
tdb |
1.2 |
} |
92 |
tdb |
1.3 |
} |
93 |
|
|
// directory has been made, check file exists |
94 |
|
|
File outFile = new File(destFile); |
95 |
|
|
if(!outFile.exists()) { |
96 |
|
|
try { |
97 |
|
|
outFile.createNewFile(); |
98 |
|
|
} catch (IOException e) { |
99 |
|
|
_logger.write(this.toString(), Logger.ERROR, "Failed to create file: "+e); |
100 |
|
|
// bail out |
101 |
|
|
return; |
102 |
|
|
} |
103 |
|
|
} |
104 |
|
|
// file should now exist |
105 |
|
|
if(outFile.canWrite()) { |
106 |
|
|
PrintWriter out; |
107 |
|
|
try { |
108 |
|
|
out = new PrintWriter(new FileWriter(outFile)); |
109 |
|
|
out.println(data); |
110 |
|
|
out.close(); |
111 |
|
|
} catch (IOException e) { |
112 |
|
|
_logger.write(this.toString(), Logger.ERROR, "Failed to write file: "+e); |
113 |
tdb |
1.2 |
} |
114 |
|
|
} |
115 |
|
|
else { |
116 |
tdb |
1.3 |
_logger.write(this.toString(), Logger.ERROR, "File not writeable: "+outFile.getPath()); |
117 |
tdb |
1.2 |
} |
118 |
tdb |
1.1 |
} |
119 |
|
|
|
120 |
|
|
public void receiveAlert(Alert alert) { |
121 |
tdb |
1.5 |
// get config proxy |
122 |
|
|
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
123 |
|
|
// get file locations |
124 |
|
|
String rootPath, alertSubDir, alertFileName; |
125 |
|
|
try { |
126 |
|
|
rootPath = cp.getProperty("WebFeeder", "WebFeeder.rootPath"); |
127 |
|
|
alertSubDir = cp.getProperty("WebFeeder", "WebFeeder.alertSubDir"); |
128 |
|
|
alertFileName = cp.getProperty("WebFeeder", "WebFeeder.alertFileName"); |
129 |
|
|
} catch (PropertyNotFoundException e) { |
130 |
|
|
_logger.write(this.toString(), Logger.ERROR, "Failed to get config for Alert Data: "+e); |
131 |
|
|
// bail out |
132 |
|
|
return; |
133 |
|
|
} |
134 |
|
|
// get raw data |
135 |
|
|
String data = alert.printAll(); |
136 |
|
|
String hostname = alert.getSource(); |
137 |
|
|
// set paths |
138 |
|
|
String destDir = rootPath+"/"+alertSubDir+"/"+hostname; |
139 |
tdb |
1.8 |
String destFile = destDir+"/"+alertFileName+"."+String.valueOf(alert.getInitialAlertTime()); |
140 |
tdb |
1.5 |
// try to create directory |
141 |
|
|
File outDir = new File(destDir); |
142 |
|
|
if(!outDir.exists()) { |
143 |
|
|
if(!outDir.mkdirs()) { |
144 |
|
|
// didn't exist, and we couldn't make it |
145 |
|
|
_logger.write(this.toString(), Logger.ERROR, "Failed to create directory: "+outDir.getPath()); |
146 |
|
|
// bail out |
147 |
|
|
return; |
148 |
|
|
} |
149 |
|
|
} |
150 |
|
|
// directory has been made, check file exists |
151 |
|
|
File outFile = new File(destFile); |
152 |
|
|
if(!outFile.exists()) { |
153 |
|
|
try { |
154 |
|
|
outFile.createNewFile(); |
155 |
|
|
} catch (IOException e) { |
156 |
|
|
_logger.write(this.toString(), Logger.ERROR, "Failed to create file: "+e); |
157 |
|
|
// bail out |
158 |
|
|
return; |
159 |
|
|
} |
160 |
|
|
} |
161 |
|
|
// file should now exist |
162 |
|
|
if(outFile.canWrite()) { |
163 |
|
|
PrintWriter out; |
164 |
|
|
try { |
165 |
tdb |
1.8 |
out = new PrintWriter(new FileWriter(outFile)); |
166 |
tdb |
1.5 |
out.println(data); |
167 |
|
|
out.close(); |
168 |
|
|
} catch (IOException e) { |
169 |
|
|
_logger.write(this.toString(), Logger.ERROR, "Failed to write file: "+e); |
170 |
|
|
} |
171 |
|
|
} |
172 |
|
|
else { |
173 |
|
|
_logger.write(this.toString(), Logger.ERROR, "File not writeable: "+outFile.getPath()); |
174 |
|
|
} |
175 |
tdb |
1.1 |
} |
176 |
|
|
|
177 |
|
|
/** |
178 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
179 |
|
|
* method to provide clean logging (every class should have this). |
180 |
|
|
* |
181 |
tdb |
1.7 |
* This uses the uk.org.iscream.util.FormatName class |
182 |
tdb |
1.1 |
* to format the toString() |
183 |
|
|
* |
184 |
|
|
* @return the name of this class and its CVS revision |
185 |
|
|
*/ |
186 |
|
|
public String toString() { |
187 |
|
|
return FormatName.getName( |
188 |
|
|
_name, |
189 |
|
|
getClass().getName(), |
190 |
|
|
REVISION); |
191 |
|
|
} |
192 |
|
|
|
193 |
|
|
//---PRIVATE METHODS--- |
194 |
|
|
|
195 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
196 |
|
|
|
197 |
|
|
//---ATTRIBUTES--- |
198 |
|
|
|
199 |
|
|
/** |
200 |
|
|
* This is the friendly identifier of the |
201 |
|
|
* component this class is running in. |
202 |
|
|
* eg, a Filter may be called "filter1", |
203 |
|
|
* If this class does not have an owning |
204 |
|
|
* component, a name from the configuration |
205 |
|
|
* can be placed here. This name could also |
206 |
|
|
* be changed to null for utility classes. |
207 |
|
|
*/ |
208 |
|
|
private String _name = ClientMain.NAME; |
209 |
|
|
|
210 |
|
|
/** |
211 |
|
|
* This holds a reference to the |
212 |
|
|
* system logger that is being used. |
213 |
|
|
*/ |
214 |
|
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
215 |
|
|
|
216 |
|
|
//---STATIC ATTRIBUTES--- |
217 |
|
|
|
218 |
|
|
/** |
219 |
|
|
* A reference to the single instance of this class |
220 |
|
|
*/ |
221 |
|
|
private static WebFeeder _instance; |
222 |
|
|
|
223 |
|
|
} |