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.2 2001/03/08 20:57:35 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.2 $"; |
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.machine_name"); |
72 |
// set paths |
73 |
String destDir = rootPath+"/"+latestSubDir+"/"+hostname; |
74 |
String destFile = destDir+"/"+latestFileName; |
75 |
// try to create directory |
76 |
File outDir = new File(destDir); |
77 |
if(!outDir.exists()) { |
78 |
if(!outDir.mkdirs()) { |
79 |
// didn't exist, and we couldn't make it |
80 |
_logger.write(this.toString(), Logger.ERROR, "Failed to create directory: "+outDir.getPath()); |
81 |
// bail out |
82 |
return; |
83 |
} |
84 |
} |
85 |
// directory has been made, check file exists |
86 |
File outFile = new File(destFile); |
87 |
if(!outFile.exists()) { |
88 |
try { |
89 |
outFile.createNewFile(); |
90 |
} catch (IOException e) { |
91 |
_logger.write(this.toString(), Logger.ERROR, "Failed to create file: "+e); |
92 |
// bail out |
93 |
return; |
94 |
} |
95 |
} |
96 |
// file should now exist |
97 |
if(outFile.canWrite()) { |
98 |
PrintWriter out; |
99 |
try { |
100 |
out = new PrintWriter(new FileWriter(outFile)); |
101 |
out.println(data); |
102 |
out.close(); |
103 |
} catch (IOException e) { |
104 |
_logger.write(this.toString(), Logger.ERROR, "Failed to write file: "+e); |
105 |
} |
106 |
} |
107 |
else { |
108 |
_logger.write(this.toString(), Logger.ERROR, "File not writeable: "+outFile.getPath()); |
109 |
} |
110 |
} |
111 |
|
112 |
public void receiveAlert(Alert alert) { |
113 |
// process and save |
114 |
} |
115 |
|
116 |
/** |
117 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
118 |
* method to provide clean logging (every class should have this). |
119 |
* |
120 |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
121 |
* to format the toString() |
122 |
* |
123 |
* @return the name of this class and its CVS revision |
124 |
*/ |
125 |
public String toString() { |
126 |
return FormatName.getName( |
127 |
_name, |
128 |
getClass().getName(), |
129 |
REVISION); |
130 |
} |
131 |
|
132 |
//---PRIVATE METHODS--- |
133 |
|
134 |
//---ACCESSOR/MUTATOR METHODS--- |
135 |
|
136 |
//---ATTRIBUTES--- |
137 |
|
138 |
/** |
139 |
* This is the friendly identifier of the |
140 |
* component this class is running in. |
141 |
* eg, a Filter may be called "filter1", |
142 |
* If this class does not have an owning |
143 |
* component, a name from the configuration |
144 |
* can be placed here. This name could also |
145 |
* be changed to null for utility classes. |
146 |
*/ |
147 |
private String _name = ClientMain.NAME; |
148 |
|
149 |
/** |
150 |
* This holds a reference to the |
151 |
* system logger that is being used. |
152 |
*/ |
153 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
154 |
|
155 |
//---STATIC ATTRIBUTES--- |
156 |
|
157 |
/** |
158 |
* A reference to the single instance of this class |
159 |
*/ |
160 |
private static WebFeeder _instance; |
161 |
|
162 |
} |