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

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


The following commit(s) were added to refs/heads/main by this push:
     new 5cb8b06b645 Add Variables support to FlexibleAggregationStrategy
5cb8b06b645 is described below

commit 5cb8b06b64546f790774a45dd36d866aa0a8883e
Author: Jeremy Ross <jeremy.g.r...@gmail.com>
AuthorDate: Mon Jun 17 14:31:34 2024 -0500

    Add Variables support to FlexibleAggregationStrategy
---
 .../camel/builder/FlexibleAggregationStrategy.java | 52 ++++++++++++++++++++++
 .../toolbox/FlexibleAggregationStrategiesTest.java | 16 +++++++
 2 files changed, 68 insertions(+)

diff --git 
a/core/camel-core-model/src/main/java/org/apache/camel/builder/FlexibleAggregationStrategy.java
 
b/core/camel-core-model/src/main/java/org/apache/camel/builder/FlexibleAggregationStrategy.java
index 50f182e7240..d7353a73c1c 100644
--- 
a/core/camel-core-model/src/main/java/org/apache/camel/builder/FlexibleAggregationStrategy.java
+++ 
b/core/camel-core-model/src/main/java/org/apache/camel/builder/FlexibleAggregationStrategy.java
@@ -129,6 +129,18 @@ public class FlexibleAggregationStrategy<E> implements 
AggregationStrategy {
         return this;
     }
 
+    /**
+     * Store the result of this Aggregation Strategy (whether an atomic 
element or a Collection) in a variable with the
+     * designated name.
+     *
+     * @param  variableName The variable name.
+     * @return              This instance.
+     */
+    public FlexibleAggregationStrategy<E> storeInVariable(String variableName) 
{
+        this.injector = new VariableInjector(castAs, variableName);
+        return this;
+    }
+
     /**
      * Store the result of this Aggregation Strategy (whether an atomic 
element or a Collection) in an IN message header
      * with the designated name.
@@ -370,7 +382,47 @@ public class FlexibleAggregationStrategy<E> implements 
AggregationStrategy {
         public void setValueAsCollection(Exchange exchange, Collection<E> obj) 
{
             exchange.setProperty(propertyName, obj);
         }
+    }
+
+    private class VariableInjector extends FlexibleAggregationStrategyInjector 
{
+        private final String variableName;
+
+        VariableInjector(Class<E> type, String variableName) {
+            super(type);
+            this.variableName = variableName;
+        }
+
+        @Override
+        public void prepareAggregationExchange(Exchange exchange) {
+            exchange.removeVariable(variableName);
+        }
+
+        @Override
+        public E getValue(Exchange exchange) {
+            return exchange.getVariable(variableName, type);
+        }
+
+        @Override
+        public void setValue(Exchange exchange, E obj) {
+            exchange.setVariable(variableName, obj);
+        }
 
+        @Override
+        @SuppressWarnings("unchecked")
+        public Collection<E> getValueAsCollection(Exchange exchange, Class<? 
extends Collection> type) {
+            Object value = exchange.getVariable(variableName);
+            if (value == null) {
+                // empty so create a new collection to host this
+                return exchange.getContext().getInjector().newInstance(type);
+            } else {
+                return exchange.getVariable(variableName, type);
+            }
+        }
+
+        @Override
+        public void setValueAsCollection(Exchange exchange, Collection<E> obj) 
{
+            exchange.setVariable(variableName, obj);
+        }
     }
 
     private class HeaderInjector extends FlexibleAggregationStrategyInjector {
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/util/toolbox/FlexibleAggregationStrategiesTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/util/toolbox/FlexibleAggregationStrategiesTest.java
index 373d13f30a0..c4ea4bf8ed3 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/util/toolbox/FlexibleAggregationStrategiesTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/util/toolbox/FlexibleAggregationStrategiesTest.java
@@ -124,6 +124,17 @@ public class FlexibleAggregationStrategiesTest extends 
ContextTestSupport {
         assertMockEndpointsSatisfied();
     }
 
+    @Test
+    public void testFlexibleAggregationStrategyStoreInVariableSingleValue() 
throws Exception {
+        getMockEndpoint("mock:result7").expectedMessageCount(1);
+        
getMockEndpoint("mock:result7").message(0).variable("AggregationResult").isInstanceOf(String.class);
+        
getMockEndpoint("mock:result7").message(0).variable("AggregationResult").isEqualTo("AGGREGATE1");
+
+        template.sendBody("direct:start7", "AGGREGATE1");
+
+        assertMockEndpointsSatisfied();
+    }
+
     @Test
     @SuppressWarnings("rawtypes")
     public void testFlexibleAggregationStrategyGenericArrayListWithoutNulls() 
throws Exception {
@@ -267,6 +278,11 @@ public class FlexibleAggregationStrategiesTest extends 
ContextTestSupport {
                                 .accumulateInCollection(ArrayList.class))
                         .constant(true).completionSize(3).to("mock:result6");
 
+                from("direct:start7")
+                        
.aggregate(AggregationStrategies.flexible(String.class).storeInVariable("AggregationResult"))
+                        .constant(true).completionSize(1)
+                        .to("mock:result7");
+
                 AggregationStrategy timeoutCompletionStrategy
                         = 
AggregationStrategies.flexible(String.class).condition(simple("${body} contains 
'AGGREGATE'"))
                                 .timeoutAware(new TimeoutAwareMixin() {

Reply via email to