Assign image keyname and pubkey at vnf level
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / devicemanager / impl / src / main / test / TestFileChangeNotification.java
1
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8
9
10 public class TestFileChangeNotification {
11         /**
12          * Check every now and then that a certain file has not changed. If it has, then
13          * call the {@link #doOnChange} method.
14          *
15          * @author JunHo Yoon
16          * @since 3.1.1
17          */
18         public static abstract class FileWatchdog extends Thread {
19
20                 private static final Logger LOGGER = LoggerFactory.getLogger(FileWatchdog.class);
21                 /**
22                  * The default delay between every file modification check, set to 60
23                  * seconds.
24                  */
25                 public static final long DEFAULT_DELAY = 60000;
26                 /**
27                  * The name of the file to observe for changes.
28                  */
29                 private String filename;
30
31                 /**
32                  * The delay to observe between every check. By default set
33                  * {@link #DEFAULT_DELAY}.
34                  */
35                 private long delay = DEFAULT_DELAY;
36
37                 private File file;
38                 private long lastModified = 0;
39                 private boolean warnedAlready = false;
40                 private boolean interrupted = false;
41
42                 protected  FileWatchdog(String filename) {
43                         this.filename = filename;
44                         file = new File(filename);
45                         setDaemon(true);
46                         checkAndConfigure();
47                 }
48
49                 /**
50                  * Set the delay to observe between each check of the file changes.
51                  *
52                  * @param delay
53                  *            the frequency of file watch.
54                  */
55                 public  void setDelay(long delay) {
56                         this.delay = delay;
57                 }
58
59                 /**
60                  * abstract method to be run when the file is changed.
61                  */
62                 protected abstract void doOnChange();
63
64                 protected  void checkAndConfigure() {
65                         boolean fileExists;
66                         try {
67                                 fileExists = file.exists();
68                         } catch (SecurityException e) {
69                                 LOGGER.warn("Was not allowed to read check file existence, file:[" + filename + "].");
70                                 interrupted = true; // there is no point in continuing
71                                 return;
72                         }
73
74                         if (fileExists) {
75                                 long l = file.lastModified(); // this can also throw a
76                                 if (lastModified ==0) {
77                                         lastModified = l; // is very unlikely.
78                                 }
79                                 if (l > lastModified) { // however, if we reached this point this
80                                         lastModified = l; // is very unlikely.
81                                         doOnChange();
82                                         warnedAlready = false;
83                                 }
84                         } else {
85                                 if (!warnedAlready) {
86                                         LOGGER.debug("[" + filename + "] does not exist.");
87                                         warnedAlready = true;
88                                 }
89                         }
90                 }
91
92                 @Override
93                 public  void run() {
94                         while (!interrupted && !isInterrupted()) {
95                                 try {
96                                         Thread.sleep(delay);
97                                 } catch (InterruptedException e) {
98                                 }
99                                 checkAndConfigure();
100                         }
101                 }
102         }
103
104         public static class SomeWatchFile extends FileWatchdog{
105
106               protected SomeWatchFile(String filename) {
107                     super(filename);
108                     this.setDelay(1000);
109                 }
110
111                 @Override
112                 protected void doOnChange() {
113                     System.out.println("File has changed");
114                 }
115         }
116
117     public static void main(String args[]) throws IOException {
118                     SomeWatchFile someWatchFile = new SomeWatchFile ("watchedFile.txt");
119                     someWatchFile.start();
120
121                     String fileName = "watchedFile.txt";
122                     File tempFile = new File(fileName);
123                     tempFile.createNewFile();
124
125                     System.out.println("1. Press F5 in eclipse to see and modify 2. Press enter to exit");
126                     System.in.read();
127                     System.out.println("Remove file "+fileName);
128                     tempFile.delete();
129                 }
130
131
132 }