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/03/14 08:50:43 UTC
[camel] branch master updated: CAMEL-14716: @BeanConfigInject add
support for Map
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 bc5d106 CAMEL-14716: @BeanConfigInject add support for Map
bc5d106 is described below
commit bc5d10639193d15f9778a1b8441be84a6facfc89
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Mar 14 09:50:23 2020 +0100
CAMEL-14716: @BeanConfigInject add support for Map
---
.../impl/engine/CamelPostProcessorHelper.java | 61 +++++++++++-----
.../camel/main/MainIoCBeanConfigInjectMapTest.java | 83 ++++++++++++++++++++++
.../MainIoCBeanConfigInjectPropertiesTest.java | 83 ++++++++++++++++++++++
3 files changed, 211 insertions(+), 16 deletions(-)
diff --git a/core/camel-base/src/main/java/org/apache/camel/impl/engine/CamelPostProcessorHelper.java b/core/camel-base/src/main/java/org/apache/camel/impl/engine/CamelPostProcessorHelper.java
index fc94c42..423b857 100644
--- a/core/camel-base/src/main/java/org/apache/camel/impl/engine/CamelPostProcessorHelper.java
+++ b/core/camel-base/src/main/java/org/apache/camel/impl/engine/CamelPostProcessorHelper.java
@@ -17,7 +17,9 @@
package org.apache.camel.impl.engine;
import java.lang.reflect.Method;
+import java.util.HashMap;
import java.util.LinkedHashMap;
+import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
@@ -311,20 +313,33 @@ public class CamelPostProcessorHelper implements CamelContextAware {
public Object getInjectionBeanConfigValue(Class<?> type, String name) {
ExtendedCamelContext ecc = (ExtendedCamelContext) getCamelContext();
+ // is it a map or properties
+ boolean mapType = false;
+ Map map = null;
+ if (type.isAssignableFrom(Map.class)) {
+ map = new LinkedHashMap();
+ mapType = true;
+ } else if (type.isAssignableFrom(Properties.class)) {
+ map = new Properties();
+ mapType = true;
+ }
+
// create an instance of type
- Object bean;
- Set<?> instances = ecc.getRegistry().findByType(type);
- if (instances.size() == 1) {
- bean = instances.iterator().next();
- } else if (instances.size() > 1) {
- return null;
- } else {
- // attempt to create a new instance
- try {
- bean = ecc.getInjector().newInstance(type);
- } catch (Throwable e) {
- // ignore
+ Object bean = null;
+ if (map == null) {
+ Set<?> instances = ecc.getRegistry().findByType(type);
+ if (instances.size() == 1) {
+ bean = instances.iterator().next();
+ } else if (instances.size() > 1) {
return null;
+ } else {
+ // attempt to create a new instance
+ try {
+ bean = ecc.getInjector().newInstance(type);
+ } catch (Throwable e) {
+ // ignore
+ return null;
+ }
}
}
@@ -334,12 +349,27 @@ public class CamelPostProcessorHelper implements CamelContextAware {
if (rootKey.endsWith(".")) {
rootKey = rootKey.substring(0, rootKey.length() - 1);
}
+ String uRootKey = rootKey.toUpperCase(Locale.US);
- // get all properties and transfer to map
+ // get all properties and transfer to map
Properties props = ecc.getPropertiesComponent().loadProperties();
- Map<String, Object> map = new LinkedHashMap<>();
+ if (map == null) {
+ map = new LinkedHashMap<>();
+ }
for (String key : props.stringPropertyNames()) {
- map.put(key, props.getProperty(key));
+ String uKey = key.toUpperCase(Locale.US);
+ // need to ignore case
+ if (uKey.startsWith(uRootKey)) {
+ // strip prefix
+ String sKey = key.substring(rootKey.length());
+ if (sKey.startsWith(".")) {
+ sKey = sKey.substring(1);
+ }
+ map.put(sKey, props.getProperty(key));
+ }
+ }
+ if (mapType) {
+ return map;
}
// lookup configurer if there is any
@@ -358,7 +388,6 @@ public class CamelPostProcessorHelper implements CamelContextAware {
.withIgnoreCase(true)
.withTarget(bean)
.withConfigurer(configurer)
- .withOptionPrefix(rootKey + ".")
.withProperties(map)
.bind();
diff --git a/core/camel-main/src/test/java/org/apache/camel/main/MainIoCBeanConfigInjectMapTest.java b/core/camel-main/src/test/java/org/apache/camel/main/MainIoCBeanConfigInjectMapTest.java
new file mode 100644
index 0000000..48e5b6e
--- /dev/null
+++ b/core/camel-main/src/test/java/org/apache/camel/main/MainIoCBeanConfigInjectMapTest.java
@@ -0,0 +1,83 @@
+/*
+ * 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.Map;
+
+import org.apache.camel.BeanConfigInject;
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class MainIoCBeanConfigInjectMapTest extends Assert {
+
+ @Test
+ public void testMainIoC() throws Exception {
+ Main main = new Main();
+ main.addRoutesBuilder(new MyRouteBuilder());
+ main.addInitialProperty("bar.Name", "Thirsty Bear");
+ main.addInitialProperty("Bar.AGE", "23");
+ main.addInitialProperty("foo", "blah");
+ main.start();
+
+ CamelContext camelContext = main.getCamelContext();
+ assertNotNull(camelContext);
+
+ MockEndpoint endpoint = camelContext.getEndpoint("mock:results", MockEndpoint.class);
+ endpoint.expectedBodiesReceived("Thirsty Bear (minimum age: 23)");
+
+ main.getCamelTemplate().sendBody("direct:start", "Which bar");
+
+ endpoint.assertIsSatisfied();
+
+ main.stop();
+ }
+
+ public static class MyBar {
+
+ private final String description;
+
+ public MyBar(String description) {
+ this.description = description;
+ }
+
+ @Override
+ public String toString() {
+ return description;
+ }
+ }
+
+ public static class MyRouteBuilder extends RouteBuilder {
+
+ @BindToRegistry("bar")
+ public MyBar createBar(@BeanConfigInject("bar") Map config) {
+ assertEquals(2, config.size());
+ assertNull(config.get("foo"));
+
+ String text = config.get("Name") + " (minimum age: " + config.get("AGE") + ")";
+ return new MyBar(text);
+ }
+
+ @Override
+ public void configure() throws Exception {
+ from("direct:start").bean("bar").to("mock:results");
+ }
+ }
+}
diff --git a/core/camel-main/src/test/java/org/apache/camel/main/MainIoCBeanConfigInjectPropertiesTest.java b/core/camel-main/src/test/java/org/apache/camel/main/MainIoCBeanConfigInjectPropertiesTest.java
new file mode 100644
index 0000000..737ca02
--- /dev/null
+++ b/core/camel-main/src/test/java/org/apache/camel/main/MainIoCBeanConfigInjectPropertiesTest.java
@@ -0,0 +1,83 @@
+/*
+ * 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.Properties;
+
+import org.apache.camel.BeanConfigInject;
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class MainIoCBeanConfigInjectPropertiesTest extends Assert {
+
+ @Test
+ public void testMainIoC() throws Exception {
+ Main main = new Main();
+ main.addRoutesBuilder(new MyRouteBuilder());
+ main.addInitialProperty("bar.Name", "Thirsty Bear");
+ main.addInitialProperty("Bar.AGE", "23");
+ main.addInitialProperty("foo", "blah");
+ main.start();
+
+ CamelContext camelContext = main.getCamelContext();
+ assertNotNull(camelContext);
+
+ MockEndpoint endpoint = camelContext.getEndpoint("mock:results", MockEndpoint.class);
+ endpoint.expectedBodiesReceived("Thirsty Bear (minimum age: 23)");
+
+ main.getCamelTemplate().sendBody("direct:start", "Which bar");
+
+ endpoint.assertIsSatisfied();
+
+ main.stop();
+ }
+
+ public static class MyBar {
+
+ private final String description;
+
+ public MyBar(String description) {
+ this.description = description;
+ }
+
+ @Override
+ public String toString() {
+ return description;
+ }
+ }
+
+ public static class MyRouteBuilder extends RouteBuilder {
+
+ @BindToRegistry("bar")
+ public MyBar createBar(@BeanConfigInject("bar") Properties config) {
+ assertEquals(2, config.size());
+ assertNull(config.get("foo"));
+
+ String text = config.getProperty("Name") + " (minimum age: " + config.getProperty("AGE") + ")";
+ return new MyBar(text);
+ }
+
+ @Override
+ public void configure() throws Exception {
+ from("direct:start").bean("bar").to("mock:results");
+ }
+ }
+}