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      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          // setConfigurationWatchListResetFlag(context, true);
48          cwl.setMainURL(url);
49      }
50  
51      /**
52       * Returns true if there are watchable files, false otherwise.
53       * @return true if there are watchable files,  false otherwise.
54       * @since 1.5.8
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 }