1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.kryo;
15
16 import ch.qos.logback.classic.spi.LoggerContextVO;
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.serializers.MapSerializer;
21 import com.esotericsoftware.kryo.kryo5.util.MapReferenceResolver;
22 import org.slf4j.event.KeyValuePair;
23
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.Map;
27
28
29 public class KryoBuilder {
30
31
32 static public Kryo build(EncoderVersionTag encoderVersionTag) {
33 return build(encoderVersionTag.serializerType(), encoderVersionTag.streamVersion());
34 }
35
36
37
38
39 static public Kryo build(KryoEncoder.SerializerType serializerType, byte streamVersion) {
40 switch (serializerType) {
41 case Vanilla:
42 return buildLoggerEventKryoVO_Vanilla();
43 case Custom:
44 return buildCustomForVersion(streamVersion);
45 default:
46 throw new IllegalArgumentException("Unknown serializer type: " + serializerType);
47 }
48 }
49
50 static Kryo buildCustomForVersion(byte streamVersion) {
51 switch (streamVersion) {
52 case KryoClassicConstants.STREAM_VERSION_5:
53 return buildLoggerEventKryoVO_CustomV5();
54 default:
55 throw new IllegalArgumentException("Unsupported stream version for Custom: "
56 + (streamVersion & 0xFF));
57 }
58 }
59
60 static public Kryo buildLoggerEventKryoVO_Vanilla() {
61
62 MapReferenceResolver mapReferenceResolver = new MapReferenceResolver();
63 Kryo kryo = new Kryo(mapReferenceResolver);
64
65 kryo.register(LoggingEventKryoVO.class);
66 kryo.register(LoggerContextVO.class, new VanillaLoggerContextVOSerializer());
67
68 kryo.register(Object[].class);
69 kryo.register(HashMap.class);
70 kryo.register(java.util.ArrayList.class);
71
72
73 kryo.register(java.util.concurrent.CopyOnWriteArrayList.class);
74 kryo.register(ch.qos.logback.classic.spi.IThrowableProxy[].class);
75 kryo.register(ch.qos.logback.classic.spi.ThrowableProxyVO.class, new VanillaThrowableProxyVOSerializer());
76 kryo.register(ch.qos.logback.classic.spi.StackTraceElementProxy[].class);
77 kryo.register(ch.qos.logback.classic.spi.StackTraceElementProxy.class, new StackTraceElementProxySerializer());
78 kryo.register(StackTraceElement[].class);
79 kryo.register(java.lang.StackTraceElement.class, new StackTraceElementSerializer());
80 kryo.register(org.slf4j.helpers.BasicMarker.class, new BasicMarkerSerializer());
81 kryo.register(KeyValuePair.class, new KeyValuePairSerializer());
82
83
84 kryo.register(Collections.unmodifiableMap(new HashMap<String, String>()).getClass(),
85 new MapSerializer<Map<String, String>>() {
86 @Override
87 protected Map<String, String> create(Kryo kryo, Input input,
88 Class<? extends Map<String, String>> type, int size) {
89 return new HashMap<>(Math.max(4, (int) (size / 0.75f) + 1));
90 }
91 });
92 return kryo;
93 }
94
95
96
97
98
99 static public Kryo buildLoggerEventKryoVO_CustomV5() {
100 return buildCustomKryo(new LoggingEventKryoVOSerializerV5());
101 }
102
103
104 static Kryo buildCustomKryo(Serializer<LoggingEventKryoVO> eventSerializer) {
105 Kryo kryo = new Kryo();
106
107 kryo.setReferences(false);
108
109 kryo.register(LoggingEventKryoVO.class, eventSerializer);
110 kryo.register(Object[].class);
111
112 kryo.register(StackTraceElement[].class);
113 kryo.register(java.util.ArrayList.class);
114
115
116
117 kryo.register(ch.qos.logback.classic.spi.ThrowableProxyVO.class, new VanillaThrowableProxyVOSerializer());
118 kryo.register(ch.qos.logback.classic.spi.IThrowableProxy[].class);
119 kryo.register(org.slf4j.helpers.BasicMarker.class, new BasicMarkerSerializer());
120 kryo.register(KeyValuePair.class, new KeyValuePairSerializer());
121 kryo.register(java.util.concurrent.CopyOnWriteArrayList.class);
122 kryo.register(ch.qos.logback.classic.spi.StackTraceElementProxy[].class);
123
124 kryo.register(java.util.HashMap.class);
125
126 return kryo;
127 }
128 }