This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 45da7713c036 CAMEL-27164: camel-core - the configurer of a data format
is the one of its own class (#30338)
45da7713c036 is described below
commit 45da7713c036b50854273f8f96cf392374c3f58c
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Nov 18 21:59:21 2026 +0100
CAMEL-27164: camel-core - the configurer of a data format is the one of its
own class (#30338)
When both camel-jackson and camel-jackson3 are on the classpath, Camel
creates the Jackson 3.x data format for jackson,
avroJackson, protobufJackson and jacksonXml (CAMEL-22857), but the
configurer was found by its name, such as
jackson-dataformat, which both JARs register. When camel-jackson came first
on the classpath, binding any option failed
with a ClassCastException.
PropertyConfigurerHelper.resolvePropertyConfigurerByName finds the
configurer by name, and when it is a generated
configurer for another type than the target, it uses the configurer
generated for the type of the target (or a super
type). The data format reifier, the Rest DSL binding, the rest producer,
the dataformat component, autowiring and the
camel.dataformat options of Camel Main use it, and so does the class to
name mapping of
PropertyConfigurerHelper.resolvePropertyConfigurer(context, Class), which
covers components and languages too.
Tests: PropertyConfigurerHelperByNameTest (camel-core) and
KameletJacksonBothLinesTest (camel-kamelet).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../component/dataformat/DataFormatComponent.java | 5 +-
...nXMLRestBindingJacksonXmlDataFormatFactory.java | 6 +-
...nXMLRestBindingJacksonXmlDataFormatFactory.java | 6 +-
.../kamelet/KameletJacksonBothLinesTest.java | 168 +++++++++++++++++++++
.../apache/camel/component/rest/RestProducer.java | 5 +-
.../reifier/dataformat/DataFormatReifier.java | 8 +-
.../PropertyConfigurerHelperByNameTest.java | 149 ++++++++++++++++++
.../java/org/apache/camel/main/MainHelper.java | 3 +-
.../camel/support/LifecycleStrategySupport.java | 4 +-
.../camel/support/PropertyConfigurerHelper.java | 80 +++++++++-
.../processor/RestBindingAdviceFactory.java | 4 +-
11 files changed, 414 insertions(+), 24 deletions(-)
diff --git
a/components/camel-dataformat/src/main/java/org/apache/camel/component/dataformat/DataFormatComponent.java
b/components/camel-dataformat/src/main/java/org/apache/camel/component/dataformat/DataFormatComponent.java
index 75da315ab5c2..772429e65fe7 100644
---
a/components/camel-dataformat/src/main/java/org/apache/camel/component/dataformat/DataFormatComponent.java
+++
b/components/camel-dataformat/src/main/java/org/apache/camel/component/dataformat/DataFormatComponent.java
@@ -26,7 +26,6 @@ import org.apache.camel.spi.ExtendedPropertyConfigurerGetter;
import org.apache.camel.spi.PropertyConfigurer;
import org.apache.camel.spi.annotations.Component;
import org.apache.camel.support.DefaultComponent;
-import org.apache.camel.support.PluginHelper;
import org.apache.camel.support.PropertyBindingSupport;
import org.apache.camel.support.PropertyConfigurerHelper;
import org.apache.camel.util.StringHelper;
@@ -61,8 +60,8 @@ public class DataFormatComponent extends DefaultComponent {
}
// find configurer if any
- PropertyConfigurer configurer =
PluginHelper.getConfigurerResolver(getCamelContext())
- .resolvePropertyConfigurer(name + "-dataformat",
getCamelContext());
+ PropertyConfigurer configurer
+ =
PropertyConfigurerHelper.resolvePropertyConfigurerByName(getCamelContext(),
name + "-dataformat", df);
// when a brand-new instance was created, first copy any global
camel.dataformat.<name>.*
// configuration from the auto-configured data format onto it, so the
dataformat: component
diff --git
a/components/camel-jackson3xml/src/main/java/org/apache/camel/component/jackson3xml/JacksonXMLRestBindingJacksonXmlDataFormatFactory.java
b/components/camel-jackson3xml/src/main/java/org/apache/camel/component/jackson3xml/JacksonXMLRestBindingJacksonXmlDataFormatFactory.java
index 2830239ccb47..f30e10dfe737 100644
---
a/components/camel-jackson3xml/src/main/java/org/apache/camel/component/jackson3xml/JacksonXMLRestBindingJacksonXmlDataFormatFactory.java
+++
b/components/camel-jackson3xml/src/main/java/org/apache/camel/component/jackson3xml/JacksonXMLRestBindingJacksonXmlDataFormatFactory.java
@@ -25,8 +25,8 @@ import org.apache.camel.spi.PropertyConfigurer;
import org.apache.camel.spi.RestBindingJacksonXmlDataFormatFactory;
import org.apache.camel.spi.RestConfiguration;
import org.apache.camel.spi.annotations.JdkService;
-import org.apache.camel.support.PluginHelper;
import org.apache.camel.support.PropertyBindingSupport;
+import org.apache.camel.support.PropertyConfigurerHelper;
/**
* Jackson based {@link RestBindingJacksonXmlDataFormatFactory}.
@@ -43,8 +43,8 @@ public class JacksonXMLRestBindingJacksonXmlDataFormatFactory
implements RestBin
DataFormat xml, DataFormat outXml)
throws Exception {
// lookup configurer
- PropertyConfigurer configurer =
PluginHelper.getConfigurerResolver(camelContext)
- .resolvePropertyConfigurer("jacksonXml-dataformat-configurer",
camelContext);
+ PropertyConfigurer configurer =
PropertyConfigurerHelper.resolvePropertyConfigurerByName(camelContext,
+ "jacksonXml-dataformat-configurer", xml);
if (configurer == null) {
throw new IllegalStateException("Cannot find configurer for
dataformat: jacksonXml");
}
diff --git
a/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/JacksonXMLRestBindingJacksonXmlDataFormatFactory.java
b/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/JacksonXMLRestBindingJacksonXmlDataFormatFactory.java
index a79c14247b82..efe4b08bd355 100644
---
a/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/JacksonXMLRestBindingJacksonXmlDataFormatFactory.java
+++
b/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/JacksonXMLRestBindingJacksonXmlDataFormatFactory.java
@@ -25,8 +25,8 @@ import org.apache.camel.spi.PropertyConfigurer;
import org.apache.camel.spi.RestBindingJacksonXmlDataFormatFactory;
import org.apache.camel.spi.RestConfiguration;
import org.apache.camel.spi.annotations.JdkService;
-import org.apache.camel.support.PluginHelper;
import org.apache.camel.support.PropertyBindingSupport;
+import org.apache.camel.support.PropertyConfigurerHelper;
/**
* Jackson based {@link RestBindingJacksonXmlDataFormatFactory}.
@@ -41,8 +41,8 @@ public class JacksonXMLRestBindingJacksonXmlDataFormatFactory
implements RestBin
DataFormat xml, DataFormat outXml)
throws Exception {
// lookup configurer
- PropertyConfigurer configurer =
PluginHelper.getConfigurerResolver(camelContext)
- .resolvePropertyConfigurer("jacksonXml-dataformat-configurer",
camelContext);
+ PropertyConfigurer configurer =
PropertyConfigurerHelper.resolvePropertyConfigurerByName(camelContext,
+ "jacksonXml-dataformat-configurer", xml);
if (configurer == null) {
throw new IllegalStateException("Cannot find configurer for
dataformat: jacksonXml");
}
diff --git
a/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletJacksonBothLinesTest.java
b/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletJacksonBothLinesTest.java
new file mode 100644
index 000000000000..73195508a9eb
--- /dev/null
+++
b/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletJacksonBothLinesTest.java
@@ -0,0 +1,168 @@
+/*
+ * 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.camel.component.kamelet;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UncheckedIOException;
+import java.net.URL;
+import java.security.CodeSource;
+import java.util.Enumeration;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.jackson3.JacksonDataFormat;
+import org.apache.camel.component.jackson3.JacksonDataFormatConfigurer;
+import org.apache.camel.impl.engine.DefaultClassResolver;
+import org.apache.camel.model.dataformat.JsonLibrary;
+import org.apache.camel.spi.ConfigurerResolver;
+import org.apache.camel.support.PluginHelper;
+import org.apache.camel.support.PropertyConfigurerHelper;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * camel-jackson (Jackson 2.x) and camel-jackson3 (Jackson 3.x) register the
same configurer name, jackson-dataformat.
+ * With both on the classpath, as when a Kamelet adds camel-jackson to an
application with camel-jackson3, Camel creates
+ * the Jackson 3.x data format for the name jackson, while the configurer
found by that name is the one of the JAR first
+ * on the classpath (CAMEL-27164). This test puts camel-jackson first: the
options of the data format must still be set
+ * with the configurer of the Jackson 3.x data format.
+ */
+public class KameletJacksonBothLinesTest extends CamelTestSupport {
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext context = super.createCamelContext();
+ context.setClassResolver(new
Jackson2ConfigurersFirstClassResolver(context));
+ // a data format configured on the context (as with
camel.dataformat.jackson.* in Camel Main), whose options
+ // the data formats in the routes take
+ JacksonDataFormat configured = (JacksonDataFormat)
context.resolveDataFormat("jackson");
+ configured.setPrettyPrint(true);
+ return context;
+ }
+
+ @Test
+ public void theConfigurerIsTheOneOfTheDataFormat() {
+
assertThat(PluginHelper.getConfigurerResolver(context).resolvePropertyConfigurer("jackson-dataformat",
context))
+ .as("the configurer of camel-jackson is found first by name")
+
.isInstanceOf(org.apache.camel.component.jackson.JacksonDataFormatConfigurer.class);
+
+ JacksonDataFormat dataFormat = new JacksonDataFormat();
+
assertThat(PropertyConfigurerHelper.resolvePropertyConfigurerByName(context,
"jackson-dataformat", dataFormat))
+ .isInstanceOf(JacksonDataFormatConfigurer.class);
+ assertThat(PropertyConfigurerHelper.resolvePropertyConfigurer(context,
dataFormat))
+ .isInstanceOf(JacksonDataFormatConfigurer.class);
+ }
+
+ @Test
+ public void unmarshalType() {
+ Object body = template.requestBody("direct:unmarshal",
"{\"name\":\"Camel\"}");
+
+ assertThat(body).isInstanceOfSatisfying(Greeting.class, g ->
assertThat(g.getName()).isEqualTo("Camel"));
+ }
+
+ @Test
+ public void optionsOfTheDataFormatConfiguredOnTheContext() {
+ Greeting greeting = new Greeting();
+ greeting.setName("Camel");
+
+ String json = template.requestBody("direct:marshal", greeting,
String.class);
+
+ assertThat(json).isEqualTo("{%s \"name\" : \"Camel\"%s}",
System.lineSeparator(), System.lineSeparator());
+ }
+
+ @Override
+ protected RoutesBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ routeTemplate("json-to-greeting")
+ .from("kamelet:source")
+ .unmarshal().json(JsonLibrary.Jackson, Greeting.class);
+
+ routeTemplate("greeting-to-json")
+ .from("kamelet:source")
+ .marshal().json(JsonLibrary.Jackson);
+
+ from("direct:unmarshal").kamelet("json-to-greeting");
+ from("direct:marshal").kamelet("greeting-to-json");
+ }
+ };
+ }
+
+ public static class Greeting {
+ private String name;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+ }
+
+ /**
+ * Finds the configurers that camel-jackson registers before those of
camel-jackson3, as when the JAR of
+ * camel-jackson comes first on the classpath.
+ */
+ private static final class Jackson2ConfigurersFirstClassResolver extends
DefaultClassResolver {
+
+ private Jackson2ConfigurersFirstClassResolver(CamelContext
camelContext) {
+ super(camelContext);
+ }
+
+ @Override
+ public InputStream loadResourceAsStream(String uri) {
+ if (uri.startsWith(ConfigurerResolver.RESOURCE_PATH)) {
+ URL url = findInJackson2(uri);
+ if (url != null) {
+ try {
+ return url.openStream();
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
+ }
+ return super.loadResourceAsStream(uri);
+ }
+
+ private static URL findInJackson2(String uri) {
+ CodeSource source =
org.apache.camel.component.jackson.JacksonDataFormat.class.getProtectionDomain()
+ .getCodeSource();
+ if (source == null) {
+ return null;
+ }
+ String jar = source.getLocation().toString();
+ try {
+ Enumeration<URL> urls =
Jackson2ConfigurersFirstClassResolver.class.getClassLoader().getResources(uri);
+ while (urls.hasMoreElements()) {
+ URL url = urls.nextElement();
+ if (url.toString().contains(jar)) {
+ return url;
+ }
+ }
+ return null;
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
+ }
+}
diff --git
a/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestProducer.java
b/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestProducer.java
index bb1d9822992e..93c84a5f9849 100644
---
a/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestProducer.java
+++
b/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestProducer.java
@@ -40,6 +40,7 @@ import org.apache.camel.support.AsyncProcessorConverterHelper;
import org.apache.camel.support.DefaultAsyncProducer;
import org.apache.camel.support.PluginHelper;
import org.apache.camel.support.PropertyBindingSupport;
+import org.apache.camel.support.PropertyConfigurerHelper;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.util.ArtifactUtils;
import org.apache.camel.util.FileUtil;
@@ -374,8 +375,8 @@ public class RestProducer extends DefaultAsyncProducer {
if (json != null) {
// lookup configurer
- PropertyConfigurer configurer =
PluginHelper.getConfigurerResolver(camelContext)
- .resolvePropertyConfigurer(name +
"-dataformat-configurer", camelContext);
+ PropertyConfigurer configurer =
PropertyConfigurerHelper.resolvePropertyConfigurerByName(camelContext,
+ name + "-dataformat-configurer", json);
if (configurer == null) {
throw new IllegalStateException("Cannot find configurer for
dataformat: " + name);
}
diff --git
a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/dataformat/DataFormatReifier.java
b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/dataformat/DataFormatReifier.java
index 6db4f512f39b..7cbadf949191 100644
---
a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/dataformat/DataFormatReifier.java
+++
b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/dataformat/DataFormatReifier.java
@@ -32,7 +32,6 @@ import org.apache.camel.spi.ExtendedPropertyConfigurerGetter;
import org.apache.camel.spi.PropertyConfigurer;
import org.apache.camel.spi.PropertyConfigurerAware;
import org.apache.camel.support.CamelContextHelper;
-import org.apache.camel.support.PluginHelper;
import org.apache.camel.support.PropertyBindingSupport;
import org.apache.camel.support.PropertyConfigurerHelper;
import org.apache.camel.util.ArtifactUtils;
@@ -295,8 +294,8 @@ public abstract class DataFormatReifier<T extends
DataFormatDefinition> extends
if (dataFormatName != null &&
camelContext.getDataFormatNames().contains(dataFormatName)) {
DataFormat df = camelContext.resolveDataFormat(dataFormatName);
if (df != null) {
- PropertyConfigurer configurer =
PluginHelper.getConfigurerResolver(camelContext)
- .resolvePropertyConfigurer(dataFormatName +
"-dataformat", camelContext);
+ PropertyConfigurer configurer =
PropertyConfigurerHelper.resolvePropertyConfigurerByName(camelContext,
+ dataFormatName + "-dataformat", df);
if (configurer == null) {
configurer =
PropertyConfigurerHelper.resolvePropertyConfigurer(camelContext, df);
}
@@ -341,8 +340,7 @@ public abstract class DataFormatReifier<T extends
DataFormatDefinition> extends
}
if (configurer == null) {
String configurerName = name + "-dataformat-configurer";
- configurer = PluginHelper.getConfigurerResolver(camelContext)
- .resolvePropertyConfigurer(configurerName, camelContext);
+ configurer =
PropertyConfigurerHelper.resolvePropertyConfigurerByName(camelContext,
configurerName, dataFormat);
}
return configurer;
}
diff --git
a/core/camel-core/src/test/java/org/apache/camel/support/PropertyConfigurerHelperByNameTest.java
b/core/camel-core/src/test/java/org/apache/camel/support/PropertyConfigurerHelperByNameTest.java
new file mode 100644
index 000000000000..b21b332c1b85
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/support/PropertyConfigurerHelperByNameTest.java
@@ -0,0 +1,149 @@
+/*
+ * 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.camel.support;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.spi.ConfigurerResolver;
+import org.apache.camel.spi.GeneratedPropertyConfigurer;
+import org.apache.camel.spi.PropertyConfigurer;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+/**
+ * Two JARs can register a configurer with the same name (such as
jackson-dataformat in camel-jackson and
+ * camel-jackson3). {@link
PropertyConfigurerHelper#resolvePropertyConfigurerByName(CamelContext, String,
Object)} then
+ * uses the configurer generated for the type of the target when the one found
by name is generated for another type.
+ */
+public class PropertyConfigurerHelperByNameTest extends ContextTestSupport {
+
+ private static final String NAME = "foo-dataformat";
+
+ @Test
+ public void testFallbackToConfigurerOfTarget() {
+ // the configurer found by name is the one of another JAR
+ PropertyConfigurer other = new OtherDataFormatConfigurer();
+ resolveByName(other);
+
+ PropertyConfigurer configurer
+ =
PropertyConfigurerHelper.resolvePropertyConfigurerByName(context, NAME, new
FooDataFormat());
+ assertInstanceOf(FooDataFormatConfigurer.class, configurer);
+
+ // the lookup by the type of the target maps FooDataFormat to
foo-dataformat
+ configurer =
PropertyConfigurerHelper.resolvePropertyConfigurer(context,
FooDataFormat.class);
+ assertInstanceOf(FooDataFormatConfigurer.class, configurer);
+ }
+
+ @Test
+ public void testConfigurerOfTargetIsKept() {
+ PropertyConfigurer own = new FooDataFormatConfigurer();
+ resolveByName(own);
+
+ assertSame(own,
PropertyConfigurerHelper.resolvePropertyConfigurerByName(context, NAME, new
FooDataFormat()));
+ }
+
+ @Test
+ public void
testTargetWithoutGeneratedConfigurerKeepsConfigurerFoundByName() {
+ PropertyConfigurer other = new OtherDataFormatConfigurer();
+ resolveByName(other);
+
+ assertSame(other,
PropertyConfigurerHelper.resolvePropertyConfigurerByName(context, NAME, new
BarDataFormat()));
+ }
+
+ @Test
+ public void testSubclassTargetKeepsConfigurerOfParent() {
+ PropertyConfigurer parent = new FooDataFormatConfigurer();
+ resolveByName(parent);
+
+ assertSame(parent,
+
PropertyConfigurerHelper.resolvePropertyConfigurerByName(context, NAME, new
SubFooDataFormat()));
+ }
+
+ @Test
+ public void testSubclassTargetFallsBackToConfigurerOfParent() {
+ resolveByName(new OtherDataFormatConfigurer());
+
+ PropertyConfigurer configurer
+ =
PropertyConfigurerHelper.resolvePropertyConfigurerByName(context, NAME, new
SubFooDataFormat());
+ assertInstanceOf(FooDataFormatConfigurer.class, configurer);
+ }
+
+ @Test
+ public void testConfigurerNotGeneratedIsKept() {
+ PropertyConfigurer custom = new CustomConfigurer();
+ resolveByName(custom);
+
+ assertSame(custom,
PropertyConfigurerHelper.resolvePropertyConfigurerByName(context, NAME, new
FooDataFormat()));
+ }
+
+ @Test
+ public void testNoTargetKeepsConfigurerFoundByName() {
+ PropertyConfigurer other = new OtherDataFormatConfigurer();
+ resolveByName(other);
+
+ assertSame(other,
PropertyConfigurerHelper.resolvePropertyConfigurerByName(context, NAME, null));
+ }
+
+ @Test
+ public void testNoConfigurerFoundByName() {
+ resolveByName(null);
+
+
assertNull(PropertyConfigurerHelper.resolvePropertyConfigurerByName(context,
NAME, new FooDataFormat()));
+ }
+
+ private void resolveByName(PropertyConfigurer configurer) {
+ ConfigurerResolver resolver = (name, camelContext) ->
NAME.equals(name) ? configurer : null;
+
context.getCamelContextExtension().addContextPlugin(ConfigurerResolver.class,
resolver);
+ }
+
+ public static class FooDataFormat {
+ }
+
+ public static class SubFooDataFormat extends FooDataFormat {
+ }
+
+ public static class BarDataFormat {
+ }
+
+ public static class OtherDataFormat {
+ }
+
+ // named after the type with the Configurer suffix, as Camel generates it
+ public static class FooDataFormatConfigurer implements
GeneratedPropertyConfigurer {
+ @Override
+ public boolean configure(CamelContext camelContext, Object target,
String name, Object value, boolean ignoreCase) {
+ return false;
+ }
+ }
+
+ public static class OtherDataFormatConfigurer implements
GeneratedPropertyConfigurer {
+ @Override
+ public boolean configure(CamelContext camelContext, Object target,
String name, Object value, boolean ignoreCase) {
+ return false;
+ }
+ }
+
+ public static class CustomConfigurer implements PropertyConfigurer {
+ @Override
+ public boolean configure(CamelContext camelContext, Object target,
String name, Object value, boolean ignoreCase) {
+ return false;
+ }
+ }
+}
diff --git
a/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java
b/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java
index 6c79ea6b51f9..bc586fac0734 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java
@@ -42,6 +42,7 @@ import org.apache.camel.spi.PropertyConfigurer;
import org.apache.camel.support.CamelContextHelper;
import org.apache.camel.support.PluginHelper;
import org.apache.camel.support.PropertyBindingSupport;
+import org.apache.camel.support.PropertyConfigurerHelper;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.ObjectHelper;
@@ -501,7 +502,7 @@ public final class MainHelper {
return null;
}
PropertyConfigurer configurer
- =
PluginHelper.getConfigurerResolver(context).resolvePropertyConfigurer(name +
"-" + kind, context);
+ =
PropertyConfigurerHelper.resolvePropertyConfigurerByName(context, name + "-" +
kind, target);
// the configurer that PackageDataFormatMojo or PackageLanguageMojo
generates for a type is named after it
if (configurer != null &&
configurer.getClass().getName().equals(target.getClass().getName() +
"Configurer")) {
return configurer;
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/LifecycleStrategySupport.java
b/core/camel-support/src/main/java/org/apache/camel/support/LifecycleStrategySupport.java
index 14db96b922ea..1ea4a6e11bb8 100644
---
a/core/camel-support/src/main/java/org/apache/camel/support/LifecycleStrategySupport.java
+++
b/core/camel-support/src/main/java/org/apache/camel/support/LifecycleStrategySupport.java
@@ -245,8 +245,8 @@ public abstract class LifecycleStrategySupport implements
LifecycleStrategy {
}
protected static void doAutoWire(String name, String kind, Object target,
CamelContext camelContext) {
- PropertyConfigurer pc =
PluginHelper.getConfigurerResolver(camelContext)
- .resolvePropertyConfigurer(name + "-" + kind, camelContext);
+ PropertyConfigurer pc
+ =
PropertyConfigurerHelper.resolvePropertyConfigurerByName(camelContext, name +
"-" + kind, target);
if (pc instanceof PropertyConfigurerGetter getter) {
String[] names = getter.getAutowiredNames();
if (names != null) {
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/PropertyConfigurerHelper.java
b/core/camel-support/src/main/java/org/apache/camel/support/PropertyConfigurerHelper.java
index 119f950eaf67..5b2250588c8a 100644
---
a/core/camel-support/src/main/java/org/apache/camel/support/PropertyConfigurerHelper.java
+++
b/core/camel-support/src/main/java/org/apache/camel/support/PropertyConfigurerHelper.java
@@ -18,10 +18,13 @@ package org.apache.camel.support;
import org.apache.camel.CamelContext;
import org.apache.camel.Component;
+import org.apache.camel.spi.GeneratedPropertyConfigurer;
import org.apache.camel.spi.PropertyConfigurer;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.StringHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Helper class for dealing with configurers.
@@ -31,6 +34,8 @@ import org.apache.camel.util.StringHelper;
*/
public final class PropertyConfigurerHelper {
+ private static final Logger LOG =
LoggerFactory.getLogger(PropertyConfigurerHelper.class);
+
private PropertyConfigurerHelper() {
}
@@ -74,15 +79,15 @@ public final class PropertyConfigurerHelper {
if (targetType.getName().endsWith("Component")) {
String name = StringHelper.before(targetType.getSimpleName(),
"Component");
name = StringHelper.camelCaseToDash(name) + "-component";
- return
PluginHelper.getConfigurerResolver(context).resolvePropertyConfigurer(name,
context);
+ return resolvePropertyConfigurerByNameForType(context, name,
targetType);
} else if (targetType.getName().endsWith("Language")) {
String name = StringHelper.before(targetType.getSimpleName(),
"Language");
name = StringHelper.camelCaseToDash(name) + "-language";
- return
PluginHelper.getConfigurerResolver(context).resolvePropertyConfigurer(name,
context);
+ return resolvePropertyConfigurerByNameForType(context, name,
targetType);
} else if (targetType.getName().endsWith("DataFormat")) {
String name = StringHelper.before(targetType.getSimpleName(),
"DataFormat");
name = StringHelper.camelCaseToDash(name) + "-dataformat";
- return
PluginHelper.getConfigurerResolver(context).resolvePropertyConfigurer(name,
context);
+ return resolvePropertyConfigurerByNameForType(context, name,
targetType);
}
if (targetType.isAnonymousClass()) {
@@ -103,6 +108,75 @@ public final class PropertyConfigurerHelper {
return null;
}
+ /**
+ * Resolves the configurer of a component, data format or language by its
name (such as jackson-dataformat), and
+ * makes sure it is the configurer of the given target.
+ * <p>
+ * Two JARs can register a configurer with the same name, and then the one
first on the classpath is found. For
+ * example both camel-jackson (Jackson 2.x) and camel-jackson3 (Jackson
3.x) register jackson-dataformat, while the
+ * jackson data format is created from camel-jackson3 when both are on the
classpath. When the configurer found by
+ * name is generated for another type than the target, the configurer
generated for the type of the target is used
+ * instead.
+ *
+ * @param context the camel context
+ * @param name the name of the configurer, such as jackson-dataformat
+ * @param target the component, data format or language to configure, or
<code>null</code> to only resolve the
+ * configurer by its name
+ * @return the resolved configurer, or <code>null</code> if no
configurer could be found
+ */
+ public static PropertyConfigurer
resolvePropertyConfigurerByName(CamelContext context, String name, Object
target) {
+ return resolvePropertyConfigurerByNameForType(context, name, target !=
null ? target.getClass() : null);
+ }
+
+ private static PropertyConfigurer resolvePropertyConfigurerByNameForType(
+ CamelContext context, String name, Class<?> targetType) {
+ ObjectHelper.notNull(context, "context");
+
+ PropertyConfigurer configurer =
PluginHelper.getConfigurerResolver(context).resolvePropertyConfigurer(name,
context);
+ if (targetType != null && configurer instanceof
GeneratedPropertyConfigurer
+ && !isGeneratedFor(configurer, targetType)) {
+ PropertyConfigurer own =
resolveGeneratedPropertyConfigurer(context, targetType);
+ if (own != null) {
+ LOG.debug("The configurer: {} found by name: {} is not for
type: {}. Using the configurer: {} instead.",
+ configurer.getClass().getName(), name,
targetType.getName(), own.getClass().getName());
+ return own;
+ }
+ }
+ return configurer;
+ }
+
+ /**
+ * Whether the configurer is the one Camel generates for the type (or a
super type), which is named after the type
+ * with the Configurer suffix.
+ */
+ private static boolean isGeneratedFor(PropertyConfigurer configurer,
Class<?> type) {
+ String name = configurer.getClass().getName();
+ for (Class<?> t = type; t != null && t != Object.class; t =
t.getSuperclass()) {
+ if (name.equals(t.getName() + "Configurer")) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * The configurer Camel generates for the type (or a super type), which is
named after the type with the Configurer
+ * suffix.
+ */
+ private static PropertyConfigurer
resolveGeneratedPropertyConfigurer(CamelContext context, Class<?> type) {
+ for (Class<?> t = type; t != null && t != Object.class; t =
t.getSuperclass()) {
+ ClassLoader loader = t.getClassLoader();
+ if (loader == null) {
+ continue;
+ }
+ Class<?> clazz =
context.getClassResolver().resolveClass(t.getName() + "Configurer", loader);
+ if (clazz != null &&
GeneratedPropertyConfigurer.class.isAssignableFrom(clazz)) {
+ return (PropertyConfigurer)
context.getInjector().newInstance(clazz, false);
+ }
+ }
+ return null;
+ }
+
/**
* Resolves the given configurer.
*
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/processor/RestBindingAdviceFactory.java
b/core/camel-support/src/main/java/org/apache/camel/support/processor/RestBindingAdviceFactory.java
index 06eead3cc321..b0289739da76 100644
---
a/core/camel-support/src/main/java/org/apache/camel/support/processor/RestBindingAdviceFactory.java
+++
b/core/camel-support/src/main/java/org/apache/camel/support/processor/RestBindingAdviceFactory.java
@@ -198,8 +198,8 @@ public class RestBindingAdviceFactory {
}
private static void copyOptions(CamelContext camelContext, String name,
DataFormat source, DataFormat target) {
- PropertyConfigurer configurer =
PluginHelper.getConfigurerResolver(camelContext)
- .resolvePropertyConfigurer(name + "-dataformat", camelContext);
+ PropertyConfigurer configurer
+ =
PropertyConfigurerHelper.resolvePropertyConfigurerByName(camelContext, name +
"-dataformat", source);
if (configurer == null) {
configurer =
PropertyConfigurerHelper.resolvePropertyConfigurer(camelContext, source);
}