Search in sources :

Example 6 with EndpointInject

use of org.apache.camel.EndpointInject 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 7 with EndpointInject

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

the class CamelPostProcessorHelperTest method testEndpointInjectProducerTemplateFieldNameUnknown.

public void testEndpointInjectProducerTemplateFieldNameUnknown() throws Exception {
    CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
    MyEndpointInjectProducerTemplateNameUnknown bean = new MyEndpointInjectProducerTemplateNameUnknown();
    Field field = bean.getClass().getField("producer");
    EndpointInject endpointInject = field.getAnnotation(EndpointInject.class);
    Class<?> type = field.getType();
    String propertyName = "producer";
    try {
        helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), endpointInject.property(), propertyName, bean, "foo");
        fail("Should throw exception");
    } catch (NoSuchBeanException e) {
        assertEquals("No bean could be found in the registry for: unknown of type: org.apache.camel.Endpoint", e.getMessage());
    }
}
Also used : EndpointInject(org.apache.camel.EndpointInject) Field(java.lang.reflect.Field) NoSuchBeanException(org.apache.camel.NoSuchBeanException)

Example 8 with EndpointInject

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

the class CamelPostProcessorHelperTest method testEndpointInjectProducerTemplateFieldNoDefaultEndpoint.

public void testEndpointInjectProducerTemplateFieldNoDefaultEndpoint() throws Exception {
    CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
    MyEndpointInjectProducerTemplateNoDefaultEndpoint bean = new MyEndpointInjectProducerTemplateNoDefaultEndpoint();
    Field field = bean.getClass().getField("producer");
    EndpointInject endpointInject = field.getAnnotation(EndpointInject.class);
    Class<?> type = field.getType();
    String propertyName = "producer";
    Object value = helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), endpointInject.property(), propertyName, bean, "foo");
    field.set(bean, value);
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("Hello World");
    Exchange exchange = new DefaultExchange(context);
    exchange.getIn().setBody("Hello World");
    bean.send(exchange);
    assertMockEndpointsSatisfied();
}
Also used : EndpointInject(org.apache.camel.EndpointInject) Exchange(org.apache.camel.Exchange) Field(java.lang.reflect.Field) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

Example 9 with EndpointInject

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

the class CamelPostProcessorHelperTest method testEndpointInjectProducerTemplate.

public void testEndpointInjectProducerTemplate() throws Exception {
    CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
    MyEndpointInjectBeanProducerTemplate bean = new MyEndpointInjectBeanProducerTemplate();
    Method method = bean.getClass().getMethod("setProducer", ProducerTemplate.class);
    EndpointInject endpointInject = method.getAnnotation(EndpointInject.class);
    Class<?>[] parameterTypes = method.getParameterTypes();
    for (Class<?> type : parameterTypes) {
        String propertyName = ObjectHelper.getPropertyName(method);
        Object value = helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), endpointInject.property(), propertyName, bean, "foo");
        ObjectHelper.invokeMethod(method, bean, value);
    }
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("Hello World");
    assertNotNull(bean.getProducer());
    bean.send("Hello World");
    assertMockEndpointsSatisfied();
}
Also used : EndpointInject(org.apache.camel.EndpointInject) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Method(java.lang.reflect.Method)

Example 10 with EndpointInject

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

the class CamelPostProcessorHelperTest method testEndpointInjectFluentProducerTemplateField.

public void testEndpointInjectFluentProducerTemplateField() throws Exception {
    CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
    MyEndpointInjectFluentProducerTemplate bean = new MyEndpointInjectFluentProducerTemplate();
    Field field = bean.getClass().getField("producer");
    EndpointInject endpointInject = field.getAnnotation(EndpointInject.class);
    Class<?> type = field.getType();
    String propertyName = "producer";
    Object value = helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), endpointInject.property(), propertyName, bean, "foo");
    field.set(bean, value);
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("Hello World");
    Exchange exchange = new DefaultExchange(context);
    exchange.getIn().setBody("Hello World");
    bean.send(exchange);
    assertMockEndpointsSatisfied();
}
Also used : EndpointInject(org.apache.camel.EndpointInject) Exchange(org.apache.camel.Exchange) Field(java.lang.reflect.Field) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

Aggregations

EndpointInject (org.apache.camel.EndpointInject)11 Field (java.lang.reflect.Field)6 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)6 Exchange (org.apache.camel.Exchange)5 Method (java.lang.reflect.Method)3 BeanInject (org.apache.camel.BeanInject)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 NoSuchBeanException (org.apache.camel.NoSuchBeanException)1 ResolveEndpointFailedException (org.apache.camel.ResolveEndpointFailedException)1