1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.joran.spi;
15
16 import ch.qos.logback.core.spi.ContextAwareBase;
17
18 import java.io.File;
19 import java.net.URL;
20 import java.net.URLDecoder;
21 import java.util.ArrayList;
22 import java.util.List;
23
24
25
26
27 public class ConfigurationWatchList extends ContextAwareBase {
28
29 URL mainURL;
30 List<File> fileWatchList = new ArrayList<File>();
31 List<Long> lastModifiedList = new ArrayList<Long>();
32
33 public ConfigurationWatchList buildClone() {
34 ConfigurationWatchList out = new ConfigurationWatchList();
35 out.mainURL = this.mainURL;
36 out.fileWatchList = new ArrayList<File>(this.fileWatchList);
37 out.lastModifiedList = new ArrayList<Long>(this.lastModifiedList);
38 return out;
39 }
40
41 public void clear() {
42 this.mainURL = null;
43 lastModifiedList.clear();
44 fileWatchList.clear();
45 }
46
47
48
49
50
51
52 public void setMainURL(URL mainURL) {
53
54 this.mainURL = mainURL;
55 if (mainURL != null)
56 addAsFileToWatch(mainURL);
57 }
58
59 private void addAsFileToWatch(URL url) {
60 File file = convertToFile(url);
61 if (file != null) {
62 fileWatchList.add(file);
63 lastModifiedList.add(file.lastModified());
64 }
65 }
66
67 public void addToWatchList(URL url) {
68 addAsFileToWatch(url);
69 }
70
71 public URL getMainURL() {
72 return mainURL;
73 }
74
75 public List<File> getCopyOfFileWatchList() {
76 return new ArrayList<File>(fileWatchList);
77 }
78
79 public boolean changeDetected() {
80 int len = fileWatchList.size();
81 for (int i = 0; i < len; i++) {
82 long lastModified = lastModifiedList.get(i);
83 File file = fileWatchList.get(i);
84 if (lastModified != file.lastModified()) {
85 return true;
86 }
87 }
88 return false;
89
90
91 }
92
93 @SuppressWarnings("deprecation")
94 File convertToFile(URL url) {
95 String protocol = url.getProtocol();
96 if ("file".equals(protocol)) {
97 return new File(URLDecoder.decode(url.getFile()));
98 } else {
99 addInfo("URL [" + url + "] is not of type file");
100 return null;
101 }
102 }
103
104 }