|
| 1 | +/* |
| 2 | + * Copyright 2013-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package net.logstash.logback.pattern; |
| 17 | + |
| 18 | +import java.lang.reflect.Method; |
| 19 | +import java.lang.reflect.ParameterizedType; |
| 20 | +import java.lang.reflect.Type; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.function.Supplier; |
| 23 | + |
| 24 | +import ch.qos.logback.classic.spi.ILoggingEvent; |
| 25 | +import ch.qos.logback.core.pattern.DynamicConverter; |
| 26 | +import ch.qos.logback.core.pattern.PatternLayoutBase; |
| 27 | + |
| 28 | +/** |
| 29 | + * Utilities for working around backwards incompatibilities introduced in logback's |
| 30 | + * PatternLayout related classes to maintain support for multiple logback versions. |
| 31 | + * |
| 32 | + * <p>This can be removed in a major version of logstash-logback-encoder, |
| 33 | + * which bumps the minimum logback version to >= 1.5.13.</p> |
| 34 | + */ |
| 35 | +class PatternLayoutCompatibilityUtil { |
| 36 | + |
| 37 | + /** |
| 38 | + * Registers the given converter in the layout's {@link PatternLayoutBase#getInstanceConverterMap()}. |
| 39 | + * |
| 40 | + * @param layout the layout to which to add the converter |
| 41 | + * @param converterName the name of the converter |
| 42 | + * @param converterClass the class of the converter |
| 43 | + * @param converterSupplier a supplier of instances of the converter |
| 44 | + * @param <E> type of object converted by the converter |
| 45 | + * @param <C> type of converter |
| 46 | + */ |
| 47 | + static <E, C extends DynamicConverter<E>> void registerConverter( |
| 48 | + PatternLayoutBase<ILoggingEvent> layout, |
| 49 | + String converterName, |
| 50 | + Class<C> converterClass, |
| 51 | + Supplier<C> converterSupplier) { |
| 52 | + |
| 53 | + try { |
| 54 | + Method method = PatternLayoutBase.class.getDeclaredMethod("getInstanceConverterMap"); |
| 55 | + ParameterizedType type = (ParameterizedType) method.getGenericReturnType(); |
| 56 | + Type valueType = type.getActualTypeArguments()[1]; |
| 57 | + |
| 58 | + if (valueType.getTypeName().equals("java.lang.String")) { |
| 59 | + /* |
| 60 | + * In logback <= 1.5.12, getInstanceConverterMap() is a Map<String, String>. |
| 61 | + * key = converter name |
| 62 | + * value = name of converter class |
| 63 | + */ |
| 64 | + putUnchecked(layout.getInstanceConverterMap(), converterName, converterClass.getName()); |
| 65 | + } else if (valueType.getTypeName().equals("java.util.function.Supplier<ch.qos.logback.core.pattern.DynamicConverter>")) { |
| 66 | + /* |
| 67 | + * In logback >= 1.5.13, getInstanceConverterMap() is a Map<String, Supplier<DynamicConverter>. |
| 68 | + * key = converter name |
| 69 | + * value = supplier of converter instances |
| 70 | + * |
| 71 | + * Related issues: |
| 72 | + * https://github.com/qos-ch/logback/issues/803 |
| 73 | + * https://github.com/qos-ch/logback/issues/931 |
| 74 | + * https://github.com/qos-ch/logback/issues/932 |
| 75 | + */ |
| 76 | + putUnchecked(layout.getInstanceConverterMap(), converterName, converterSupplier); |
| 77 | + } else { |
| 78 | + throw new IncompatibleClassChangeError( |
| 79 | + "Unexpected return type found for PatternLayoutBase.getInstanceConverterMap(), logback-core dependency version is not compatible"); |
| 80 | + } |
| 81 | + |
| 82 | + } catch (NoSuchMethodException e) { |
| 83 | + throw new IncompatibleClassChangeError( |
| 84 | + "Method PatternLayoutBase.getInstanceConverterMap() not found, logback-core dependency version is not compatible"); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @SuppressWarnings({"rawtypes", "unchecked"}) |
| 89 | + private static void putUnchecked(Map map, String key, Object value) { |
| 90 | + map.put(key, value); |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments