package com.redant.core;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;

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

/**
 * DataHolder 将请求消息对象存放在静态ThreadLocal的实例变量中
 * @author gris.wang
 * @since 2017-10-20
 */
public class DataHolder {
	
	private static final ThreadLocal<Map<String,Object>> LOCAL_DATA = new ThreadLocal<Map<String,Object>>();


	public enum HolderType{
		/**
		 * request
		 */
		REQUEST("request"),
		/**
		 * response
		 */
		RESPONSE("response"),
		/**
		 * context
		 */
		CONTEXT("context"),
		/**
		 * forceClose
		 */
		FORCE_CLOSE("forceClose"),
		;

		private String type;

		HolderType(String type){
			this.type = type;
		}

		public String getType() {
			return type;
		}
	}

	//===================================


	public static void store(HolderType holderType,Object value){
		Map<String,Object> map = LOCAL_DATA.get();
		if(map==null){
			map = new HashMap<String,Object>();
		}
		map.put(holderType.getType(),value);
		LOCAL_DATA.set(map);
	}

	public static Object get(HolderType holderType){
		return LOCAL_DATA.get()!=null?LOCAL_DATA.get().get(holderType.getType()):null;
	}

	public static HttpRequest getHttpRequest(){
		return (HttpRequest)DataHolder.get(DataHolder.HolderType.REQUEST);
	}

	public static FullHttpResponse getHttpResponse(){
		return (FullHttpResponse)DataHolder.get(DataHolder.HolderType.RESPONSE);
	}

	public static boolean getForceClose(){
		return (boolean)DataHolder.get(DataHolder.HolderType.FORCE_CLOSE);
	}

	public static ChannelHandlerContext getContext(){
		return (ChannelHandlerContext)DataHolder.get(DataHolder.HolderType.CONTEXT);
	}

	public static void remove(HolderType holderType){
		Map<String,Object> map = LOCAL_DATA.get();
		if(map==null){
			map = new HashMap<String,Object>();
		}
		map.remove(holderType.getType());
		LOCAL_DATA.set(map);
	}

	public static void removeAll(){
		LOCAL_DATA.remove();
	}

}

@@ -187,39 +187,39 @@
 tty.
-handler.codec.http.HttpResponse
+util.concurrent.FastThreadLocal
 ;%0A%0Ai
@@ -390,11 +390,66 @@
 r %7B%0A
-%09
 %0A%09
+/**%0A%09 * %E4%BD%BF%E7%94%A8FastThreadLocal%E6%9B%BF%E4%BB%A3JDK%E8%87%AA%E5%B8%A6%E7%9A%84ThreadLocal%E4%BB%A5%E6%8F%90%E5%8D%87%E5%B9%B6%E5%8F%91%E6%80%A7%E8%83%BD%0A%09 * 
 priv
@@ -549,16 +549,135 @@
 ct%3E%3E();%0A
+%09 */%0A%09private static final FastThreadLocal%3CMap%3CString,Object%3E%3E LOCAL_DATA = new FastThreadLocal%3CMap%3CString,Object%3E%3E();%0A
 %0A%0A%09publi

Use FastThreadLocal instead of ThreadLocal