FileDocCategorySizeDatePackage
FileWatchdog.javaAPI DocApache log4j 1.2.152954Sat Aug 25 00:09:40 BST 2007org.apache.log4j.helpers

FileWatchdog

public abstract class FileWatchdog extends Thread
Check every now and then that a certain file has not changed. If it has, then call the {@link #doOnChange} method.
author
Ceki Gülcü
since
version 0.9.1

Fields Summary
public static final long
DEFAULT_DELAY
The default delay between every file modification check, set to 60 seconds.
protected String
filename
The name of the file to observe for changes.
protected long
delay
The delay to observe between every check. By default set {@link #DEFAULT_DELAY}.
File
file
long
lastModif
boolean
warnedAlready
boolean
interrupted
Constructors Summary
protected FileWatchdog(String filename)


  
    
    this.filename = filename;
    file = new File(filename);
    setDaemon(true);
    checkAndConfigure();
  
Methods Summary
protected voidcheckAndConfigure()

    boolean fileExists;
    try {
      fileExists = file.exists();
    } catch(SecurityException  e) {
      LogLog.warn("Was not allowed to read check file existance, file:["+
		  filename+"].");
      interrupted = true; // there is no point in continuing
      return;
    }

    if(fileExists) {
      long l = file.lastModified(); // this can also throw a SecurityException
      if(l > lastModif) {           // however, if we reached this point this
	lastModif = l;              // is very unlikely.
	doOnChange();
	warnedAlready = false;
      }
    } else {
      if(!warnedAlready) {
	LogLog.debug("["+filename+"] does not exist.");
	warnedAlready = true;
      }
    }
  
protected abstract voiddoOnChange()

public voidrun()

    
    while(!interrupted) {
      try {
	    Thread.sleep(delay);
      } catch(InterruptedException e) {
	// no interruption expected
      }
      checkAndConfigure();
    }
  
public voidsetDelay(long delay)
Set the delay to observe between each check of the file changes.

    this.delay = delay;