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/13 15:52:08 UTC
[camel] 04/04: CAMEL-14670: @BeanConfigInject for injecting bean
configuration classes that are pre configured from properties files.
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
commit ae9cfdcc2118292707f4d5fdfcd1a7ee91990345
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Mar 13 16:21:12 2020 +0100
CAMEL-14670: @BeanConfigInject for injecting bean configuration classes that are pre configured from properties files.
---
.../impl/engine/DefaultCamelBeanPostProcessor.java | 81 +++++-----------------
.../impl/engine/CamelPostProcessorHelperTest.java | 36 ++++++++++
2 files changed, 53 insertions(+), 64 deletions(-)
diff --git a/core/camel-base/src/main/java/org/apache/camel/impl/engine/DefaultCamelBeanPostProcessor.java b/core/camel-base/src/main/java/org/apache/camel/impl/engine/DefaultCamelBeanPostProcessor.java
index ca9e7dc..ad395ca 100644
--- a/core/camel-base/src/main/java/org/apache/camel/impl/engine/DefaultCamelBeanPostProcessor.java
+++ b/core/camel-base/src/main/java/org/apache/camel/impl/engine/DefaultCamelBeanPostProcessor.java
@@ -21,38 +21,31 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Comparator;
-import java.util.LinkedHashMap;
import java.util.List;
-import java.util.Map;
-import java.util.Properties;
import java.util.Set;
+import org.apache.camel.BeanConfigInject;
import org.apache.camel.BeanInject;
import org.apache.camel.BindToRegistry;
import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
-import org.apache.camel.BeanConfigInject;
import org.apache.camel.DeferredContextBinding;
import org.apache.camel.EndpointInject;
import org.apache.camel.ExtendedCamelContext;
-import org.apache.camel.NoSuchBeanException;
import org.apache.camel.Produce;
import org.apache.camel.PropertyInject;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.TypeConverter;
import org.apache.camel.spi.CamelBeanPostProcessor;
-import org.apache.camel.spi.GeneratedPropertyConfigurer;
-import org.apache.camel.spi.PropertiesComponent;
import org.apache.camel.spi.Registry;
import org.apache.camel.support.DefaultEndpoint;
-import org.apache.camel.support.PropertyBindingSupport;
import org.apache.camel.util.ReflectionHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+
import static org.apache.camel.support.ObjectHelper.invokeMethod;
import static org.apache.camel.util.ObjectHelper.isEmpty;
-import static org.apache.camel.util.ObjectHelper.loadClass;
/**
* A bean post processor which implements the <a href="http://camel.apache.org/bean-integration.html">Bean Integration</a>
@@ -342,6 +335,11 @@ public class DefaultCamelBeanPostProcessor implements CamelBeanPostProcessor {
setterBeanInjection(method, beanInject.value(), bean, beanName);
}
+ BeanConfigInject beanConfigInject = method.getAnnotation(BeanConfigInject.class);
+ if (beanConfigInject != null) {
+ setterBeanConfigInjection(method, beanConfigInject.value(), bean, beanName);
+ }
+
EndpointInject endpointInject = method.getAnnotation(EndpointInject.class);
if (endpointInject != null) {
String uri = endpointInject.value().isEmpty() ? endpointInject.uri() : endpointInject.value();
@@ -389,6 +387,16 @@ public class DefaultCamelBeanPostProcessor implements CamelBeanPostProcessor {
}
}
+ public void setterBeanConfigInjection(Method method, String name, Object bean, String beanName) {
+ Class<?>[] parameterTypes = method.getParameterTypes();
+ if (parameterTypes.length != 1) {
+ LOG.warn("Ignoring badly annotated method for injection due to incorrect number of parameters: {}", method);
+ } else {
+ Object value = getPostProcessorHelper().getInjectionBeanConfigValue(parameterTypes[0], name);
+ invokeMethod(method, bean, value);
+ }
+ }
+
private void bindToRegistry(Class<?> clazz, String name, Object bean, String beanName, boolean beanPostProcess) {
if (isEmpty(name)) {
name = clazz.getSimpleName();
@@ -513,59 +521,4 @@ public class DefaultCamelBeanPostProcessor implements CamelBeanPostProcessor {
return parameters;
}
- private Object resolveBeanConfigInject(ExtendedCamelContext ecc, BeanConfigInject pi, Class<?> type) {
- // 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
- return null;
- }
- }
-
- // root key
- String rootKey = pi.value();
- // clip trailing dot
- if (rootKey.endsWith(".")) {
- rootKey = rootKey.substring(0, rootKey.length() - 1);
- }
-
- // get all properties and transfer to map
- Properties props = ecc.getPropertiesComponent().loadProperties();
- Map<String, Object> map = new LinkedHashMap<>();
- for (String key : props.stringPropertyNames()) {
- map.put(key, props.getProperty(key));
- }
-
- // lookup configurer if there is any
- // use FQN class name first, then simple name, and root key last
- GeneratedPropertyConfigurer configurer = null;
- String[] names = new String[]{type.getName() + "-configurer", type.getSimpleName() + "-configurer", rootKey + "-configurer"};
- for (String name : names) {
- configurer = ecc.getConfigurerResolver().resolvePropertyConfigurer(name, ecc);
- if (configurer != null) {
- break;
- }
- }
-
- new PropertyBindingSupport.Builder()
- .withCamelContext(ecc)
- .withIgnoreCase(true)
- .withTarget(bean)
- .withConfigurer(configurer)
- .withOptionPrefix(rootKey + ".")
- .withProperties(map)
- .bind();
-
- return bean;
- }
-
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/engine/CamelPostProcessorHelperTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/engine/CamelPostProcessorHelperTest.java
index fedf639..ea7653e 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/engine/CamelPostProcessorHelperTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/engine/CamelPostProcessorHelperTest.java
@@ -487,6 +487,27 @@ public class CamelPostProcessorHelperTest extends ContextTestSupport {
}
@Test
+ public void testBeanConfigInjectByMethod() throws Exception {
+ Properties initial = new Properties();
+ initial.put("foobar.name", "Goofy");
+ initial.put("foobar.age", "34");
+ context.getPropertiesComponent().setInitialProperties(initial);
+
+ CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
+
+ MyBeanConfigInjectByMethod bean = new MyBeanConfigInjectByMethod();
+ Method method = bean.getClass().getMethod("initFooBar", FooBarConfig.class);
+
+ BeanConfigInject beanInject = method.getAnnotation(BeanConfigInject.class);
+ Class<?> type = method.getParameterTypes()[0];
+ Object value = helper.getInjectionBeanConfigValue(type, beanInject.value());
+ method.invoke(bean, value);
+
+ String out = bean.doSomething("Camel");
+ assertEquals("Goofy (age: 34) likes Camel", out);
+ }
+
+ @Test
public void testFluentProducerTemplateWithNoInjection() throws Exception {
CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
NoBeanInjectionTestClass myBean = new NoBeanInjectionTestClass();
@@ -777,4 +798,19 @@ public class CamelPostProcessorHelperTest extends ContextTestSupport {
}
}
+ public class MyBeanConfigInjectByMethod {
+
+ public FooBar fooBar;
+
+ @BeanConfigInject("foobar")
+ public void initFooBar(FooBarConfig config) {
+ fooBar = new FooBar();
+ fooBar.setGreeting(config.getName() + " (age: " + config.getAge() + ") likes");
+ }
+
+ public String doSomething(String body) {
+ return fooBar.hello(body);
+ }
+ }
+
}