View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.kryo;
15  
16  import ch.qos.logback.core.kryo.SerializerBase;
17  import com.esotericsoftware.kryo.kryo5.Kryo;
18  import com.esotericsoftware.kryo.kryo5.Serializer;
19  import com.esotericsoftware.kryo.kryo5.io.Input;
20  import com.esotericsoftware.kryo.kryo5.io.Output;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  /**
26   * Serializer for MDC property maps ({@code Map<String, String>}).
27   *
28   * <p>Each map is written as:
29   * <ol>
30   *   <li>entry count ({@code writeVarInt}), excluding null keys and null values</li>
31   *   <li>for each remaining entry: key string, then value string</li>
32   * </ol>
33   *
34   * <p>The default constructor caches whole maps with {@link BiMapType#DIRECT_MAP}
35   * ({@code equals}/{@code hashCode}). {@link MDCPropertyMapSerializerV5} uses
36   * {@link BiMapType#IDENTITY}. Decode returns {@link Map#copyOf}. An older stream
37   * may still contain a null value; that entry is read and dropped.
38   *
39   * <p>By default key and value strings share one nested string serializer.
40   * {@link MDCPropertyMapSerializerV5} uses {@link BasicStringSerializer} for keys
41   * and a caller-supplied value codec (the V5 message/argument long-string serializer).
42   *
43   * <p>Null maps are not handled here; callers should use {@code writeObjectOrNull} /
44   * {@code readObjectOrNull}.
45   */
46  public class MDCPropertyMapSerializer extends SerializerBase<Map<String, String>> {
47  
48      private final Serializer<String> keySerializer;
49      private final Serializer<String> valueSerializer;
50  
51      /**
52       * @param capacity capacity of the whole-map cache and of the nested string cache
53       */
54      public MDCPropertyMapSerializer(int capacity) {
55          this(BiMapType.DIRECT_MAP, capacity);
56      }
57  
58      /**
59       * @param biMapType whole-map cache: {@link BiMapType#DIRECT_MAP} (equals) or
60       *                  {@link BiMapType#IDENTITY} ({@code ==})
61       * @param capacity  capacity of the whole-map cache and of the nested string cache
62       */
63      public MDCPropertyMapSerializer(BiMapType biMapType, int capacity) {
64          this(biMapType, capacity, null, null);
65      }
66  
67      /**
68       * @param biMapType        whole-map cache: {@link BiMapType#DIRECT_MAP} (equals) or
69       *                         {@link BiMapType#IDENTITY} ({@code ==})
70       * @param capacity         capacity of the whole-map cache; also used when this
71       *                         constructor allocates nested string serializers
72       * @param keySerializer    key codec; {@code null} means {@link #getNewEntrySerializer(int)}
73       * @param valueSerializer  value codec; {@code null} means the same instance as keys
74       */
75      public MDCPropertyMapSerializer(BiMapType biMapType, int capacity,
76                                      Serializer<String> keySerializer,
77                                      Serializer<String> valueSerializer) {
78          super(biMapType, capacity);
79          setAcceptsNull(false);
80          // acceptsNull stays false: Kryo writeObjectOrNull handles null values via NULL/NOT_NULL.
81          if (keySerializer == null) {
82              keySerializer = getNewEntrySerializer(capacity);
83          }
84          if (valueSerializer == null) {
85              valueSerializer = keySerializer;
86          }
87          this.keySerializer = keySerializer;
88          this.valueSerializer = valueSerializer;
89      }
90  
91  
92      @Override
93      protected void writeNewEntry(Kryo kryo, Output output, Map<String, String> map) {
94          int count = 0;
95          for (Map.Entry<String, String> entry : map.entrySet()) {
96              if (entry.getKey() != null && entry.getValue() != null) {
97                  count++;
98              }
99          }
100         output.writeVarInt(count, true);
101         for (Map.Entry<String, String> entry : map.entrySet()) {
102             String key = entry.getKey();
103             String value = entry.getValue();
104             if (key == null || value == null) {
105                 continue;
106             }
107             kryo.writeObject(output, key, keySerializer);
108             kryo.writeObjectOrNull(output, value, valueSerializer);
109         }
110     }
111 
112     @Override
113     protected Map<String, String> readNewEntry(Kryo kryo, Input input) {
114         int size = input.readVarInt(true);
115         HashMap<String, String> map = new HashMap<>(Math.max(4, (int) (size / 0.75f) + 1));
116         for (int i = 0; i < size; i++) {
117             String key = kryo.readObject(input, String.class, keySerializer);
118             String value = kryo.readObjectOrNull(input, String.class, valueSerializer);
119             // Older streams included null values in the count and wrote them.
120             if (key == null || value == null) {
121                 continue;
122             }
123             map.put(key, value);
124         }
125         return Map.copyOf(map);
126     }
127 
128     protected Serializer<String> getNewEntrySerializer(int capacity) {
129         return new BasicStringSerializer(BiMapType.DIRECT_MAP, capacity);
130     }
131 }