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

davsclaus 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 8dc54ec88fd CAMEL-20815: exchange.getVariable does not get Global 
variables in custom processor
8dc54ec88fd is described below

commit 8dc54ec88fd443d67cd6b3e1bf685509488c50d1
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu May 30 10:57:46 2024 +0200

    CAMEL-20815: exchange.getVariable does not get Global variables in custom 
processor
---
 .../main/java/org/apache/camel/CamelContext.java   |  4 +-
 .../src/main/java/org/apache/camel/Exchange.java   | 25 ++++++++-----
 .../org/apache/camel/impl/DefaultExchangeTest.java | 22 +++++++++++
 .../org/apache/camel/support/AbstractExchange.java | 37 ++++++++++++++++---
 .../org/apache/camel/support/ExchangeHelper.java   | 43 +++++++++++++++++-----
 5 files changed, 106 insertions(+), 25 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/CamelContext.java 
b/core/camel-api/src/main/java/org/apache/camel/CamelContext.java
index 4a0812fbe25..f34b50d5d61 100644
--- a/core/camel-api/src/main/java/org/apache/camel/CamelContext.java
+++ b/core/camel-api/src/main/java/org/apache/camel/CamelContext.java
@@ -821,8 +821,8 @@ public interface CamelContext extends 
CamelContextLifecycle, RuntimeConfiguratio
     /**
      * Sets a variable
      *
-     * @param name  the variable name. Can be prefixed with repo-id:name to 
lookup the variable from a specific
-     *              repository. If no repo-id is provided, then global 
repository will be used.
+     * @param name  the variable name. Can be prefixed with repo-id:name to 
store the variable in a specific repository.
+     *              If no repo-id is provided, then global repository will be 
used.
      * @param value the value of the variable
      */
     void setVariable(String name, Object value);
