2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.common.file;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Check every now and then that a certain file has not changed. If it has, then call the
30 * {@link #doOnChange} method.
35 public abstract class FileWatchdog extends Thread {
36 private static final Logger LOGGER = LoggerFactory.getLogger(FileWatchdog.class);
38 * The default delay between every file modification check, set to 60 seconds.
40 public static final long DEFAULT_DELAY = 60000;
42 * The name of the file to observe for changes.
44 private final String filename;
47 * The delay to observe between every check. By default set {@link #DEFAULT_DELAY}.
49 private long delay = DEFAULT_DELAY;
51 private final File file;
52 private long lastModified = 0;
53 private boolean warnedAlready = 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.
65 * @param delay the frequency of file watch.
67 public void setDelay(long delay) {
72 * abstract method to be run when the file is changed.
74 protected abstract void doOnChange();
76 protected void checkAndConfigure() {
79 fileExists = file.exists();
80 } catch (SecurityException e) {
81 LOGGER.warn("Was not allowed to read check file existence, file:[{}].",filename);
82 this.interrupt(); // there is no point in continuing
87 long l = file.lastModified(); // this can also throw a
88 if (lastModified == 0) {
89 lastModified = l; // is very unlikely.
91 if (l > lastModified) { // however, if we reached this point this
92 lastModified = l; // is very unlikely.
94 warnedAlready = false;
98 LOGGER.debug("[{}] does not exist.", filename);
106 while (!isInterrupted()) {
110 } catch (InterruptedException e) {
111 LOGGER.debug("Interrupted sleep. {}", e.getMessage());
112 Thread.currentThread().interrupt();
115 LOGGER.debug("Stoppen file watchdog for file {}", filename);