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 = new ConfigurationWatchList();
044            cwl.setContext(context);
045            context.putObject(CoreConstants.CONFIGURATION_WATCH_LIST, cwl);
046        } else {
047            cwl.clear();
048        }
049        // setConfigurationWatchListResetFlag(context, true);
050        cwl.setMainURL(url);
051    }
052
053    public static URL getMainWatchURL(Context context) {
054        ConfigurationWatchList cwl = getConfigurationWatchList(context);
055        if (cwl == null) {
056            return null;
057        } else {
058            return cwl.getMainURL();
059        }
060    }
061
062    public static void addToWatchList(Context context, URL url) {
063        ConfigurationWatchList cwl = getConfigurationWatchList(context);
064        if (cwl == null) {
065            addWarn(context, "Null ConfigurationWatchList. Cannot add " + url);
066        } else {
067            addInfo(context, "Adding [" + url + "] to configuration watch list.");
068            cwl.addToWatchList(url);
069        }
070    }
071
072//    public static boolean wasConfigurationWatchListReset(Context context) {
073//        Object o = context.getObject(CoreConstants.CONFIGURATION_WATCH_LIST_RESET);
074//        if (o == null)
075//            return false;
076//        else {
077//            return ((Boolean) o).booleanValue();
078//        }
079//    }
080
081//    public static void setConfigurationWatchListResetFlag(Context context, boolean val) {
082//        context.putObject(CoreConstants.CONFIGURATION_WATCH_LIST_RESET, new Boolean(val));
083//    }
084
085    public static ConfigurationWatchList getConfigurationWatchList(Context context) {
086        return (ConfigurationWatchList) context.getObject(CoreConstants.CONFIGURATION_WATCH_LIST);
087    }
088
089    static void addStatus(Context context, Status s) {
090        if (context == null) {
091            System.out.println("Null context in " + ConfigurationWatchList.class.getName());
092            return;
093        }
094        StatusManager sm = context.getStatusManager();
095        if (sm == null)
096            return;
097        sm.add(s);
098    }
099
100    static void addInfo(Context context, String msg) {
101        addStatus(context, new InfoStatus(msg, origin));
102    }
103
104    static void addWarn(Context context, String msg) {
105        addStatus(context, new WarnStatus(msg, origin));
106    }
107}