JAL-3026 srcjar files for VARNA and log4j
[jalview.git] / srcjar / org / apache / log4j / helpers / FileWatchdog.java
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
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 {
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 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 file;
48   long lastModif = 0; 
49   boolean warnedAlready = false;
50   boolean interrupted = false;
51
52   protected
53   FileWatchdog(String filename) {
54     super("FileWatchdog");
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 }