package com.eactive.eai.rms.common.interceptor.dynamicLog;

import ch.qos.logback.core.FileAppender;

public class DynamicLogContext {
	private static final ThreadLocal<ThreadObject> local = new ThreadLocal<ThreadObject>();

	public static String getKey() {
		ThreadObject obj = local.get();
		if (obj == null)
			return null;

		return obj.getKey();
	}

	public static void setKey(String key) {
		ThreadObject obj = local.get();
		if (obj == null) {
			obj = new ThreadObject();
		}
		obj.setKey(key);
		local.set(obj);
	}

	public static FileAppender getAppender() {
		ThreadObject obj = local.get();
		if (obj == null)
			return null;

		return obj.getAppender();
	}

	public static void setAppender(FileAppender appender) {
		ThreadObject obj = local.get();
		if (obj == null) {
			obj = new ThreadObject();
		}
		obj.setAppender(appender);
		local.set(obj);
	}

	public static void set(String key, FileAppender appender) {
		ThreadObject obj = local.get();
		if (obj == null) {
			obj = new ThreadObject();
		}
		obj.setKey(key);
		obj.setAppender(appender);
		local.set(obj);
	}

	public static void remove() {
		local.remove();
	}

}

class ThreadObject {
	private FileAppender appender;
	private String key;

	public ThreadObject() {
		super();
	}

	public ThreadObject(FileAppender appender, String key) {
		super();
		this.appender = appender;
		this.key = key;
	}

	public FileAppender getAppender() {
		return appender;
	}

	public void setAppender(FileAppender appender) {
		this.appender = appender;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

}
