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
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/client/WebFeeder.java (file contents):
Revision 1.7 by tdb, Wed Mar 14 23:25:29 2001 UTC vs.
Revision 1.13 by tdb, Fri Mar 16 02:40:04 2001 UTC

# Line 10 | Line 10 | import java.io.*;
10   /**
11   * Provides a feed to the webpage system.
12   *
13 + * !! There may also be need to have a Thread to grab any
14 + * !! required config (groups, "nice names, etc) and dump
15 + * !! that to a file.
16 + *
17   * @author  $Author$
18   * @version $Id$
19   */
20 < public class WebFeeder {
20 > public class WebFeeder extends Thread {
21  
22   //---FINAL ATTRIBUTES---
23  
# Line 22 | Line 26 | public class WebFeeder {
26       */
27      public static final String REVISION = "$Revision$";
28      
29 +    /**
30 +     * Default check period in seconds (30 minutes)
31 +     */
32 +    public final int DEFAULT_CHECK_PERIOD = 1800;
33 +    
34 +    /**
35 +     * Delete alerts older than this in seconds, default.
36 +     */
37 +    public final int DEFAULT_AGE = 3600;
38 +    
39   //---STATIC METHODS---
40  
41      /**
# Line 36 | Line 50 | public class WebFeeder {
50      }
51  
52   //---CONSTRUCTORS---
53 <
53 >    
54 >    /**
55 >     * Construct a new WebFeeder. This will also wipe out any
56 >     * old Alerts, as these can't be carried from one session
57 >     * until the next.
58 >     */
59      private WebFeeder() {
60          // do something, or nothing.. but must be private
61 +        // don't need to cleanup latest data
62 +        
63 +        // -- cleanup old alerts
64 +        // get config proxy
65 +        ConfigurationProxy cp = ConfigurationProxy.getInstance();
66 +        // get file locations
67 +        String rootPath, alertSubDir, alertFileName;
68 +        try {
69 +            // work out where things are
70 +            rootPath = cp.getProperty("WebFeeder", "WebFeeder.rootPath");
71 +            alertSubDir = cp.getProperty("WebFeeder", "WebFeeder.alertSubDir");
72 +            File alertsDir = new File(rootPath, alertSubDir);
73 +            if(deleteContents(alertsDir)) {
74 +                _logger.write(this.toString(), Logger.DEBUG, "Deleted all files and directories from: "+rootPath+"/"+alertSubDir);
75 +            } else {
76 +                _logger.write(this.toString(), Logger.WARNING, "Failed to delete all files and directories from: "+rootPath+"/"+alertSubDir);
77 +            }
78 +            // cleanup complete
79 +        } catch (PropertyNotFoundException e) {
80 +            _logger.write(this.toString(), Logger.ERROR, "Failed to cleanup on construction, due to failing to get config for Alert Data: "+e);
81 +            // just leave it at that
82 +        }
83 +        
84 +        // set our name and startup
85 +        setName("client.WebFeeder");
86 +        start();
87      }
88  
89   //---PUBLIC METHODS---
90      
91 <    // TO BE COMPLETED
92 <    // The following two methods should process and save
93 <    // XMLPackets and Alerts, in a directory structure that
94 <    // has been defined by the web page designer.
91 >    /**
92 >     * Thread loop, will check at intervals for any files
93 >     * that need to be "cleaned up". This will normally
94 >     * be OK and FINAL alerts that have been around for longer
95 >     * than a specified period of time.
96 >     */
97 >    public void run() {
98 >        boolean running = true;
99 >        // get config proxy
100 >        ConfigurationProxy cp = ConfigurationProxy.getInstance();
101 >        // loop round
102 >        while(running) {
103 >            // get our check period
104 >            int checkPeriod = 0;
105 >            try {
106 >                checkPeriod = Integer.parseInt(cp.getProperty("WebFeeder", "WebFeeder.checkPeriod"));
107 >            } catch (NumberFormatException e) {
108 >                checkPeriod = DEFAULT_CHECK_PERIOD;
109 >                _logger.write(toString(), Logger.WARNING, "Erronous WebFeeder.checkPeriod value in configuration using default of " + checkPeriod + " seconds");
110 >            } catch (PropertyNotFoundException e) {
111 >                checkPeriod = DEFAULT_CHECK_PERIOD;
112 >                _logger.write(toString(), Logger.WARNING, "WebFeeder.checkPeriod value unavailable using default of " + checkPeriod + " seconds");
113 >            }
114 >            // wait for the check period
115 >            try {
116 >                Thread.sleep(checkPeriod * 1000);
117 >            } catch (InterruptedException e) {
118 >            }
119 >            
120 >            // get alerts directory
121 >            String rootPath, alertSubDir, alertFileName;
122 >            try {
123 >                rootPath = cp.getProperty("WebFeeder", "WebFeeder.rootPath");
124 >                alertSubDir = cp.getProperty("WebFeeder", "WebFeeder.alertSubDir");
125 >                alertFileName = cp.getProperty("WebFeeder", "WebFeeder.alertFileName");
126 >            } catch (PropertyNotFoundException e) {
127 >                _logger.write(this.toString(), Logger.ERROR, "Failed to get config for Alert Data: "+e);
128 >                // bail out
129 >                continue;
130 >            }
131 >            
132 >            // get the "age" barrier
133 >            int deleteOlderThan = 0;
134 >            try {
135 >                deleteOlderThan = Integer.parseInt(cp.getProperty("WebFeeder", "WebFeeder.alertDeleteOlderThan"));
136 >            } catch (NumberFormatException e) {
137 >                deleteOlderThan = DEFAULT_AGE;
138 >                _logger.write(toString(), Logger.WARNING, "Erronous WebFeeder.alertDeleteOlderThan value in configuration using default of " + deleteOlderThan + " seconds");
139 >            } catch (PropertyNotFoundException e) {
140 >                deleteOlderThan = DEFAULT_AGE;
141 >                _logger.write(toString(), Logger.WARNING, "WebFeeder.alertDeleteOlderThan value unavailable using default of " + deleteOlderThan + " seconds");
142 >            }
143 >            
144 >            // list the files and delete as appropriate
145 >            File alertsDir = new File(rootPath, alertSubDir);
146 >            File[] contents = alertsDir.listFiles();
147 >            for(int i=0; i < contents.length; i++) {
148 >                File hostdir = contents[i];
149 >                if(hostdir.isDirectory()) {
150 >                    File[] hostdirContents = hostdir.listFiles();
151 >                    for(int j=0; j < hostdirContents.length; j++) {
152 >                        File alertFile = hostdirContents[j];
153 >                        String filename = alertFile.getName();
154 >                        if(filename.endsWith(Alert.alertLevels[0]) ||
155 >                           filename.endsWith(Alert.alertLevels[Alert.alertLevels.length-1])) {
156 >                            // it ends with either OK or FINAL
157 >                            // ... so we can check it for deletion
158 >                            long lastModified = alertFile.lastModified();
159 >                            long age = System.currentTimeMillis() - lastModified;
160 >                            if(age > ((long) deleteOlderThan*1000)) {
161 >                                // it's also older than our age to delete older than
162 >                                if(!alertFile.delete()) {
163 >                                    _logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'old' alert file: "+alertFile.getPath());
164 >                                }
165 >                            }
166 >                        }
167 >                    }
168 >                }
169 >            }
170 >        }
171 >    }
172      
173 <    // There may also be need to have a Thread to grab any
174 <    // required config (groups, "nice names, etc) and dump
175 <    // that to a file.
176 <    
173 >    /**
174 >     * Handles an XMLPacket. This will write it out to disk
175 >     * in an appropriate manner.
176 >     *
177 >     * @param packet the XMLPacket to write
178 >     */
179      public void receiveXMLPacket(XMLPacket packet) {
180          String packetType = packet.getParam("packet.attributes.type");
181          if(packetType == null || !packetType.equals("data")) {
# Line 76 | Line 200 | public class WebFeeder {
200          String data = packet.printAll();
201          String hostname = packet.getParam("packet.attributes.machine_name");
202          // set paths
203 <        String destDir = rootPath+"/"+latestSubDir+"/"+hostname;
204 <        String destFile = destDir+"/"+latestFileName;
205 <        // try to create directory
206 <        File outDir = new File(destDir);
83 <        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 <            }
90 <        }
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 <            }
112 <        }
113 <        else {
114 <            _logger.write(this.toString(), Logger.ERROR, "File not writeable: "+outFile.getPath());
115 <        }
203 >        File outDir = new File(rootPath, latestSubDir+"/"+hostname);
204 >        File outFile = new File(rootPath, latestSubDir+"/"+hostname+"/"+latestFileName);
205 >        // write the data out
206 >        writeData(outDir, outFile, data);
207      }
208      
209 +    /**
210 +     * Handles an Alert. This will write it out to disk
211 +     * in an appropriate manner.
212 +     *
213 +     * @param alert the Alert object to write
214 +     */
215      public void receiveAlert(Alert alert) {
216          // get config proxy
217          ConfigurationProxy cp = ConfigurationProxy.getInstance();
# Line 133 | Line 230 | public class WebFeeder {
230          String data = alert.printAll();
231          String hostname = alert.getSource();
232          // set paths
233 <        String destDir = rootPath+"/"+alertSubDir+"/"+hostname;
234 <        String destFile = destDir+"/"+alertFileName;
233 >        File outDir = new File(rootPath, alertSubDir+"/"+hostname);
234 >        String destFile = alertSubDir+"/"+hostname+"/"+alertFileName+"."+String.valueOf(alert.getInitialAlertTime());
235 >        File outFile;
236 >        // check if we're at a special "end case" (OK or FINAL)
237 >        if(alert.getLevel()==0 || alert.getLevel()==Alert.alertLevels.length-1) {
238 >            File oldFile = new File(rootPath, destFile);
239 >            outFile = new File(rootPath, destFile+"."+Alert.alertLevels[alert.getLevel()]);
240 >            if(!oldFile.renameTo(outFile)) {
241 >                _logger.write(this.toString(), Logger.WARNING, "Failed to rename old file, "+oldFile.getPath()+" to new file, "+outFile.getPath());
242 >            }
243 >        } else {
244 >            outFile = new File(rootPath, destFile);
245 >        }
246 >        // write the data out
247 >        writeData(outDir, outFile, data);
248 >    }
249 >    
250 >    /**
251 >     * Overrides the {@link java.lang.Object#toString() Object.toString()}
252 >     * method to provide clean logging (every class should have this).
253 >     *
254 >     * This uses the uk.org.iscream.util.FormatName class
255 >     * to format the toString()
256 >     *
257 >     * @return the name of this class and its CVS revision
258 >     */
259 >    public String toString() {
260 >        return FormatName.getName(
261 >            _name,
262 >            getClass().getName(),
263 >            REVISION);
264 >    }
265 >
266 > //---PRIVATE METHODS---
267 >    
268 >    /**
269 >     * Attempts to write "data" to "outFile" in "outDir".
270 >     *
271 >     * Performs checks to create the directories, and the file.
272 >     * Does not "return" anything to indicate failure or success.
273 >     *
274 >     * @param outDir the directory to put the file in
275 >     * @param outFile the filename to put the data in
276 >     * @param data the String of data to write
277 >     */
278 >    private void writeData(File outDir, File outFile, String data) {
279          // try to create directory
139        File outDir = new File(destDir);
280          if(!outDir.exists()) {
281              if(!outDir.mkdirs()) {
282                  // didn't exist, and we couldn't make it
# Line 146 | Line 286 | public class WebFeeder {
286              }
287          }
288          // directory has been made, check file exists
149        File outFile = new File(destFile);
289          if(!outFile.exists()) {
290              try {
291                  outFile.createNewFile();
# Line 160 | Line 299 | public class WebFeeder {
299          if(outFile.canWrite()) {
300              PrintWriter out;
301              try {
302 <                out = new PrintWriter(new FileWriter(outFile.getPath(), true));
302 >                out = new PrintWriter(new FileWriter(outFile));
303                  out.println(data);
304                  out.close();
305              } catch (IOException e) {
# Line 173 | Line 312 | public class WebFeeder {
312      }
313      
314      /**
315 <     * Overrides the {@link java.lang.Object#toString() Object.toString()}
316 <     * method to provide clean logging (every class should have this).
315 >     * Iterates through dir (a directory) and deletes
316 >     * all files and subdirectories.
317       *
318 <     * This uses the uk.org.iscream.util.FormatName class
319 <     * to format the toString()
181 <     *
182 <     * @return the name of this class and its CVS revision
318 >     * @param dir the directory to clear
319 >     * @return true if it succeeded
320       */
321 <    public String toString() {
322 <        return FormatName.getName(
323 <            _name,
324 <            getClass().getName(),
325 <            REVISION);
321 >    private boolean deleteContents(File dir) {
322 >        boolean success = true;
323 >        if(dir.isDirectory()) {
324 >            // is a directory
325 >            File[] contents = dir.listFiles();
326 >            for(int i=0; i < contents.length; i++) {
327 >                File sub = contents[i];
328 >                if(sub.isDirectory()) {
329 >                    // lets get recursive
330 >                    success = success & deleteContents(sub);
331 >                }
332 >                // it's a file or empty dir
333 >                success = success & sub.delete();
334 >            }
335 >        }
336 >        else {
337 >            // not a directory?
338 >            success=false;
339 >        }
340 >        return success;
341      }
342 <
191 < //---PRIVATE METHODS---
192 <
342 >        
343   //---ACCESSOR/MUTATOR METHODS---
344  
345   //---ATTRIBUTES---

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines