You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/11/25 05:11:23 UTC
[camel] branch master updated: CAMEL-15887: add support for
java.util.Properties (support for using properties as map values) (#4677)
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push:
new 612d441 CAMEL-15887: add support for java.util.Properties (support for using properties as map values) (#4677)
612d441 is described below
commit 612d4416fa2c89829f10557bb2127b7521bac969
Author: Luca Burgazzoli <lb...@users.noreply.github.com>
AuthorDate: Wed Nov 25 06:11:11 2020 +0100
CAMEL-15887: add support for java.util.Properties (support for using properties as map values) (#4677)
---
.../PropertyBindingSupportPropertiesTest.java | 95 ++++++++++++++++++++++
.../camel/support/PropertyBindingSupport.java | 40 +++++++--
2 files changed, 129 insertions(+), 6 deletions(-)
diff --git a/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportPropertiesTest.java b/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportPropertiesTest.java
index 8f1f0c9..d795210 100644
--- a/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportPropertiesTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportPropertiesTest.java
@@ -17,6 +17,7 @@
package org.apache.camel.support;
import java.util.Locale;
+import java.util.Map;
import java.util.Properties;
import org.apache.camel.CamelContext;
@@ -64,6 +65,39 @@ public class PropertyBindingSupportPropertiesTest extends ContextTestSupport {
assertEquals("company2", bar.getWorks().getProperty("burger"));
}
+ @Test
+ public void testPropertiesMap() {
+ BarWithMap bar = new BarWithMap();
+
+ PropertyBindingSupport.build()
+ .withCamelContext(context)
+ .withReflection(true)
+ .withTarget(bar)
+ .withProperty("works[acme].name", "company1")
+ .withProperty("works[burger].name", "company2")
+ .bind();
+
+ assertEquals("company1", bar.getWorks().get("acme").getProperty("name"));
+ assertEquals("company2", bar.getWorks().get("burger").getProperty("name"));
+ }
+
+ @Test
+ public void testPropertiesMapWithConfigurer() {
+ BarWithMap bar = new BarWithMap();
+
+ PropertyBindingSupport.build()
+ .withCamelContext(context)
+ .withReflection(false)
+ .withConfigurer(new BarWithMapConfigurer())
+ .withTarget(bar)
+ .withProperty("works[acme].name", "company1")
+ .withProperty("works[burger].name", "company2")
+ .bind();
+
+ assertEquals("company1", bar.getWorks().get("acme").getProperty("name"));
+ assertEquals("company2", bar.getWorks().get("burger").getProperty("name"));
+ }
+
public static class Bar {
private Properties works;
@@ -76,6 +110,18 @@ public class PropertyBindingSupportPropertiesTest extends ContextTestSupport {
}
}
+ public static class BarWithMap {
+ private Map<String, Properties> works;
+
+ public Map<String, Properties> getWorks() {
+ return works;
+ }
+
+ public void setWorks(Map<String, Properties> works) {
+ this.works = works;
+ }
+ }
+
private static class BarConfigurer implements PropertyConfigurer, PropertyConfigurerGetter {
@Override
public boolean configure(CamelContext camelContext, Object target, String name, Object value, boolean ignoreCase) {
@@ -116,4 +162,53 @@ public class PropertyBindingSupportPropertiesTest extends ContextTestSupport {
}
}
+ private static class BarWithMapConfigurer implements PropertyConfigurer, PropertyConfigurerGetter {
+ @Override
+ public boolean configure(CamelContext camelContext, Object target, String name, Object value, boolean ignoreCase) {
+ if (ignoreCase) {
+ name = name.toLowerCase(Locale.ENGLISH);
+ }
+ if (target instanceof PropertyBindingSupportPropertiesTest.BarWithMap) {
+ PropertyBindingSupportPropertiesTest.BarWithMap bar = (PropertyBindingSupportPropertiesTest.BarWithMap) target;
+ if ("works".equals(name)) {
+ bar.setWorks((Map) value);
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public Class<?> getOptionType(String name, boolean ignoreCase) {
+ if ("works".equals(name)) {
+ return Map.class;
+ }
+
+ return null;
+ }
+
+ @Override
+ public Object getOptionValue(Object target, String name, boolean ignoreCase) {
+ if (ignoreCase) {
+ name = name.toLowerCase(Locale.ENGLISH);
+ }
+ if (target instanceof PropertyBindingSupportPropertiesTest.BarWithMap) {
+ PropertyBindingSupportPropertiesTest.BarWithMap bar = (PropertyBindingSupportPropertiesTest.BarWithMap) target;
+ if ("works".equals(name)) {
+ return bar.getWorks();
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public Object getCollectionValueType(Object target, String name, boolean ignoreCase) {
+ if ("works".equals(name)) {
+ return Properties.class;
+ }
+
+ return null;
+ }
+ }
+
}
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 735cf47..5401b7e 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
@@ -297,10 +297,16 @@ public final class PropertyBindingSupport {
// so we can use that to lookup as configurer
Class<?> collectionType = (Class<?>) ((PropertyConfigurerGetter) configurer)
.getCollectionValueType(newTarget, undashKey(key), ignoreCase);
- if (collectionType != null) {
- configurer = PropertyConfigurerHelper.resolvePropertyConfigurer(camelContext, collectionType);
- } else {
- configurer = PropertyConfigurerHelper.resolvePropertyConfigurer(camelContext, prop.getClass());
+
+ if (collectionType == null) {
+ collectionType = prop.getClass();
+ }
+
+ configurer = PropertyConfigurerHelper.resolvePropertyConfigurer(camelContext, collectionType);
+ if (configurer == null) {
+ if (Map.class.isAssignableFrom(collectionType)) {
+ configurer = MapConfigurer.INSTANCE;
+ }
}
}
// prepare for next iterator
@@ -446,6 +452,12 @@ public final class PropertyBindingSupport {
if (configurer != null) {
bound = setSimplePropertyViaConfigurer(camelContext, target, key, value, ignoreCase, configurer);
}
+ // if the target value is a map type, then we can skip reflection
+ // and set the entry
+ if (!bound && Map.class.isAssignableFrom(target.getClass())) {
+ ((Map) target).put(key, value);
+ bound = true;
+ }
if (!bound && reflection) {
// fallback to reflection based
bound = setSimplePropertyViaReflection(camelContext, target, key, value, fluentBuilder, allowPrivateSetter,
@@ -857,7 +869,9 @@ public final class PropertyBindingSupport {
if (answer == null) {
if (lookupKey != null) {
- if (Map.class.isAssignableFrom(type)) {
+ if (Properties.class.isAssignableFrom(type)) {
+ answer = new Properties();
+ } else if (Map.class.isAssignableFrom(type)) {
answer = new LinkedHashMap<>();
} else if (Collection.class.isAssignableFrom(type)) {
answer = new ArrayList<>();
@@ -986,7 +1000,9 @@ public final class PropertyBindingSupport {
if (answer == null) {
if (lookupKey != null) {
- if (Map.class.isAssignableFrom(type)) {
+ if (Properties.class.isAssignableFrom(type)) {
+ answer = new Properties();
+ } else if (Map.class.isAssignableFrom(type)) {
answer = new LinkedHashMap<>();
} else if (Collection.class.isAssignableFrom(type)) {
answer = new ArrayList<>();
@@ -1923,4 +1939,16 @@ public final class PropertyBindingSupport {
}
}
+ private static final class MapConfigurer implements PropertyConfigurer {
+ public static final PropertyConfigurer INSTANCE = new MapConfigurer();
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public boolean configure(
+ CamelContext camelContext, Object target, String name, Object value, boolean ignoreCase) {
+ ((Map) target).put(name, value);
+ return true;
+ }
+ }
+
}