Class CopyOnWriteSortedArrayThreadContextMap
- java.lang.Object
-
- org.apache.logging.log4j.spi.CopyOnWriteSortedArrayThreadContextMap
-
- All Implemented Interfaces:
CleanableThreadContextMap
,CopyOnWrite
,ObjectThreadContextMap
,ReadOnlyThreadContextMap
,ThreadContextMap
,ThreadContextMap2
class CopyOnWriteSortedArrayThreadContextMap extends java.lang.Object implements ReadOnlyThreadContextMap, ObjectThreadContextMap, CopyOnWrite
SortedArrayStringMap
-based implementation of theThreadContextMap
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 Summary
Fields Modifier and Type Field Description protected static int
DEFAULT_INITIAL_CAPACITY
The default initial capacity.private static StringMap
EMPTY_CONTEXT_DATA
static java.lang.String
INHERITABLE_MAP
Property name ("isThreadContextMapInheritable" ) for selectingInheritableThreadLocal
(value "true") or plainThreadLocal
(value is not "true") in the implementation.private static boolean
inheritableMap
private static int
initialCapacity
private java.lang.ThreadLocal<StringMap>
localMap
protected static java.lang.String
PROPERTY_NAME_INITIAL_CAPACITY
System property name that can be used to control the data structure's initial capacity.
-
Constructor Summary
Constructors Constructor Description CopyOnWriteSortedArrayThreadContextMap()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Clears the context.boolean
containsKey(java.lang.String key)
Determines if the key is in the context.protected StringMap
createStringMap()
Returns an implementation of theStringMap
used to back this thread context map.protected StringMap
createStringMap(ReadOnlyStringMap original)
Returns an implementation of theStringMap
used to back this thread context map, pre-populated with the contents of the specified context data.private java.lang.ThreadLocal<StringMap>
createThreadLocalMap()
boolean
equals(java.lang.Object obj)
java.lang.String
get(java.lang.String key)
Gets the context identified by thekey
parameter.java.util.Map<java.lang.String,java.lang.String>
getCopy()
Gets a non-null
mutable copy of current thread's context Map.java.util.Map<java.lang.String,java.lang.String>
getImmutableMapOrNull()
Returns an immutable view on the context Map ornull
if the context map is empty.StringMap
getReadOnlyContextData()
Returns the context data for reading.<V> V
getValue(java.lang.String key)
Returns the Object value for the specified key, ornull
if the specified key does not exist in this collection.int
hashCode()
(package private) static void
init()
Initializes static variables based on system properties.boolean
isEmpty()
Returns true if the Map is empty.void
put(java.lang.String key, java.lang.String value)
Puts a context value (theo
parameter) as identified with thekey
parameter into the current thread's context map.void
putAll(java.util.Map<java.lang.String,java.lang.String> values)
Puts all given context map entries into the current thread's context map.<V> void
putAllValues(java.util.Map<java.lang.String,V> values)
Puts all given key-value pairs into the collection.void
putValue(java.lang.String key, java.lang.Object value)
Puts the specified key-value pair into the collection.void
remove(java.lang.String key)
Removes the context identified by thekey
parameter.void
removeAll(java.lang.Iterable<java.lang.String> keys)
Removes all given context map keys from the current thread's context map.java.lang.String
toString()
-
-
-
Field Detail
-
INHERITABLE_MAP
public static final java.lang.String INHERITABLE_MAP
Property name ("isThreadContextMapInheritable" ) for selectingInheritableThreadLocal
(value "true") or plainThreadLocal
(value is not "true") in the implementation.- See Also:
- Constant Field Values
-
DEFAULT_INITIAL_CAPACITY
protected static final int DEFAULT_INITIAL_CAPACITY
The default initial capacity.- See Also:
- Constant Field Values
-
PROPERTY_NAME_INITIAL_CAPACITY
protected static final java.lang.String PROPERTY_NAME_INITIAL_CAPACITY
System property name that can be used to control the data structure's initial capacity.- See Also:
- Constant Field Values
-
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 java.lang.ThreadLocal<StringMap> localMap
-
-
Method Detail
-
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 java.lang.ThreadLocal<StringMap> createThreadLocalMap()
-
createStringMap
protected StringMap createStringMap()
Returns an implementation of theStringMap
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 theStringMap
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(java.lang.String key, java.lang.String value)
Description copied from interface:ThreadContextMap
Puts a context value (theo
parameter) as identified with thekey
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 interfaceThreadContextMap
- Parameters:
key
- The key name.value
- The key value.
-
putValue
public void putValue(java.lang.String key, java.lang.Object value)
Description copied from interface:ObjectThreadContextMap
Puts the specified key-value pair into the collection.- Specified by:
putValue
in interfaceObjectThreadContextMap
- Parameters:
key
- the key to add or remove. Keys may benull
.value
- the value to add. Values may benull
.
-
putAll
public void putAll(java.util.Map<java.lang.String,java.lang.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 interfaceThreadContextMap2
- Parameters:
values
- The map.
-
putAllValues
public <V> void putAllValues(java.util.Map<java.lang.String,V> values)
Description copied from interface:ObjectThreadContextMap
Puts all given key-value pairs into the collection.- Specified by:
putAllValues
in interfaceObjectThreadContextMap
- Parameters:
values
- the map of key-value pairs to add
-
get
public java.lang.String get(java.lang.String key)
Description copied from interface:ReadOnlyThreadContextMap
Gets the context identified by thekey
parameter.This method has no side effects.
- Specified by:
get
in interfaceReadOnlyThreadContextMap
- Specified by:
get
in interfaceThreadContextMap
- Parameters:
key
- The key to locate.- Returns:
- The value associated with the key or null.
-
getValue
public <V> V getValue(java.lang.String key)
Description copied from interface:ObjectThreadContextMap
Returns the Object value for the specified key, ornull
if the specified key does not exist in this collection.- Specified by:
getValue
in interfaceObjectThreadContextMap
- Parameters:
key
- the key whose value to return- Returns:
- the value for the specified key or
null
-
remove
public void remove(java.lang.String key)
Description copied from interface:ThreadContextMap
Removes the context identified by thekey
parameter.- Specified by:
remove
in interfaceThreadContextMap
- Parameters:
key
- The key to remove.
-
removeAll
public void removeAll(java.lang.Iterable<java.lang.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 interfaceCleanableThreadContextMap
- Parameters:
keys
- The keys.
-
clear
public void clear()
Description copied from interface:ReadOnlyThreadContextMap
Clears the context.- Specified by:
clear
in interfaceReadOnlyThreadContextMap
- Specified by:
clear
in interfaceThreadContextMap
-
containsKey
public boolean containsKey(java.lang.String key)
Description copied from interface:ReadOnlyThreadContextMap
Determines if the key is in the context.- Specified by:
containsKey
in interfaceReadOnlyThreadContextMap
- Specified by:
containsKey
in interfaceThreadContextMap
- Parameters:
key
- The key to locate.- Returns:
- True if the key is in the context, false otherwise.
-
getCopy
public java.util.Map<java.lang.String,java.lang.String> getCopy()
Description copied from interface:ReadOnlyThreadContextMap
Gets a non-null
mutable copy of current thread's context Map.- Specified by:
getCopy
in interfaceReadOnlyThreadContextMap
- Specified by:
getCopy
in interfaceThreadContextMap
- 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
implementsCopyOnWrite
, then the returnedStringMap
can safely be passed to another thread: future changes in the underlying context data will not be reflected in the returnedStringMap
.Otherwise, if this
ReadOnlyThreadContextMap
does not implementCopyOnWrite
, then it is not safe to pass the returnedStringMap
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 interfaceReadOnlyThreadContextMap
- Specified by:
getReadOnlyContextData
in interfaceThreadContextMap2
- Returns:
- a
StringMap
containing context data key-value pairs
-
getImmutableMapOrNull
public java.util.Map<java.lang.String,java.lang.String> getImmutableMapOrNull()
Description copied from interface:ReadOnlyThreadContextMap
Returns an immutable view on the context Map ornull
if the context map is empty.- Specified by:
getImmutableMapOrNull
in interfaceReadOnlyThreadContextMap
- Specified by:
getImmutableMapOrNull
in interfaceThreadContextMap
- 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 interfaceReadOnlyThreadContextMap
- Specified by:
isEmpty
in interfaceThreadContextMap
- Returns:
- true if the Map is empty, false otherwise.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
-