1
2
3
4
5
6
7
8
9
10
11
12
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
28
29 public class ConfigurationWatchListUtil {
30
31 final static ConfigurationWatchListUtil ORIGIN = new ConfigurationWatchListUtil();
32
33 private ConfigurationWatchListUtil() {
34 }
35
36 public static void registerConfigurationWatchList(Context context, ConfigurationWatchList cwl) {
37 context.putObject(CoreConstants.CONFIGURATION_WATCH_LIST, cwl);
38 }
39
40 public static void setMainWatchURL(Context context, URL url) {
41 ConfigurationWatchList cwl = getConfigurationWatchList(context);
42 if (cwl == null) {
43 cwl = registerNewConfigurationWatchListWithContext(context);
44 } else {
45 cwl.clear();
46 }
47
48 cwl.setMainURL(url);
49 }
50
51
52
53
54
55
56 public static boolean watchPredicateFulfilled(Context context) {
57 ConfigurationWatchList cwl = getConfigurationWatchList(context);
58 if (cwl == null) {
59 return false;
60 }
61 return cwl.watchPredicateFulfilled();
62 }
63
64 public static URL getMainWatchURL(Context context) {
65 ConfigurationWatchList cwl = getConfigurationWatchList(context);
66 if (cwl == null) {
67 return null;
68 } else {
69 return cwl.getMainURL();
70 }
71 }
72
73 public static void addToWatchList(Context context, URL url) {
74 addToWatchList(context, url, false);
75 }
76
77 public static void addToWatchList(Context context, URL url, boolean createCWL) {
78 ConfigurationWatchList cwl = getConfigurationWatchList(context);
79 if(cwl == null) {
80 if(createCWL && ConfigurationWatchList.isWatchableProtocol(url)) {
81 cwl = registerNewConfigurationWatchListWithContext(context);
82 } else {
83 addWarn(context, "Null ConfigurationWatchList. Cannot add " + url);
84 return;
85 }
86 }
87
88 addInfo(context, "Adding [" + url + "] to configuration watch list.");
89 cwl.addToWatchList(url);
90
91 }
92
93 private static ConfigurationWatchList registerNewConfigurationWatchListWithContext(Context context) {
94 ConfigurationWatchList cwl = new ConfigurationWatchList();
95 cwl.setContext(context);
96 context.putObject(CoreConstants.CONFIGURATION_WATCH_LIST, cwl);
97 return cwl;
98 }
99
100 public static ConfigurationWatchList getConfigurationWatchList(Context context) {
101 return (ConfigurationWatchList) context.getObject(CoreConstants.CONFIGURATION_WATCH_LIST);
102 }
103
104 static void addStatus(Context context, Status s) {
105 if (context == null) {
106 System.out.println("Null context in " + ConfigurationWatchList.class.getName());
107 return;
108 }
109 StatusManager sm = context.getStatusManager();
110 if (sm == null)
111 return;
112 sm.add(s);
113 }
114
115 static void addInfo(Context context, String msg) {
116 addStatus(context, new InfoStatus(msg, ORIGIN));
117 }
118
119 static void addWarn(Context context, String msg) {
120 addStatus(context, new WarnStatus(msg, ORIGIN));
121 }
122 }