You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rp...@apache.org on 2016/08/20 00:04:14 UTC

[5/6] logging-log4j2 git commit: LOG4J2-1516 moved putAll(Map) method into separate ThreadContextMap2 interface

LOG4J2-1516 moved putAll(Map) method into separate ThreadContextMap2 interface


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/6a233016
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/6a233016
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/6a233016

Branch: refs/heads/LOG4J2-1010&LOG4J2-1447-injectable-contextdata&better-datastructure
Commit: 6a23301660830fcd4728b1b952b607a9e1e26f65
Parents: 60649ef
Author: rpopma <rp...@apache.org>
Authored: Sat Aug 20 09:00:38 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Sat Aug 20 09:00:38 2016 +0900

----------------------------------------------------------------------
 .../org/apache/logging/log4j/ThreadContext.java | 55 +++++++++++---------
 .../log4j/spi/DefaultThreadContextMap.java      |  6 +--
 .../logging/log4j/spi/ThreadContextMap.java     | 13 +----
 .../logging/log4j/spi/ThreadContextMap2.java    | 40 ++++++++++++++
 .../org/apache/logging/slf4j/MDCContextMap.java |  7 +--
 src/changes/changes.xml                         |  4 +-
 6 files changed, 81 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6a233016/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 cea2e8d..c932746 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.DefaultThreadContextMap;
 import org.apache.logging.log4j.spi.DefaultThreadContextStack;
 import org.apache.logging.log4j.spi.Provider;
 import org.apache.logging.log4j.spi.ThreadContextMap;
+import org.apache.logging.log4j.spi.ThreadContextMap2;
 import org.apache.logging.log4j.spi.ThreadContextStack;
 import org.apache.logging.log4j.status.StatusLogger;
 import org.apache.logging.log4j.util.PropertiesUtil;
