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.11 by tdb, Thu Mar 15 14:30:55 2001 UTC vs.
Revision 1.19 by tdb, Tue May 29 17:02:34 2001 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 < package uk.org.iscream.client;
2 > package uk.org.iscream.cms.server.client;
3  
4   //---IMPORTS---
5 < import uk.org.iscream.componentmanager.*;
6 < import uk.org.iscream.core.*;
7 < import uk.org.iscream.util.*;
5 > import uk.org.iscream.cms.server.componentmanager.*;
6 > import uk.org.iscream.cms.server.core.*;
7 > import uk.org.iscream.cms.server.util.*;
8   import java.io.*;
9  
10   /**
# Line 36 | Line 36 | public class WebFeeder extends Thread {
36       */
37      public final int DEFAULT_AGE = 3600;
38      
39 +    /**
40 +     * The default path seperator, here for convienience
41 +     */
42 +    private final String sep = File.separator;
43 +    
44   //---STATIC METHODS---
45  
46      /**
47       * Return a reference to the single class.
48       * Construct it if it does not already exist, otherwise just return the reference.
49       */
50 <    public static WebFeeder getInstance() {
50 >    public synchronized static WebFeeder getInstance() {
51          if (_instance == null){
52              _instance = new WebFeeder();
53          }
# Line 69 | Line 74 | public class WebFeeder extends Thread {
74              // work out where things are
75              rootPath = cp.getProperty("WebFeeder", "WebFeeder.rootPath");
76              alertSubDir = cp.getProperty("WebFeeder", "WebFeeder.alertSubDir");
77 <            File alertsDir = new File(rootPath+"/"+alertSubDir);
77 >            File alertsDir = new File(rootPath, alertSubDir);
78              if(deleteContents(alertsDir)) {
79 <                _logger.write(this.toString(), Logger.DEBUG, "Deleted all files and directories from: "+rootPath+"/"+alertSubDir);
79 >                _logger.write(this.toString(), Logger.DEBUG, "Deleted all files and directories from: "+rootPath+sep+alertSubDir);
80              } else {
81 <                _logger.write(this.toString(), Logger.WARNING, "Failed to delete all files and directories from: "+rootPath+"/"+alertSubDir);
81 >                _logger.write(this.toString(), Logger.WARNING, "Failed to delete all files and directories from: "+rootPath+sep+alertSubDir);
82              }
83              // cleanup complete
84          } catch (PropertyNotFoundException e) {
# Line 141 | Line 146 | public class WebFeeder extends Thread {
146                  _logger.write(toString(), Logger.WARNING, "WebFeeder.alertDeleteOlderThan value unavailable using default of " + deleteOlderThan + " seconds");
147              }
148              
149 <            // list the files
150 <            String alertsPath = rootPath+"/"+alertSubDir;
151 <            File alertsDir = new File(alertsPath);
149 >            // list the files and delete as appropriate
150 >            File alertsDir = new File(rootPath, alertSubDir);
151 >            // get all the hostnames directories
152              File[] contents = alertsDir.listFiles();
153              for(int i=0; i < contents.length; i++) {
154 +                // get a single directory from the array..
155                  File hostdir = contents[i];
156 <                File[] hostdirContents = hostdir.listFiles();
157 <                for(int j=0; j < hostdirContents.length; j++) {
158 <                    File alertFile = hostdirContents[j];
159 <                    String filename = alertFile.getName();
160 <                    if(filename.endsWith(Alert.alertLevels[0]) ||
161 <                       filename.endsWith(Alert.alertLevels[Alert.alertLevels.length-1])) {
162 <                        // it ends with either OK or FINAL
163 <                        // ... so we can check it for deletion
164 <                        long lastModified = alertFile.lastModified();
165 <                        long age = System.currentTimeMillis() - lastModified;
166 <                        if(age > ((long) deleteOlderThan*1000)) {
167 <                            // it's also older than our age to delete older than
168 <                            if(!alertFile.delete()) {
169 <                                _logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'old' alert file: "+alertFile.getPath());
156 >                // ..and check it's a directory
157 >                if(hostdir.isDirectory()) {
158 >                    // if this is set, we clean files older than it
159 >                    long deleteFiles = -1;
160 >                    // get all the contents of that directory
161 >                    File[] hostdirContents = hostdir.listFiles();
162 >                    for(int j=0; j < hostdirContents.length; j++) {
163 >                        File alertFile = hostdirContents[j];
164 >                        // get the filename..
165 >                        String filename = alertFile.getName();
166 >                        // ..and see if it ends with OK or FINAL
167 >                        if(filename.endsWith(Alert.alertLevels[0]) ||
168 >                           filename.endsWith(Alert.alertLevels[Alert.alertLevels.length-1])) {
169 >                            // it does end with either OK or FINAL
170 >                            // ... so we can check it for deletion
171 >                            long lastModified = alertFile.lastModified();
172 >                            long age = System.currentTimeMillis() - lastModified;
173 >                            if(age > ((long) deleteOlderThan*1000)) {
174 >                                // if we're on a final heartbeat, we probably want to
175 >                                // clean up any stale alerts left behind
176 >                                // by setting this flag, we'll clean them up on leaving this loop
177 >                                if(filename.endsWith(".HB."+Alert.alertLevels[Alert.alertLevels.length-1])) {
178 >                                    // we do this so that delete files is set to the
179 >                                    // latest date of a HB.FINAL. There should only be
180 >                                    // one of them though :)
181 >                                    if(lastModified > deleteFiles) {
182 >                                        deleteFiles = lastModified;
183 >                                    }
184 >                                }
185 >                                // it's also older than our age to delete older than
186 >                                if(!alertFile.delete()) {
187 >                                    _logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'old' alert file: "+alertFile.getPath());
188 >                                }
189                              }
190                          }
191                      }
192 +                    // cleanup stale alerts
193 +                    if(deleteFiles >= 0) {
194 +                        File[] remainingHostdirContents = hostdir.listFiles();
195 +                        for(int j=0; j < remainingHostdirContents.length; j++) {
196 +                            File alertFile = remainingHostdirContents[j];
197 +                            if(alertFile.lastModified() < deleteFiles) {
198 +                                // alert file is older than the most recent
199 +                                // FINAL Heartbeat alert.
200 +                                if(alertFile.delete()) {
201 +                                    _logger.write(this.toString(), Logger.DEBUG, "Deleted stale alert file: "+alertFile.getPath());
202 +                                }
203 +                                else {
204 +                                    _logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'stale' alert file: "+alertFile.getPath());
205 +                                }
206 +                            }
207 +                        }
208 +                    }
209 +                    // ---- RECAP ----
210 +                    // at this point, we have cleaned up any OK or FINAL alerts
211 +                    // that have passed our age limit. We have then cleaned up
212 +                    // any alerts older the most recent Heartbeat FINAL alert,
213 +                    // as these are probably stale. Any files left are valid and
214 +                    // active alerts. We are now in a position to remove the host
215 +                    // directory if it's empty.
216 +                    // ---------------
217 +                    // do a quick check to see if the directory is now empty
218 +                    File[] newHostdirContents = hostdir.listFiles();
219 +                    if(newHostdirContents.length == 0) {
220 +                        // it does seem to be, try and delete it
221 +                        // this will fail anyway if files still remain
222 +                        if(!hostdir.delete()) {
223 +                            _logger.write(this.toString(), Logger.WARNING, "Failed to delete the following empty host directory: "+hostdir.getPath());
224 +                        }
225 +                    }
226                  }
227              }
169            // check through, deleting those passed the age barrier
228          }
229      }
230      
# Line 200 | Line 258 | public class WebFeeder extends Thread {
258          String data = packet.printAll();
259          String hostname = packet.getParam("packet.attributes.machine_name");
260          // set paths
261 <        String destDir = rootPath+"/"+latestSubDir+"/"+hostname;
262 <        File outDir = new File(destDir);
205 <        String destFile = destDir+"/"+latestFileName;
206 <        File outFile = new File(destFile);
261 >        File outDir = new File(rootPath, latestSubDir+sep+hostname);
262 >        File outFile = new File(rootPath, latestSubDir+sep+hostname+sep+latestFileName);
263          // write the data out
264          writeData(outDir, outFile, data);
265      }
# Line 232 | Line 288 | public class WebFeeder extends Thread {
288          String data = alert.printAll();
289          String hostname = alert.getSource();
290          // set paths
291 <        String destDir = rootPath+"/"+alertSubDir+"/"+hostname;
292 <        File outDir = new File(destDir);
237 <        String destFile = destDir+"/"+alertFileName+"."+String.valueOf(alert.getInitialAlertTime());
291 >        File outDir = new File(rootPath, alertSubDir+sep+hostname);
292 >        String destFile = alertSubDir+sep+hostname+sep+alertFileName+"."+String.valueOf(alert.getInitialAlertTime());
293          File outFile;
294          // check if we're at a special "end case" (OK or FINAL)
295          if(alert.getLevel()==0 || alert.getLevel()==Alert.alertLevels.length-1) {
296 <            File oldFile = new File(destFile);
297 <            outFile = new File(destFile+"."+Alert.alertLevels[alert.getLevel()]);
296 >            if(alert.getAttributeName().equals("Heartbeat") && alert.getLevel()==Alert.alertLevels.length-1) {
297 >                // new file is something like alert.nnnnnnnn.HB.FINAL
298 >                outFile = new File(rootPath, destFile+".HB."+Alert.alertLevels[alert.getLevel()]);
299 >            } else {
300 >                // new file is something like alert.nnnnnnnn.OK
301 >                outFile = new File(rootPath, destFile+"."+Alert.alertLevels[alert.getLevel()]);
302 >            }
303 >            File oldFile = new File(rootPath, destFile);
304              if(!oldFile.renameTo(outFile)) {
305                  _logger.write(this.toString(), Logger.WARNING, "Failed to rename old file, "+oldFile.getPath()+" to new file, "+outFile.getPath());
306              }
307          } else {
308 <            outFile = new File(destFile);
308 >            outFile = new File(rootPath, destFile);
309          }
310          // write the data out
311          writeData(outDir, outFile, data);
# Line 254 | Line 315 | public class WebFeeder extends Thread {
315       * Overrides the {@link java.lang.Object#toString() Object.toString()}
316       * method to provide clean logging (every class should have this).
317       *
318 <     * This uses the uk.org.iscream.util.FormatName class
318 >     * This uses the uk.org.iscream.cms.server.util.FormatName class
319       * to format the toString()
320       *
321       * @return the name of this class and its CVS revision

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines