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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 // Contributors: Mathias Bogaert
20 package org.apache.log4j.helpers;
25 Check every now and then that a certain file has not changed. If it
26 has, then call the {@link #doOnChange} method.
29 @author Ceki Gülcü
30 @since version 0.9.1 */
31 public abstract class FileWatchdog extends Thread {
34 The default delay between every file modification check, set to 60
36 static final public long DEFAULT_DELAY = 60000;
38 The name of the file to observe for changes.
40 protected String filename;
43 The delay to observe between every check. By default set {@link
45 protected long delay = DEFAULT_DELAY;
49 boolean warnedAlready = false;
50 boolean interrupted = false;
53 FileWatchdog(String filename) {
54 super("FileWatchdog");
55 this.filename = filename;
56 file = new File(filename);
62 Set the delay to observe between each check of the file changes.
65 void setDelay(long delay) {
74 void checkAndConfigure() {
77 fileExists = file.exists();
78 } catch(SecurityException e) {
79 LogLog.warn("Was not allowed to read check file existance, file:["+
81 interrupted = true; // there is no point in continuing
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.
90 warnedAlready = false;
94 LogLog.debug("["+filename+"] does not exist.");
102 while(!interrupted) {
105 } catch(InterruptedException e) {
106 // no interruption expected