Class CopyOnWriteSortedArrayThreadContextMap

java.lang.Object
org.apache.logging.log4j.spi.CopyOnWriteSortedArrayThreadContextMap
All Implemented Interfaces:
CleanableThreadContextMap, CopyOnWrite, ObjectThreadContextMap, ReadOnlyThreadContextMap, ThreadContextMap, ThreadContextMap2

class CopyOnWriteSortedArrayThreadContextMap extends Object implements ReadOnlyThreadContextMap, ObjectThreadContextMap, CopyOnWrite
SortedArrayStringMap-based implementation of the ThreadContextMap interface that creates a copy of the data structure on every modification. Any particular instance of the data structure is a snapshot of the ThreadContext at some point in time and can safely be passed off to other threads. Since it is 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.
Since:
2.7
  • Field Details

    • INHERITABLE_MAP

      public static final String INHERITABLE_MAP
      Property name ("isThreadContextMapInheritable" ) for selecting InheritableThreadLocal (value "true") or plain ThreadLocal (value is not "true") in the implementation.
      See Also:
    • DEFAULT_INITIAL_CAPACITY

      protected static final int DEFAULT_INITIAL_CAPACITY
      The default initial capacity.
      See Also:
    • PROPERTY_NAME_INITIAL_CAPACITY

      protected static final String PROPERTY_NAME_INITIAL_CAPACITY
      System property name that can be used to control the data structure's initial capacity.
      See Also:
    • EMPTY_CONTEXT_DATA

      private static final StringMap EMPTY_CONTEXT_DATA
    • initialCapacity

      private static volatile int initialCapacity
    • inheritableMap

      private static volatile boolean inheritableMap
    • localMap

      private final ThreadLocal<StringMap> localMap
  • Constructor Details

    • CopyOnWriteSortedArrayThreadContextMap

      public CopyOnWriteSortedArrayThreadContextMap()
  • Method Details

    • init

      static void init()
      Initializes static variables based on system properties. Normally called when this class is initialized by the VM and when Log4j is reconfigured.
    • createThreadLocalMap

      private ThreadLocal<StringMap> createThreadLocalMap()
    • createStringMap

      protected StringMap createStringMap()
      Returns an implementation of the StringMap used to back this thread context map.

      Subclasses may override.

      Returns:
      an implementation of the StringMap used to back this thread context map
    • createStringMap

      protected StringMap createStringMap(ReadOnlyStringMap original)
      Returns an implementation of the StringMap used to back this thread context map, pre-populated with the contents of the specified context data.

      Subclasses may override.

      Parameters:
      original - the key-value pairs to initialize the returned context data with
      Returns:
      an implementation of the StringMap used to back this thread context map
    • put

      public void put(String key, String value)
      Description copied from interface: ThreadContextMap
      Puts 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.

      Specified by:
      put in interface ThreadContextMap
      Parameters:
      key - The key name.
      value - The key value.
    • putValue

      public void putValue(String key, Object value)
      Description copied from interface: ObjectThreadContextMap
      Puts the specified key-value pair into the collection.
      Specified by:
      putValue in interface ObjectThreadContextMap
      Parameters:
      key - the key to add or remove. Keys may be null.
      value - the value to add. Values may be null.
    • putAll

      public void putAll(Map<String,String> values)
      Description copied from interface: ThreadContextMap2
      Puts all given context map entries into the current thread's context map.

      If the current thread does not have a context map it is created as a side effect.

      Specified by:
      putAll in interface ThreadContextMap2
      Parameters:
      values - The map.
    • putAllValues

      public <V> void putAllValues(Map<String,V> values)
      Description copied from interface: ObjectThreadContextMap
      Puts all given key-value pairs into the collection.
      Specified by:
      putAllValues in interface ObjectThreadContextMap
      Parameters:
      values - the map of key-value pairs to add
    • get

      public String get(String key)
      Description copied from interface: ReadOnlyThreadContextMap
      Gets the context identified by the key parameter.

      This method has no side effects.

      Specified by:
      get in interface ReadOnlyThreadContextMap
      Specified by:
      get in interface ThreadContextMap
      Parameters:
      key - The key to locate.
      Returns:
      The value associated with the key or null.
    • getValue

      public <V> V getValue(String key)
      Description copied from interface: ObjectThreadContextMap
      Returns the Object value for the specified key, or null if the specified key does not exist in this collection.
      Specified by:
      getValue in interface ObjectThreadContextMap
      Parameters:
      key - the key whose value to return
      Returns:
      the value for the specified key or null
    • remove

      public void remove(String key)
      Description copied from interface: ThreadContextMap
      Removes the context identified by the key parameter.
      Specified by:
      remove in interface ThreadContextMap
      Parameters:
      key - The key to remove.
    • removeAll

      public void removeAll(Iterable<String> keys)
      Description copied from interface: CleanableThreadContextMap
      Removes all given context map keys from the current thread's context map.

      If the current thread does not have a context map it is created as a side effect.

      Specified by:
      removeAll in interface CleanableThreadContextMap
      Parameters:
      keys - The keys.
    • clear

      public void clear()
      Description copied from interface: ReadOnlyThreadContextMap
      Clears the context.
      Specified by:
      clear in interface ReadOnlyThreadContextMap
      Specified by:
      clear in interface ThreadContextMap
    • containsKey

      public boolean containsKey(String key)
      Description copied from interface: ReadOnlyThreadContextMap
      Determines if the key is in the context.
      Specified by:
      containsKey in interface ReadOnlyThreadContextMap
      Specified by:
      containsKey in interface ThreadContextMap
      Parameters:
      key - The key to locate.
      Returns:
      True if the key is in the context, false otherwise.
    • getCopy

      public Map<String,String> getCopy()
      Description copied from interface: ReadOnlyThreadContextMap
      Gets a non-null mutable copy of current thread's context Map.
      Specified by:
      getCopy in interface ReadOnlyThreadContextMap
      Specified by:
      getCopy in interface ThreadContextMap
      Returns:
      a mutable copy of the context.
    • getReadOnlyContextData

      public StringMap getReadOnlyContextData()
      Returns the context data for reading. Note that regardless of whether the returned context data has been frozen (made read-only) or not, callers should not attempt to modify the returned data structure.

      Thread safety note:

      If this ReadOnlyThreadContextMap implements CopyOnWrite, then the returned StringMap can safely be passed to another thread: future changes in the underlying context data will not be reflected in the returned StringMap.

      Otherwise, if this ReadOnlyThreadContextMap does not implement CopyOnWrite, then it is not safe to pass the returned StringMap to another thread because changes in the underlying context may be reflected in the returned object. It is the responsibility of the caller to make a copy to pass to another thread.

      Specified by:
      getReadOnlyContextData in interface ReadOnlyThreadContextMap
      Specified by:
      getReadOnlyContextData in interface ThreadContextMap2
      Returns:
      a StringMap containing context data key-value pairs
    • getImmutableMapOrNull

      public Map<String,String> getImmutableMapOrNull()
      Description copied from interface: ReadOnlyThreadContextMap
      Returns an immutable view on the context Map or null if the context map is empty.
      Specified by:
      getImmutableMapOrNull in interface ReadOnlyThreadContextMap
      Specified by:
      getImmutableMapOrNull in interface ThreadContextMap
      Returns:
      an immutable context Map or null.
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: ReadOnlyThreadContextMap
      Returns true if the Map is empty.
      Specified by:
      isEmpty in interface ReadOnlyThreadContextMap
      Specified by:
      isEmpty in interface ThreadContextMap
      Returns:
      true if the Map is empty, false otherwise.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object