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

davsclaus pushed a commit to branch kamelet-local-registry
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/kamelet-local-registry by this 
push:
     new d1509bd  CAMEL-16394: Route Template local beans. WIP
d1509bd is described below

commit d1509bd262ebfe73ded4ebc8b409452e1c82c501
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed May 5 11:56:57 2021 +0200

    CAMEL-16394: Route Template local beans. WIP
---
 .../src/main/java/org/apache/camel/spi/Registry.java         |  2 ++
 .../org/apache/camel/builder/RouteTemplateLocalBeanTest.java |  1 -
 .../java/org/apache/camel/support/DefaultRegistryTest.java   | 12 ++++++------
 .../main/java/org/apache/camel/support/SimpleRegistry.java   |  5 ++---
 .../main/java/org/apache/camel/support/SupplierRegistry.java | 12 +++++++-----
 5 files changed, 17 insertions(+), 15 deletions(-)

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 fb1061b..cbabc0c 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
@@ -56,6 +56,8 @@ public interface Registry extends BeanRepository {
     /**
      * Binds the bean (via a supplier) to the repository (if possible).
      * <p/>
+     * Notice that the supplier will be called each time the bean is being 
looked up (not cached).
+     * <p/>
      * 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.
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/builder/RouteTemplateLocalBeanTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/builder/RouteTemplateLocalBeanTest.java
index f2590fb..32a44f9 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/builder/RouteTemplateLocalBeanTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/builder/RouteTemplateLocalBeanTest.java
@@ -26,7 +26,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
 
-// TODO: bind supplier (do not cache) mention in docs that on each lookup it 
will call the supplier
 // TODO: local bind extrapolate {{key}} with hashed so they are local 
(endpoint uri lookup problem)
 
 public class RouteTemplateLocalBeanTest extends ContextTestSupport {
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 19e637e..bf32a4e 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
@@ -56,9 +56,9 @@ public class DefaultRegistryTest {
 
         FooBar bar1 = (FooBar) registry.lookupByName("myBar");
         FooBar bar2 = (FooBar) registry.lookupByName("myBar");
-        assertSame(bar1, bar2);
+        assertNotSame(bar1, bar2);
         assertEquals("I am lazy 1 me", bar1.hello("me"));
-        assertEquals("I am lazy 1 me", bar2.hello("me"));
+        assertEquals("I am lazy 2 me", bar2.hello("me"));
     }
 
     @Test
@@ -73,9 +73,9 @@ public class DefaultRegistryTest {
 
         FooBar bar1 = registry.lookupByNameAndType("myBar", FooBar.class);
         FooBar bar2 = registry.lookupByNameAndType("myBar", FooBar.class);
-        assertSame(bar1, bar2);
+        assertNotSame(bar1, bar2);
         assertEquals("I am lazy 1 me", bar1.hello("me"));
-        assertEquals("I am lazy 1 me", bar2.hello("me"));
+        assertEquals("I am lazy 2 me", bar2.hello("me"));
     }
 
     @Test
@@ -99,7 +99,7 @@ public class DefaultRegistryTest {
         set = registry.findByType(FooBar.class);
         assertEquals(2, set.size());
         it = set.iterator();
-        assertEquals("I am lazy 1 me", it.next().hello("me"));
+        assertEquals("I am lazy 2 me", it.next().hello("me"));
         assertSame(myFooBar, it.next());
     }
 
@@ -124,7 +124,7 @@ public class DefaultRegistryTest {
         map = registry.findByTypeWithName(FooBar.class);
         assertEquals(2, map.size());
         it = map.values().iterator();
-        assertEquals("I am lazy 1 me", it.next().hello("me"));
+        assertEquals("I am lazy 2 me", it.next().hello("me"));
         assertSame(myFooBar, it.next());
     }
 
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/SimpleRegistry.java 
b/core/camel-support/src/main/java/org/apache/camel/support/SimpleRegistry.java
index e697f9e..2f9bb91 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/SimpleRegistry.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/SimpleRegistry.java
@@ -25,7 +25,6 @@ import java.util.Set;
 import java.util.function.Supplier;
 
 import org.apache.camel.NoSuchBeanException;
-import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.spi.Registry;
 
 /**
@@ -106,8 +105,8 @@ public class SimpleRegistry extends LinkedHashMap<String, 
Map<Class<?>, Object>>
     }
 
     @Override
-    public void bind(String id, Class<?> type, Supplier<Object> bean) throws 
RuntimeCamelException {
-        computeIfAbsent(id, k -> new LinkedHashMap<>()).put(type, wrap(bean));
+    public void bind(String id, Class<?> type, Supplier<Object> bean) {
+        throw new UnsupportedOperationException("Use SupplierRegistry");
     }
 
     @Override
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/SupplierRegistry.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/SupplierRegistry.java
index 081ee51..eff10da 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/SupplierRegistry.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/SupplierRegistry.java
@@ -25,7 +25,9 @@ import java.util.function.Supplier;
 import org.apache.camel.NoSuchBeanException;
 
 /**
- * Used for storing beans that are lazy supplied (on first demand) via a 
{@link Supplier}.
+ * Used for storing beans that are supplied via a {@link Supplier}.
+ * <p/>
+ * Notice that the supplier will be called each time the bean is being looked 
up (not cached).
  *
  * To bind a bean as a supplier, then use the {@link 
org.apache.camel.spi.Registry#bind(String, Class, Supplier)}
  * method.
@@ -43,7 +45,6 @@ public class SupplierRegistry extends SimpleRegistry {
         if (answer instanceof Supplier) {
             // okay then eval the supplier to get the actual value
             answer = ((Supplier<?>) answer).get();
-            map.put(type, answer);
         }
         if (answer == null) {
             // look for first entry that is the type
@@ -53,7 +54,6 @@ public class SupplierRegistry extends SimpleRegistry {
                     if (value instanceof Supplier) {
                         // okay then eval the supplier to get the actual value
                         value = ((Supplier<?>) value).get();
-                        entry.setValue(value);
                     }
                     answer = value;
                     break;
@@ -83,7 +83,6 @@ public class SupplierRegistry extends SimpleRegistry {
                     if (value instanceof Supplier) {
                         // okay then eval the supplier to get the actual value
                         value = ((Supplier<?>) value).get();
-                        subEntry.setValue(value);
                     }
                     result.add(type.cast(value));
                 }
@@ -102,7 +101,6 @@ public class SupplierRegistry extends SimpleRegistry {
                     if (value instanceof Supplier) {
                         // okay then eval the supplier to get the actual value
                         value = ((Supplier<?>) value).get();
-                        subEntry.setValue(value);
                     }
                     result.put(entry.getKey(), type.cast(value));
                 }
@@ -111,4 +109,8 @@ public class SupplierRegistry extends SimpleRegistry {
         return result;
     }
 
+    @Override
+    public void bind(String id, Class<?> type, Supplier<Object> bean) {
+        computeIfAbsent(id, k -> new LinkedHashMap<>()).put(type, wrap(bean));
+    }
 }

Reply via email to