e3b9923effc99257d477ce12ce799acacc10f538
[aai/babel.git] / src / main / java / org / onap / aai / auth / FileWatcher.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 European Software Marketing Ltd.
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.auth;
24
25 import java.io.File;
26 import java.util.TimerTask;
27
28 public abstract class FileWatcher extends TimerTask {
29     private long timeStamp;
30     private File file;
31
32     /**
33      * Instantiates a new file watcher.
34      *
35      * @param file the file
36      */
37     public FileWatcher(File file) {
38         this.file = file;
39         this.timeStamp = file.lastModified();
40     }
41
42     /**
43      * runs a timer task
44      * 
45      * @see java.util.TimerTask.run
46      */
47     @Override
48     public final void run() {
49         long newTimeStamp = file.lastModified();
50
51         if ((newTimeStamp - this.timeStamp) > 500) {
52             this.timeStamp = newTimeStamp;
53             onChange(file);
54         }
55     }
56
57     /**
58      * On change.
59      *
60      * @param file the file
61      */
62     protected abstract void onChange(File file);
63 }