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 2013/08/26 14:32:17 UTC
[2/4] git commit: CAMEL-3215: Added new @PropertyInject to inject
property placeholders in POJOs.
CAMEL-3215: Added new @PropertyInject to inject property placeholders in POJOs.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/8a90b273
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/8a90b273
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/8a90b273
Branch: refs/heads/master
Commit: 8a90b273dbca17bf899e06cbaf301fb7affe111f
Parents: 70ca8f3
Author: Claus Ibsen <da...@apache.org>
Authored: Mon Aug 26 14:30:19 2013 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Mon Aug 26 14:30:19 2013 +0200
----------------------------------------------------------------------
.../java/org/apache/camel/PropertyInject.java | 3 +-
.../camel/impl/CamelPostProcessorHelper.java | 9 ++++-
.../impl/DefaultCamelBeanPostProcessor.java | 13 ++++---
.../impl/CamelPostProcessorHelperTest.java | 41 ++++++++++++++++++--
.../handler/CamelNamespaceHandler.java | 12 +++---
5 files changed, 60 insertions(+), 18 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/8a90b273/camel-core/src/main/java/org/apache/camel/PropertyInject.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/PropertyInject.java b/camel-core/src/main/java/org/apache/camel/PropertyInject.java
index 35bfdef..b6d6852 100644
--- a/camel-core/src/main/java/org/apache/camel/PropertyInject.java
+++ b/camel-core/src/main/java/org/apache/camel/PropertyInject.java
@@ -28,8 +28,9 @@ import java.lang.annotation.Target;
*/
@Retention(RetentionPolicy.RUNTIME)
@Documented
-@Target({ElementType.FIELD, ElementType.METHOD, ElementType.CONSTRUCTOR })
+@Target({ElementType.FIELD, ElementType.METHOD, ElementType.CONSTRUCTOR})
public @interface PropertyInject {
String value();
+ String defaultValue() default "";
String context() default "";
}
http://git-wip-us.apache.org/repos/asf/camel/blob/8a90b273/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java b/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java
index 7208552..e13a91d 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java
@@ -221,7 +221,7 @@ public class CamelPostProcessorHelper implements CamelContextAware {
}
}
- public Object getInjectionPropertyValue(Class<?> type, String propertyName, String injectionPointName, Object bean, String beanName) {
+ public Object getInjectionPropertyValue(Class<?> type, String propertyName, String propertyDefaultValue, String injectionPointName, Object bean, String beanName) {
try {
String key;
String prefix = getCamelContext().getPropertyPrefixToken();
@@ -240,6 +240,13 @@ public class CamelPostProcessorHelper implements CamelContextAware {
return null;
}
} catch (Exception e) {
+ if (ObjectHelper.isNotEmpty(propertyDefaultValue)) {
+ try {
+ return getCamelContext().getTypeConverter().mandatoryConvertTo(type, propertyDefaultValue);
+ } catch (Exception e2) {
+ throw ObjectHelper.wrapRuntimeCamelException(e2);
+ }
+ }
throw ObjectHelper.wrapRuntimeCamelException(e);
}
}
http://git-wip-us.apache.org/repos/asf/camel/blob/8a90b273/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
index 29d4f0b..be44e3c 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
@@ -168,7 +168,7 @@ public class DefaultCamelBeanPostProcessor {
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
PropertyInject propertyInject = field.getAnnotation(PropertyInject.class);
if (propertyInject != null && getPostProcessorHelper().matchContext(propertyInject.context())) {
- injectFieldProperty(field, propertyInject.value(), bean, beanName);
+ injectFieldProperty(field, propertyInject.value(), propertyInject.defaultValue(), bean, beanName);
}
EndpointInject endpointInject = field.getAnnotation(EndpointInject.class);
@@ -191,9 +191,9 @@ public class DefaultCamelBeanPostProcessor {
field.getName(), bean, beanName));
}
- public void injectFieldProperty(Field field, String propertyName, Object bean, String beanName) {
+ public void injectFieldProperty(Field field, String propertyName, String propertyDefaultValue, Object bean, String beanName) {
ReflectionHelper.setField(field, bean,
- getPostProcessorHelper().getInjectionPropertyValue(field.getType(), propertyName,
+ getPostProcessorHelper().getInjectionPropertyValue(field.getType(), propertyName, propertyDefaultValue,
field.getName(), bean, beanName));
}
@@ -209,7 +209,7 @@ public class DefaultCamelBeanPostProcessor {
protected void setterInjection(Method method, Object bean, String beanName) {
PropertyInject propertyInject = method.getAnnotation(PropertyInject.class);
if (propertyInject != null && getPostProcessorHelper().matchContext(propertyInject.context())) {
- setterPropertyInjection(method, propertyInject.value(), bean, beanName);
+ setterPropertyInjection(method, propertyInject.value(), propertyInject.defaultValue(), bean, beanName);
}
EndpointInject endpointInject = method.getAnnotation(EndpointInject.class);
@@ -237,14 +237,15 @@ public class DefaultCamelBeanPostProcessor {
}
}
- public void setterPropertyInjection(Method method, String name, Object bean, String beanName) {
+ public void setterPropertyInjection(Method method, String propertyValue, String propertyDefaultValue,
+ Object bean, String beanName) {
Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes != null) {
if (parameterTypes.length != 1) {
LOG.warn("Ignoring badly annotated method for injection due to incorrect number of parameters: " + method);
} else {
String propertyName = ObjectHelper.getPropertyName(method);
- Object value = getPostProcessorHelper().getInjectionPropertyValue(parameterTypes[0], name, propertyName, bean, beanName);
+ Object value = getPostProcessorHelper().getInjectionPropertyValue(parameterTypes[0], propertyValue, propertyDefaultValue, propertyName, bean, beanName);
ObjectHelper.invokeMethod(method, bean, value);
}
}
http://git-wip-us.apache.org/repos/asf/camel/blob/8a90b273/camel-core/src/test/java/org/apache/camel/impl/CamelPostProcessorHelperTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/impl/CamelPostProcessorHelperTest.java b/camel-core/src/test/java/org/apache/camel/impl/CamelPostProcessorHelperTest.java
index 2e07bf6..0edb046 100644
--- a/camel-core/src/test/java/org/apache/camel/impl/CamelPostProcessorHelperTest.java
+++ b/camel-core/src/test/java/org/apache/camel/impl/CamelPostProcessorHelperTest.java
@@ -342,13 +342,33 @@ public class CamelPostProcessorHelperTest extends ContextTestSupport {
Field field = bean.getClass().getField("timeout");
PropertyInject propertyInject = field.getAnnotation(PropertyInject.class);
Class<?> type = field.getType();
- Object value = helper.getInjectionPropertyValue(type, propertyInject.value(), "timeout", bean, "foo");
+ Object value = helper.getInjectionPropertyValue(type, propertyInject.value(), "", "timeout", bean, "foo");
assertEquals(Integer.valueOf("2000"), Integer.valueOf("" + value));
field = bean.getClass().getField("greeting");
propertyInject = field.getAnnotation(PropertyInject.class);
type = field.getType();
- value = helper.getInjectionPropertyValue(type, propertyInject.value(), "greeting", bean, "foo");
+ value = helper.getInjectionPropertyValue(type, propertyInject.value(), "", "greeting", bean, "foo");
+ assertEquals("Hello Camel", value);
+ }
+
+ public void testPropertyFieldDefaultValueInject() throws Exception {
+ myProp.put("myApp", "Camel");
+
+ CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
+
+ MyPropertyFieldBean bean = new MyPropertyFieldBean();
+
+ Field field = bean.getClass().getField("timeout");
+ PropertyInject propertyInject = field.getAnnotation(PropertyInject.class);
+ Class<?> type = field.getType();
+ Object value = helper.getInjectionPropertyValue(type, propertyInject.value(), "5000", "timeout", bean, "foo");
+ assertEquals(Integer.valueOf("5000"), Integer.valueOf("" + value));
+
+ field = bean.getClass().getField("greeting");
+ propertyInject = field.getAnnotation(PropertyInject.class);
+ type = field.getType();
+ value = helper.getInjectionPropertyValue(type, propertyInject.value(), "", "greeting", bean, "foo");
assertEquals("Hello Camel", value);
}
@@ -363,13 +383,13 @@ public class CamelPostProcessorHelperTest extends ContextTestSupport {
Method method = bean.getClass().getMethod("setTimeout", int.class);
PropertyInject propertyInject = method.getAnnotation(PropertyInject.class);
Class<?> type = method.getParameterTypes()[0];
- Object value = helper.getInjectionPropertyValue(type, propertyInject.value(), "timeout", bean, "foo");
+ Object value = helper.getInjectionPropertyValue(type, propertyInject.value(), "", "timeout", bean, "foo");
assertEquals(Integer.valueOf("2000"), Integer.valueOf("" + value));
method = bean.getClass().getMethod("setGreeting", String.class);
propertyInject = method.getAnnotation(PropertyInject.class);
type = method.getParameterTypes()[0];
- value = helper.getInjectionPropertyValue(type, propertyInject.value(), "greeting", bean, "foo");
+ value = helper.getInjectionPropertyValue(type, propertyInject.value(), "", "greeting", bean, "foo");
assertEquals("Hello Camel", value);
}
@@ -558,6 +578,19 @@ public class CamelPostProcessorHelperTest extends ContextTestSupport {
}
}
+ public class MyPropertyFieldDefaultValueBean {
+
+ @PropertyInject(value = "myTimeout", defaultValue = "5000")
+ public int timeout;
+
+ @PropertyInject("Hello {{myApp}}")
+ public String greeting;
+
+ public String doSomething(String body) {
+ return greeting + " " + body + " with timeout=" + timeout;
+ }
+ }
+
public class MyPropertyMethodBean {
private int timeout;
http://git-wip-us.apache.org/repos/asf/camel/blob/8a90b273/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java
----------------------------------------------------------------------
diff --git a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java
index 38ea065..3125158 100644
--- a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java
+++ b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java
@@ -665,7 +665,7 @@ public class CamelNamespaceHandler implements NamespaceHandler {
for (Field field : fields) {
PropertyInject propertyInject = field.getAnnotation(PropertyInject.class);
if (propertyInject != null && matchContext(propertyInject.context())) {
- injectFieldProperty(field, propertyInject.value(), bean, beanName);
+ injectFieldProperty(field, propertyInject.value(), propertyInject.defaultValue(), bean, beanName);
}
EndpointInject endpointInject = field.getAnnotation(EndpointInject.class);
@@ -686,8 +686,8 @@ public class CamelNamespaceHandler implements NamespaceHandler {
setField(field, bean, getInjectionValue(field.getType(), endpointUri, endpointRef, endpointProperty, field.getName(), bean, beanName));
}
- protected void injectFieldProperty(Field field, String propertyName, Object bean, String beanName) {
- setField(field, bean, getInjectionPropertyValue(field.getType(), propertyName, field.getName(), bean, beanName));
+ protected void injectFieldProperty(Field field, String propertyName, String propertyDefaultValue, Object bean, String beanName) {
+ setField(field, bean, getInjectionPropertyValue(field.getType(), propertyName, propertyDefaultValue, field.getName(), bean, beanName));
}
protected static void setField(Field field, Object instance, Object value) {
@@ -723,7 +723,7 @@ public class CamelNamespaceHandler implements NamespaceHandler {
protected void setterInjection(Method method, Object bean, String beanName) {
PropertyInject propertyInject = method.getAnnotation(PropertyInject.class);
if (propertyInject != null && matchContext(propertyInject.context())) {
- setterPropertyInjection(method, propertyInject.value(), bean, beanName);
+ setterPropertyInjection(method, propertyInject.value(), propertyInject.defaultValue(), bean, beanName);
}
EndpointInject endpointInject = method.getAnnotation(EndpointInject.class);
@@ -737,14 +737,14 @@ public class CamelNamespaceHandler implements NamespaceHandler {
}
}
- protected void setterPropertyInjection(Method method, String name, Object bean, String beanName) {
+ protected void setterPropertyInjection(Method method, String propertyValue, String propertyDefaultValue, Object bean, String beanName) {
Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes != null) {
if (parameterTypes.length != 1) {
LOG.warn("Ignoring badly annotated method for injection due to incorrect number of parameters: " + method);
} else {
String propertyName = ObjectHelper.getPropertyName(method);
- Object value = getInjectionPropertyValue(parameterTypes[0], name, propertyName, bean, beanName);
+ Object value = getInjectionPropertyValue(parameterTypes[0], propertyValue, propertyDefaultValue, propertyName, bean, beanName);
ObjectHelper.invokeMethod(method, bean, value);
}
}