|
22 | 22 | import java.nio.charset.StandardCharsets;
|
23 | 23 | import java.util.Locale;
|
24 | 24 | import java.util.TimeZone;
|
25 |
| -import org.apache.logging.log4j.layout.template.json.util.RecyclerFactories; |
| 25 | +import org.apache.logging.log4j.core.config.plugins.convert.TypeConverter; |
| 26 | +import org.apache.logging.log4j.core.config.plugins.convert.TypeConverterRegistry; |
26 | 27 | import org.apache.logging.log4j.layout.template.json.util.RecyclerFactory;
|
27 | 28 | import org.apache.logging.log4j.util.PropertiesUtil;
|
28 | 29 |
|
@@ -116,7 +117,34 @@ public static String getTruncatedStringSuffix() {
|
116 | 117 | }
|
117 | 118 |
|
118 | 119 | public static RecyclerFactory getRecyclerFactory() {
|
119 |
| - final String recyclerFactorySpec = PROPERTIES.getStringProperty("log4j.layout.jsonTemplate.recyclerFactory"); |
120 |
| - return RecyclerFactories.ofSpec(recyclerFactorySpec); |
| 120 | + |
| 121 | + // Get the recycler factory specification |
| 122 | + final String propertyName = "log4j.layout.jsonTemplate.recyclerFactory"; |
| 123 | + final String recyclerFactorySpec = PROPERTIES.getStringProperty(propertyName); |
| 124 | + |
| 125 | + // Read the specification |
| 126 | + @SuppressWarnings("unchecked") |
| 127 | + final TypeConverter<RecyclerFactory> typeConverter = (TypeConverter<RecyclerFactory>) |
| 128 | + TypeConverterRegistry.getInstance().findCompatibleConverter(RecyclerFactory.class); |
| 129 | + final RecyclerFactory recyclerFactory; |
| 130 | + try { |
| 131 | + // Using `TypeConverter#convert()` instead of `TypeConverters.convert`. |
| 132 | + // The latter doesn't pass the converter a null value, which is a valid input. |
| 133 | + recyclerFactory = typeConverter.convert(recyclerFactorySpec); |
| 134 | + } catch (final Exception error) { |
| 135 | + final String message = String.format( |
| 136 | + "failed converting the recycler factory specified by the `%s` property: %s", |
| 137 | + propertyName, recyclerFactorySpec == null ? null : '`' + recyclerFactorySpec + '`'); |
| 138 | + throw new RuntimeException(message, error); |
| 139 | + } |
| 140 | + |
| 141 | + // Verify and return loaded recycler factory |
| 142 | + if (recyclerFactory == null) { |
| 143 | + final String message = String.format( |
| 144 | + "could not determine the recycler factory specified by the `%s` property: %s", |
| 145 | + propertyName, recyclerFactorySpec == null ? null : '`' + recyclerFactorySpec + '`'); |
| 146 | + throw new IllegalArgumentException(message); |
| 147 | + } |
| 148 | + return recyclerFactory; |
121 | 149 | }
|
122 | 150 | }
|
0 commit comments