001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.joran.spi;
015
016import ch.qos.logback.core.spi.ContextAwareBase;
017
018import java.io.File;
019import java.net.URL;
020import java.net.URLDecoder;
021import java.util.ArrayList;
022import java.util.List;
023
024/**
025 * @author Ceki Gülcü
026 */
027public class ConfigurationWatchList extends ContextAwareBase {
028
029    URL mainURL;
030    List<File> fileWatchList = new ArrayList<File>();
031    List<Long> lastModifiedList = new ArrayList<Long>();
032
033    public ConfigurationWatchList buildClone() {
034        ConfigurationWatchList out = new ConfigurationWatchList();
035        out.mainURL = this.mainURL;
036        out.fileWatchList = new ArrayList<File>(this.fileWatchList);
037        out.lastModifiedList = new ArrayList<Long>(this.lastModifiedList);
038        return out;
039    }
040
041    public void clear() {
042        this.mainURL = null;
043        lastModifiedList.clear();
044        fileWatchList.clear();
045    }
046
047    /**
048     * The mainURL for the configuration file. Null values are allowed.
049     * 
050     * @param mainURL
051     */
052    public void setMainURL(URL mainURL) {
053        // main url can be null
054        this.mainURL = mainURL;
055        if (mainURL != null)
056            addAsFileToWatch(mainURL);
057    }
058
059    private void addAsFileToWatch(URL url) {
060        File file = convertToFile(url);
061        if (file != null) {
062            fileWatchList.add(file);
063            lastModifiedList.add(file.lastModified());
064        }
065    }
066
067    public void addToWatchList(URL url) {
068        addAsFileToWatch(url);
069    }
070
071    public URL getMainURL() {
072        return mainURL;
073    }
074
075    public List<File> getCopyOfFileWatchList() {
076        return new ArrayList<File>(fileWatchList);
077    }
078
079    public boolean changeDetected() {
080        int len = fileWatchList.size();
081        for (int i = 0; i < len; i++) {
082            long lastModified = lastModifiedList.get(i);
083            File file = fileWatchList.get(i);
084            if (lastModified != file.lastModified()) {
085                return true;
086            }
087        }
088        return false;
089        // return (lastModified != fileToScan.lastModified() && lastModified !=
090        // SENTINEL);
091    }
092
093    @SuppressWarnings("deprecation")
094    File convertToFile(URL url) {
095        String protocol = url.getProtocol();
096        if ("file".equals(protocol)) {
097            return new File(URLDecoder.decode(url.getFile()));
098        } else {
099            addInfo("URL [" + url + "] is not of type file");
100            return null;
101        }
102    }
103
104}