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 |
import java.io.*; |
9 |
|
10 |
/** |
11 |
* Provides a feed to the webpage system. |
12 |
* |
13 |
* @author $Author: tdb1 $ |
14 |
* @version $Id: WebFeeder.java,v 1.1 2001/03/07 01:55:51 tdb1 Exp $ |
15 |
*/ |
16 |
public class WebFeeder { |
17 |
|
18 |
//---FINAL ATTRIBUTES--- |
19 |
|
20 |
/** |
21 |
* The current CVS revision of this class |
22 |
*/ |
23 |
public static final String REVISION = "$Revision: 1.1 $"; |
24 |
|
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 |
// get config proxy |
57 |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
58 |
// get file locations |
59 |
String rootPath, latestSubDir, latestFileName; |
60 |
try { |
61 |
rootPath = cp.getProperty("WebFeeder", "WebFeeder.rootPath"); |
62 |
latestSubDir = cp.getProperty("WebFeeder", "WebFeeder.latestSubDir"); |
63 |
latestFileName = cp.getProperty("WebFeeder", "WebFeeder.latestFileName"); |
64 |
} catch (PropertyNotFoundException e) { |
65 |
_logger.write(this.toString(), Logger.ERROR, "Failed to get config for Latest Data: "+e); |
66 |
// bail out |
67 |
return; |
68 |
} |
69 |
// get raw data |
70 |
String data = packet.printAll(); |
71 |
String hostname = packet.getParam("packet.attributes.host_name"); |
72 |
// set paths |
73 |
String destDir = rootPath+"/"+latestSubDir+"/"+hostname; |
74 |
String destFile = destDir+"/"+latestFileName; |
75 |
// write data |
76 |
File outDir = new File(destDir); |
77 |
if(outDir.mkdirs()) { |
78 |
File outFile = new File(destFile); |
79 |
if(outFile.canWrite()) { |
80 |
PrintWriter out; |
81 |
try { |
82 |
out = new PrintWriter(new FileWriter(outFile)); |
83 |
out.println(data); |
84 |
out.close(); |
85 |
} catch (IOException e) { |
86 |
_logger.write(this.toString(), Logger.ERROR, "Failed to write file: "+e); |
87 |
} |
88 |
} |
89 |
else { |
90 |
_logger.write(this.toString(), Logger.ERROR, "File not readable: "+outFile.getPath()); |
91 |
} |
92 |
} |
93 |
else { |
94 |
_logger.write(this.toString(), Logger.ERROR, "Failed to create directory: "+outDir.getPath()); |
95 |
} |
96 |
} |
97 |
|
98 |
public void receiveAlert(Alert alert) { |
99 |
// process and save |
100 |
} |
101 |
|
102 |
/** |
103 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
104 |
* method to provide clean logging (every class should have this). |
105 |
* |
106 |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
107 |
* to format the toString() |
108 |
* |
109 |
* @return the name of this class and its CVS revision |
110 |
*/ |
111 |
public String toString() { |
112 |
return FormatName.getName( |
113 |
_name, |
114 |
getClass().getName(), |
115 |
REVISION); |
116 |
} |
117 |
|
118 |
//---PRIVATE METHODS--- |
119 |
|
120 |
//---ACCESSOR/MUTATOR METHODS--- |
121 |
|
122 |
//---ATTRIBUTES--- |
123 |
|
124 |
/** |
125 |
* This is the friendly identifier of the |
126 |
* component this class is running in. |
127 |
* eg, a Filter may be called "filter1", |
128 |
* If this class does not have an owning |
129 |
* component, a name from the configuration |
130 |
* can be placed here. This name could also |
131 |
* be changed to null for utility classes. |
132 |
*/ |
133 |
private String _name = ClientMain.NAME; |
134 |
|
135 |
/** |
136 |
* This holds a reference to the |
137 |
* system logger that is being used. |
138 |
*/ |
139 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
140 |
|
141 |
//---STATIC ATTRIBUTES--- |
142 |
|
143 |
/** |
144 |
* A reference to the single instance of this class |
145 |
*/ |
146 |
private static WebFeeder _instance; |
147 |
|
148 |
} |