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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
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
25 * http://www.apache.org/licenses/LICENSE-2.0
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
33 package org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Check every now and then that a certain file has not changed. If it has, then call the
41 * {@link #doOnChange} method.
46 public abstract class FileWatchdog extends Thread {
47 private static final Logger LOGGER = LoggerFactory.getLogger(FileWatchdog.class);
49 * The default delay between every file modification check, set to 60 seconds.
51 public static final long DEFAULT_DELAY = 60000;
53 * The name of the file to observe for changes.
55 private final String filename;
58 * The delay to observe between every check. By default set {@link #DEFAULT_DELAY}.
60 private long delay = DEFAULT_DELAY;
62 private final File file;
63 private long lastModified = 0;
64 private boolean warnedAlready = false;
66 protected FileWatchdog(String filename) {
67 this.filename = filename;
68 file = new File(filename);
74 * Set the delay to observe between each check of the file changes.
76 * @param delay the frequency of file watch.
78 public void setDelay(long delay) {
83 * abstract method to be run when the file is changed.
85 protected abstract void doOnChange();
87 protected void checkAndConfigure() {
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
98 long l = file.lastModified(); // this can also throw a
99 if (lastModified == 0) {
100 lastModified = l; // is very unlikely.
102 if (l > lastModified) { // however, if we reached this point this
103 lastModified = l; // is very unlikely.
105 warnedAlready = false;
108 if (!warnedAlready) {
109 LOGGER.debug("[{}] does not exist.", filename);
110 warnedAlready = true;
117 while (!isInterrupted()) {
121 } catch (InterruptedException e) {
122 LOGGER.debug("Interrupted sleep. {}", e.getMessage());
123 Thread.currentThread().interrupt();
126 LOGGER.debug("Stoppen file watchdog for file {}", filename);