Search in sources :

Example 1 with BeanInject

use of org.apache.camel.BeanInject in project camel by apache.

the class CamelPostProcessorHelperTest method testBeanInject.

public void testBeanInject() throws Exception {
    CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
    MyBeanInjectBean bean = new MyBeanInjectBean();
    Field field = bean.getClass().getField("foo");
    BeanInject beanInject = field.getAnnotation(BeanInject.class);
    Class<?> type = field.getType();
    Object value = helper.getInjectionBeanValue(type, beanInject.value());
    field.set(bean, value);
    String out = bean.doSomething("World");
    assertEquals("Hello World", out);
}
Also used : Field(java.lang.reflect.Field) BeanInject(org.apache.camel.BeanInject)

Example 2 with BeanInject

use of org.apache.camel.BeanInject in project camel by apache.

the class CamelPostProcessorHelperTest method testBeanInjectByType.

public void testBeanInjectByType() throws Exception {
    CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
    MyBeanInjectByTypeBean bean = new MyBeanInjectByTypeBean();
    Field field = bean.getClass().getField("foo");
    BeanInject beanInject = field.getAnnotation(BeanInject.class);
    Class<?> type = field.getType();
    Object value = helper.getInjectionBeanValue(type, beanInject.value());
    field.set(bean, value);
    String out = bean.doSomething("Camel");
    assertEquals("Hello Camel", out);
}
Also used : Field(java.lang.reflect.Field) BeanInject(org.apache.camel.BeanInject)

Example 3 with BeanInject

use of org.apache.camel.BeanInject 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());
    }
}
Also used : EndpointInject(org.apache.camel.EndpointInject) BeanInject(org.apache.camel.BeanInject) Produce(org.apache.camel.Produce) PropertyInject(org.apache.camel.PropertyInject)

Example 4 with BeanInject

use of org.apache.camel.BeanInject in project camel by apache.

the class CdiCamelBeanPostProcessor method injectFields.

protected void injectFields(final Object bean, final String beanName) {
    ReflectionHelper.doWithFields(bean.getClass(), field -> {
        PropertyInject propertyInject = field.getAnnotation(PropertyInject.class);
        if (propertyInject != null) {
            try {
                injectFieldProperty(field, propertyInject.value(), propertyInject.defaultValue(), propertyInject.context(), bean, beanName);
            } catch (Exception cause) {
                throw new InjectionException("Injection of [" + propertyInject + "] for field [" + field + "] failed!", cause);
            }
        }
        BeanInject beanInject = field.getAnnotation(BeanInject.class);
        if (beanInject != null && getPostProcessorHelper().matchContext(beanInject.context())) {
            try {
                injectFieldBean(field, beanInject.value(), bean, beanName);
            } catch (Exception cause) {
                throw new InjectionException("Injection of [" + beanInject + "] for field [" + field + "] failed!", cause);
            }
        }
        EndpointInject endpointInject = field.getAnnotation(EndpointInject.class);
        if (endpointInject != null) {
            try {
                injectField(field, endpointInject.uri(), endpointInject.ref(), endpointInject.property(), endpointInject.context(), bean, beanName);
            } catch (Exception cause) {
                throw new InjectionException("Injection of [" + endpointInject + "] for field [" + field + "] failed!", cause);
            }
        }
        Produce produce = field.getAnnotation(Produce.class);
        if (produce != null) {
            try {
                injectField(field, produce.uri(), produce.ref(), produce.property(), produce.context(), bean, beanName);
            } catch (Exception cause) {
                throw new InjectionException("Injection of [" + produce + "] for field [" + field + "] failed!", cause);
            }
        }
    });
}
Also used : InjectionException(javax.enterprise.inject.InjectionException) EndpointInject(org.apache.camel.EndpointInject) BeanInject(org.apache.camel.BeanInject) Produce(org.apache.camel.Produce) PropertyInject(org.apache.camel.PropertyInject) UnsatisfiedResolutionException(javax.enterprise.inject.UnsatisfiedResolutionException) InjectionException(javax.enterprise.inject.InjectionException)

Aggregations

BeanInject (org.apache.camel.BeanInject)4 Field (java.lang.reflect.Field)2 EndpointInject (org.apache.camel.EndpointInject)2 Produce (org.apache.camel.Produce)2 PropertyInject (org.apache.camel.PropertyInject)2 InjectionException (javax.enterprise.inject.InjectionException)1 UnsatisfiedResolutionException (javax.enterprise.inject.UnsatisfiedResolutionException)1