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 de07d6242c4 CAMEL-22210: Add UUIDv4 option to simple uuid function 
(#18519)
de07d6242c4 is described below

commit de07d6242c4244c22e35ec7c47b4c0713ec62a48
Author: ogomezdi <[email protected]>
AuthorDate: Wed Jul 2 10:01:54 2025 +0200

    CAMEL-22210: Add UUIDv4 option to simple uuid function (#18519)
    
    * Cambios para incorporar random a la función uuid del lenguaje Simple 
según CAMEL-22210
    
    * After format
    
    * Revert changes in camel-4x-upgrade-guide-4_13.adoc according to davsclaus 
PR comment
---
 .../modules/languages/pages/simple-language.adoc   |  2 +-
 .../language/simple/SimpleExpressionBuilder.java   |  3 ++
 .../simple/ast/SimpleFunctionExpression.java       |  3 ++
 .../apache/camel/impl/RandomUuidGeneratorTest.java | 56 ++++++++++++++++++++++
 .../apache/camel/support/RandomUuidGenerator.java  | 34 +++++++++++++
 .../modules/ROOT/pages/uuidgenerator.adoc          |  2 +
 6 files changed, 99 insertions(+), 1 deletion(-)

diff --git 
a/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc
 
b/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc
index 1ec24b1a428..cc12e22a36c 100644
--- 
a/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc
+++ 
b/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc
@@ -291,7 +291,7 @@ includes the route stack-trace). This can be used if you do 
not want to
 log sensitive data from the message itself.
 
 |uuid(type) |String |Returns a UUID using the Camel `UuidGenerator`.
-You can choose between `default`, `classic`, `short` and `simple` as the type.
+You can choose between `default`, `classic`, `short`, `simple` and `random` as 
the type.
 If no type is given, the default is used. It is also possible to use a custom 
`UuidGenerator`
 and bind the bean to the xref:manual::registry.adoc[Registry] with an id. For 
example `${uuid(myGenerator)}`
 where the ID is _myGenerator_.
diff --git 
a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleExpressionBuilder.java
 
b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleExpressionBuilder.java
index 0e583d41d80..013f7523888 100644
--- 
a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleExpressionBuilder.java
+++ 
b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleExpressionBuilder.java
@@ -46,6 +46,7 @@ import org.apache.camel.support.ExchangeHelper;
 import org.apache.camel.support.ExpressionAdapter;
 import org.apache.camel.support.LanguageHelper;
 import org.apache.camel.support.MessageHelper;
+import org.apache.camel.support.RandomUuidGenerator;
 import org.apache.camel.support.ShortUuidGenerator;
 import org.apache.camel.support.SimpleUuidGenerator;
 import org.apache.camel.support.builder.ExpressionBuilder;
@@ -525,6 +526,8 @@ public final class SimpleExpressionBuilder {
                     uuid = new ShortUuidGenerator();
                 } else if ("simple".equals(generator)) {
                     uuid = new SimpleUuidGenerator();
+                } else if ("random".equals(generator)) {
+                    uuid = new RandomUuidGenerator();
                 } else if (generator == null || "default".equals(generator)) {
                     uuid = new DefaultUuidGenerator();
                 } else {
diff --git 
a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
 
b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
index 040db8bc383..79704aa41d7 100644
--- 
a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
+++ 
b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
@@ -2058,6 +2058,9 @@ public class SimpleFunctionExpression extends 
LiteralExpression {
             } else if ("default".equals(generator)) {
                 sb.append("    UuidGenerator uuid = new 
org.apache.camel.support.DefaultUuidGenerator();\n");
                 sb.append("return uuid.generateUuid();");
+            } else if ("random".equals(generator)) {
+                sb.append("    UuidGenerator uuid = new 
org.apache.camel.support.RandomUuidGenerator();\n");
+                sb.append("return uuid.generateUuid();");
             } else {
                 generator = StringQuoteHelper.doubleQuote(generator);
                 sb.append("if (uuid == null) uuid = 
customUuidGenerator(exchange, ").append(generator)
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/impl/RandomUuidGeneratorTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/impl/RandomUuidGeneratorTest.java
new file mode 100644
index 00000000000..db959e37a9d
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/impl/RandomUuidGeneratorTest.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.impl;
+
+import org.apache.camel.spi.UuidGenerator;
+import org.apache.camel.support.RandomUuidGenerator;
+import org.apache.camel.util.StopWatch;
+import org.apache.camel.util.TimeUtils;
+import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+
+public class RandomUuidGeneratorTest {
+    private static final Logger LOG = 
LoggerFactory.getLogger(RandomUuidGeneratorTest.class);
+
+    @Test
+    public void testGenerateUUID() {
+        UuidGenerator uuidGenerator = new RandomUuidGenerator();
+
+        String firstUUID = uuidGenerator.generateUuid();
+        String secondUUID = uuidGenerator.generateUuid();
+
+        assertNotSame(firstUUID, secondUUID);
+    }
+
+    @Test
+    public void testPerformance() {
+        UuidGenerator uuidGenerator = new RandomUuidGenerator();
+        StopWatch watch = new StopWatch();
+
+        LOG.info("First id: {}", uuidGenerator.generateUuid());
+        for (int i = 0; i < 500000; i++) {
+            uuidGenerator.generateUuid();
+        }
+        LOG.info("Last id: {}", uuidGenerator.generateUuid());
+
+        LOG.info("Took {}", TimeUtils.printDuration(watch.taken(), true));
+    }
+
+}
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/RandomUuidGenerator.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/RandomUuidGenerator.java
new file mode 100644
index 00000000000..8378cfc88fa
--- /dev/null
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/RandomUuidGenerator.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.support;
+
+import java.util.UUID;
+
+import org.apache.camel.spi.UuidGenerator;
+
+/**
+ * Random {@link UuidGenerator} type 4 (pseudo randomly generated) UUID. The 
UUID is generated using a cryptographically
+ * strong pseudo random number generator.
+ */
+public class RandomUuidGenerator implements UuidGenerator {
+
+    @Override
+    public String generateUuid() {
+        return UUID.randomUUID().toString();
+    }
+
+}
diff --git a/docs/user-manual/modules/ROOT/pages/uuidgenerator.adoc 
b/docs/user-manual/modules/ROOT/pages/uuidgenerator.adoc
index 83cdaaf900f..14f80a59d91 100644
--- a/docs/user-manual/modules/ROOT/pages/uuidgenerator.adoc
+++ b/docs/user-manual/modules/ROOT/pages/uuidgenerator.adoc
@@ -43,3 +43,5 @@ Camel comes with the following implementations out of the box:
 * `org.apache.camel.support.SimpleUuidGenerator`: This implementation uses
 internally a `java.util.concurrent.atomic.AtomicLong` and increases the
 ID for every call by one. Starting with 1 as the first id.
+* `org.apache.camel.support.RandomUuidGenerator`: type 4 (pseudo randomly 
generated) UUID. 
+The UUID is generated using a cryptographically strong pseudo random number 
generator.
\ No newline at end of file

Reply via email to