1 16 17 19 package org.apache.log4j.helpers; 20 21 import java.io.File ; 22 import org.apache.log4j.helpers.LogLog; 23 24 31 public abstract class FileWatchdog extends Thread { 32 33 36 static final public long DEFAULT_DELAY = 60000; 37 40 protected String filename; 41 42 45 protected long delay = DEFAULT_DELAY; 46 47 File file; 48 long lastModif = 0; 49 boolean warnedAlready = false; 50 boolean interrupted = false; 51 52 protected 53 FileWatchdog(String filename) { 54 this.filename = filename; 55 file = new File (filename); 56 setDaemon(true); 57 checkAndConfigure(); 58 } 59 60 63 public 64 void setDelay(long delay) { 65 this.delay = delay; 66 } 67 68 abstract 69 protected 70 void doOnChange(); 71 72 protected 73 void checkAndConfigure() { 74 boolean fileExists; 75 try { 76 fileExists = file.exists(); 77 } catch(SecurityException e) { 78 LogLog.warn("Was not allowed to read check file existance, file:["+ 79 filename+"]."); 80 interrupted = true; return; 82 } 83 84 if(fileExists) { 85 long l = file.lastModified(); if(l > lastModif) { lastModif = l; doOnChange(); 89 warnedAlready = false; 90 } 91 } else { 92 if(!warnedAlready) { 93 LogLog.debug("["+filename+"] does not exist."); 94 warnedAlready = true; 95 } 96 } 97 } 98 99 public 100 void run() { 101 while(!interrupted) { 102 try { 103 Thread.currentThread().sleep(delay); 104 } catch(InterruptedException e) { 105 } 107 checkAndConfigure(); 108 } 109 } 110 } 111 | Popular Tags |