KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > slf4j > MDC


1 /*
2  * Copyright (c) 2004-2007 QOS.ch
3  * All rights reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24
25 package org.slf4j;
26
27 import org.slf4j.helpers.Util;
28 import org.slf4j.impl.StaticMDCBinder;
29 import org.slf4j.spi.MDCAdapter;
30
31 /**
32  * This class hides and serves as a substitute for the underlying logging
33  * system's MDC implementation.
34  *
35  * <p>
36  * If the underlying logging system offers MDC
37  * functionality, then SLF4J's MDC, i.e. this class, will delegate to
38  * the underlying system's MDC. Note that at this time, only two logging
39  * systems, namely log4j and logback, offer MDC functionality. If the
40  * undelying system does not support MDC, then SLF4J will silently
41  * drop MDC information.
42  *
43  * <p>
44  * Thus, as a SLF4J user, you can take advantage of MDC in
45  * the presence of log4j or logback, but without forcing
46  * log4j or logback as dependencies upon your users.
47  *
48  * <p>
49  * For more information on MDC please see the
50  * <a HREF="http://logback.qos.ch/manual/mdc.html">chapter on
51  * MDC</a> in the logback manual.
52  *
53  * <p>
54  * Please note that all methods in this class are static.
55  *
56  * @author Ceki G&uuml;lc&uuml;
57  * @since 1.4.1
58  */

59 public class MDC {
60
61   static final String JavaDoc NULL_MDCA_URL = "http://www.slf4j.org/codes.html#null_MDCA";
62   static final String JavaDoc NO_STATIC_MDC_BINDER_URL = "http://www.slf4j.org/codes.html#no_static_mdc_binder";
63   static MDCAdapter mdcAdapter;
64
65   private MDC() {
66   }
67
68   static {
69     try {
70       mdcAdapter = StaticMDCBinder.SINGLETON.getMDCA();
71     } catch(NoClassDefFoundError JavaDoc ncde) {
72       String JavaDoc msg = ncde.getMessage();
73       if(msg != null && msg.indexOf("org/slf4j/impl/StaticMDCBinder") != -1) {
74         Util.reportFailure("Failed to load class \"org.slf4j.impl.StaticMDCBinder\".");
75         Util.reportFailure("See "+NO_STATIC_MDC_BINDER_URL+" for further details.");
76         
77       }
78       throw ncde;
79     } catch (Exception JavaDoc e) {
80       // we should never get here
81
Util.reportFailure("Could not bind with an instance of class ["
82           + StaticMDCBinder.SINGLETON.getMDCAdapterClassStr() + "]", e);
83     }
84   }
85
86   
87   /**
88    * Put a context value (the <code>val</code> parameter) as identified with
89    * the <code>key</code> parameter into the current thread's context map. This
90    * method delegates all work to the MDC of the underlying logging system.
91    */

92   public static void put(String JavaDoc key, String JavaDoc val) {
93     if(mdcAdapter == null) {
94       throw new IllegalStateException JavaDoc("MDCAdapter cannot be null. See also "+NULL_MDCA_URL);
95     }
96     mdcAdapter.put(key, val);
97   }
98
99   /**
100    * Get the context identified by the <code>key</code> parameter.
101    * This method delegates all work to the MDC of the underlying logging system.
102    *
103    * @return the string value identified by the <code>key</code> parameter.
104    */

105   public static String JavaDoc get(String JavaDoc key) {
106     if(mdcAdapter == null) {
107       throw new IllegalStateException JavaDoc("MDCAdapter cannot be null. See also "+NULL_MDCA_URL);
108     }
109     return mdcAdapter.get(key);
110   }
111
112   /**
113    * Remove the the context identified by the <code>key</code> parameter using
114    * the underlying system's MDC implementation.
115    */

116   public static void remove(String JavaDoc key) {
117     if(mdcAdapter == null) {
118       throw new IllegalStateException JavaDoc("MDCAdapter cannot be null. See also "+NULL_MDCA_URL);
119     }
120     mdcAdapter.remove(key);
121   }
122
123   /**
124    * Clear all entries in the MDC of the underlying implementation.
125    */

126   public static void clear() {
127     if(mdcAdapter == null) {
128       throw new IllegalStateException JavaDoc("MDCAdapter cannot be null. See also "+NULL_MDCA_URL);
129     }
130     mdcAdapter.clear();
131   }
132   
133   /**
134    * Returns the MDCAdapter instance currently in use.
135    *
136    * @return the MDcAdapter instance currently in use.
137    * @since 1.4.2
138    */

139   public static MDCAdapter getMDCAdapter() {
140     return mdcAdapter;
141   }
142 }
Popular Tags