View Javadoc
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       * 
50       * @param mainURL
51       */
52      public void setMainURL(URL mainURL) {
53          // main url can be null
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          // return (lastModified != fileToScan.lastModified() && lastModified !=
90          // SENTINEL);
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 }