001: /*
002: * Copyright (c) 2004-2008 QOS.ch
003: * All rights reserved.
004: *
005: * Permission is hereby granted, free of charge, to any person obtaining
006: * a copy of this software and associated documentation files (the
007: * "Software"), to deal in the Software without restriction, including
008: * without limitation the rights to use, copy, modify, merge, publish,
009: * distribute, sublicense, and/or sell copies of the Software, and to
010: * permit persons to whom the Software is furnished to do so, subject to
011: * the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be
014: * included in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
017: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
019: * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020: * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021: * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
022: * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.slf4j.helpers;
025:
026: import org.slf4j.spi.MDCAdapter;
027:
028: import java.util.HashMap;
029: import java.util.Set;
030:
031: /**
032: * Basic MDC implementation, which can be used with logging systems that
033: * lack out-of-the-box MDC support.
034: *
035: * This code is largely based on logback's <a href="http://svn.qos.ch/viewvc/logback/trunk/logback-classic/src/main/java/org/slf4j/impl/LogbackMDCAdapter.java">
036: * LogbackMDCAdapter</a>.
037: *
038: * @author Ceki Gulcu
039: * @author Maarten Bosteels
040: *
041: * @since 1.5.0
042: */
043: public class BasicMDCAdapter implements MDCAdapter {
044:
045: private InheritableThreadLocal inheritableThreadLocal = new InheritableThreadLocal();
046:
047: /**
048: * Put a context value (the <code>val</code> parameter) as identified with
049: * the <code>key</code> parameter into the current thread's context map.
050: * Note that contrary to log4j, the <code>val</code> parameter can be null.
051: *
052: * <p>
053: * If the current thread does not have a context map it is created as a side
054: * effect of this call.
055:
056: * @throws IllegalArgumentException
057: * in case the "key" parameter is null
058: */
059: public void put(String key, String val) {
060: if (key == null) {
061: throw new IllegalArgumentException("key cannot be null");
062: }
063: HashMap map = (HashMap) inheritableThreadLocal.get();
064: if (map == null) {
065: map = new HashMap();
066: inheritableThreadLocal.set(map);
067: }
068: map.put(key, val);
069: }
070:
071: /**
072: * Get the context identified by the <code>key</code> parameter.
073: */
074: public String get(String key) {
075: HashMap hashMap = (HashMap) inheritableThreadLocal.get();
076: if ((hashMap != null) && (key != null)) {
077: return (String) hashMap.get(key);
078: } else {
079: return null;
080: }
081: }
082:
083: /**
084: * Remove the the context identified by the <code>key</code> parameter.
085: */
086: public void remove(String key) {
087: HashMap map = (HashMap) inheritableThreadLocal.get();
088: if (map != null) {
089: map.remove(key);
090: }
091: }
092:
093: /**
094: * Clear all entries in the MDC.
095: */
096: public void clear() {
097: HashMap hashMap = (HashMap) inheritableThreadLocal.get();
098: if (hashMap != null) {
099: hashMap.clear();
100: inheritableThreadLocal.remove();
101: }
102: }
103:
104: /**
105: * Returns the keys in the MDC as a {@link Set} of {@link String}s
106: * The returned value can be null.
107: * @return the keys in the MDC
108: */
109: public Set getKeys() {
110: HashMap hashMap = (HashMap) inheritableThreadLocal.get();
111: if (hashMap != null) {
112: return hashMap.keySet();
113: } else {
114: return null;
115: }
116: }
117:
118: }
|