Skip to content

Commit 8e7a925

Browse files
committed
Improve deprecated constructor and javadoc
1 parent a50fe37 commit 8e7a925

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

log4j-core/src/main/java/org/apache/logging/log4j/core/impl/JdkMapAdapterStringMap.java

+28-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.HashMap;
2222
import java.util.Map;
2323
import java.util.Objects;
24+
import java.util.WeakHashMap;
2425
import org.apache.logging.log4j.util.BiConsumer;
2526
import org.apache.logging.log4j.util.ReadOnlyStringMap;
2627
import org.apache.logging.log4j.util.StringMap;
@@ -41,13 +42,16 @@ public class JdkMapAdapterStringMap implements StringMap {
4142
}
4243
return left.compareTo(right);
4344
};
45+
// Cache of known unmodifiable map implementations.
46+
// It is a cache, no need to synchronise it between threads.
47+
private static Map<Class<?>, Void> UNMODIFIABLE_MAPS_CACHE = new WeakHashMap<>();
4448

4549
private final Map<String, String> map;
4650
private boolean immutable = false;
4751
private transient String[] sortedKeys;
4852

4953
public JdkMapAdapterStringMap() {
50-
this(new HashMap<String, String>(), false);
54+
this(new HashMap<>(), false);
5155
}
5256

5357
/**
@@ -57,25 +61,41 @@ public JdkMapAdapterStringMap() {
5761
@Deprecated
5862
public JdkMapAdapterStringMap(final Map<String, String> map) {
5963
this.map = Objects.requireNonNull(map, "map");
60-
try {
61-
map.replace(Strings.EMPTY, Strings.EMPTY, Strings.EMPTY);
62-
} catch (final UnsupportedOperationException ignored) {
64+
// Known immutable implementations
65+
if (UNMODIFIABLE_MAPS_CACHE.containsKey(map.getClass())) {
6366
immutable = true;
67+
} else {
68+
// Check with a NO-OP replacement
69+
try {
70+
map.replace(Strings.EMPTY, Strings.EMPTY, Strings.EMPTY);
71+
} catch (final UnsupportedOperationException ignored) {
72+
final WeakHashMap<Class<?>, Void> cache = new WeakHashMap<>(UNMODIFIABLE_MAPS_CACHE);
73+
cache.put(map.getClass(), null);
74+
UNMODIFIABLE_MAPS_CACHE = cache;
75+
immutable = true;
76+
}
6477
}
6578
}
6679

6780
/**
81+
* Constructs a new {@link StringMap}, based on a JDK map.
82+
* <p>
83+
* The underlying map should not be modified after this call.
84+
* </p>
85+
* <p>
86+
* If the {@link Map} implementation does not allow modifications, {@code frozen} should be set to {@code true}.
87+
* </p>
6888
* @param map a JDK map,
69-
* @param immutable must be {@code true} if the map is immutable or it should not be modified.
89+
* @param frozen if {@code true} this collection will be immutable.
7090
*/
71-
public JdkMapAdapterStringMap(final Map<String, String> map, final boolean immutable) {
91+
public JdkMapAdapterStringMap(final Map<String, String> map, final boolean frozen) {
7292
this.map = Objects.requireNonNull(map, "map");
73-
this.immutable = immutable;
93+
this.immutable = frozen;
7494
}
7595

7696
@Override
7797
public Map<String, String> toMap() {
78-
return map;
98+
return new HashMap<>(map);
7999
}
80100

81101
private void assertNotFrozen() {

log4j-core/src/main/java/org/apache/logging/log4j/core/util/ContextDataProvider.java

+8
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,20 @@ public interface ContextDataProvider {
2727

2828
/**
2929
* Returns a Map containing context data to be injected into the event or null if no context data is to be added.
30+
* <p>
31+
* Thread-safety note: The returned object can safely be passed off to another thread: future changes in the
32+
* underlying context data will not be reflected in the returned object.
33+
* </p>
3034
* @return A Map containing the context data or null.
3135
*/
3236
Map<String, String> supplyContextData();
3337

3438
/**
3539
* Returns the context data as a StringMap.
40+
* <p>
41+
* Thread-safety note: The returned object can safely be passed off to another thread: future changes in the
42+
* underlying context data will not be reflected in the returned object.
43+
* </p>
3644
* @return the context data in a StringMap.
3745
*/
3846
default StringMap supplyStringMap() {

0 commit comments

Comments
 (0)