1
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
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
51 public void setMainURL(URL mainURL) {
52
53 this.mainURL = mainURL;
54 if (mainURL != null)
55 addAsFileToWatch(mainURL);
56 }
57
58 private void addAsFileToWatch(URL url) {
59 File file = convertToFile(url);
60 if (file != null) {
61 fileWatchList.add(file);
62 lastModifiedList.add(file.lastModified());
63 }
64 }
65
66 public void addToWatchList(URL url) {
67 addAsFileToWatch(url);
68 }
69
70 public URL getMainURL() {
71 return mainURL;
72 }
73
74 public List<File> getCopyOfFileWatchList() {
75 return new ArrayList<File>(fileWatchList);
76 }
77
78 public boolean changeDetected() {
79 int len = fileWatchList.size();
80 for (int i = 0; i < len; i++) {
81 long lastModified = lastModifiedList.get(i);
82 File file = fileWatchList.get(i);
83 if (lastModified != file.lastModified()) {
84 return true;
85 }
86 }
87 return false;
88
89 }
90
91 @SuppressWarnings("deprecation")
92 File convertToFile(URL url) {
93 String protocol = url.getProtocol();
94 if ("file".equals(protocol)) {
95 return new File(URLDecoder.decode(url.getFile()));
96 } else {
97 addInfo("URL [" + url + "] is not of type file");
98 return null;
99 }
100 }
101
102 }
103