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

com.aggrepoint.utils.ThreadContext Maven / Gradle / Ivy

There is a newer version: 1.0.11
Show newest version
package com.aggrepoint.utils;

import java.util.Hashtable;
import java.util.Stack;

/**
 * @author: Yang Jiang Ming
 */
public class ThreadContext {
	private static ThreadLocal> threaContext = new ThreadLocal>() {
		protected synchronized Hashtable initialValue() {
			return new Hashtable();
		}
	};

	private static ThreadLocal> threaContextInheritable = new InheritableThreadLocal>() {
		protected synchronized Hashtable initialValue() {
			return new Hashtable();
		}

		@SuppressWarnings("unchecked")
		protected synchronized Hashtable childValue(
				Hashtable parentValue) {
			Hashtable obj = (Hashtable) parentValue
					.clone();
			return obj;
		}
	};

	private static class ThreadStack extends Stack {
		private static final long serialVersionUID = 1L;
	}

	public static void setAttribute(String name, Object value,
			boolean accessBySub) {
		if (value != null) {
			threaContext.get().put(name, value);

			if (accessBySub)
				threaContextInheritable.get().put(name, value);
		} else {
			threaContext.get().remove(name);

			if (accessBySub)
				threaContextInheritable.get().remove(name);
		}
	}

	public static void clear(boolean clearSubAsWell) {
		threaContext.get().clear();
		if (clearSubAsWell)
			threaContextInheritable.get().clear();
	}

	public static void removeAttribute(String name, boolean accessBySub) {
		threaContext.get().remove(name);

		if (accessBySub)
			threaContextInheritable.get().remove(name);
	}

	@SuppressWarnings("unchecked")
	public static void pushAttribute(String name, Object value,
			boolean accessBySub) {
		if (value == null)
			return;

		Object obj;
		ThreadStack stack;

		obj = threaContext.get().get(name);
		if (obj == null || !(obj instanceof ThreadStack)) {
			stack = new ThreadStack();
			if (obj != null)
				stack.push(obj);
			stack.push(value);
			threaContext.get().put(name, stack);
		} else {
			stack = (ThreadStack) obj;
			stack.push(value);
		}

		if (accessBySub) {
			obj = threaContextInheritable.get().get(name);
			if (obj == null || !(obj instanceof ThreadStack)) {
				stack = new ThreadStack();
				if (obj != null)
					stack.push(obj);
				stack.push(value);
				threaContextInheritable.get().put(name, stack);
			} else {
				stack = (ThreadStack) obj;
				stack.push(value);
			}
		}
	}

	@SuppressWarnings("unchecked")
	public static Object popAttribute(String name, boolean accessBySub) {
		Object val = null;
		ThreadStack stack;

		Object obj = threaContext.get().get(name);
		if (obj != null) {
			if (!(obj instanceof ThreadStack)) {
				val = obj;
				threaContext.get().remove(obj);
			} else {
				stack = (ThreadStack) obj;
				if (stack.size() > 0)
					val = stack.pop();
				if (stack.size() == 0)
					threaContext.get().remove(obj);
			}
		}

		if (accessBySub) {
			obj = threaContextInheritable.get().get(name);
			if (obj != null) {
				if (!(obj instanceof ThreadStack)) {
					if (val == null)
						val = obj;
					threaContextInheritable.get().remove(obj);
				} else {
					stack = (ThreadStack) obj;
					if (stack.size() > 0)
						if (val == null)
							val = stack.pop();
						else
							stack.pop();
					if (stack.size() == 0)
						threaContextInheritable.get().remove(obj);
				}
			}
		}

		return val;
	}

	public static void setAttribute(String name, Object value) {
		setAttribute(name, value, false);
	}

	public static void removeAttribute(String name) {
		removeAttribute(name, false);
	}

	public static Object popAttribute(String name) {
		return popAttribute(name, false);
	}

	@SuppressWarnings("unchecked")
	public static Object getAttribute(String name, boolean peekTrace,
			boolean traceUp) {
		Object obj = threaContext.get().get(name);

		if (obj == null && traceUp)
			obj = threaContextInheritable.get().get(name);

		if (obj != null && peekTrace && obj instanceof ThreadStack) {
			ThreadStack stack = (ThreadStack) obj;
			if (stack.size() == 0)
				return null;
			return stack.peek();
		}

		return obj;
	}

	public static Object getAttribute(String name, boolean traceUp) {
		return getAttribute(name, true, traceUp);
	}

	public static Object getAttribute(String name) {
		return getAttribute(name, false);
	}
}