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

org.restexpress.plugin.statechange.StateContext Maven / Gradle / Ivy

/*
    Copyright 2014, Strategic Gains, Inc.

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

		http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
 */
package org.restexpress.plugin.statechange;

import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * The StateContext class maintains a Map of name/value pairs much like the
 * Log4j mapped diagnostic context (MDC). A StateContext,
 * is an instrument for passing augmentation data from different sources to
 * lower levels in the framework for each message received.
 * 

* The StateContext is managed on a per thread basis. A child * thread automatically inherits a copy of the state context of its parent. *

* The class requires JDK 1.6 or above. *

* The StateContext is cleaned up after each request by removing the values set by that request, * since all threads are pooled and re-used. This is accomplished via a call to * StateContext.clear() in a finally processor within the StateChangePlugin. * * @author toddf * @since Feb 17, 2014 */ public class StateContext { final static StateContext SC = new StateContext(); private ThreadLocal> tlm; private StateContext() { tlm = new ThreadLocal>(); } /** * Put a context value (the o parameter) as identified with the * key parameter into the current thread's context map. *

*

* If the current thread does not have a context map it is created as a side * effect. */ public static void put(String key, Object o) { SC._put(key, o); } /** * Get the context identified by the key parameter. *

*

* This method has no side effects. */ public static Object get(String key) { return SC._get(key); } /** * Remove the the context identified by the key parameter. */ public static void remove(String key) { SC._remove(key); } /** * Get the current thread's RequestContext as a Map. This method is intended * to only be used internally. */ public static Map getContext() { return SC._getContext(); } @SuppressWarnings("unchecked") public static Set> entrySet() { Map context = SC._getContext(); return (context == null ? Collections.EMPTY_SET : context.entrySet()); } public static Iterator> iterator() { return entrySet().iterator(); } /** * Remove all values from the thread's RequestContext. */ public static void clear() { SC._clear(); } // SECTION: MUTATORS - INTERNAL, PRIVATE private void _put(String key, Object o) { Map m = tlm.get(); if (m == null) { m = new Hashtable(); tlm.set(m); } m.put(key, o); } private Object _get(String key) { Map m = tlm.get(); return (m == null ? null : m.get(key)); } private void _remove(String key) { Map m = tlm.get(); if (m != null) { m.remove(key); if (m.isEmpty()) _clear(); } } private Map _getContext() { return tlm.get(); } private void _clear() { Map m = tlm.get(); if (m != null) { m.clear(); tlm.remove(); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy