camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [3/5] camel git commit: CAMEL-10208: FluentProducerTemplate - Allow to be injected
Date Mon, 01 Aug 2016 11:51:41 GMT
CAMEL-10208: FluentProducerTemplate - Allow to be injected


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/06d4b9bd
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/06d4b9bd
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/06d4b9bd

Branch: refs/heads/master
Commit: 06d4b9bd398c6739b6fe2c8068d0d7090d9aa2a6
Parents: 687adda
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Mon Aug 1 13:43:05 2016 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Mon Aug 1 13:43:05 2016 +0200

----------------------------------------------------------------------
 .../camel/impl/CamelPostProcessorHelper.java    | 24 +++++++++++++
 .../impl/DefaultCamelBeanPostProcessor.java     |  1 -
 .../impl/CamelPostProcessorHelperTest.java      | 36 ++++++++++++++++++++
 .../test/patterns/FilterFluentTemplateTest.java |  9 +++--
 4 files changed, 66 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/06d4b9bd/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 af91ae7..7830138 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
@@ -26,6 +26,7 @@ import org.apache.camel.Consume;
 import org.apache.camel.Consumer;
 import org.apache.camel.ConsumerTemplate;
 import org.apache.camel.Endpoint;
+import org.apache.camel.FluentProducerTemplate;
 import org.apache.camel.IsSingleton;
 import org.apache.camel.NoSuchBeanException;
 import org.apache.camel.PollingConsumer;
@@ -34,6 +35,7 @@ import org.apache.camel.Producer;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.ProxyInstantiationException;
 import org.apache.camel.Service;
+import org.apache.camel.builder.DefaultFluentProducerTemplate;
 import org.apache.camel.component.bean.BeanInfo;
 import org.apache.camel.component.bean.BeanProcessor;
 import org.apache.camel.component.bean.ProxyHelper;
@@ -203,6 +205,8 @@ public class CamelPostProcessorHelper implements CamelContextAware {
                                     String injectionPointName, Object bean, String beanName)
{
         if (type.isAssignableFrom(ProducerTemplate.class)) {
             return createInjectionProducerTemplate(endpointUri, endpointRef, endpointProperty,
injectionPointName, bean);
+        } else if (type.isAssignableFrom(FluentProducerTemplate.class)) {
+            return createInjectionFluentProducerTemplate(endpointUri, endpointRef, endpointProperty,
injectionPointName, bean);
         } else if (type.isAssignableFrom(ConsumerTemplate.class)) {
             return createInjectionConsumerTemplate(endpointUri, endpointRef, endpointProperty,
injectionPointName);
         } else {
@@ -300,6 +304,26 @@ public class CamelPostProcessorHelper implements CamelContextAware {
     }
 
     /**
+     * Factory method to create a {@link org.apache.camel.FluentProducerTemplate} to be injected
into a POJO
+     */
+    protected FluentProducerTemplate createInjectionFluentProducerTemplate(String endpointUri,
String endpointRef, String endpointProperty,
+                                                                           String injectionPointName,
Object bean) {
+        // endpoint is optional for this injection point
+        Endpoint endpoint = getEndpointInjection(bean, endpointUri, endpointRef, endpointProperty,
injectionPointName, false);
+        CamelContext context = endpoint != null ? endpoint.getCamelContext() : getCamelContext();
+        FluentProducerTemplate answer = new DefaultFluentProducerTemplate(context);
+        answer.setDefaultEndpoint(endpoint);
+        // start the template so its ready to use
+        try {
+            // no need to defer the template as it can adjust to the endpoint at runtime
+            startService(answer, context, bean, null);
+        } catch (Exception e) {
+            throw ObjectHelper.wrapRuntimeCamelException(e);
+        }
+        return answer;
+    }
+
+    /**
      * Factory method to create a {@link org.apache.camel.ConsumerTemplate} to be injected
into a POJO
      */
     protected ConsumerTemplate createInjectionConsumerTemplate(String endpointUri, String
endpointRef, String endpointProperty,

http://git-wip-us.apache.org/repos/asf/camel/blob/06d4b9bd/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 3d1e9f9..dee7bdd 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
@@ -157,7 +157,6 @@ public class DefaultCamelBeanPostProcessor {
         return true;
     }
 
-
     /**
      * A strategy method to allow implementations to perform some custom JBI
      * based injection of the POJO

http://git-wip-us.apache.org/repos/asf/camel/blob/06d4b9bd/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 3bacc23..adb2dc9 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
@@ -26,6 +26,7 @@ import org.apache.camel.Consume;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.EndpointInject;
 import org.apache.camel.Exchange;
+import org.apache.camel.FluentProducerTemplate;
 import org.apache.camel.NoSuchBeanException;
 import org.apache.camel.PollingConsumer;
 import org.apache.camel.Produce;
@@ -255,6 +256,30 @@ public class CamelPostProcessorHelperTest extends ContextTestSupport
{
         assertMockEndpointsSatisfied();
     }
 
+    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();
+    }
+
     public void testEndpointInjectProducerTemplateFieldNoDefaultEndpoint() throws Exception
{
         CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
 
@@ -560,6 +585,17 @@ public class CamelPostProcessorHelperTest extends ContextTestSupport
{
 
     }
 
+    public class MyEndpointInjectFluentProducerTemplate {
+
+        @EndpointInject(uri = "mock:result")
+        public FluentProducerTemplate producer;
+
+        public void send(Exchange exchange) throws Exception {
+            producer.withExchange(exchange).send();
+        }
+
+    }
+
     public class MyEndpointInjectProducerTemplateNoDefaultEndpoint {
 
         @EndpointInject()

http://git-wip-us.apache.org/repos/asf/camel/blob/06d4b9bd/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterFluentTemplateTest.java
----------------------------------------------------------------------
diff --git a/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterFluentTemplateTest.java
b/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterFluentTemplateTest.java
index 26ee527..a653d48 100644
--- a/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterFluentTemplateTest.java
+++ b/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterFluentTemplateTest.java
@@ -17,8 +17,8 @@
 package org.apache.camel.test.patterns;
 
 import org.apache.camel.EndpointInject;
+import org.apache.camel.FluentProducerTemplate;
 import org.apache.camel.Produce;
-import org.apache.camel.ProducerTemplate;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.test.junit4.CamelTestSupport;
@@ -36,6 +36,9 @@ public class FilterFluentTemplateTest extends CamelTestSupport {
     @EndpointInject(uri = "mock:result")
     protected MockEndpoint resultEndpoint;
 
+    @Produce(uri = "direct:start")
+    protected FluentProducerTemplate fluentTemplate;
+
     @Override
     public boolean isDumpRouteCoverage() {
         return true;
@@ -47,7 +50,7 @@ public class FilterFluentTemplateTest extends CamelTestSupport {
 
         resultEndpoint.expectedBodiesReceived(expectedBody);
 
-        fluentTemplate.withBody(expectedBody).withHeader("foo", "bar").to("direct:start").send();
+        fluentTemplate.withBody(expectedBody).withHeader("foo", "bar").send();
 
         resultEndpoint.assertIsSatisfied();
     }
@@ -56,7 +59,7 @@ public class FilterFluentTemplateTest extends CamelTestSupport {
     public void testSendNotMatchingMessage() throws Exception {
         resultEndpoint.expectedMessageCount(0);
 
-        fluentTemplate.withBody("<notMatched/>").withHeader("foo", "notMatchedHeaderValue").to("direct:start").send();;
+        fluentTemplate.withBody("<notMatched/>").withHeader("foo", "notMatchedHeaderValue").send();
 
         resultEndpoint.assertIsSatisfied();
     }


Mime
View raw message