1 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
|
|
package uk.ac.ukc.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.*; |
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.4 |
* @version $Id: WebFeeder.java,v 1.3 2001/03/08 21:22:28 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.4 |
public static final String REVISION = "$Revision: 1.3 $"; |
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 |
|
|
} |
43 |
|
|
|
44 |
|
|
//---PUBLIC METHODS--- |
45 |
|
|
|
46 |
|
|
// TO BE COMPLETED |
47 |
|
|
// The following two methods should process and save |
48 |
|
|
// XMLPackets and Alerts, in a directory structure that |
49 |
|
|
// has been defined by the web page designer. |
50 |
|
|
|
51 |
|
|
// There may also be need to have a Thread to grab any |
52 |
|
|
// required config (groups, "nice names, etc) and dump |
53 |
|
|
// that to a file. |
54 |
|
|
|
55 |
|
|
public void receiveXMLPacket(XMLPacket packet) { |
56 |
tdb |
1.4 |
String packetType = packet.getParam("packet.attributes.type"); |
57 |
|
|
if(packetType == null || !packetType.equals("data")) { |
58 |
|
|
// bail out, without warning |
59 |
|
|
// this is probably a heartbeat or similar |
60 |
|
|
return; |
61 |
|
|
} |
62 |
tdb |
1.2 |
// get config proxy |
63 |
|
|
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
64 |
|
|
// get file locations |
65 |
|
|
String rootPath, latestSubDir, latestFileName; |
66 |
|
|
try { |
67 |
|
|
rootPath = cp.getProperty("WebFeeder", "WebFeeder.rootPath"); |
68 |
|
|
latestSubDir = cp.getProperty("WebFeeder", "WebFeeder.latestSubDir"); |
69 |
|
|
latestFileName = cp.getProperty("WebFeeder", "WebFeeder.latestFileName"); |
70 |
|
|
} catch (PropertyNotFoundException e) { |
71 |
|
|
_logger.write(this.toString(), Logger.ERROR, "Failed to get config for Latest Data: "+e); |
72 |
|
|
// bail out |
73 |
|
|
return; |
74 |
|
|
} |
75 |
|
|
// get raw data |
76 |
|
|
String data = packet.printAll(); |
77 |
tdb |
1.3 |
String hostname = packet.getParam("packet.attributes.machine_name"); |
78 |
tdb |
1.2 |
// set paths |
79 |
|
|
String destDir = rootPath+"/"+latestSubDir+"/"+hostname; |
80 |
|
|
String destFile = destDir+"/"+latestFileName; |
81 |
tdb |
1.3 |
// try to create directory |
82 |
tdb |
1.2 |
File outDir = new File(destDir); |
83 |
tdb |
1.3 |
if(!outDir.exists()) { |
84 |
|
|
if(!outDir.mkdirs()) { |
85 |
|
|
// didn't exist, and we couldn't make it |
86 |
|
|
_logger.write(this.toString(), Logger.ERROR, "Failed to create directory: "+outDir.getPath()); |
87 |
|
|
// bail out |
88 |
|
|
return; |
89 |
tdb |
1.2 |
} |
90 |
tdb |
1.3 |
} |
91 |
|
|
// directory has been made, check file exists |
92 |
|
|
File outFile = new File(destFile); |
93 |
|
|
if(!outFile.exists()) { |
94 |
|
|
try { |
95 |
|
|
outFile.createNewFile(); |
96 |
|
|
} catch (IOException e) { |
97 |
|
|
_logger.write(this.toString(), Logger.ERROR, "Failed to create file: "+e); |
98 |
|
|
// bail out |
99 |
|
|
return; |
100 |
|
|
} |
101 |
|
|
} |
102 |
|
|
// file should now exist |
103 |
|
|
if(outFile.canWrite()) { |
104 |
|
|
PrintWriter out; |
105 |
|
|
try { |
106 |
|
|
out = new PrintWriter(new FileWriter(outFile)); |
107 |
|
|
out.println(data); |
108 |
|
|
out.close(); |
109 |
|
|
} catch (IOException e) { |
110 |
|
|
_logger.write(this.toString(), Logger.ERROR, "Failed to write file: "+e); |
111 |
tdb |
1.2 |
} |
112 |
|
|
} |
113 |
|
|
else { |
114 |
tdb |
1.3 |
_logger.write(this.toString(), Logger.ERROR, "File not writeable: "+outFile.getPath()); |
115 |
tdb |
1.2 |
} |
116 |
tdb |
1.1 |
} |
117 |
|
|
|
118 |
|
|
public void receiveAlert(Alert alert) { |
119 |
|
|
// process and save |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
/** |
123 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
124 |
|
|
* method to provide clean logging (every class should have this). |
125 |
|
|
* |
126 |
|
|
* This uses the uk.ac.ukc.iscream.util.FormatName class |
127 |
|
|
* to format the toString() |
128 |
|
|
* |
129 |
|
|
* @return the name of this class and its CVS revision |
130 |
|
|
*/ |
131 |
|
|
public String toString() { |
132 |
|
|
return FormatName.getName( |
133 |
|
|
_name, |
134 |
|
|
getClass().getName(), |
135 |
|
|
REVISION); |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
//---PRIVATE METHODS--- |
139 |
|
|
|
140 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
141 |
|
|
|
142 |
|
|
//---ATTRIBUTES--- |
143 |
|
|
|
144 |
|
|
/** |
145 |
|
|
* This is the friendly identifier of the |
146 |
|
|
* component this class is running in. |
147 |
|
|
* eg, a Filter may be called "filter1", |
148 |
|
|
* If this class does not have an owning |
149 |
|
|
* component, a name from the configuration |
150 |
|
|
* can be placed here. This name could also |
151 |
|
|
* be changed to null for utility classes. |
152 |
|
|
*/ |
153 |
|
|
private String _name = ClientMain.NAME; |
154 |
|
|
|
155 |
|
|
/** |
156 |
|
|
* This holds a reference to the |
157 |
|
|
* system logger that is being used. |
158 |
|
|
*/ |
159 |
|
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
160 |
|
|
|
161 |
|
|
//---STATIC ATTRIBUTES--- |
162 |
|
|
|
163 |
|
|
/** |
164 |
|
|
* A reference to the single instance of this class |
165 |
|
|
*/ |
166 |
|
|
private static WebFeeder _instance; |
167 |
|
|
|
168 |
|
|
} |