KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > helpers > FileWatchdog


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 // Contributors: Mathias Bogaert
18

19 package org.apache.log4j.helpers;
20
21 import java.io.File JavaDoc;
22 import org.apache.log4j.helpers.LogLog;
23
24 /**
25    Check every now and then that a certain file has not changed. If it
26    has, then call the {@link #doOnChange} method.
27
28
29    @author Ceki Gülcü
30    @since version 0.9.1 */

31 public abstract class FileWatchdog extends Thread JavaDoc {
32
33   /**
34      The default delay between every file modification check, set to 60
35      seconds. */

36   static final public long DEFAULT_DELAY = 60000;
37   /**
38      The name of the file to observe for changes.
39    */

40   protected String JavaDoc filename;
41   
42   /**
43      The delay to observe between every check. By default set {@link
44      #DEFAULT_DELAY}. */

45   protected long delay = DEFAULT_DELAY;
46   
47   File JavaDoc file;
48   long lastModif = 0;
49   boolean warnedAlready = false;
50   boolean interrupted = false;
51
52   protected
53   FileWatchdog(String JavaDoc filename) {
54     this.filename = filename;
55     file = new File JavaDoc(filename);
56     setDaemon(true);
57     checkAndConfigure();
58   }
59
60   /**
61      Set the delay to observe between each check of the file changes.
62    */

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 JavaDoc e) {
78       LogLog.warn("Was not allowed to read check file existance, file:["+
79           filename+"].");
80       interrupted = true; // there is no point in continuing
81
return;
82     }
83
84     if(fileExists) {
85       long l = file.lastModified(); // this can also throw a SecurityException
86
if(l > lastModif) { // however, if we reached this point this
87
lastModif = l; // is very unlikely.
88
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 JavaDoc e) {
105     // no interruption expected
106
}
107       checkAndConfigure();
108     }
109   }
110 }
111
Popular Tags