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

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

commit ff79f30890a79065ce62e0839968dfff08587012
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Mon May 18 14:27:11 2020 +0200

    CAMEL-15078: camel-core - Registry.bind should inject context into 
CamelContextAware beans
---
 .../main/java/org/apache/camel/spi/Registry.java   |  7 ++++
 .../apache/camel/support/DefaultRegistryTest.java  | 44 ++++++++++++++++++++++
 .../org/apache/camel/support/DefaultRegistry.java  |  4 ++
 3 files changed, 55 insertions(+)

diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/Registry.java 
b/core/camel-api/src/main/java/org/apache/camel/spi/Registry.java
index a7d7e86..c62b21b 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/Registry.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/Registry.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.spi;
 
+import org.apache.camel.CamelContextAware;
 import org.apache.camel.RuntimeCamelException;
 
 /**
@@ -26,6 +27,9 @@ public interface Registry extends BeanRepository {
 
     /**
      * Binds the bean to the repository (if possible).
+     * 
+     * If the bean is {@link CamelContextAware} then the registry
+     * will automatic inject the context if possible.
      *
      * @param id   the id of the bean
      * @param bean the bean
@@ -41,6 +45,9 @@ public interface Registry extends BeanRepository {
      * Binding by id and type allows to bind multiple entries with the same
      * id but with different type.
      *
+     * If the bean is {@link CamelContextAware} then the registry
+     * will automatic inject the context if possible.
+     *
      * @param id   the id of the bean
      * @param type the type of the bean to associate the binding
      * @param bean the bean
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/support/DefaultRegistryTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/support/DefaultRegistryTest.java
index b98ca49..e24c06d 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/support/DefaultRegistryTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/support/DefaultRegistryTest.java
@@ -20,6 +20,9 @@ import java.util.Iterator;
 
 import junit.framework.TestCase;
 
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.impl.FooBar;
 import org.junit.Test;
 
@@ -86,4 +89,45 @@ public class DefaultRegistryTest extends TestCase {
         assertEquals("myFooBar", it.next());
     }
 
+    @Test
+    public void testBindCamelContextAwareInject() throws Exception {
+        CamelContext context = new DefaultCamelContext();
+        registry.setCamelContext(context);
+
+        MyBean my = new MyBean("Tiger");
+        registry.bind("tiger", my);
+
+        MyBean lookup = (MyBean) registry.lookupByName("tiger");
+        assertSame(my, lookup);
+
+        assertNotNull(lookup.getCamelContext());
+        assertSame(context, lookup.getCamelContext());
+    }
+
+    private class MyBean implements CamelContextAware {
+
+        private CamelContext camelContext;
+
+        private String name;
+
+        public MyBean(String name) {
+            this.name = name;
+        }
+
+        @Override
+        public CamelContext getCamelContext() {
+            return camelContext;
+        }
+
+        @Override
+        public void setCamelContext(CamelContext camelContext) {
+            this.camelContext = camelContext;
+        }
+
+        public String getName() {
+            return name;
+        }
+    }
+
+
 }
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/DefaultRegistry.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultRegistry.java
index ccee7b1..6704bca 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/DefaultRegistry.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultRegistry.java
@@ -119,6 +119,10 @@ public class DefaultRegistry implements Registry, 
CamelContextAware {
 
     @Override
     public void bind(String id, Class<?> type, Object bean) throws 
RuntimeCamelException {
+        // automatic inject camel context in bean if its aware
+        if (camelContext != null && bean instanceof CamelContextAware) {
+            ((CamelContextAware) bean).setCamelContext(camelContext);
+        }
         fallbackRegistry.bind(id, type, bean);
     }
 

Reply via email to