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 696d5af CAMEL-16394: Route Template local beans. WIP
696d5af is described below
commit 696d5af24291791f654f579c62a5328f54abf445
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed May 5 14:47:35 2021 +0200
CAMEL-16394: Route Template local beans. WIP
---
.../org/apache/camel/RouteTemplateContext.java | 26 ++++++
.../main/java/org/apache/camel/spi/Registry.java | 19 ++++-
.../java/org/apache/camel/impl/DefaultModel.java | 7 +-
.../camel/builder/TemplatedRouteBuilder.java | 10 +--
.../DefaultRouteTemplateContext.java | 22 +++--
.../main/java/org/apache/camel/model/Model.java | 12 +--
.../apache/camel/support/DefaultRegistryTest.java | 94 ++++++++++++++++++++--
.../org/apache/camel/support/DefaultRegistry.java | 7 ++
.../org/apache/camel/support/SimpleRegistry.java | 6 ++
.../org/apache/camel/support/SupplierRegistry.java | 3 +-
10 files changed, 179 insertions(+), 27 deletions(-)
diff --git
a/core/camel-api/src/main/java/org/apache/camel/RouteTemplateContext.java
b/core/camel-api/src/main/java/org/apache/camel/RouteTemplateContext.java
index c6e8842..3db1612 100644
--- a/core/camel-api/src/main/java/org/apache/camel/RouteTemplateContext.java
+++ b/core/camel-api/src/main/java/org/apache/camel/RouteTemplateContext.java
@@ -54,6 +54,9 @@ public interface RouteTemplateContext {
/**
* Binds the bean (via a supplier) to the repository (if possible).
* <p/>
+ * Camel will cache the result from the supplier from first lookup
(singleton scope). If you do not need cached then
+ * use {@link #bindAsPrototype(String, Class, Supplier)} instead.
+ * <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.
@@ -65,6 +68,21 @@ public interface RouteTemplateContext {
void bind(String id, Class<?> type, Supplier<Object> bean);
/**
+ * 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.
+ *
+ * @param id the id of the bean
+ * @param type the type of the bean to associate the binding
+ * @param bean a supplier for the bean
+ */
+ void bindAsPrototype(String id, Class<?> type, Supplier<Object> bean);
+
+ /**
* Gets the property with the given name
*
* @param name name of property
@@ -73,6 +91,14 @@ public interface RouteTemplateContext {
Object getProperty(String name);
/**
+ * Sets a parameter
+ *
+ * @param name the parameter name
+ * @param value the parameter value
+ */
+ void setParameter(String name, Object value);
+
+ /**
* The parameters to use for the route template when creating the new route
*/
Map<String, Object> getParameters();
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 cbabc0c..a15a465 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,12 +56,11 @@ 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).
+ * Camel will cache the result from the supplier from first lookup
(singleton scope). If you do not need cached then
+ * use {@link #bindAsPrototype(String, Class, Supplier)} instead.
* <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.
- *
* @param id the id of the bean
* @param type the type of the bean to associate the
binding
* @param bean a supplier for the bean
@@ -70,6 +69,20 @@ public interface Registry extends BeanRepository {
void bind(String id, Class<?> type, Supplier<Object> bean) throws
RuntimeCamelException;
/**
+ * 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.
+ *
+ * @param id the id of the bean
+ * @param type the type of the bean to associate the
binding
+ * @param bean a supplier for the bean
+ * @throws RuntimeCamelException is thrown if binding is not possible
+ */
+ void bindAsPrototype(String id, Class<?> type, Supplier<Object> bean)
throws RuntimeCamelException;
+
+ /**
* Strategy to wrap the value to be stored in the registry.
*
* @param value the value
diff --git
a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java
b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java
index 7759a27..e7bfc13 100644
---
a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java
+++
b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java
@@ -30,8 +30,8 @@ import org.apache.camel.CamelContext;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.FailedToCreateRouteFromTemplateException;
import org.apache.camel.RouteTemplateContext;
-import org.apache.camel.builder.DefaultRouteTemplateContext;
import org.apache.camel.model.DataFormatDefinition;
+import org.apache.camel.model.DefaultRouteTemplateContext;
import org.apache.camel.model.FaultToleranceConfigurationDefinition;
import org.apache.camel.model.HystrixConfigurationDefinition;
import org.apache.camel.model.Model;
@@ -225,7 +225,10 @@ public class DefaultModel implements Model {
@Deprecated
public String addRouteFromTemplate(final String routeId, final String
routeTemplateId, final Map<String, Object> parameters)
throws Exception {
- RouteTemplateContext rtc = new
DefaultRouteTemplateContext(camelContext, parameters);
+ RouteTemplateContext rtc = new
DefaultRouteTemplateContext(camelContext);
+ if (parameters != null) {
+ parameters.forEach(rtc::setParameter);
+ }
return addRouteFromTemplate(routeId, routeTemplateId, rtc);
}
diff --git
a/core/camel-core-model/src/main/java/org/apache/camel/builder/TemplatedRouteBuilder.java
b/core/camel-core-model/src/main/java/org/apache/camel/builder/TemplatedRouteBuilder.java
index 65e5c0b..05a31eb 100644
---
a/core/camel-core-model/src/main/java/org/apache/camel/builder/TemplatedRouteBuilder.java
+++
b/core/camel-core-model/src/main/java/org/apache/camel/builder/TemplatedRouteBuilder.java
@@ -16,7 +16,6 @@
*/
package org.apache.camel.builder;
-import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;
@@ -24,6 +23,7 @@ import java.util.function.Supplier;
import org.apache.camel.CamelContext;
import org.apache.camel.RouteTemplateContext;
import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.model.DefaultRouteTemplateContext;
import org.apache.camel.model.ModelCamelContext;
import org.apache.camel.model.RouteTemplateDefinition;
@@ -34,8 +34,6 @@ public final class TemplatedRouteBuilder {
private final CamelContext camelContext;
private final String routeTemplateId;
- // TODO: use parameters directly on routeTemplateContext (need SPI update)
- private final Map<String, Object> parameters = new HashMap<>();
private final RouteTemplateContext routeTemplateContext;
private String routeId;
private Consumer<RouteTemplateDefinition> handler;
@@ -44,7 +42,7 @@ public final class TemplatedRouteBuilder {
private TemplatedRouteBuilder(CamelContext camelContext, String
routeTemplateId) {
this.camelContext = camelContext;
this.routeTemplateId = routeTemplateId;
- this.routeTemplateContext = new
DefaultRouteTemplateContext(camelContext, parameters);
+ this.routeTemplateContext = new
DefaultRouteTemplateContext(camelContext);
}
/**
@@ -76,7 +74,7 @@ public final class TemplatedRouteBuilder {
* @param value parameter value
*/
public TemplatedRouteBuilder parameter(String name, Object value) {
- parameters.put(name, value);
+ routeTemplateContext.setParameter(name, value);
return this;
}
@@ -86,7 +84,7 @@ public final class TemplatedRouteBuilder {
* @param parameters the template parameters to add
*/
public TemplatedRouteBuilder parameters(Map<String, Object> parameters) {
- this.parameters.putAll(parameters);
+ parameters.forEach(routeTemplateContext::setParameter);
return this;
}
diff --git
a/core/camel-core-model/src/main/java/org/apache/camel/builder/DefaultRouteTemplateContext.java
b/core/camel-core-model/src/main/java/org/apache/camel/model/DefaultRouteTemplateContext.java
similarity index 83%
rename from
core/camel-core-model/src/main/java/org/apache/camel/builder/DefaultRouteTemplateContext.java
rename to
core/camel-core-model/src/main/java/org/apache/camel/model/DefaultRouteTemplateContext.java
index 645b440..a91da2c 100644
---
a/core/camel-core-model/src/main/java/org/apache/camel/builder/DefaultRouteTemplateContext.java
+++
b/core/camel-core-model/src/main/java/org/apache/camel/model/DefaultRouteTemplateContext.java
@@ -14,8 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.camel.builder;
+package org.apache.camel.model;
+import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
@@ -24,19 +25,20 @@ import org.apache.camel.RouteTemplateContext;
import org.apache.camel.spi.BeanRepository;
import org.apache.camel.support.LocalBeanRegistry;
-// TODO: Should this be moved to some other module/package?
-
+/**
+ * Default {@link RouteTemplateContext}.
+ */
public final class DefaultRouteTemplateContext implements RouteTemplateContext
{
private final CamelContext camelContext;
private final LocalBeanRegistry registry;
private final Map<String, Object> parameters;
- public DefaultRouteTemplateContext(CamelContext camelContext, Map<String,
Object> parameters) {
+ public DefaultRouteTemplateContext(CamelContext camelContext) {
this.camelContext = camelContext;
// we just need the simple registry that also supports supplier style
this.registry = new LocalBeanRegistry();
- this.parameters = parameters;
+ this.parameters = new HashMap<>();
}
@Override
@@ -55,11 +57,21 @@ public final class DefaultRouteTemplateContext implements
RouteTemplateContext {
}
@Override
+ public void bindAsPrototype(String id, Class<?> type, Supplier<Object>
bean) {
+ registry.bindAsPrototype(id, type, bean);
+ }
+
+ @Override
public Object getProperty(String name) {
return parameters.get(name);
}
@Override
+ public void setParameter(String name, Object value) {
+ parameters.put(name, value);
+ }
+
+ @Override
public Map<String, Object> getParameters() {
return parameters;
}
diff --git
a/core/camel-core-model/src/main/java/org/apache/camel/model/Model.java
b/core/camel-core-model/src/main/java/org/apache/camel/model/Model.java
index 5d41864..5af3271 100644
--- a/core/camel-core-model/src/main/java/org/apache/camel/model/Model.java
+++ b/core/camel-core-model/src/main/java/org/apache/camel/model/Model.java
@@ -23,6 +23,7 @@ import java.util.function.Function;
import org.apache.camel.CamelContext;
import org.apache.camel.RouteTemplateContext;
+import org.apache.camel.builder.TemplatedRouteBuilder;
import org.apache.camel.model.cloud.ServiceCallConfigurationDefinition;
import org.apache.camel.model.rest.RestDefinition;
import org.apache.camel.model.transformer.TransformerDefinition;
@@ -169,11 +170,12 @@ public interface Model {
/**
* Adds a new route from a given route template
*
- * @param routeId the id of the new route to add (optional)
- * @param routeTemplateId the id of the route template (mandatory)
- * @param parameters parameters to use for the route template when
creating the new route
- * @return the id of the route added (for example when an
id was auto assigned)
- * @throws Exception is thrown if error creating and adding the new
route
+ * @param routeId the id of the new route to add (optional)
+ * @param routeTemplateId the id of the route template (mandatory)
+ * @param parameters parameters to use for the route template
when creating the new route
+ * @return the id of the route added (for example when
an id was auto assigned)
+ * @throws Exception is thrown if error creating and adding the
new route
+ * @deprecated use {@link TemplatedRouteBuilder} instead.
*/
@Deprecated
String addRouteFromTemplate(String routeId, String routeTemplateId,
Map<String, Object> parameters) throws Exception;
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..00b81f1 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,16 +56,33 @@ public class DefaultRegistryTest {
FooBar bar1 = (FooBar) registry.lookupByName("myBar");
FooBar bar2 = (FooBar) registry.lookupByName("myBar");
+ assertSame(bar1, bar2);
+ assertEquals("I am lazy 1 me", bar1.hello("me"));
+ assertEquals("I am lazy 1 me", bar2.hello("me"));
+ }
+
+ @Test
+ public void testBindAsPrototypeSupplierLookupByName() throws Exception {
+ counter.set(0);
+
+ registry.bindAsPrototype("myBar", FooBar.class, () -> {
+ FooBar bar = new FooBar();
+ bar.setGreeting("I am lazy " + counter.incrementAndGet());
+ return bar;
+ });
+
+ FooBar bar1 = (FooBar) registry.lookupByName("myBar");
+ FooBar bar2 = (FooBar) registry.lookupByName("myBar");
assertNotSame(bar1, bar2);
assertEquals("I am lazy 1 me", bar1.hello("me"));
assertEquals("I am lazy 2 me", bar2.hello("me"));
}
@Test
- public void testBindAsSupplierLookupByNameAndType() throws Exception {
+ public void testBindAsPrototypeSupplierLookupByNameAndType() throws
Exception {
counter.set(0);
- registry.bind("myBar", FooBar.class, () -> {
+ registry.bindAsPrototype("myBar", FooBar.class, () -> {
FooBar bar = new FooBar();
bar.setGreeting("I am lazy " + counter.incrementAndGet());
return bar;
@@ -79,6 +96,23 @@ public class DefaultRegistryTest {
}
@Test
+ public void testBindAsSupplierLookupByNameAndType() throws Exception {
+ counter.set(0);
+
+ registry.bind("myBar", FooBar.class, () -> {
+ FooBar bar = new FooBar();
+ bar.setGreeting("I am lazy " + counter.incrementAndGet());
+ return bar;
+ });
+
+ FooBar bar1 = registry.lookupByNameAndType("myBar", FooBar.class);
+ FooBar bar2 = registry.lookupByNameAndType("myBar", FooBar.class);
+ assertSame(bar1, bar2);
+ assertEquals("I am lazy 1 me", bar1.hello("me"));
+ assertEquals("I am lazy 1 me", bar2.hello("me"));
+ }
+
+ @Test
public void testBindAsSupplierFindByType() throws Exception {
counter.set(0);
@@ -95,7 +129,32 @@ public class DefaultRegistryTest {
assertEquals("I am lazy 1 me", it.next().hello("me"));
assertSame(myFooBar, it.next());
- // find secoond time which we find the supplier and then then from
fallback
+ // find second time which we find the supplier and then then from
fallback
+ set = registry.findByType(FooBar.class);
+ assertEquals(2, set.size());
+ it = set.iterator();
+ assertEquals("I am lazy 1 me", it.next().hello("me"));
+ assertSame(myFooBar, it.next());
+ }
+
+ @Test
+ public void testBindAsPrototypeSupplierFindByType() throws Exception {
+ counter.set(0);
+
+ registry.bindAsPrototype("myBar", FooBar.class, () -> {
+ FooBar bar = new FooBar();
+ bar.setGreeting("I am lazy " + counter.incrementAndGet());
+ return bar;
+ });
+
+ // find first time which we find the supplier and then then from
fallback
+ Set<FooBar> set = registry.findByType(FooBar.class);
+ assertEquals(2, set.size());
+ Iterator<FooBar> it = set.iterator();
+ assertEquals("I am lazy 1 me", it.next().hello("me"));
+ assertSame(myFooBar, it.next());
+
+ // find second time which we find the supplier and then then from
fallback
set = registry.findByType(FooBar.class);
assertEquals(2, set.size());
it = set.iterator();
@@ -104,6 +163,31 @@ public class DefaultRegistryTest {
}
@Test
+ public void testBindAsPrototypeSupplierFindByTypeWithName() throws
Exception {
+ counter.set(0);
+
+ registry.bindAsPrototype("myBar", FooBar.class, () -> {
+ FooBar bar = new FooBar();
+ bar.setGreeting("I am lazy " + counter.incrementAndGet());
+ return bar;
+ });
+
+ // find first time which we find the supplier and then then from
fallback
+ Map<String, FooBar> map = registry.findByTypeWithName(FooBar.class);
+ assertEquals(2, map.size());
+ Iterator<FooBar> it = map.values().iterator();
+ assertEquals("I am lazy 1 me", it.next().hello("me"));
+ assertSame(myFooBar, it.next());
+
+ // find second time which we find the supplier and then then from
fallback
+ map = registry.findByTypeWithName(FooBar.class);
+ assertEquals(2, map.size());
+ it = map.values().iterator();
+ assertEquals("I am lazy 2 me", it.next().hello("me"));
+ assertSame(myFooBar, it.next());
+ }
+
+ @Test
public void testBindAsSupplierFindByTypeWithName() throws Exception {
counter.set(0);
@@ -120,11 +204,11 @@ public class DefaultRegistryTest {
assertEquals("I am lazy 1 me", it.next().hello("me"));
assertSame(myFooBar, it.next());
- // find secoond time which we find the supplier and then then from
fallback
+ // find second time which we find the supplier and then then from
fallback
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/DefaultRegistry.java
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultRegistry.java
index e4c480b..7864fb7 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
@@ -37,6 +37,7 @@ import org.apache.camel.spi.Registry;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.support.service.ServiceSupport;
import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.function.Suppliers;
/**
* The default {@link Registry} which supports using a given first-choice
repository to lookup the beans, such as
@@ -173,6 +174,12 @@ public class DefaultRegistry extends ServiceSupport
implements Registry, LocalBe
@Override
public void bind(String id, Class<?> type, Supplier<Object> bean) throws
RuntimeCamelException {
+ // wrap in cached supplier (memorize)
+ supplierRegistry.bind(id, type, Suppliers.memorize(bean));
+ }
+
+ @Override
+ public void bindAsPrototype(String id, Class<?> type, Supplier<Object>
bean) throws RuntimeCamelException {
supplierRegistry.bind(id, type, bean);
}
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 2f9bb91..1ec61d4 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,6 +25,7 @@ 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;
/**
@@ -110,6 +111,11 @@ public class SimpleRegistry extends LinkedHashMap<String,
Map<Class<?>, Object>>
}
@Override
+ public void bindAsPrototype(String id, Class<?> type, Supplier<Object>
bean) throws RuntimeCamelException {
+ throw new UnsupportedOperationException("Use SupplierRegistry");
+ }
+
+ @Override
public void close() throws IOException {
clear();
}
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 eff10da..851847b 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
@@ -27,7 +27,8 @@ import org.apache.camel.NoSuchBeanException;
/**
* 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).
+ * Camel will cache the result from the supplier from first lookup (singleton
scope). If you do not need cached then use
+ * {@link #bindAsPrototype(String, Class, Supplier)} instead.
*
* To bind a bean as a supplier, then use the {@link
org.apache.camel.spi.Registry#bind(String, Class, Supplier)}
* method.