All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.javaclub.cdl.client.util.ThreadLocalMap Maven / Gradle / Ivy

There is a newer version: 2.3.9
Show newest version
package com.github.javaclub.cdl.client.util;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ThreadLocalMap {
	private static final Logger log = LoggerFactory.getLogger(ThreadLocalMap.class);
	protected final static ThreadLocal> threadContext = new MapThreadLocal();

	private ThreadLocalMap() {
	};

	public static void put(Object key, Object value) {
		getContextMap().put(key, value);
	}

	public static Object remove(Object key) {
		return getContextMap().remove(key);
	}

	public static Object get(Object key) {
		return getContextMap().get(key);
	}

	public static boolean containsKey(Object key) {
		return getContextMap().containsKey(key);
	}

	private static class MapThreadLocal extends ThreadLocal> {
		protected Map initialValue() {
			return new HashMap() {

				private static final long serialVersionUID = 3637958959138295593L;

				public Object put(Object key, Object value) {
					if (log.isDebugEnabled()) {
						if (containsKey(key)) {
							log.debug("Overwritten attribute to thread context: " + key + " = " + value);
						} else {
							log.debug("Added attribute to thread context: " + key + " = " + value);
						}
					}

					return super.put(key, value);
				}
			};
		}
	}

	/**
     *
     */
	protected static Map getContextMap() {
		return (Map) threadContext.get();
	}

	/**
     */

	public static void reset() {
		getContextMap().clear();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy