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 f3c4118fcc2 CAMEL-18865: camel-main - Configuring options on beans dot
vs square style. Dot style should use getter/setter and square should use
map/list style first.
f3c4118fcc2 is described below
commit f3c4118fcc2cb6d5860e58955f2d8f0cfe3e3b7b
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Jan 6 12:53:06 2023 +0100
CAMEL-18865: camel-main - Configuring options on beans dot vs square style.
Dot style should use getter/setter and square should use map/list style first.
---
.../org/apache/camel/main/BaseMainSupport.java | 10 ++++-
.../java/org/apache/camel/main/MainHelper.java | 18 +++++++-
.../java/org/apache/camel/main/MainBeansTest.java | 49 ++++++++++++++++++++++
.../java/org/apache/camel/main/MyFooFactory.java | 31 ++++++++++++++
.../camel/support/PropertyBindingSupport.java | 24 +++++++----
5 files changed, 122 insertions(+), 10 deletions(-)
diff --git
a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
index 329eac41901..c6c39c3c566 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
@@ -1445,7 +1445,15 @@ public abstract class BaseMainSupport extends
BaseService {
"Cannot resolve bean with name " + name);
}
// configure all the properties on the bean at once (to ensure
they are configured in right order)
- OrderedLocationProperties config =
MainHelper.extractProperties(properties, name + "[", "]");
+ OrderedLocationProperties config =
MainHelper.extractProperties(properties, name + "[", "]", true, (key) -> {
+ // when configuring map/list we want the keys to include the
square brackets
+ // (so we know it is a map/list style and not dot style syntax)
+ // and therefore only remove the option prefix name
+ if (key.startsWith(name + "[")) {
+ return key.substring(name.length());
+ }
+ return key;
+ });
setPropertiesOnTarget(camelContext, bean, config, optionPrefix +
name + ".", failIfNotSet, ignoreCase,
autoConfiguredProperties);
}
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 df8dcf85cd2..4a874fb4328 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
@@ -503,6 +503,12 @@ public final class MainHelper {
public static OrderedLocationProperties extractProperties(
OrderedLocationProperties properties, String optionPrefix, String
optionSuffix, boolean remove) {
+ return extractProperties(properties, optionPrefix, optionSuffix,
remove, null);
+ }
+
+ public static OrderedLocationProperties extractProperties(
+ OrderedLocationProperties properties, String optionPrefix, String
optionSuffix,
+ boolean remove, Function<String, String> onRemove) {
if (properties == null) {
return new OrderedLocationProperties();
}
@@ -514,9 +520,17 @@ public final class MainHelper {
String loc = properties.getLocation(key);
if (key.startsWith(optionPrefix)) {
Object value = properties.get(key);
- key = key.substring(optionPrefix.length());
+ if (onRemove != null) {
+ key = onRemove.apply(key);
+ } else {
+ key = key.substring(optionPrefix.length());
+ }
if (optionSuffix != null && key.endsWith(optionSuffix)) {
- key = key.substring(0, key.length() -
optionSuffix.length());
+ if (onRemove != null) {
+ key = onRemove.apply(key);
+ } else {
+ key = key.substring(0, key.length() -
optionSuffix.length());
+ }
}
rc.put(loc, key, value);
if (remove) {
diff --git
a/core/camel-main/src/test/java/org/apache/camel/main/MainBeansTest.java
b/core/camel-main/src/test/java/org/apache/camel/main/MainBeansTest.java
index 6fa07348eb1..cb649abfccd 100644
--- a/core/camel-main/src/test/java/org/apache/camel/main/MainBeansTest.java
+++ b/core/camel-main/src/test/java/org/apache/camel/main/MainBeansTest.java
@@ -26,6 +26,7 @@ import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
public class MainBeansTest {
@@ -197,6 +198,54 @@ public class MainBeansTest {
main.stop();
}
+ @Test
+ public void testBindBeansDottedHybridMap() {
+ Main main = new Main();
+ main.configure().addRoutesBuilder(new MyRouteBuilder());
+
+ // defining a factory bean
+ main.addProperty("camel.beans.myfactory",
"#class:org.apache.camel.main.MyFooFactory");
+ main.addProperty("camel.beans.myfactory.hostName", "localhost");
+ main.start();
+
+ CamelContext camelContext = main.getCamelContext();
+ assertNotNull(camelContext);
+
+ Object bean = camelContext.getRegistry().lookupByName("myfactory");
+ assertNotNull(bean);
+ assertInstanceOf(MyFooFactory.class, bean);
+
+ MyFooFactory factory = (MyFooFactory) bean;
+ assertNull(factory.get("hostName"));
+ assertEquals("localhost", factory.getHostName());
+
+ main.stop();
+ }
+
+ @Test
+ public void testBindBeansSquareHybridMap() {
+ Main main = new Main();
+ main.configure().addRoutesBuilder(new MyRouteBuilder());
+
+ // defining a factory bean
+ main.addProperty("camel.beans.myfactory",
"#class:org.apache.camel.main.MyFooFactory");
+ main.addProperty("camel.beans.myfactory[hostName]", "localhost");
+ main.start();
+
+ CamelContext camelContext = main.getCamelContext();
+ assertNotNull(camelContext);
+
+ Object bean = camelContext.getRegistry().lookupByName("myfactory");
+ assertNotNull(bean);
+ assertInstanceOf(MyFooFactory.class, bean);
+
+ MyFooFactory factory = (MyFooFactory) bean;
+ assertNull(factory.getHostName());
+ assertEquals("localhost", factory.get("hostName"));
+
+ main.stop();
+ }
+
public static class MyRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
diff --git
a/core/camel-main/src/test/java/org/apache/camel/main/MyFooFactory.java
b/core/camel-main/src/test/java/org/apache/camel/main/MyFooFactory.java
new file mode 100644
index 00000000000..53e48cbf4cf
--- /dev/null
+++ b/core/camel-main/src/test/java/org/apache/camel/main/MyFooFactory.java
@@ -0,0 +1,31 @@
+/*
+ * 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.main;
+
+import java.util.HashMap;
+
+public class MyFooFactory extends HashMap<String, Object> {
+ private String hostName;
+
+ public String getHostName() {
+ return hostName;
+ }
+
+ public void setHostName(String hostName) {
+ this.hostName = hostName;
+ }
+}
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
index 1288a6ea9b6..0af669f5a2a 100644
---
a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
+++
b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
@@ -493,6 +493,11 @@ public final class PropertyBindingSupport {
if (configurer != null) {
bound = setSimplePropertyViaConfigurer(camelContext,
target, key, value, ignoreCase, configurer);
}
+ if (!bound && reflection) {
+ // fallback to reflection based
+ bound = setSimplePropertyViaReflection(camelContext,
target, key, value, fluentBuilder, allowPrivateSetter,
+ reflection, ignoreCase);
+ }
// if the target value is a map type, then we can skip
reflection
// and set the entry
if (!bound && Map.class.isAssignableFrom(target.getClass())) {
@@ -511,12 +516,6 @@ public final class PropertyBindingSupport {
// ignore
}
}
- if (!bound && reflection) {
- // fallback to reflection based
- bound = setSimplePropertyViaReflection(camelContext,
target, key, value, fluentBuilder, allowPrivateSetter,
- reflection, ignoreCase);
- }
-
}
} catch (PropertyBindingException e) {
throw e;
@@ -544,9 +543,19 @@ public final class PropertyBindingSupport {
int pos = name.indexOf('[');
String lookupKey = name.substring(pos + 1, name.length() - 1);
+ lookupKey = StringHelper.removeLeadingAndEndingQuotes(lookupKey);
String key = name.substring(0, pos);
- Object obj = bi.getOrElseProperty(target, key, null, ignoreCase);
+ Object obj = null;
+ if (pos == 0) {
+ // there are no prefix key to invoke as getter first, so check if
target is an object
+ // we can use for collection
+ if (target instanceof Map || target instanceof List ||
target.getClass().isArray()) {
+ obj = target;
+ }
+ } else {
+ obj = bi.getOrElseProperty(target, key, null, ignoreCase);
+ }
if (obj == null) {
// it was supposed to be a list or map, but its null, so lets
create a new list or map and set it automatically
Method getter = bi.getPropertyGetter(target.getClass(), key,
ignoreCase);
@@ -635,6 +644,7 @@ public final class PropertyBindingSupport {
int pos = name.indexOf('[');
String lookupKey = name.substring(pos + 1, name.length() - 1);
+ lookupKey = StringHelper.removeLeadingAndEndingQuotes(lookupKey);
String key = name.substring(0, pos);
String undashKey = undashKey(key);