1 /**
2  * Logback: the reliable, generic, fast and flexible logging framework.
3  * Copyright (C) 1999-2015, 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.util;
15
16 import ch.qos.logback.core.Context;
17 import ch.qos.logback.core.CoreConstants;
18 import ch.qos.logback.core.joran.spi.ConfigurationWatchList;
19 import ch.qos.logback.core.status.InfoStatus;
20 import ch.qos.logback.core.status.Status;
21 import ch.qos.logback.core.status.StatusManager;
22 import ch.qos.logback.core.status.WarnStatus;
23
24 import java.net.URL;
25
26 /**
27  * @author Ceki Gülcü
28  */

29 public class ConfigurationWatchListUtil {
30
31     final static ConfigurationWatchListUtil origin = new ConfigurationWatchListUtil();
32
33     private ConfigurationWatchListUtil() {
34     }
35
36     
37     public static void registerConfigurationWatchList(Context context, ConfigurationWatchList cwl) {
38         context.putObject(CoreConstants.CONFIGURATION_WATCH_LIST, cwl);
39     }
40     public static void setMainWatchURL(Context context, URL url) {
41         ConfigurationWatchList cwl = getConfigurationWatchList(context);
42         if (cwl == null) {
43             cwl = new ConfigurationWatchList();
44             cwl.setContext(context);
45             context.putObject(CoreConstants.CONFIGURATION_WATCH_LIST, cwl);
46         } else {
47             cwl.clear();
48         }
49         //setConfigurationWatchListResetFlag(context, true);
50         cwl.setMainURL(url);
51     }
52
53     public static URL getMainWatchURL(Context context) {
54         ConfigurationWatchList cwl = getConfigurationWatchList(context);
55         if (cwl == null) {
56             return null;
57         } else {
58             return cwl.getMainURL();
59         }
60     }
61
62     public static void addToWatchList(Context context, URL url) {
63         ConfigurationWatchList cwl = getConfigurationWatchList(context);
64         if (cwl == null) {
65             addWarn(context, "Null ConfigurationWatchList. Cannot add " + url);
66         } else {
67             addInfo(context, "Adding [" + url + "] to configuration watch list.");
68             cwl.addToWatchList(url);
69         }
70     }
71
72 //    public static boolean wasConfigurationWatchListReset(Context context) {
73 //        Object o = context.getObject(CoreConstants.CONFIGURATION_WATCH_LIST_RESET);
74 //        if (o == null)
75 //            return false;
76 //        else {
77 //            return ((Boolean) o).booleanValue();
78 //        }
79 //    }
80
81 //    public static void setConfigurationWatchListResetFlag(Context context, boolean val) {
82 //        context.putObject(CoreConstants.CONFIGURATION_WATCH_LIST_RESET, new Boolean(val));
83 //    }
84
85     public static ConfigurationWatchList getConfigurationWatchList(Context context) {
86         return (ConfigurationWatchList) context.getObject(CoreConstants.CONFIGURATION_WATCH_LIST);
87     }
88
89     static void addStatus(Context context, Status s) {
90         if (context == null) {
91             System.out.println("Null context in " + ConfigurationWatchList.class.getName());
92             return;
93         }
94         StatusManager sm = context.getStatusManager();
95         if (sm == null)
96             return;
97         sm.add(s);
98     }
99
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 }
108