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 2019/03/05 06:25:00 UTC

[camel] 03/07: CAMEL-13283: Add @BindRegistry annotation to allow to bind beans/classes to registry.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch bind
in repository https://gitbox.apache.org/repos/asf/camel.git

commit d8bc6ff097b85087e7b82afe83b96f92ac83d918
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Mar 5 05:19:14 2019 +0100

    CAMEL-13283: Add @BindRegistry annotation to allow to bind beans/classes to registry.
---
 .../apache/camel/{BindRegistry.java => BindToRegistry.java}    |  2 +-
 .../src/main/java/org/apache/camel/builder/RouteBuilder.java   | 10 ++++++++++
 .../org/apache/camel/impl/DefaultCamelBeanPostProcessor.java   |  6 +++---
 .../apache/camel/impl/DefaultCamelBeanPostProcessorTest.java   |  6 +++---
 .../org/apache/camel/processor/CBRHeaderPredicateTest.java     | 10 ++--------
 5 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/BindRegistry.java b/core/camel-api/src/main/java/org/apache/camel/BindToRegistry.java
similarity index 97%
rename from core/camel-api/src/main/java/org/apache/camel/BindRegistry.java
rename to core/camel-api/src/main/java/org/apache/camel/BindToRegistry.java
index b2aebca..d5a446c 100644
--- a/core/camel-api/src/main/java/org/apache/camel/BindRegistry.java
+++ b/core/camel-api/src/main/java/org/apache/camel/BindToRegistry.java
@@ -31,7 +31,7 @@ import java.lang.annotation.Target;
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
 @Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
-public @interface BindRegistry {
+public @interface BindToRegistry {
     String name() default "";
     String context() default "";
 }
diff --git a/core/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java b/core/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
index 6cac0ce..d3c8b34 100644
--- a/core/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
+++ b/core/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
@@ -81,6 +81,16 @@ public abstract class RouteBuilder extends BuilderSupport implements RoutesBuild
     public abstract void configure() throws Exception;
 
     /**
+     * Binds the bean to the repository (if possible).
+     *
+     * @param id   the id of the bean
+     * @param bean the bean
+     */
+    public void bindToRegistry(String id, Object bean) {
+        getContext().getRegistry().bind(id, bean);
+    }
+
+    /**
      * Configures the REST services
      *
      * @return the builder
diff --git a/core/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java b/core/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
index e7dc03f..6dfc82d 100644
--- a/core/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
+++ b/core/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
@@ -20,7 +20,7 @@ import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 
 import org.apache.camel.BeanInject;
-import org.apache.camel.BindRegistry;
+import org.apache.camel.BindToRegistry;
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
 import org.apache.camel.DeferredContextBinding;
@@ -88,7 +88,7 @@ public class DefaultCamelBeanPostProcessor {
         injectMethods(bean, beanName);
 
         // the bean may also need to be registered into the registry
-        BindRegistry bind = bean.getClass().getAnnotation(BindRegistry.class);
+        BindToRegistry bind = bean.getClass().getAnnotation(BindToRegistry.class);
         if (bind != null) {
             String name = bind.name();
             if (isEmpty(name)) {
@@ -203,7 +203,7 @@ public class DefaultCamelBeanPostProcessor {
                     injectField(field, produce.uri(), produce.ref(), produce.property(), bean, beanName, produce.binding());
                 }
 
-                BindRegistry bind = field.getAnnotation(BindRegistry.class);
+                BindToRegistry bind = field.getAnnotation(BindToRegistry.class);
                 if (bind != null && getPostProcessorHelper().matchContext(bind.context())) {
                     bindRegistry(field, bind.name(), bean, beanName);
                 }
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelBeanPostProcessorTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelBeanPostProcessorTest.java
index f6b7ef8..8814848 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelBeanPostProcessorTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelBeanPostProcessorTest.java
@@ -16,7 +16,7 @@
  */
 package org.apache.camel.impl;
 
-import org.apache.camel.BindRegistry;
+import org.apache.camel.BindToRegistry;
 import org.apache.camel.Consume;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Produce;
@@ -57,14 +57,14 @@ public class DefaultCamelBeanPostProcessorTest extends ContextTestSupport {
         postProcessor = new DefaultCamelBeanPostProcessor(context);
     }
 
-    @BindRegistry
+    @BindToRegistry
     public class FooService {
 
         private String fooEndpoint;
         private String barEndpoint;
         @Produce
         private ProducerTemplate bar;
-        @BindRegistry(name = "myCoolBean")
+        @BindToRegistry(name = "myCoolBean")
         private MySerialBean myBean = new MySerialBean();
 
         public String getFooEndpoint() {
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/CBRHeaderPredicateTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/CBRHeaderPredicateTest.java
index f97ec2c..a253cfe 100644
--- a/core/camel-core/src/test/java/org/apache/camel/processor/CBRHeaderPredicateTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/CBRHeaderPredicateTest.java
@@ -21,18 +21,10 @@ import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.impl.JndiRegistry;
 import org.junit.Test;
 
 public class CBRHeaderPredicateTest extends ContextTestSupport {
 
-    @Override
-    protected JndiRegistry createRegistry() throws Exception {
-        JndiRegistry jndi = super.createRegistry();
-        jndi.bind("cbrBean", new MyCBRBean());
-        return jndi;
-    }
-
     @Test
     public void testCBR() throws Exception {
         MockEndpoint foo = getMockEndpoint("mock:foo");
@@ -52,6 +44,8 @@ public class CBRHeaderPredicateTest extends ContextTestSupport {
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
+                bindToRegistry("cbrBean", new MyCBRBean());
+
                 from("direct:start")
                     .choice()
                         .when().method("cbrBean", "checkHeader")