View Javadoc

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