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

com.sta.mlogger.MThreadContext Maven / Gradle / Ivy

Go to download

Tool to convert CSV and XLS to XML, to transform XML and to convert XML to CSV, HTML, other text files, PDF etc., useful as command line tool and integrated in other projects.

There is a newer version: 3.119
Show newest version

package com.sta.mlogger;

import java.util.Hashtable;

/**
 * 

Name: MThreadContext

*

Description: Thread-Context zur Verwendung in Verbindung mit MDC-Feldern und -Werten. *

*

Comment: ... *

*

Copyright: Copyright (c) 2019

*

Company: >StA-Soft<

* @author StA * @version 1.0 */ public class MThreadContext { /** * Hash-Tabelle mit Mapping von Thread auf Context. */ private static Hashtable> statThreadMaps = new Hashtable<>(); //=========================================================================== /** * Schlüssel für aktuellen Thread ermitteln. * @return Schlüssel für aktuellen Thread */ private static Object getKey4CurrentThread() { return Thread.currentThread().getName(); } /** * Thread-Context für aktuellen Thread ermitteln. * @return Thread-Context für aktuellen Thread, kann null sein */ public static Hashtable getThreadContext() { return statThreadMaps.get(getKey4CurrentThread()); } /** * Prüfen, ob Thread-Context für aktuellen Thread vorhanden, falls nicht: anlegen. * @return Thread-Context für aktuellen Thread, ist nicht null */ public static Hashtable chkThreadContext() { Object key = getKey4CurrentThread(); Hashtable ctx = statThreadMaps.get(key); if (ctx == null) { ctx = new Hashtable<>(); statThreadMaps.put(key, ctx); } return ctx; } /** * Thread-Context für aktuellen Thread ermitteln. */ public static void removeThreadContext() { statThreadMaps.remove(getKey4CurrentThread()); } //--------------------------------------------------------------------------- /** * Wert aus Thread-Context des aktuellen Threads ermitteln. * @param key Schlüssel * @return Wert */ public static Object getValue(String key) { Hashtable ctx = getThreadContext(); return ctx != null ? ctx.get(key) : null; } /** * Wert in Thread-Context des aktuellen Threads eintragen. * @param key Schlüssel * @param value Wert, darf nicht null sein */ public static void putValue(String key, Object value) { Hashtable ctx = chkThreadContext(); ctx.put(key, value); } /** * Wert aus Thread-Context des aktuellen Threads entfernen. * @param key Schlüssel */ public static void removeValue(String key) { Hashtable ctx = getThreadContext(); if (ctx != null) { ctx.remove(key); } } /** * Wert in Thread-Context des aktuellen Threads eintragen bzw. daraus entfernen. * @param key Schlüssel * @param value Wert (nicht null: eintragen, null: entfernen) */ public static void setValue(String key, Object value) { if (value != null) { putValue(key, value); } else { removeValue(key); } } /** * Alle Werte aus Thread-Context des aktuellen Threads entfernen. */ public static void removeAllValues() { Hashtable ctx = getThreadContext(); if (ctx != null) { ctx.clear(); } } //=========================================================================== /** * Dummy-Constructor. */ protected MThreadContext() { } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy