90bb8432aa7f1af1bd8e05a1ec95c0ac7fd926c7
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 /*
19  * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
20  * agreements. See the NOTICE file distributed with this work for additional information regarding
21  * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
22  * "License"); you may not use this file except in compliance with the License. You may obtain a
23  * copy of the License at
24  *
25  * http://www.apache.org/licenses/LICENSE-2.0
26  *
27  * Unless required by applicable law or agreed to in writing, software distributed under the License
28  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29  * or implied. See the License for the specific language governing permissions and limitations under
30  * the License.
31  */
32
33 package org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes;
34
35 import java.io.File;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * Check every now and then that a certain file has not changed. If it has, then call the
41  * {@link #doOnChange} method.
42  *
43  * @author JunHo Yoon
44  * @since 3.1.1
45  */
46 public abstract class FileWatchdog extends Thread {
47     private static final Logger LOGGER = LoggerFactory.getLogger(FileWatchdog.class);
48     /**
49      * The default delay between every file modification check, set to 60 seconds.
50      */
51     public static final long DEFAULT_DELAY = 60000;
52     /**
53      * The name of the file to observe for changes.
54      */
55     private final String filename;
56
57     /**
58      * The delay to observe between every check. By default set {@link #DEFAULT_DELAY}.
59      */
60     private long delay = DEFAULT_DELAY;
61
62     private final File file;
63     private long lastModified = 0;
64     private boolean warnedAlready = false;
65
66     protected FileWatchdog(String filename) {
67         this.filename = filename;
68         file = new File(filename);
69         setDaemon(true);
70         checkAndConfigure();
71     }
72
73     /**
74      * Set the delay to observe between each check of the file changes.
75      *
76      * @param delay the frequency of file watch.
77      */
78     public void setDelay(long delay) {
79         this.delay = delay;
80     }
81
82     /**
83      * abstract method to be run when the file is changed.
84      */
85     protected abstract void doOnChange();
86
87     protected void checkAndConfigure() {
88         boolean fileExists;
89         try {
90             fileExists = file.exists();
91         } catch (SecurityException e) {
92             LOGGER.warn("Was not allowed to read check file existence, file:[{}].",filename);
93             this.interrupt(); // there is no point in continuing
94             return;
95         }
96
97         if (fileExists) {
98             long l = file.lastModified(); // this can also throw a
99             if (lastModified == 0) {
100                 lastModified = l; // is very unlikely.
101             }
102             if (l > lastModified) { // however, if we reached this point this
103                 lastModified = l; // is very unlikely.
104                 doOnChange();
105                 warnedAlready = false;
106             }
107         } else {
108             if (!warnedAlready) {
109                 LOGGER.debug("[{}] does not exist.", filename);
110                 warnedAlready = true;
111             }
112         }
113     }
114
115     @Override
116     public void run() {
117         while (!isInterrupted()) {
118             checkAndConfigure();
119             try {
120                 Thread.sleep(delay);
121             } catch (InterruptedException e) {
122                 LOGGER.debug("Interrupted sleep. {}", e.getMessage());
123                 Thread.currentThread().interrupt();
124             }
125         }
126         LOGGER.debug("Stoppen file watchdog for file {}", filename);
127     }
128 }