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 package org.opendaylight.mwtn.base.internalTypes;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * Check every now and then that a certain file has not changed. If it has, then
27 * call the {@link #doOnChange} method.
32 public abstract class FileWatchdog extends Thread {
33 private static final Logger LOGGER = LoggerFactory.getLogger(FileWatchdog.class);
35 * The default delay between every file modification check, set to 60
38 public static final long DEFAULT_DELAY = 60000;
40 * The name of the file to observe for changes.
42 private String filename;
45 * The delay to observe between every check. By default set
46 * {@link #DEFAULT_DELAY}.
48 private long delay = DEFAULT_DELAY;
51 private long lastModified = 0;
52 private boolean warnedAlready = false;
53 private boolean interrupted = false;
55 protected FileWatchdog(String filename) {
56 this.filename = filename;
57 file = new File(filename);
63 * Set the delay to observe between each check of the file changes.
66 * the frequency of file watch.
68 public void setDelay(long delay) {
73 * abstract method to be run when the file is changed.
75 protected abstract void doOnChange();
77 protected void checkAndConfigure() {
80 fileExists = file.exists();
81 } catch (SecurityException e) {
82 LOGGER.warn("Was not allowed to read check file existence, file:[" + filename + "].");
83 interrupted = true; // there is no point in continuing
88 long l = file.lastModified(); // this can also throw a
89 if (lastModified ==0) {
90 lastModified = l; // is very unlikely.
92 if (l > lastModified) { // however, if we reached this point this
93 lastModified = l; // is very unlikely.
95 warnedAlready = false;
99 LOGGER.debug("[" + filename + "] does not exist.");
100 warnedAlready = true;
108 * @see java.lang.Thread#run()
112 while (!interrupted && !isInterrupted()) {
115 } catch (InterruptedException e) {