e3b0ef543585f175e001f74075ae65c9f3fdd303
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / devicemanager / impl / src / main / java / org / opendaylight / mwtn / base / internalTypes / 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 package org.opendaylight.mwtn.base.internalTypes;
19
20 import java.io.File;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Check every now and then that a certain file has not changed. If it has, then
27  * call the {@link #doOnChange} method.
28  *
29  * @author JunHo Yoon
30  * @since 3.1.1
31  */
32 public abstract class FileWatchdog extends Thread {
33         private static final Logger LOGGER = LoggerFactory.getLogger(FileWatchdog.class);
34         /**
35          * The default delay between every file modification check, set to 60
36          * seconds.
37          */
38         public static final long DEFAULT_DELAY = 60000;
39         /**
40          * The name of the file to observe for changes.
41          */
42         private String filename;
43
44         /**
45          * The delay to observe between every check. By default set
46          * {@link #DEFAULT_DELAY}.
47          */
48         private long delay = DEFAULT_DELAY;
49
50         private File file;
51         private long lastModified = 0;
52         private boolean warnedAlready = false;
53         private boolean interrupted = 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
66          *            the frequency of file watch.
67          */
68         public  void setDelay(long delay) {
69                 this.delay = delay;
70         }
71
72         /**
73          * abstract method to be run when the file is changed.
74          */
75         protected abstract void doOnChange();
76
77         protected  void checkAndConfigure() {
78                 boolean fileExists;
79                 try {
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
84                         return;
85                 }
86
87                 if (fileExists) {
88                         long l = file.lastModified(); // this can also throw a
89                         if (lastModified ==0) {
90                                 lastModified = l; // is very unlikely.
91                         }
92                         if (l > lastModified) { // however, if we reached this point this
93                                 lastModified = l; // is very unlikely.
94                                 doOnChange();
95                                 warnedAlready = false;
96                         }
97                 } else {
98                         if (!warnedAlready) {
99                                 LOGGER.debug("[" + filename + "] does not exist.");
100                                 warnedAlready = true;
101                         }
102                 }
103         }
104
105         /*
106          * (non-Javadoc)
107          *
108          * @see java.lang.Thread#run()
109          */
110         @Override
111         public  void run() {
112                 while (!interrupted && !isInterrupted()) {
113                         try {
114                                 Thread.sleep(delay);
115                         } catch (InterruptedException e) {
116
117                         }
118                         checkAndConfigure();
119                 }
120         }
121 }