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