Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id A984C200B63 for ; Mon, 15 Aug 2016 18:24:22 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id A8061160A8A; Mon, 15 Aug 2016 16:24:22 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id A4925160AA7 for ; Mon, 15 Aug 2016 18:24:21 +0200 (CEST) Received: (qmail 49968 invoked by uid 500); 15 Aug 2016 16:24:20 -0000 Mailing-List: contact log4j-dev-help@logging.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Log4J Developers List" Reply-To: "Log4J Developers List" Delivered-To: mailing list log4j-dev@logging.apache.org Received: (qmail 49957 invoked by uid 99); 15 Aug 2016 16:24:20 -0000 Received: from arcas.apache.org (HELO arcas) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 15 Aug 2016 16:24:20 +0000 Received: from arcas.apache.org (localhost [127.0.0.1]) by arcas (Postfix) with ESMTP id A84012C02A5 for ; Mon, 15 Aug 2016 16:24:20 +0000 (UTC) Date: Mon, 15 Aug 2016 16:24:20 +0000 (UTC) From: "Gary Gregory (JIRA)" To: log4j-dev@logging.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Updated] (LOG4J2-1517) Add ThreadContext.setContext(Map) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 archived-at: Mon, 15 Aug 2016 16:24:22 -0000 [ https://issues.apache.org/jira/browse/LOG4J2-1517?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ] Gary Gregory updated LOG4J2-1517: --------------------------------- Description: Add {{Add ThreadContext.setContext(Map)}}. My immediate goal is to be able to build a JUnit Rule to save and restore the thread context around each unit test method and/or a given class. I have (not committed yet): {code:java} /** * Restores the ThreadContext to it's initial map and stack values after a JUnit test. */ public class ThreadContextRule extends ExternalResource { private final boolean restoreMap; private final boolean restoreStack; private ThreadContextHolder threadContextHolder; /** * Constructs an instance initialized to restore the stack and map. */ public ThreadContextRule() { this(true, true); } /** * Constructs an instance initialized to restore the given structures. * * @param restoreMap * Whether to restore the thread context map. * @param restoreStack * Whether to restore the thread context stack. */ public ThreadContextRule(final boolean restoreMap, final boolean restoreStack) { super(); this.restoreMap = restoreMap; this.restoreStack = restoreStack; } @Override protected void after() { if (threadContextHolder != null) { threadContextHolder.restore(); } } @Override protected void before() throws Throwable { threadContextHolder = new ThreadContextHolder(restoreMap, restoreStack); if (restoreMap) { ThreadContext.clearMap(); } if (restoreStack) { ThreadContext.clearStack(); } } } {code} and: {code:java} /** * Holds an immutable copy of the ThreadContext stack and map. * * @since 2.7 */ public class ThreadContextHolder { private final Map immutableContext; private final ContextStack immutableStack; private final boolean restoreContext; private final boolean restoreStack; /** * Constructs a new holder initialized with an immutable copy of the ThreadContext stack and map. * @param restoreContext * @param restoreStack */ public ThreadContextHolder(final boolean restoreContext, final boolean restoreStack) { this.restoreContext = restoreContext; this.restoreStack = restoreStack; this.immutableContext = restoreContext ? ThreadContext.getImmutableContext() : null; this.immutableStack = restoreStack ? ThreadContext.getImmutableStack() : null; } /** * Restores the ThreadContext stack and map based on the values saved in the constructor. */ public void restore() { if (restoreStack) { ThreadContext.setStack(immutableStack); } if (restoreContext) { ThreadContext.setContext(immutableContext); } } } {code} and in ThreadContext: {code:java} /** * Sets this thread's context. * * @param map The map to use. * @since 2.7 */ public static void setContext(final Map map) { if (map.isEmpty() || !useMap) { return; } contextMap.clear(); contextMap.putAll(map); } {code} For convenience: {code:java} /** * Restores the ThreadContext to it's initial map values after a JUnit test. */ public class ThreadContextMapRule extends ThreadContextRule { /** * Constructs an initialized instance. */ public ThreadContextMapRule() { super(true, false); } } /** * Restores the ThreadContext to it's initial stack values after a JUnit test. */ public class ThreadContextStackRule extends ThreadContextRule { /** * Constructs an initialized instance. */ public ThreadContextStackRule() { super(false, true); } } {code} was: Add {{Add ThreadContext.setContext(Map)}}. My immediate goal is to be able to build a JUnit Rule to save and restore the thread context around each unit test method and/or a given class. I have (not committed yet): {code:java} /** * Restores the ThreadContext to it's initial map and stack values after a JUnit test. */ public class ThreadContextRule extends ExternalResource { private final boolean restoreMap; private final boolean restoreStack; private ThreadContextHolder threadContextHolder; /** * Constructs an instance initialized to restore the stack and map. */ public ThreadContextRule() { this(true, true); } /** * Constructs an instance initialized to restore the given structures. * * @param restoreMap * Whether to restore the thread context map. * @param restoreStack * Whether to restore the thread context stack. */ public ThreadContextRule(final boolean restoreMap, final boolean restoreStack) { super(); this.restoreMap = restoreMap; this.restoreStack = restoreStack; } @Override protected void after() { if (threadContextHolder != null) { threadContextHolder.restore(); } } @Override protected void before() throws Throwable { threadContextHolder = new ThreadContextHolder(restoreMap, restoreStack); if (restoreMap) { ThreadContext.clearMap(); } if (restoreStack) { ThreadContext.clearStack(); } } } {code} and: {code:java} /** * Holds an immutable copy of the ThreadContext stack and map. * * @since 2.7 */ public class ThreadContextHolder { private final Map immutableContext; private final ContextStack immutableStack; private final boolean restoreContext; private final boolean restoreStack; /** * Constructs a new holder initialized with an immutable copy of the ThreadContext stack and map. * @param restoreContext * @param restoreStack */ public ThreadContextHolder(final boolean restoreContext, final boolean restoreStack) { this.restoreContext = restoreContext; this.restoreStack = restoreStack; this.immutableContext = restoreContext ? ThreadContext.getImmutableContext() : null; this.immutableStack = restoreStack ? ThreadContext.getImmutableStack() : null; } /** * Restores the ThreadContext stack and map based on the values saved in the constructor. */ public void restore() { if (restoreStack) { ThreadContext.setStack(immutableStack); } if (restoreContext) { ThreadContext.setContext(immutableContext); } } } {code} and in ThreadContext: {code:java} /** * Sets this thread's context. * * @param map The map to use. * @since 2.7 */ public static void setContext(final Map map) { if (map.isEmpty() || !useMap) { return; } contextMap.clear(); contextMap.putAll(map); } {code} > Add ThreadContext.setContext(Map) > ------------------------------------------------- > > Key: LOG4J2-1517 > URL: https://issues.apache.org/jira/browse/LOG4J2-1517 > Project: Log4j 2 > Issue Type: New Feature > Reporter: Gary Gregory > Assignee: Gary Gregory > Fix For: 2.7 > > > Add {{Add ThreadContext.setContext(Map)}}. > My immediate goal is to be able to build a JUnit Rule to save and restore the thread context around each unit test method and/or a given class. > I have (not committed yet): > {code:java} > /** > * Restores the ThreadContext to it's initial map and stack values after a JUnit test. > */ > public class ThreadContextRule extends ExternalResource { > private final boolean restoreMap; > private final boolean restoreStack; > private ThreadContextHolder threadContextHolder; > /** > * Constructs an instance initialized to restore the stack and map. > */ > public ThreadContextRule() { > this(true, true); > } > /** > * Constructs an instance initialized to restore the given structures. > * > * @param restoreMap > * Whether to restore the thread context map. > * @param restoreStack > * Whether to restore the thread context stack. > */ > public ThreadContextRule(final boolean restoreMap, final boolean restoreStack) { > super(); > this.restoreMap = restoreMap; > this.restoreStack = restoreStack; > } > @Override > protected void after() { > if (threadContextHolder != null) { > threadContextHolder.restore(); > } > } > @Override > protected void before() throws Throwable { > threadContextHolder = new ThreadContextHolder(restoreMap, restoreStack); > if (restoreMap) { > ThreadContext.clearMap(); > } > if (restoreStack) { > ThreadContext.clearStack(); > } > } > } > {code} > and: > {code:java} > /** > * Holds an immutable copy of the ThreadContext stack and map. > * > * @since 2.7 > */ > public class ThreadContextHolder { > private final Map immutableContext; > private final ContextStack immutableStack; > private final boolean restoreContext; > private final boolean restoreStack; > /** > * Constructs a new holder initialized with an immutable copy of the ThreadContext stack and map. > * @param restoreContext > * @param restoreStack > */ > public ThreadContextHolder(final boolean restoreContext, final boolean restoreStack) { > this.restoreContext = restoreContext; > this.restoreStack = restoreStack; > this.immutableContext = restoreContext ? ThreadContext.getImmutableContext() : null; > this.immutableStack = restoreStack ? ThreadContext.getImmutableStack() : null; > } > /** > * Restores the ThreadContext stack and map based on the values saved in the constructor. > */ > public void restore() { > if (restoreStack) { > ThreadContext.setStack(immutableStack); > } > if (restoreContext) { > ThreadContext.setContext(immutableContext); > } > } > } > {code} > and in ThreadContext: > {code:java} > /** > * Sets this thread's context. > * > * @param map The map to use. > * @since 2.7 > */ > public static void setContext(final Map map) { > if (map.isEmpty() || !useMap) { > return; > } > contextMap.clear(); > contextMap.putAll(map); > } > {code} > For convenience: > {code:java} > /** > * Restores the ThreadContext to it's initial map values after a JUnit test. > */ > public class ThreadContextMapRule extends ThreadContextRule { > /** > * Constructs an initialized instance. > */ > public ThreadContextMapRule() { > super(true, false); > } > } > /** > * Restores the ThreadContext to it's initial stack values after a JUnit test. > */ > public class ThreadContextStackRule extends ThreadContextRule { > /** > * Constructs an initialized instance. > */ > public ThreadContextStackRule() { > super(false, true); > } > } > {code} -- This message was sent by Atlassian JIRA (v6.3.4#6332) --------------------------------------------------------------------- To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org For additional commands, e-mail: log4j-dev-help@logging.apache.org