diff --git a/core/camel-api/src/main/java/org/apache/camel/Exchange.java 
b/core/camel-api/src/main/java/org/apache/camel/Exchange.java
index 50d41f2a8c5..13cda74cb76 100644
--- a/core/camel-api/src/main/java/org/apache/camel/Exchange.java
+++ b/core/camel-api/src/main/java/org/apache/camel/Exchange.java
@@ -477,7 +477,8 @@ public interface Exchange extends VariableAware {
     /**
      * Returns a variable by name
      *
-     * @param  name the name of the variable
+     * @param  name the variable name. Can be prefixed with repo-id:name to 
lookup the variable from a specific
+     *              repository. If no repo-id is provided, then variables will 
be from the current exchange.
      * @return      the value of the given variable or <tt>null</tt> if there 
is no variable for the given name
      */
     Object getVariable(String name);
@@ -485,7 +486,8 @@ public interface Exchange extends VariableAware {
     /**
      * Returns a variable by name and specifying the type required
      *
-     * @param  name the name of the variable
+     * @param  name the variable name. Can be prefixed with repo-id:name to 
lookup the variable from a specific
+     *              repository. If no repo-id is provided, then variables will 
be from the current exchange.
      * @param  type the type of the variable
      * @return      the value of the given variable or <tt>null</tt> if there 
is no variable for the given name or
      *              <tt>null</tt> if it cannot be converted to the given type
@@ -495,7 +497,8 @@ public interface Exchange extends VariableAware {
     /**
      * Returns a variable by name and specifying the type required
      *
-     * @param  name         the name of the variable
+     * @param  name         the variable name. Can be prefixed with 
repo-id:name to lookup the variable from a specific
+     *                      repository. If no repo-id is provided, then 
variables will be from the current exchange.
      * @param  defaultValue the default value to return if variable was absent
      * @param  type         the type of the variable
      * @return              the value of the given variable or 
<tt>defaultValue</tt> if there is no variable for the
@@ -506,7 +509,8 @@ public interface Exchange extends VariableAware {
     /**
      * Sets a variable on the exchange
      *
-     * @param name  of the variable
+     * @param name  the variable name. Can be prefixed with repo-id:name to 
store the variable in a specific repository.
+     *              If no repo-id is provided, then variables will be stored 
in the current exchange.
      * @param value the value of the variable
      */
     void setVariable(String name, Object value);
@@ -514,22 +518,25 @@ public interface Exchange extends VariableAware {
     /**
      * Removes the given variable
      *
-     * @param  name of the variable, or use * to remove all variables
+     * If the name is <tt>*</tt> then all variables from the current exchange 
is removed, and null is returned.
+     *
+     * @param  name the variable name. Can be prefixed with repo-id:name to 
remove the variable in a specific
+     *              repository. If no repo-id is provided, then the variable 
from the current exchange will be removed
      * @return      the old value of the variable, or <tt>null</tt> if there 
was no variable for the given name
      */
     Object removeVariable(String name);
 
     /**
-     * Returns the variables
+     * Returns the variables from the current exchange
      *
-     * @return the variables in a Map.
+     * @return the variables from the current exchange in a Map.
      */
     Map<String, Object> getVariables();
 
     /**
-     * Returns whether any variables have been set
+     * Returns whether any variables have been set on the current exchange
      *
-     * @return <tt>true</tt> if any variables has been set
+     * @return <tt>true</tt> if any variables has been set on the current 
exchange
      */
     boolean hasVariables();
 
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java 
b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java
index bf763b50a90..954e60898cb 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java
@@ -166,6 +166,23 @@ public class DefaultExchangeTest extends 
ExchangeTestSupport {
         assertEquals("banana", exchange.getVariable("beer", "banana", 
String.class));
     }
 
+    @Test
+    public void testGlobalVariable() {
+        exchange.removeVariable("cheese");
+        assertFalse(exchange.hasVariables());
+
+        exchange.setVariable("fruit", "apple");
+        assertTrue(exchange.hasVariables());
+        assertEquals("apple", exchange.getVariable("fruit"));
+
+        exchange.setVariable("global:myGlob", "myGlobVar");
+        assertEquals("myGlobVar", exchange.getVariable("global:myGlob"));
+        assertNull(exchange.getVariable("myGlob"));
+
+        exchange.removeVariable("fruit");
+        assertFalse(exchange.hasVariables());
+    }
+
     @Test
     public void testRemoveProperties() {
         exchange.removeProperty("foobar");
@@ -201,6 +218,11 @@ public class DefaultExchangeTest extends 
ExchangeTestSupport {
         assertEquals("apple", exchange.getVariable("fruit"));
         assertEquals("banana", exchange.getVariable("fruit1"));
         assertEquals("Africa", exchange.getVariable("zone"));
+
+        exchange.setVariable("global:myGlob", "myGlobVar");
+        assertEquals("myGlobVar", exchange.getVariable("global:myGlob"));
+        exchange.removeVariable("global:myGlob");
+        assertNull(exchange.getVariable("global:myGlob"));
     }
 
     @Test
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/AbstractExchange.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/AbstractExchange.java
index 593e5f047dc..e059b0e32a0 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/AbstractExchange.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/AbstractExchange.java
@@ -34,6 +34,7 @@ import org.apache.camel.Message;
 import org.apache.camel.MessageHistory;
 import org.apache.camel.SafeCopyProperty;
 import org.apache.camel.spi.UnitOfWork;
+import org.apache.camel.spi.VariableRepository;
 import org.apache.camel.trait.message.MessageTrait;
 import org.apache.camel.trait.message.RedeliveryTraitPayload;
 import org.apache.camel.util.ObjectHelper;
@@ -382,7 +383,15 @@ abstract class AbstractExchange implements Exchange {
 
     @Override
     public Object getVariable(String name) {
-        if (variableRepository != null) {
+        VariableRepository repo = null;
+        final String id = ExchangeHelper.getVariableRepositoryId(name);
+        if (id != null) {
+            repo = ExchangeHelper.getVariableRepository(this, id);
+            name = ExchangeHelper.resolveVariableRepositoryName(this, name, 
id);
+        }
+        if (repo != null && name != null) {
+            return repo.getVariable(name);
+        } else if (variableRepository != null) {
             return variableRepository.getVariable(name);
         }
         return null;
@@ -402,15 +411,33 @@ abstract class AbstractExchange implements Exchange {
 
     @Override
     public void setVariable(String name, Object value) {
-        if (variableRepository == null) {
-            variableRepository = new ExchangeVariableRepository(getContext());
+        VariableRepository repo = null;
+        final String id = ExchangeHelper.getVariableRepositoryId(name);
+        if (id != null) {
+            repo = ExchangeHelper.getVariableRepository(this, id);
+            name = ExchangeHelper.resolveVariableRepositoryName(this, name, 
id);
+        }
+        if (repo != null) {
+            repo.setVariable(name, value);
+        } else {
+            if (variableRepository == null) {
+                variableRepository = new 
ExchangeVariableRepository(getContext());
+            }
+            variableRepository.setVariable(name, value);
         }
-        variableRepository.setVariable(name, value);
     }
 
     @Override
     public Object removeVariable(String name) {
-        if (variableRepository != null) {
+        VariableRepository repo = null;
+        final String id = ExchangeHelper.getVariableRepositoryId(name);
+        if (id != null) {
+            repo = ExchangeHelper.getVariableRepository(this, id);
+            name = ExchangeHelper.resolveVariableRepositoryName(this, name, 
id);
+        }
+        if (repo != null) {
+            return repo.removeVariable(name);
+        } else if (variableRepository != null) {
             if ("*".equals(name)) {
                 variableRepository.clear();
                 return null;
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java 
b/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java
index e201a16448f..9f087318d0a 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java
@@ -1088,16 +1088,22 @@ public final class ExchangeHelper {
      */
     public static void setVariable(Exchange exchange, String name, Object 
value) {
         VariableRepository repo = null;
-        final String id = getRepositoryId(name);
+        final String id = getVariableRepositoryId(name);
         if (id != null) {
             repo = getVariableRepository(exchange, id);
-            name = resolveRepositoryName(exchange, name, id);
+            name = resolveVariableRepositoryName(exchange, name, id);
         }
         final VariableAware va = getVariableAware(exchange, repo);
         va.setVariable(name, value);
     }
 
-    private static String getRepositoryId(String name) {
+    /**
+     * Gets the variable repository id
+     *
+     * @param  name the variable name
+     * @return      the repository id if any given, or null
+     */
+    public static String getVariableRepositoryId(String name) {
         String id = StringHelper.before(name, ":");
         // header and exchange is reserved
         if (isReserved(id)) {
@@ -1106,13 +1112,24 @@ public final class ExchangeHelper {
         return id;
     }
 
-    private static String resolveRepositoryName(Exchange exchange, String 
name, String id) {
+    /**
+     * Resolves the variable name
+     *
+     * @param  exchange the exchange
+     * @param  name     the variable name
+     * @param  id       the repository id
+     * @return          the resolved variable name
+     */
+    public static String resolveVariableRepositoryName(Exchange exchange, 
String name, String id) {
         name = StringHelper.after(name, ":");
         // special for route, where we need to enrich the name with current 
route id if none given
         if ("route".equals(id) && !name.contains(":")) {
             String prefix = getAtRouteId(exchange);
             if (prefix != null) {
                 name = prefix + ":" + name;
+            } else {
+                // we are not currently in a given route
+                return null;
             }
         }
         return name;
@@ -1128,10 +1145,10 @@ public final class ExchangeHelper {
      */
     public static void setVariableFromMessageBodyAndHeaders(Exchange exchange, 
String name, Message message) {
         VariableRepository repo = null;
-        final String id = getRepositoryId(name);
+        final String id = getVariableRepositoryId(name);
         if (id != null) {
             repo = getVariableRepository(exchange, id);
-            name = resolveRepositoryName(exchange, name, id);
+            name = resolveVariableRepositoryName(exchange, name, id);
         }
         final VariableAware va = getVariableAware(exchange, repo);
 
@@ -1178,16 +1195,24 @@ public final class ExchangeHelper {
      */
     public static Object getVariable(Exchange exchange, String name) {
         VariableRepository repo = null;
-        final String id = getRepositoryId(name);
+        final String id = getVariableRepositoryId(name);
         if (id != null) {
             repo = getVariableRepository(exchange, id);
-            name = resolveRepositoryName(exchange, name, id);
+            name = resolveVariableRepositoryName(exchange, name, id);
         }
         final VariableAware va = getVariableAware(exchange, repo);
         return va.getVariable(name);
     }
 
-    private static VariableRepository getVariableRepository(Exchange exchange, 
String id) {
+    /**
+     * Gets the variable repository by the given id
+     *
+     * @param  exchange                 the exchange
+     * @param  id                       the repository id
+     * @return                          the variable repository
+     * @throws IllegalArgumentException is thrown if the repository does not 
eists
+     */
+    public static VariableRepository getVariableRepository(Exchange exchange, 
String id) {
         VariableRepositoryFactory factory
                 = 
exchange.getContext().getCamelContextExtension().getContextPlugin(VariableRepositoryFactory.class);
         VariableRepository repo = factory.getVariableRepository(id);

Reply via email to