LOG4J2-1349 system properties disableThreadContextMap and disableThreadContext must work regardless of ThreadContextMap implementation
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/996d54b1 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/996d54b1 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/996d54b1 Branch: refs/heads/LOG4J2-1349-gcfree-threadcontext Commit: 996d54b18d8773074db0c0eb560d369cbcbda88a Parents: 70807a5 Author: rpopma <[email protected]> Authored: Wed Aug 31 22:53:37 2016 +0900 Committer: rpopma <[email protected]> Committed: Wed Aug 31 22:53:37 2016 +0900 ---------------------------------------------------------------------- .../org/apache/logging/log4j/ThreadContext.java | 4 ++ .../logging/log4j/spi/NoOpThreadContextMap.java | 65 ++++++++++++++++++++ 2 files changed, 69 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/996d54b1/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java b/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java index 0ca13ab..bc01bf8 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java @@ -31,6 +31,7 @@ import org.apache.logging.log4j.spi.CopyOnWriteSortedArrayThreadContextMap; import org.apache.logging.log4j.spi.DefaultThreadContextMap; import org.apache.logging.log4j.spi.DefaultThreadContextStack; import org.apache.logging.log4j.spi.GarbageFreeSortedArrayThreadContextMap; +import org.apache.logging.log4j.spi.NoOpThreadContextMap; import org.apache.logging.log4j.spi.Provider; import org.apache.logging.log4j.spi.ThreadContextMap; import org.apache.logging.log4j.spi.ThreadContextMap2; @@ -259,6 +260,9 @@ public final class ThreadContext { } private static ThreadContextMap createThreadContextMap(final boolean doUseMap) { + if (!doUseMap) { + return new NoOpThreadContextMap(); + } if (Constants.ENABLE_THREADLOCALS) { if (PropertiesUtil.getProperties().getBooleanProperty(GC_FREE_THREAD_CONTEXT_KEY)) { return new GarbageFreeSortedArrayThreadContextMap(); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/996d54b1/log4j-api/src/main/java/org/apache/logging/log4j/spi/NoOpThreadContextMap.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/NoOpThreadContextMap.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/NoOpThreadContextMap.java new file mode 100644 index 0000000..a9f8a57 --- /dev/null +++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/NoOpThreadContextMap.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.logging.log4j.spi; + +import java.util.HashMap; +import java.util.Map; + +/** + * {@code ThreadContextMap} implementation used when either of system properties {@code disableThreadContextMap} or . + * {@code disableThreadContext} is {@code true}. This implementation does nothing. + * + * @since 2.7 + */ +class NoOpThreadContextMap implements ThreadContextMap { + @Override + public void clear() { + } + + @Override + public boolean containsKey(final String key) { + return false; + } + + @Override + public String get(final String key) { + return null; + } + + @Override + public Map<String, String> getCopy() { + return new HashMap<String, String>(); + } + + @Override + public Map<String, String> getImmutableMapOrNull() { + return null; + } + + @Override + public boolean isEmpty() { + return true; + } + + @Override + public void put(final String key, final String value) { + } + + @Override + public void remove(final String key) { + } +}
