View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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  
17  import ch.qos.logback.core.spi.ContextAwareBase;
18  
19  import java.io.File;
20  import java.net.URL;
21  import java.net.URLDecoder;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  /**
26   * @author Ceki Gücü
27   */
28  public class ConfigurationWatchList extends ContextAwareBase {
29  
30    URL mainURL;
31    List<File> fileWatchList = new ArrayList<File>();
32    List<Long> lastModifiedList = new ArrayList<Long>();
33  
34    public void clear() {
35      this.mainURL = null;
36      lastModifiedList.clear();
37      fileWatchList.clear();
38    }
39  
40    /**
41     * The mainURL for the configuration file. Null values are allowed.
42     * @param mainURL
43     */
44    public void setMainURL(URL mainURL) {
45      // main url can be null
46      this.mainURL = mainURL;
47      if (mainURL != null)
48        addAsFileToWatch(mainURL);
49    }
50  
51    private void addAsFileToWatch(URL url) {
52      File file = convertToFile(url);
53      if (file != null) {
54        fileWatchList.add(file);
55        lastModifiedList.add(file.lastModified());
56      }
57    }
58  
59    public void addToWatchList(URL url) {
60      addAsFileToWatch(url);
61    }
62  
63    public URL getMainURL() {
64      return mainURL;
65    }
66  
67    public List<File> getCopyOfFileWatchList() {
68      return new ArrayList<File>(fileWatchList);
69    }
70  
71    public boolean changeDetected() {
72      int len = fileWatchList.size();
73      for (int i = 0; i < len; i++) {
74        long lastModified = lastModifiedList.get(i);
75        File file = fileWatchList.get(i);
76        if (lastModified != file.lastModified()) {
77          return true;
78        }
79      }
80      return false;
81      //return (lastModified != fileToScan.lastModified() && lastModified != SENTINEL);
82    }
83  
84    @SuppressWarnings("deprecation")
85    File convertToFile(URL url) {
86      String protocol = url.getProtocol();
87      if ("file".equals(protocol)) {
88        return new File(URLDecoder.decode(url.getFile()));
89      } else {
90        addInfo("URL [" + url + "] is not of type file");
91        return null;
92      }
93    }
94  
95  }