ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/client/WebFeeder.java
Revision: 1.9
Committed: Thu Mar 15 04:03:24 2001 UTC (23 years, 2 months ago) by tdb
Branch: MAIN
Changes since 1.8: +37 -5 lines
Log Message:
Added functionality to the constructor to tidy up any existing alerts from any
previous invocations of the server. Also changed the routine that dumps the data
to rename an old file, rather than delete and recreate a new one.

File Contents

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