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 239bdae CAMEL-16575: Camel registry can now bind a bean that is
supplied via a Supplier. This allows to bind beans that are lazy evaluated via
lambda and assists for the local bean registry work that is needed for Kamelets
and route templates.
239bdae is described below
commit 239bdaed463ff28b9c83fb008ed9fa00722122f3
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue May 4 16:26:59 2021 +0200
CAMEL-16575: Camel registry can now bind a bean that is supplied via a
Supplier. This allows to bind beans that are lazy evaluated via lambda and
assists for the local bean registry work that is needed for Kamelets and route
templates.
---
.../apache/camel/support/DefaultRegistryTest.java | 12 ++--
.../org/apache/camel/support/SupplierRegistry.java | 70 ++++++++++++++++++----
2 files changed, 64 insertions(+), 18 deletions(-)
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 bf32a4e..19e637e 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");
- assertNotSame(bar1, bar2);
+ assertSame(bar1, bar2);
assertEquals("I am lazy 1 me", bar1.hello("me"));
- assertEquals("I am lazy 2 me", bar2.hello("me"));
+ assertEquals("I am lazy 1 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);
- assertNotSame(bar1, bar2);
+ assertSame(bar1, bar2);
assertEquals("I am lazy 1 me", bar1.hello("me"));
- assertEquals("I am lazy 2 me", bar2.hello("me"));
+ assertEquals("I am lazy 1 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 2 me", it.next().hello("me"));
+ assertEquals("I am lazy 1 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 2 me", it.next().hello("me"));
+ assertEquals("I am lazy 1 me", it.next().hello("me"));
assertSame(myFooBar, it.next());
}
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 572007c..081ee51 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
@@ -22,20 +22,69 @@ import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
+import org.apache.camel.NoSuchBeanException;
+
/**
- * Used for storing beans that are lazy supplied via a {@link Supplier}.
+ * Used for storing beans that are lazy supplied (on first demand) via a
{@link Supplier}.
*
- * To bind a bean as a supplier, then use the {@link
org.apache.camel.spi.Registry#bind(String, Class, Supplier)} method.
+ * To bind a bean as a supplier, then use the {@link
org.apache.camel.spi.Registry#bind(String, Class, Supplier)}
+ * method.
*/
public class SupplierRegistry extends SimpleRegistry {
@Override
+ public <T> T lookupByNameAndType(String name, Class<T> type) {
+ Map<Class<?>, Object> map = this.get(name);
+ if (map == null) {
+ return null;
+ }
+
+ Object answer = map.get(type);
+ 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
+ for (Map.Entry<Class<?>, Object> entry : map.entrySet()) {
+ if (type.isAssignableFrom(entry.getKey())) {
+ Object value = entry.getValue();
+ if (value instanceof Supplier) {
+ // okay then eval the supplier to get the actual value
+ value = ((Supplier<?>) value).get();
+ entry.setValue(value);
+ }
+ answer = value;
+ break;
+ }
+ }
+ }
+ if (answer == null) {
+ return null;
+ }
+ try {
+ answer = unwrap(answer);
+ return type.cast(answer);
+ } catch (Throwable e) {
+ String msg = "Found bean: " + name + " in SimpleRegistry: " + this
+ + " of type: " + answer.getClass().getName() + "
expected type was: " + type;
+ throw new NoSuchBeanException(name, msg, e);
+ }
+ }
+
+ @Override
public <T> Set<T> findByType(Class<T> type) {
Set<T> result = new LinkedHashSet<>();
for (Map.Entry<String, Map<Class<?>, Object>> entry : entrySet()) {
for (Map.Entry<Class<?>, Object> subEntry :
entry.getValue().entrySet()) {
if (type.isAssignableFrom(subEntry.getKey())) {
- Object value = unwrap(subEntry.getValue());
+ Object value = subEntry.getValue();
+ 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));
}
}
@@ -49,7 +98,12 @@ public class SupplierRegistry extends SimpleRegistry {
for (Map.Entry<String, Map<Class<?>, Object>> entry : entrySet()) {
for (Map.Entry<Class<?>, Object> subEntry :
entry.getValue().entrySet()) {
if (type.isAssignableFrom(subEntry.getKey())) {
- Object value = unwrap(subEntry.getValue());
+ Object value = subEntry.getValue();
+ 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));
}
}
@@ -57,12 +111,4 @@ public class SupplierRegistry extends SimpleRegistry {
return result;
}
- @Override
- public Object unwrap(Object value) {
- if (value instanceof Supplier) {
- value = ((Supplier<?>) value).get();
- }
- return value;
- }
-
}