bbdc505cdca8578aca1d33ef623e37b693c80786
[ccsdk/features.git] / sdnr / wt / common / src / main / java / org / onap / ccsdk / features / sdnr / wt / common / file / FileWatchdog.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.common.file;
23
24 import java.io.File;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Check every now and then that a certain file has not changed. If it has, then call the
30  * {@link #doOnChange} method.
31  *
32  * @author JunHo Yoon
33  * @since 3.1.1
34  */
35 public abstract class FileWatchdog extends Thread {
36     private static final Logger LOGGER = LoggerFactory.getLogger(FileWatchdog.class);
37     /**
38      * The default delay between every file modification check, set to 60 seconds.
39      */
40     public static final long DEFAULT_DELAY = 60000;
41     /**
42      * The name of the file to observe for changes.
43      */
44     private final String filename;
45
46     /**
47      * The delay to observe between every check. By default set {@link #DEFAULT_DELAY}.
48      */
49     private long delay = DEFAULT_DELAY;
50
51     private final File file;
52     private long lastModified = 0;
53     private boolean warnedAlready = false;
54
55     protected FileWatchdog(String filename) {
56         this.filename = filename;
57         file = new File(filename);
58         setDaemon(true);
59         checkAndConfigure();
60     }
61
62     /**
63      * Set the delay to observe between each check of the file changes.
64      *
65      * @param delay the frequency of file watch.
66      */
67     public void setDelay(long delay) {
68         this.delay = delay;
69     }
70
71     /**
72      * abstract method to be run when the file is changed.
73      */
74     protected abstract void doOnChange();
75
76     protected void checkAndConfigure() {
77         boolean fileExists;
78         try {
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
83             return;
84         }
85
86         if (fileExists) {
87             long l = file.lastModified(); // this can also throw a
88             if (lastModified == 0) {
89                 lastModified = l; // is very unlikely.
90             }
91             if (l > lastModified) { // however, if we reached this point this
92                 lastModified = l; // is very unlikely.
93                 doOnChange();
94                 warnedAlready = false;
95             }
96         } else {
97             if (!warnedAlready) {
98                 LOGGER.debug("[{}] does not exist.", filename);
99                 warnedAlready = true;
100             }
101         }
102     }
103
104     @Override
105     public void run() {
106         while (!isInterrupted()) {
107             checkAndConfigure();
108             try {
109                 Thread.sleep(delay);
110             } catch (InterruptedException e) {
111                 LOGGER.debug("Interrupted sleep. {}", e.getMessage());
112                 Thread.currentThread().interrupt();
113             }
114         }
115         LOGGER.debug("Stoppen file watchdog for file {}", filename);
116     }
117 }