Created
April 30, 2013 05:12
-
-
Save debop/5486704 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package kr.debop4j.core; | |
| import com.google.common.collect.Maps; | |
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| import java.util.HashMap; | |
| import static kr.debop4j.core.Guard.shouldNotBeNull; | |
| /** | |
| * Thread Context 별로 격리된 저장소를 제공합니다. | |
| * | |
| * @author [email protected] | |
| * @since 12. 9. 12 | |
| */ | |
| public class Local { | |
| private static final Logger log = LoggerFactory.getLogger(Local.class); | |
| private Local() { } | |
| private static ThreadLocal<HashMap> threadLocal = | |
| new ThreadLocal<HashMap>() { | |
| @Override | |
| public HashMap initialValue() { | |
| log.debug("현 ThreadContext 에 저장소를 생성합니다..."); | |
| return Maps.newLinkedHashMap(); | |
| } | |
| }; | |
| private static HashMap getMap() { | |
| return threadLocal.get(); | |
| } | |
| public static Object get(Object key) { | |
| shouldNotBeNull(key, "key"); | |
| return threadLocal.get().get(key); | |
| } | |
| @SuppressWarnings("unchecked") | |
| public static <T> T get(Object key, Class<T> clazz) { | |
| assert key != null; | |
| return (T) threadLocal.get().get(key); | |
| } | |
| @SuppressWarnings("unchecked") | |
| public static void put(Object key, Object value) { | |
| assert key != null; | |
| if (log.isTraceEnabled()) | |
| log.trace("Local 저장소에 key=[{}], value=[{}]를 저장합니다.", key, value); | |
| threadLocal.get().put(key, value); | |
| } | |
| public static void clear() { | |
| threadLocal.get().clear(); | |
| log.debug("Local 저장소의 모든 정보를 삭제했습니다."); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment