/**
* Alipay.com Inc.
* Copyright (c) 2004-2012 All Rights Reserved.
*/
package com.alipay.zdal.client.util;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
/**
* ThreadLocal Context
* @author ����
* @version $Id: ThreadLocalMap.java, v 0.1 2014-1-6 ����05:15:36 Exp $
*/
public class ThreadLocalMap {
private static final Logger log = Logger
.getLogger(ThreadLocalMap.class);
protected final static ThreadLocal<Map<Object, Object>> 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<Map<Object, Object>> {
protected Map<Object, Object> initialValue() {
return new HashMap<Object, Object>() {
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);
}
};
}
}
/**
* ȡ��thread context Map��ʵ����
*
* @return thread context Map��ʵ��
*/
protected static Map<Object, Object> getContextMap() {
return (Map<Object, Object>) threadContext.get();
}
/**
* �����߳����б�holdס�Ķ����Ա����ã�
*/
public static void reset() {
getContextMap().clear();
}
}