1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.logging.log4j;
19
20 import java.io.Serializable;
21 import java.util.AbstractCollection;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.NoSuchElementException;
28
29 import org.apache.logging.log4j.message.ParameterizedMessage;
30 import org.apache.logging.log4j.spi.DefaultThreadContextMap;
31 import org.apache.logging.log4j.spi.DefaultThreadContextStack;
32 import org.apache.logging.log4j.spi.NoOpThreadContextMap;
33 import org.apache.logging.log4j.spi.ReadOnlyThreadContextMap;
34 import org.apache.logging.log4j.spi.ThreadContextMap;
35 import org.apache.logging.log4j.spi.ThreadContextMap2;
36 import org.apache.logging.log4j.spi.CleanableThreadContextMap;
37 import org.apache.logging.log4j.spi.ThreadContextMapFactory;
38 import org.apache.logging.log4j.spi.ThreadContextStack;
39 import org.apache.logging.log4j.util.PropertiesUtil;
40
41
42
43
44
45
46
47
48
49
50 public final class ThreadContext {
51
52
53
54
55 private static class EmptyThreadContextStack extends AbstractCollection<String> implements ThreadContextStack {
56
57 private static final long serialVersionUID = 1L;
58
59 private static final Iterator<String> EMPTY_ITERATOR = new EmptyIterator<>();
60
61 @Override
62 public String pop() {
63 return null;
64 }
65
66 @Override
67 public String peek() {
68 return null;
69 }
70
71 @Override
72 public void push(final String message) {
73 throw new UnsupportedOperationException();
74 }
75
76 @Override
77 public int getDepth() {
78 return 0;
79 }
80
81 @Override
82 public List<String> asList() {
83 return Collections.emptyList();
84 }
85
86 @Override
87 public void trim(final int depth) {
88
89 }
90
91 @Override
92 public boolean equals(final Object o) {
93
94 return (o instanceof Collection) && ((Collection<?>) o).isEmpty();
95 }
96
97 @Override
98 public int hashCode() {
99
100 return 1;
101 }
102
103 @Override
104 public ContextStack copy() {
105 return this;
106 }
107
108 @Override
109 public <T> T[] toArray(final T[] a) {
110 throw new UnsupportedOperationException();
111 }
112
113 @Override
114 public boolean add(final String e) {
115 throw new UnsupportedOperationException();
116 }
117
118 @Override
119 public boolean containsAll(final Collection<?> c) {
120 return false;
121 }
122
123 @Override
124 public boolean addAll(final Collection<? extends String> c) {
125 throw new UnsupportedOperationException();
126 }
127
128 @Override
129 public boolean removeAll(final Collection<?> c) {
130 throw new UnsupportedOperationException();
131 }
132
133 @Override
134 public boolean retainAll(final Collection<?> c) {
135 throw new UnsupportedOperationException();
136 }
137
138 @Override
139 public Iterator<String> iterator() {
140 return EMPTY_ITERATOR;
141 }
142
143 @Override
144 public int size() {
145 return 0;
146 }
147
148 @Override
149 public ContextStack getImmutableStackOrNull() {
150 return this;
151 }
152 }
153
154
155
156
157
158
159 private static class EmptyIterator<E> implements Iterator<E> {
160
161 @Override
162 public boolean hasNext() {
163 return false;
164 }
165
166 @Override
167 public E next() {
168 throw new NoSuchElementException("This is an empty iterator!");
169 }
170
171 @Override
172 public void remove() {
173
174 }
175 }
176
177
178
179
180
181 @SuppressWarnings("PublicStaticCollectionField")
182
183 public static final Map<String, String> EMPTY_MAP = Collections.emptyMap();
184
185
186
187
188
189 @SuppressWarnings("PublicStaticCollectionField")
190 public static final ThreadContextStack EMPTY_STACK = new EmptyThreadContextStack();
191
192 private static final String DISABLE_MAP = "disableThreadContextMap";
193 private static final String DISABLE_STACK = "disableThreadContextStack";
194 private static final String DISABLE_ALL = "disableThreadContext";
195
196 private static boolean useStack;
197 private static ThreadContextMap contextMap;
198 private static ThreadContextStack contextStack;
199 private static ReadOnlyThreadContextMap readOnlyContextMap;
200
201 static {
202 init();
203 }
204
205 private ThreadContext() {
206
207 }
208
209
210
211
212 static void init() {
213 ThreadContextMapFactory.init();
214 contextMap = null;
215 final PropertiesUtil managerProps = PropertiesUtil.getProperties();
216 boolean disableAll = managerProps.getBooleanProperty(DISABLE_ALL);
217 useStack = !(managerProps.getBooleanProperty(DISABLE_STACK) || disableAll);
218 boolean useMap = !(managerProps.getBooleanProperty(DISABLE_MAP) || disableAll);
219
220 contextStack = new DefaultThreadContextStack(useStack);
221 if (!useMap) {
222 contextMap = new NoOpThreadContextMap();
223 } else {
224 contextMap = ThreadContextMapFactory.createThreadContextMap();
225 }
226 if (contextMap instanceof ReadOnlyThreadContextMap) {
227 readOnlyContextMap = (ReadOnlyThreadContextMap) contextMap;
228 } else {
229 readOnlyContextMap = null;
230 }
231 }
232
233
234
235
236
237
238
239
240
241
242
243
244 public static void put(final String key, final String value) {
245 contextMap.put(key, value);
246 }
247
248
249
250
251
252
253
254
255
256
257
258
259
260 public static void putIfNull(final String key, final String value) {
261 if(!contextMap.containsKey(key)) {
262 contextMap.put(key, value);
263 }
264 }
265
266
267
268
269
270
271
272
273
274
275 public static void putAll(final Map<String, String> m) {
276 if (contextMap instanceof ThreadContextMap2) {
277 ((ThreadContextMap2) contextMap).putAll(m);
278 } else if (contextMap instanceof DefaultThreadContextMap) {
279 ((DefaultThreadContextMap) contextMap).putAll(m);
280 } else {
281 for (final Map.Entry<String, String> entry: m.entrySet()) {
282 contextMap.put(entry.getKey(), entry.getValue());
283 }
284 }
285 }
286
287
288
289
290
291
292
293
294
295
296
297 public static String get(final String key) {
298 return contextMap.get(key);
299 }
300
301
302
303
304
305
306 public static void remove(final String key) {
307 contextMap.remove(key);
308 }
309
310
311
312
313
314
315
316
317 public static void removeAll(final Iterable<String> keys) {
318 if (contextMap instanceof CleanableThreadContextMap) {
319 ((CleanableThreadContextMap) contextMap).removeAll(keys);
320 } else if (contextMap instanceof DefaultThreadContextMap) {
321 ((DefaultThreadContextMap) contextMap).removeAll(keys);
322 } else {
323 for (final String key : keys) {
324 contextMap.remove(key);
325 }
326 }
327 }
328
329
330
331
332 public static void clearMap() {
333 contextMap.clear();
334 }
335
336
337
338
339 public static void clearAll() {
340 clearMap();
341 clearStack();
342 }
343
344
345
346
347
348
349
350 public static boolean containsKey(final String key) {
351 return contextMap.containsKey(key);
352 }
353
354
355
356
357
358
359 public static Map<String, String> getContext() {
360 return contextMap.getCopy();
361 }
362
363
364
365
366
367
368 public static Map<String, String> getImmutableContext() {
369 final Map<String, String> map = contextMap.getImmutableMapOrNull();
370 return map == null ? EMPTY_MAP : map;
371 }
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389 public static ReadOnlyThreadContextMap getThreadContextMap() {
390 return readOnlyContextMap;
391 }
392
393
394
395
396
397
398 public static boolean isEmpty() {
399 return contextMap.isEmpty();
400 }
401
402
403
404
405 public static void clearStack() {
406 contextStack.clear();
407 }
408
409
410
411
412
413
414 public static ContextStack cloneStack() {
415 return contextStack.copy();
416 }
417
418
419
420
421
422
423 public static ContextStack getImmutableStack() {
424 final ContextStack result = contextStack.getImmutableStackOrNull();
425 return result == null ? EMPTY_STACK : result;
426 }
427
428
429
430
431
432
433 public static void setStack(final Collection<String> stack) {
434 if (stack.isEmpty() || !useStack) {
435 return;
436 }
437 contextStack.clear();
438 contextStack.addAll(stack);
439 }
440
441
442
443
444
445
446
447
448 public static int getDepth() {
449 return contextStack.getDepth();
450 }
451
452
453
454
455
456
457
458
459
460
461
462 public static String pop() {
463 return contextStack.pop();
464 }
465
466
467
468
469
470
471
472
473
474
475
476 public static String peek() {
477 return contextStack.peek();
478 }
479
480
481
482
483
484
485
486
487
488
489 public static void push(final String message) {
490 contextStack.push(message);
491 }
492
493
494
495
496
497
498
499
500
501
502
503
504
505 public static void push(final String message, final Object... args) {
506 contextStack.push(ParameterizedMessage.format(message, args));
507 }
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525 public static void removeStack() {
526 contextStack.clear();
527 }
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561 public static void trim(final int depth) {
562 contextStack.trim(depth);
563 }
564
565
566
567
568 public interface ContextStack extends Serializable, Collection<String> {
569
570
571
572
573
574
575
576 String pop();
577
578
579
580
581
582
583 String peek();
584
585
586
587
588
589
590 void push(String message);
591
592
593
594
595
596
597 int getDepth();
598
599
600
601
602
603
604 List<String> asList();
605
606
607
608
609
610
611 void trim(int depth);
612
613
614
615
616
617
618 ContextStack copy();
619
620
621
622
623
624
625
626 ContextStack getImmutableStackOrNull();
627 }
628 }