@@ -149,7 +150,7 @@ public final class ThreadContext {
 
     /**
      * An empty iterator. Since Java 1.7 added the Collections.emptyIterator() method, we have to make do.
-     * 
+     *
      * @param <E> the type of the empty iterator
      */
     private static class EmptyIterator<E> implements Iterator<E> {
@@ -260,7 +261,7 @@ public final class ThreadContext {
      * <p>
      * If the current thread does not have a context map it is created as a side effect.
      * </p>
-     * 
+     *
      * @param key The key name.
      * @param value The key value.
      */
@@ -278,7 +279,13 @@ public final class ThreadContext {
      * @since 2.7
      */
     public static void putAll(final Map<String, String> m) {
-        contextMap.putAll(m);
+        if (contextMap instanceof ThreadContextMap2) {
+            ((ThreadContextMap2) contextMap).putAll(m);
+        } else {
+            for (final Map.Entry<String, String> entry: m.entrySet()) {
+                contextMap.put(entry.getKey(), entry.getValue());
+            }
+        }
     }
 
     /**
@@ -287,7 +294,7 @@ public final class ThreadContext {
      * <p>
      * This method has no side effects.
      * </p>
-     * 
+     *
      * @param key The key to locate.
      * @return The value associated with the key or null.
      */
@@ -297,7 +304,7 @@ public final class ThreadContext {
 
     /**
      * Removes the context value identified by the <code>key</code> parameter.
-     * 
+     *
      * @param key The key to remove.
      */
     public static void remove(final String key) {
@@ -321,7 +328,7 @@ public final class ThreadContext {
 
     /**
      * Determines if the key is in the context.
-     * 
+     *
      * @param key The key to locate.
      * @return True if the key is in the context, false otherwise.
      */
@@ -331,7 +338,7 @@ public final class ThreadContext {
 
     /**
      * Returns a mutable copy of current thread's context Map.
-     * 
+     *
      * @return a mutable copy of the context.
      */
     public static Map<String, String> getContext() {
@@ -340,7 +347,7 @@ public final class ThreadContext {
 
     /**
      * Returns an immutable view of the current thread's context Map.
-     * 
+     *
      * @return An immutable view of the ThreadContext Map.
      */
     public static Map<String, String> getImmutableContext() {
@@ -350,7 +357,7 @@ public final class ThreadContext {
 
     /**
      * Returns true if the Map is empty.
-     * 
+     *
      * @return true if the Map is empty, false otherwise.
      */
     public static boolean isEmpty() {
@@ -366,7 +373,7 @@ public final class ThreadContext {
 
     /**
      * Returns a copy of this thread's stack.
-     * 
+     *
      * @return A copy of this thread's stack.
      */
     public static ContextStack cloneStack() {
@@ -375,7 +382,7 @@ public final class ThreadContext {
 
     /**
      * Gets an immutable copy of this current thread's context stack.
-     * 
+     *
      * @return an immutable copy of the ThreadContext stack.
      */
     public static ContextStack getImmutableStack() {
@@ -385,7 +392,7 @@ public final class ThreadContext {
 
     /**
      * Sets this thread's stack.
-     * 
+     *
      * @param stack The stack to use.
      */
     public static void setStack(final Collection<String> stack) {
@@ -398,7 +405,7 @@ public final class ThreadContext {
 
     /**
      * Gets the current nesting depth of this thread's stack.
-     * 
+     *
      * @return the number of items in the stack.
      *
      * @see #trim
@@ -498,13 +505,13 @@ public final class ThreadContext {
      * <p>
      * For example, the combination
      * </p>
-     * 
+     *
      * <pre>
      * void foo() {
      *     final int depth = ThreadContext.getDepth();
-     * 
+     *
      *     // ... complex sequence of calls
-     * 
+     *
      *     ThreadContext.trim(depth);
      * }
      * </pre>
@@ -527,7 +534,7 @@ public final class ThreadContext {
 
         /**
          * Returns the element at the top of the stack.
-         * 
+         *
          * @return The element at the top of the stack.
          * @throws java.util.NoSuchElementException if the stack is empty.
          */
@@ -535,42 +542,42 @@ public final class ThreadContext {
 
         /**
          * Returns the element at the top of the stack without removing it or null if the stack is empty.
-         * 
+         *
          * @return the element at the top of the stack or null if the stack is empty.
          */
         String peek();
 
         /**
          * Pushes an element onto the stack.
-         * 
+         *
          * @param message The element to add.
          */
         void push(String message);
 
         /**
          * Returns the number of elements in the stack.
-         * 
+         *
          * @return the number of elements in the stack.
          */
         int getDepth();
 
         /**
          * Returns all the elements in the stack in a List.
-         * 
+         *
          * @return all the elements in the stack in a List.
          */
         List<String> asList();
 
         /**
          * Trims elements from the end of the stack.
-         * 
+         *
          * @param depth The maximum number of items in the stack to keep.
          */
         void trim(int depth);
 
         /**
          * Returns a copy of the ContextStack.
-         * 
+         *
          * @return a copy of the ContextStack.
          */
         ContextStack copy();
@@ -578,7 +585,7 @@ public final class ThreadContext {
         /**
          * Returns a ContextStack with the same contents as this ContextStack or {@code null}. Attempts to modify the
          * returned stack may or may not throw an exception, but will not affect the contents of this ContextStack.
-         * 
+         *
          * @return a ContextStack with the same contents as this ContextStack or {@code null}.
          */
         ContextStack getImmutableStackOrNull();

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6a233016/log4j-api/src/main/java/org/apache/logging/log4j/spi/DefaultThreadContextMap.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/DefaultThreadContextMap.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/DefaultThreadContextMap.java
index 0acf47d..a45ece5 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/spi/DefaultThreadContextMap.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/DefaultThreadContextMap.java
@@ -28,8 +28,8 @@ import org.apache.logging.log4j.util.PropertiesUtil;
  * expected that the Map will be passed to many more log events than the number of keys it contains the performance
  * should be much better than if the Map was copied for each event.
  */
-public class DefaultThreadContextMap implements ThreadContextMap {
-    
+public class DefaultThreadContextMap implements ThreadContextMap, ThreadContextMap2 {
+
     /**
      * Property name ({@value} ) for selecting {@code InheritableThreadLocal} (value "true") or plain
      * {@code ThreadLocal} (value is not "true") in the implementation.
@@ -86,7 +86,7 @@ public class DefaultThreadContextMap implements ThreadContextMap {
         }
         localMap.set(Collections.unmodifiableMap(map));
     }
-    
+
     @Override
     public String get(final String key) {
         final Map<String, String> map = localMap.get();

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6a233016/log4j-api/src/main/java/org/apache/logging/log4j/spi/ThreadContextMap.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/ThreadContextMap.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/ThreadContextMap.java
index 0b31d37..f0b2df4 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/spi/ThreadContextMap.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/ThreadContextMap.java
@@ -22,7 +22,7 @@ import java.util.Map;
  * Service provider interface to implement custom MDC behavior for {@link org.apache.logging.log4j.ThreadContext}.
  */
 public interface ThreadContextMap {
-    
+
     /**
      * Clears the context.
      */
@@ -75,17 +75,6 @@ public interface ThreadContextMap {
     void put(final String key, final String value);
 
     /**
-     * Puts all given context map entries into the current thread's
-     * context map.
-     *
-     * <p>If the current thread does not have a context map it is
-     * created as a side effect.</p>
-     * @param m The map.
-     * @since 2.7
-     */
-    public void putAll(final Map<String, String> m);
-
-    /**
      * Removes the the context identified by the <code>key</code>
      * parameter.
      * @param key The key to remove.

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6a233016/log4j-api/src/main/java/org/apache/logging/log4j/spi/ThreadContextMap2.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/ThreadContextMap2.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/ThreadContextMap2.java
new file mode 100644
index 0000000..7eba48d
--- /dev/null
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/ThreadContextMap2.java
@@ -0,0 +1,40 @@
+/*
+ * 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.Map;
+
+/**
+ * Extension service provider interface to implement additional custom MDC behavior for
+ * {@link org.apache.logging.log4j.ThreadContext}.
+ *
+ * @see ThreadContextMap
+ * @since 2.7
+ */
+public interface ThreadContextMap2 {
+
+    /**
+     * Puts all given context map entries into the current thread's
+     * context map.
+     *
+     * <p>If the current thread does not have a context map it is
+     * created as a side effect.</p>
+     * @param map The map.
+     * @since 2.7
+     */
+    void putAll(final Map<String, String> map);
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6a233016/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/MDCContextMap.java
----------------------------------------------------------------------
diff --git a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/MDCContextMap.java b/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/MDCContextMap.java
index b5c640f..9b0251a 100644
--- a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/MDCContextMap.java
+++ b/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/MDCContextMap.java
@@ -20,13 +20,14 @@ import java.util.Map;
 import java.util.Map.Entry;
 
 import org.apache.logging.log4j.spi.ThreadContextMap;
+import org.apache.logging.log4j.spi.ThreadContextMap2;
 import org.slf4j.MDC;
 
 /**
  * Bind the ThreadContextMap to the SLF4J MDC.
  */
-public class MDCContextMap implements ThreadContextMap {
-	
+public class MDCContextMap implements ThreadContextMap, ThreadContextMap2 {
+
     @Override
     public void put(final String key, final String value) {
         MDC.put(key, value);
@@ -35,7 +36,7 @@ public class MDCContextMap implements ThreadContextMap {
     @Override
     public void putAll(final Map<String, String> m) {
     	for (Entry<String, String> entry : m.entrySet()) {
-            MDC.put(entry.getKey(), entry.getValue());			
+            MDC.put(entry.getKey(), entry.getValue());
 		}
     }
 

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6a233016/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 675a24a..be283c7 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -87,8 +87,8 @@
       <action issue="LOG4J2-1508" dev="ggregory" type="add" due-to="Gary Gregory">
         Allow a Builder to subclass another Builder.
       </action>
-      <action issue="LOG4J2-1516" dev="ggregory" type="add" due-to="Gary Gregory">
-        Add ThreadContextMap.putAll(Map&lt;String, String&gt;).
+      <action issue="LOG4J2-1516" dev="rpopma" type="add" due-to="Gary Gregory">
+        Add ThreadContextMap2 interface supporting method putAll(Map&lt;String, String&gt;).
       </action>
       <action issue="LOG4J2-1519" dev="ggregory" type="add" due-to="Gary Gregory">
         Add ThreadContext.putAll(Map&lt;String, String&gt;).