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.util;
015
016import ch.qos.logback.core.Context;
017import ch.qos.logback.core.CoreConstants;
018import ch.qos.logback.core.joran.spi.ConfigurationWatchList;
019import ch.qos.logback.core.status.InfoStatus;
020import ch.qos.logback.core.status.Status;
021import ch.qos.logback.core.status.StatusManager;
022import ch.qos.logback.core.status.WarnStatus;
023
024import java.net.URL;
025
026/**
027 * A thin layer on top of {@link ConfigurationWatchList}.
028 *
029 * @author Ceki Gülcü
030 */
031public class ConfigurationWatchListUtil {
032
033    final static ConfigurationWatchListUtil ORIGIN = new ConfigurationWatchListUtil();
034
035    private ConfigurationWatchListUtil() {
036    }
037
038    public static void registerConfigurationWatchList(Context context, ConfigurationWatchList cwl) {
039        context.putObject(CoreConstants.CONFIGURATION_WATCH_LIST, cwl);
040    }
041
042    public static void setMainWatchURL(Context context, URL url) {
043        ConfigurationWatchList cwl = getConfigurationWatchList(context);
044        if (cwl == null) {
045            cwl = registerNewConfigurationWatchListWithContext(context);
046        } else {
047            cwl.clear();
048        }
049        // setConfigurationWatchListResetFlag(context, true);
050        cwl.setMainURL(url);
051    }
052
053    /**
054     * Returns true if there are watchable files, false otherwise.
055     * @return true if there are watchable files,  false otherwise.
056     * @since 1.5.8
057     */
058    public static boolean watchPredicateFulfilled(Context context) {
059        ConfigurationWatchList cwl = getConfigurationWatchList(context);
060        if (cwl == null) {
061            return false;
062        }
063        return cwl.watchPredicateFulfilled();
064    }
065
066    public static URL getMainWatchURL(Context context) {
067        ConfigurationWatchList cwl = getConfigurationWatchList(context);
068        if (cwl == null) {
069            return null;
070        } else {
071            return cwl.getMainURL();
072        }
073    }
074
075    public static void addToWatchList(Context context, URL url) {
076        addToWatchList(context, url, false);
077    }
078
079    public static void addToWatchList(Context context, URL url, boolean createCWL) {
080        ConfigurationWatchList cwl = getConfigurationWatchList(context);
081        if(cwl == null) {
082            if(createCWL && ConfigurationWatchList.isWatchableProtocol(url)) {
083                cwl = registerNewConfigurationWatchListWithContext(context);
084            } else {
085                addWarn(context, "Null ConfigurationWatchList. Cannot add " + url);
086                return;
087            }
088        }
089
090        String protocol = url.getProtocol();
091        if(cwl.isWatchableProtocol(protocol)) {
092            addInfo(context, "Will add [" + url + "] to configuration watch list.");
093            cwl.addToWatchList(url);
094        } else {
095            addInfo(context, "Will not add configuration file ["+url + "] to watch list, because '"+protocol+"' protocol is not watchable.");
096            addInfo(context, "Only the protocols 'file', 'http' and 'https' are watchable.");
097        }
098    }
099
100    private static ConfigurationWatchList registerNewConfigurationWatchListWithContext(Context context) {
101        ConfigurationWatchList cwl = new ConfigurationWatchList();
102        cwl.setContext(context);
103        context.putObject(CoreConstants.CONFIGURATION_WATCH_LIST, cwl);
104        return cwl;
105    }
106
107    public static ConfigurationWatchList getConfigurationWatchList(Context context) {
108        return (ConfigurationWatchList) context.getObject(CoreConstants.CONFIGURATION_WATCH_LIST);
109    }
110
111    static void addStatus(Context context, Status s) {
112        if (context == null) {
113            System.out.println("Null context in " + ConfigurationWatchList.class.getName());
114            return;
115        }
116        StatusManager sm = context.getStatusManager();
117        if (sm == null)
118            return;
119        sm.add(s);
120    }
121
122    static void addInfo(Context context, String msg) {
123       addStatus(context, new InfoStatus(msg, ORIGIN));
124    }
125
126     static void addWarn(Context context, String msg) {
127        addStatus(context, new WarnStatus(msg, ORIGIN));
128    }
129}