1 /**
2  * Logback: the reliable, generic, fast and flexible logging framework.
3  * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4  *
5  * This program and the accompanying materials are dual-licensed under
6  * either the terms of the Eclipse Public License v1.0 as published by
7  * the Eclipse Foundation
8  *
9  *   or (per the licensee's choosing)
10  *
11  * under the terms of the GNU Lesser General Public License version 2.1
12  * as published by the Free Software Foundation.
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  * @author Ceki Gülcü
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      * The mainURL for the configuration file. Null values are allowed.
49      * @param mainURL
50      */

51     public void setMainURL(URL mainURL) {
52         // main url can be null
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         // return (lastModified != fileToScan.lastModified() && lastModified != SENTINEL);
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