use of org.apache.camel.PropertyInject in project camel by apache.
the class CamelPostProcessorHelperTest method testPropertyMethodInject.
public void testPropertyMethodInject() throws Exception {
myProp.put("myTimeout", "2000");
myProp.put("myApp", "Camel");
CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
MyPropertyMethodBean bean = new MyPropertyMethodBean();
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");
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");
assertEquals("Hello Camel", value);
}
use of org.apache.camel.PropertyInject in project camel by apache.
the class DefaultCamelBeanPostProcessor method setterInjection.
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(), propertyInject.defaultValue(), bean, beanName);
}
BeanInject beanInject = method.getAnnotation(BeanInject.class);
if (beanInject != null && getPostProcessorHelper().matchContext(beanInject.context())) {
setterBeanInjection(method, beanInject.value(), bean, beanName);
}
EndpointInject endpointInject = method.getAnnotation(EndpointInject.class);
if (endpointInject != null && getPostProcessorHelper().matchContext(endpointInject.context())) {
setterInjection(method, bean, beanName, endpointInject.uri(), endpointInject.ref(), endpointInject.property());
}
Produce produce = method.getAnnotation(Produce.class);
if (produce != null && getPostProcessorHelper().matchContext(produce.context())) {
setterInjection(method, bean, beanName, produce.uri(), produce.ref(), produce.property());
}
}
use of org.apache.camel.PropertyInject in project camel by apache.
the class BeanInfo method createParameterUnmarshalExpressionForAnnotation.
private Expression createParameterUnmarshalExpressionForAnnotation(Class<?> clazz, Method method, Class<?> parameterType, Annotation annotation) {
if (annotation instanceof AttachmentObjects) {
return ExpressionBuilder.attachmentObjectsExpression();
} else if (annotation instanceof Attachments) {
return ExpressionBuilder.attachmentsExpression();
} else if (annotation instanceof Property) {
Property propertyAnnotation = (Property) annotation;
return ExpressionBuilder.exchangePropertyExpression(propertyAnnotation.value());
} else if (annotation instanceof ExchangeProperty) {
ExchangeProperty propertyAnnotation = (ExchangeProperty) annotation;
return ExpressionBuilder.exchangePropertyExpression(propertyAnnotation.value());
} else if (annotation instanceof Properties) {
return ExpressionBuilder.exchangePropertiesExpression();
} else if (annotation instanceof ExchangeProperties) {
return ExpressionBuilder.exchangePropertiesExpression();
} else if (annotation instanceof Header) {
Header headerAnnotation = (Header) annotation;
return ExpressionBuilder.headerExpression(headerAnnotation.value());
} else if (annotation instanceof Headers) {
return ExpressionBuilder.headersExpression();
} else if (annotation instanceof OutHeaders) {
return ExpressionBuilder.outHeadersExpression();
} else if (annotation instanceof ExchangeException) {
return ExpressionBuilder.exchangeExceptionExpression(CastUtils.cast(parameterType, Exception.class));
} else if (annotation instanceof PropertyInject) {
PropertyInject propertyAnnotation = (PropertyInject) annotation;
Expression inject = ExpressionBuilder.propertiesComponentExpression(propertyAnnotation.value(), null, propertyAnnotation.defaultValue());
return ExpressionBuilder.convertToExpression(inject, parameterType);
} else {
LanguageAnnotation languageAnnotation = annotation.annotationType().getAnnotation(LanguageAnnotation.class);
if (languageAnnotation != null) {
Class<?> type = languageAnnotation.factory();
Object object = camelContext.getInjector().newInstance(type);
if (object instanceof AnnotationExpressionFactory) {
AnnotationExpressionFactory expressionFactory = (AnnotationExpressionFactory) object;
return expressionFactory.createExpression(camelContext, annotation, languageAnnotation, parameterType);
} else {
LOG.warn("Ignoring bad annotation: " + languageAnnotation + "on method: " + method + " which declares a factory: " + type.getName() + " which does not implement " + AnnotationExpressionFactory.class.getName());
}
}
}
return null;
}
use of org.apache.camel.PropertyInject in project camel by apache.
the class CamelPostProcessorHelperTest method testPropertyFieldDefaultValueInject.
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);
}
use of org.apache.camel.PropertyInject in project camel by apache.
the class CamelPostProcessorHelperTest method testPropertyFieldInject.
public void testPropertyFieldInject() throws Exception {
myProp.put("myTimeout", "2000");
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(), "", "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");
assertEquals("Hello Camel", value);
}
Aggregations