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.16 by tdb, Mon Mar 19 21:30:30 2001 UTC vs.
Revision 1.20 by tdb, Mon Dec 10 22:20:22 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   /**
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 *
13   * @author  $Author$
14   * @version $Id$
15   */
# Line 47 | Line 43 | public class WebFeeder extends Thread {
43       * Return a reference to the single class.
44       * Construct it if it does not already exist, otherwise just return the reference.
45       */
46 <    public static WebFeeder getInstance() {
46 >    public synchronized static WebFeeder getInstance() {
47          if (_instance == null){
48              _instance = new WebFeeder();
49          }
# Line 155 | Line 151 | public class WebFeeder extends Thread {
151                  File hostdir = contents[i];
152                  // ..and check it's a directory
153                  if(hostdir.isDirectory()) {
154 +                    // if this is set, we clean files older than it
155 +                    long deleteFiles = -1;
156                      // get all the contents of that directory
157                      File[] hostdirContents = hostdir.listFiles();
158                      for(int j=0; j < hostdirContents.length; j++) {
# Line 169 | Line 167 | public class WebFeeder extends Thread {
167                              long lastModified = alertFile.lastModified();
168                              long age = System.currentTimeMillis() - lastModified;
169                              if(age > ((long) deleteOlderThan*1000)) {
170 +                                // if we're on a final heartbeat, we probably want to
171 +                                // clean up any stale alerts left behind
172 +                                // by setting this flag, we'll clean them up on leaving this loop
173 +                                if(filename.endsWith(".HB."+Alert.alertLevels[Alert.alertLevels.length-1])) {
174 +                                    // we do this so that delete files is set to the
175 +                                    // latest date of a HB.FINAL. There should only be
176 +                                    // one of them though :)
177 +                                    if(lastModified > deleteFiles) {
178 +                                        deleteFiles = lastModified;
179 +                                    }
180 +                                }
181                                  // it's also older than our age to delete older than
182 +                                if(!alertFile.delete()) {
183 +                                    _logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'old' alert file: "+alertFile.getPath());
184 +                                }
185 +                            }
186 +                        }
187 +                    }
188 +                    // cleanup stale alerts
189 +                    if(deleteFiles >= 0) {
190 +                        File[] remainingHostdirContents = hostdir.listFiles();
191 +                        for(int j=0; j < remainingHostdirContents.length; j++) {
192 +                            File alertFile = remainingHostdirContents[j];
193 +                            if(alertFile.lastModified() < deleteFiles) {
194 +                                // alert file is older than the most recent
195 +                                // FINAL Heartbeat alert.
196                                  if(alertFile.delete()) {
197 <                                    // do a quick check to see if the directory is now empty
175 <                                    File[] newHostdirContents = hostdir.listFiles();
176 <                                    if(newHostdirContents.length == 0) {
177 <                                        // it does seem to be, try and delete it
178 <                                        // this will fail anyway if files still remain
179 <                                        if(!hostdir.delete()) {
180 <                                            _logger.write(this.toString(), Logger.WARNING, "Failed to delete the following empty host directory: "+hostdir.getPath());
181 <                                        }
182 <                                    }
197 >                                    _logger.write(this.toString(), Logger.DEBUG, "Deleted stale alert file: "+alertFile.getPath());
198                                  }
199                                  else {
200 <                                    _logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'old' alert file: "+alertFile.getPath());
200 >                                    _logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'stale' alert file: "+alertFile.getPath());
201                                  }
202                              }
203                          }
204                      }
205 +                    // ---- RECAP ----
206 +                    // at this point, we have cleaned up any OK or FINAL alerts
207 +                    // that have passed our age limit. We have then cleaned up
208 +                    // any alerts older the most recent Heartbeat FINAL alert,
209 +                    // as these are probably stale. Any files left are valid and
210 +                    // active alerts. We are now in a position to remove the host
211 +                    // directory if it's empty.
212 +                    // ---------------
213 +                    // do a quick check to see if the directory is now empty
214 +                    File[] newHostdirContents = hostdir.listFiles();
215 +                    if(newHostdirContents.length == 0) {
216 +                        // it does seem to be, try and delete it
217 +                        // this will fail anyway if files still remain
218 +                        if(!hostdir.delete()) {
219 +                            _logger.write(this.toString(), Logger.WARNING, "Failed to delete the following empty host directory: "+hostdir.getPath());
220 +                        }
221 +                    }
222                  }
223              }
224          }
# Line 257 | Line 289 | public class WebFeeder extends Thread {
289          File outFile;
290          // check if we're at a special "end case" (OK or FINAL)
291          if(alert.getLevel()==0 || alert.getLevel()==Alert.alertLevels.length-1) {
292 +            if(alert.getAttributeName().equals("Heartbeat") && alert.getLevel()==Alert.alertLevels.length-1) {
293 +                // new file is something like alert.nnnnnnnn.HB.FINAL
294 +                outFile = new File(rootPath, destFile+".HB."+Alert.alertLevels[alert.getLevel()]);
295 +            } else {
296 +                // new file is something like alert.nnnnnnnn.OK
297 +                outFile = new File(rootPath, destFile+"."+Alert.alertLevels[alert.getLevel()]);
298 +            }
299              File oldFile = new File(rootPath, destFile);
261            outFile = new File(rootPath, destFile+"."+Alert.alertLevels[alert.getLevel()]);
300              if(!oldFile.renameTo(outFile)) {
301                  _logger.write(this.toString(), Logger.WARNING, "Failed to rename old file, "+oldFile.getPath()+" to new file, "+outFile.getPath());
302              }
# Line 273 | Line 311 | public class WebFeeder extends Thread {
311       * Overrides the {@link java.lang.Object#toString() Object.toString()}
312       * method to provide clean logging (every class should have this).
313       *
314 <     * This uses the uk.org.iscream.util.FormatName class
314 >     * This uses the uk.org.iscream.cms.server.util.FormatName class
315       * to format the toString()
316       *
317       * @return the name of this class and its CVS revision

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines