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.21 by tdb, Thu Jan 10 21:49:38 2002 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 148 | Line 144 | public class WebFeeder extends Thread {
144              
145              // list the files and delete as appropriate
146              File alertsDir = new File(rootPath, alertSubDir);
147 <            // get all the hostnames directories
148 <            File[] contents = alertsDir.listFiles();
149 <            for(int i=0; i < contents.length; i++) {
150 <                // get a single directory from the array..
151 <                File hostdir = contents[i];
152 <                // ..and check it's a directory
153 <                if(hostdir.isDirectory()) {
154 <                    // get all the contents of that directory
155 <                    File[] hostdirContents = hostdir.listFiles();
156 <                    for(int j=0; j < hostdirContents.length; j++) {
157 <                        File alertFile = hostdirContents[j];
158 <                        // get the filename..
159 <                        String filename = alertFile.getName();
160 <                        // ..and see if it ends with OK or FINAL
161 <                        if(filename.endsWith(Alert.alertLevels[0]) ||
162 <                           filename.endsWith(Alert.alertLevels[Alert.alertLevels.length-1])) {
163 <                            // it does end with either OK or FINAL
164 <                            // ... so we can check it for deletion
165 <                            long lastModified = alertFile.lastModified();
166 <                            long age = System.currentTimeMillis() - lastModified;
167 <                            if(age > ((long) deleteOlderThan*1000)) {
168 <                                // it's also older than our age to delete older than
169 <                                if(alertFile.delete()) {
170 <                                    // do a quick check to see if the directory is now empty
171 <                                    File[] newHostdirContents = hostdir.listFiles();
172 <                                    if(newHostdirContents.length == 0) {
173 <                                        // it does seem to be, try and delete it
174 <                                        // this will fail anyway if files still remain
175 <                                        if(!hostdir.delete()) {
176 <                                            _logger.write(this.toString(), Logger.WARNING, "Failed to delete the following empty host directory: "+hostdir.getPath());
147 >            // check it's a directory
148 >            if(alertsDir.isDirectory()) {
149 >                // get all the hostnames directories
150 >                File[] contents = alertsDir.listFiles();
151 >                for(int i=0; i < contents.length; i++) {
152 >                    // get a single directory from the array..
153 >                    File hostdir = contents[i];
154 >                    // ..and check it's a directory
155 >                    if(hostdir.isDirectory()) {
156 >                        // if this is set, we clean files older than it
157 >                        long deleteFiles = -1;
158 >                        // get all the contents of that directory
159 >                        File[] hostdirContents = hostdir.listFiles();
160 >                        for(int j=0; j < hostdirContents.length; j++) {
161 >                            File alertFile = hostdirContents[j];
162 >                            // get the filename..
163 >                            String filename = alertFile.getName();
164 >                            // ..and see if it ends with OK or FINAL
165 >                            if(filename.endsWith(Alert.alertLevels[0]) ||
166 >                               filename.endsWith(Alert.alertLevels[Alert.alertLevels.length-1])) {
167 >                                // it does end with either OK or FINAL
168 >                                // ... so we can check it for deletion
169 >                                long lastModified = alertFile.lastModified();
170 >                                long age = System.currentTimeMillis() - lastModified;
171 >                                if(age > ((long) deleteOlderThan*1000)) {
172 >                                    // if we're on a final heartbeat, we probably want to
173 >                                    // clean up any stale alerts left behind
174 >                                    // by setting this flag, we'll clean them up on leaving this loop
175 >                                    if(filename.endsWith(".HB."+Alert.alertLevels[Alert.alertLevels.length-1])) {
176 >                                        // we do this so that delete files is set to the
177 >                                        // latest date of a HB.FINAL. There should only be
178 >                                        // one of them though :)
179 >                                        if(lastModified > deleteFiles) {
180 >                                            deleteFiles = lastModified;
181                                          }
182                                      }
183 +                                    // it's also older than our age to delete older than
184 +                                    if(!alertFile.delete()) {
185 +                                        _logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'old' alert file: "+alertFile.getPath());
186 +                                    }
187                                  }
188 <                                else {
189 <                                    _logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'old' alert file: "+alertFile.getPath());
188 >                            }
189 >                        }
190 >                        // cleanup stale alerts
191 >                        if(deleteFiles >= 0) {
192 >                            File[] remainingHostdirContents = hostdir.listFiles();
193 >                            for(int j=0; j < remainingHostdirContents.length; j++) {
194 >                                File alertFile = remainingHostdirContents[j];
195 >                                if(alertFile.lastModified() < deleteFiles) {
196 >                                    // alert file is older than the most recent
197 >                                    // FINAL Heartbeat alert.
198 >                                    if(alertFile.delete()) {
199 >                                        _logger.write(this.toString(), Logger.DEBUG, "Deleted stale alert file: "+alertFile.getPath());
200 >                                    }
201 >                                    else {
202 >                                        _logger.write(this.toString(), Logger.WARNING, "Failed to delete the following 'stale' alert file: "+alertFile.getPath());
203 >                                    }
204                                  }
205                              }
206                          }
207 +                        // ---- RECAP ----
208 +                        // at this point, we have cleaned up any OK or FINAL alerts
209 +                        // that have passed our age limit. We have then cleaned up
210 +                        // any alerts older the most recent Heartbeat FINAL alert,
211 +                        // as these are probably stale. Any files left are valid and
212 +                        // active alerts. We are now in a position to remove the host
213 +                        // directory if it's empty.
214 +                        // ---------------
215 +                        // do a quick check to see if the directory is now empty
216 +                        File[] newHostdirContents = hostdir.listFiles();
217 +                        if(newHostdirContents.length == 0) {
218 +                            // it does seem to be, try and delete it
219 +                            // this will fail anyway if files still remain
220 +                            if(!hostdir.delete()) {
221 +                                _logger.write(this.toString(), Logger.WARNING, "Failed to delete the following empty host directory: "+hostdir.getPath());
222 +                            }
223 +                        }
224                      }
225                  }
226              }
227 +            else {
228 +                _logger.write(toString(), Logger.WARNING, "IO error reading alerts directory, maybe it doesn't exist? : " +rootPath+sep+alertSubDir);
229 +            }
230          }
231      }
232      
# Line 257 | Line 295 | public class WebFeeder extends Thread {
295          File outFile;
296          // check if we're at a special "end case" (OK or FINAL)
297          if(alert.getLevel()==0 || alert.getLevel()==Alert.alertLevels.length-1) {
298 +            if(alert.getAttributeName().equals("Heartbeat") && alert.getLevel()==Alert.alertLevels.length-1) {
299 +                // new file is something like alert.nnnnnnnn.HB.FINAL
300 +                outFile = new File(rootPath, destFile+".HB."+Alert.alertLevels[alert.getLevel()]);
301 +            } else {
302 +                // new file is something like alert.nnnnnnnn.OK
303 +                outFile = new File(rootPath, destFile+"."+Alert.alertLevels[alert.getLevel()]);
304 +            }
305              File oldFile = new File(rootPath, destFile);
261            outFile = new File(rootPath, destFile+"."+Alert.alertLevels[alert.getLevel()]);
306              if(!oldFile.renameTo(outFile)) {
307                  _logger.write(this.toString(), Logger.WARNING, "Failed to rename old file, "+oldFile.getPath()+" to new file, "+outFile.getPath());
308              }
# Line 273 | Line 317 | public class WebFeeder extends Thread {
317       * Overrides the {@link java.lang.Object#toString() Object.toString()}
318       * method to provide clean logging (every class should have this).
319       *
320 <     * This uses the uk.org.iscream.util.FormatName class
320 >     * This uses the uk.org.iscream.cms.server.util.FormatName class
321       * to format the toString()
322       *
323       * @return the name of this class and its CVS revision

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines