Save This Page
Home » slf4j-1.5.5 » org.apache » log4j » helpers » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    * 
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    * 
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   
   18   // Contributors:  Mathias Bogaert
   19   
   20   package org.apache.log4j.helpers;
   21   
   22   import java.io.File;
   23   import org.apache.log4j.helpers.LogLog;
   24   
   25   /**
   26      Check every now and then that a certain file has not changed. If it
   27      has, then call the {@link #doOnChange} method.
   28   
   29   
   30      @author Ceki Gülcü
   31      @since version 0.9.1 */
   32   public abstract class FileWatchdog extends Thread {
   33   
   34     /**
   35        The default delay between every file modification check, set to 60
   36        seconds.  */
   37     static final public long DEFAULT_DELAY = 60000; 
   38     /**
   39        The name of the file to observe  for changes.
   40      */
   41     protected String filename;
   42     
   43     /**
   44        The delay to observe between every check. By default set {@link
   45        #DEFAULT_DELAY}. */
   46     protected long delay = DEFAULT_DELAY; 
   47     
   48     File file;
   49     long lastModif = 0; 
   50     boolean warnedAlready = false;
   51     boolean interrupted = false;
   52   
   53     protected
   54     FileWatchdog(String filename) {
   55       this.filename = filename;
   56       file = new File(filename);
   57       setDaemon(true);
   58       checkAndConfigure();
   59     }
   60   
   61     /**
   62        Set the delay to observe between each check of the file changes.
   63      */
   64     public
   65     void setDelay(long delay) {
   66       this.delay = delay;
   67     }
   68   
   69     abstract 
   70     protected 
   71     void doOnChange();
   72   
   73     protected
   74     void checkAndConfigure() {
   75       boolean fileExists;
   76       try {
   77         fileExists = file.exists();
   78       } catch(SecurityException  e) {
   79         LogLog.warn("Was not allowed to read check file existance, file:["+
   80   		  filename+"].");
   81         interrupted = true; // there is no point in continuing
   82         return;
   83       }
   84   
   85       if(fileExists) {
   86         long l = file.lastModified(); // this can also throw a SecurityException
   87         if(l > lastModif) {           // however, if we reached this point this
   88   	lastModif = l;              // is very unlikely.
   89   	doOnChange();
   90   	warnedAlready = false;
   91         }
   92       } else {
   93         if(!warnedAlready) {
   94   	LogLog.debug("["+filename+"] does not exist.");
   95   	warnedAlready = true;
   96         }
   97       }
   98     }
   99   
  100     public
  101     void run() {    
  102       while(!interrupted) {
  103         try {
  104   	    Thread.sleep(delay);
  105         } catch(InterruptedException e) {
  106   	// no interruption expected
  107         }
  108         checkAndConfigure();
  109       }
  110     }
  111   }

Save This Page
Home » slf4j-1.5.5 » org.apache » log4j » helpers » [javadoc | source]