This is an automated email from the ASF dual-hosted git repository.
vy pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/2.x by this push:
new f5c6d73772 Fix loading of the effective `RecyclerFactoryConverter` in
JSON Template Layout (#3398)
f5c6d73772 is described below
commit f5c6d737723b7cd6e617565c69a245e4b206a01c
Author: Volkan Yazıcı <[email protected]>
AuthorDate: Mon Jan 27 10:53:20 2025 +0100
Fix loading of the effective `RecyclerFactoryConverter` in JSON Template
Layout (#3398)
---
.../json/resolver/MessageResolverTest.java | 2 +
.../json/util/RecyclerFactoryConverterTest.java | 33 ++++++++++++
.../json/util/RecyclerFactoryCustomConverter.java | 63 ++++++++++++++++++++++
.../template/json/JsonTemplateLayoutDefaults.java | 34 ++++++++++--
.../template/json/util/RecyclerFactories.java | 4 ++
.../3398_fix_JTL_RecyclerFactoryConverter.xml | 8 +++
6 files changed, 141 insertions(+), 3 deletions(-)
diff --git
a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/resolver/MessageResolverTest.java
b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/resolver/MessageResolverTest.java
index dab3bab00f..ba0d4f65a1 100644
---
a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/resolver/MessageResolverTest.java
+++
b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/resolver/MessageResolverTest.java
@@ -228,6 +228,8 @@ class MessageResolverTest {
private static final class TestMultiformatMessage implements
MultiformatMessage {
+ private static final long serialVersionUID = 1L;
+
@Override
public String getFormattedMessage() {
return "{\"foo\": \"bar\"}";
diff --git
a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/util/RecyclerFactoryConverterTest.java
b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/util/RecyclerFactoryConverterTest.java
new file mode 100644
index 0000000000..7753caf45a
--- /dev/null
+++
b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/util/RecyclerFactoryConverterTest.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.logging.log4j.layout.template.json.util;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import
org.apache.logging.log4j.layout.template.json.JsonTemplateLayoutDefaults;
+import org.junit.jupiter.api.Test;
+import org.junitpioneer.jupiter.SetSystemProperty;
+
+class RecyclerFactoryConverterTest {
+
+ @Test
+ @SetSystemProperty(key = "log4j.layout.jsonTemplate.recyclerFactory",
value = RecyclerFactoryCustomConverter.NAME)
+ void custom_converter_should_be_effective() {
+ assertThat(JsonTemplateLayoutDefaults.getRecyclerFactory())
+ .isSameAs(RecyclerFactoryCustomConverter.RECYCLER_FACTORY);
+ }
+}
diff --git
a/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/util/RecyclerFactoryCustomConverter.java
b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/util/RecyclerFactoryCustomConverter.java
new file mode 100644
index 0000000000..3675b58206
--- /dev/null
+++
b/log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/util/RecyclerFactoryCustomConverter.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.logging.log4j.layout.template.json.util;
+
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.convert.TypeConverter;
+import org.apache.logging.log4j.core.config.plugins.convert.TypeConverters;
+
+/**
+ * A custom {@link RecyclerFactory} type converter that always returns the
same instance if the specification is {@value
RecyclerFactoryCustomConverter#NAME}; otherwise, falls back to {@link
RecyclerFactories#ofSpec(String) the default}.
+ */
+@SuppressWarnings("ComparableType")
+@Plugin(name = "RecyclerFactoryCustomConverter", category =
TypeConverters.CATEGORY)
+public final class RecyclerFactoryCustomConverter
+ implements TypeConverter<RecyclerFactory>,
Comparable<TypeConverter<RecyclerFactory>> {
+
+ static final String NAME = "魔法";
+
+ static final RecyclerFactory RECYCLER_FACTORY = new RecyclerFactory() {
+
+ @Override
+ public <V> Recycler<V> create(final Supplier<V> supplier, final
Consumer<V> cleaner) {
+ return new Recycler<V>() {
+
+ @Override
+ public V acquire() {
+ return supplier.get();
+ }
+
+ @Override
+ public void release(V value) {
+ // Do nothing;
+ }
+ };
+ }
+ };
+
+ @Override
+ public int compareTo(TypeConverter<RecyclerFactory> typeConverter) {
+ return -1;
+ }
+
+ @Override
+ public RecyclerFactory convert(final String recyclerFactorySpec) {
+ return NAME.equals(recyclerFactorySpec) ? RECYCLER_FACTORY :
RecyclerFactories.ofSpec(recyclerFactorySpec);
+ }
+}
diff --git
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutDefaults.java
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutDefaults.java
index 316517b3a5..3afc7927fa 100644
---
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutDefaults.java
+++
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutDefaults.java
@@ -22,7 +22,8 @@ import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.TimeZone;
-import org.apache.logging.log4j.layout.template.json.util.RecyclerFactories;
+import org.apache.logging.log4j.core.config.plugins.convert.TypeConverter;
+import
org.apache.logging.log4j.core.config.plugins.convert.TypeConverterRegistry;
import org.apache.logging.log4j.layout.template.json.util.RecyclerFactory;
import org.apache.logging.log4j.util.PropertiesUtil;
@@ -116,7 +117,34 @@ public final class JsonTemplateLayoutDefaults {
}
public static RecyclerFactory getRecyclerFactory() {
- final String recyclerFactorySpec =
PROPERTIES.getStringProperty("log4j.layout.jsonTemplate.recyclerFactory");
- return RecyclerFactories.ofSpec(recyclerFactorySpec);
+
+ // Get the recycler factory specification
+ final String propertyName =
"log4j.layout.jsonTemplate.recyclerFactory";
+ final String recyclerFactorySpec =
PROPERTIES.getStringProperty(propertyName);
+
+ // Read the specification
+ @SuppressWarnings("unchecked")
+ final TypeConverter<RecyclerFactory> typeConverter =
(TypeConverter<RecyclerFactory>)
+
TypeConverterRegistry.getInstance().findCompatibleConverter(RecyclerFactory.class);
+ final RecyclerFactory recyclerFactory;
+ try {
+ // Using `TypeConverter#convert()` instead of
`TypeConverters.convert`.
+ // The latter doesn't pass the converter a null value, which is a
valid input.
+ recyclerFactory = typeConverter.convert(recyclerFactorySpec);
+ } catch (final Exception error) {
+ final String message = String.format(
+ "failed converting the recycler factory specified by the
`%s` property: %s",
+ propertyName, recyclerFactorySpec == null ? null : '`' +
recyclerFactorySpec + '`');
+ throw new RuntimeException(message, error);
+ }
+
+ // Verify and return loaded recycler factory
+ if (recyclerFactory == null) {
+ final String message = String.format(
+ "could not determine the recycler factory specified by the
`%s` property: %s",
+ propertyName, recyclerFactorySpec == null ? null : '`' +
recyclerFactorySpec + '`');
+ throw new IllegalArgumentException(message);
+ }
+ return recyclerFactory;
}
}
diff --git
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/util/RecyclerFactories.java
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/util/RecyclerFactories.java
index 7f4ed7a1cf..573b55327c 100644
---
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/util/RecyclerFactories.java
+++
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/util/RecyclerFactories.java
@@ -29,6 +29,10 @@ import org.apache.logging.log4j.core.util.Integers;
import org.apache.logging.log4j.util.LoaderUtil;
import org.jctools.queues.MpmcArrayQueue;
+/**
+ * @deprecated As of version {@code 2.25.0}, planned to be removed!
+ */
+@Deprecated
public final class RecyclerFactories {
private RecyclerFactories() {}
diff --git a/src/changelog/.2.x.x/3398_fix_JTL_RecyclerFactoryConverter.xml
b/src/changelog/.2.x.x/3398_fix_JTL_RecyclerFactoryConverter.xml
new file mode 100644
index 0000000000..7678cd7fb7
--- /dev/null
+++ b/src/changelog/.2.x.x/3398_fix_JTL_RecyclerFactoryConverter.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns="https://logging.apache.org/xml/ns"
+ xsi:schemaLocation="https://logging.apache.org/xml/ns
https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
+ type="fixed">
+ <issue id="3398"
link="https://github.com/apache/logging-log4j2/pull/3398"/>
+ <description format="asciidoc">Fix loading of the effective
`RecyclerFactoryConverter` in JSON Template Layout</description>
+</entry>