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 19ff3593ebb5 CAMEL-27072: Detect bundled Kamelets versions in launcher 
distributions (#30221)
19ff3593ebb5 is described below

commit 19ff3593ebb52448e89cdfb5db00faa6d40d1fe9
Author: Salvatore Mongiardo <[email protected]>
AuthorDate: Mon Nov 16 22:55:59 2026 +0100

    CAMEL-27072: Detect bundled Kamelets versions in launcher distributions 
(#30221)
---
 .../org/apache/camel/main/util/VersionHelper.java  |  22 ++++
 .../apache/camel/main/util/VersionHelperTest.java  | 113 +++++++++++++++++++++
 2 files changed, 135 insertions(+)

diff --git 
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/util/VersionHelper.java
 
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/util/VersionHelper.java
index 86245b9d95fd..affd5ce9c8f1 100644
--- 
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/util/VersionHelper.java
+++ 
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/util/VersionHelper.java
@@ -16,16 +16,23 @@
  */
 package org.apache.camel.main.util;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.lang.management.ManagementFactory;
 import java.lang.management.RuntimeMXBean;
+import java.util.Properties;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.StringHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public final class VersionHelper {
 
+    private static final Logger LOG = 
LoggerFactory.getLogger(VersionHelper.class);
+
     private static String camelVersion;
 
     private static final Pattern KAMELETS_LIBRARY = 
Pattern.compile("camel-kamelets-(\\d[\\w\\d.-]*).jar", Pattern.DOTALL);
@@ -124,6 +131,21 @@ public final class VersionHelper {
             }
         }
 
+        // Launcher dependencies (nested JARs or a manifest Class-Path) are 
absent from java.class.path.
+        try (InputStream is = VersionHelper.class.getResourceAsStream(
+                
"/META-INF/maven/org.apache.camel.kamelets/camel-kamelets/pom.properties")) {
+            if (is != null) {
+                Properties properties = new Properties();
+                properties.load(is);
+                String version = properties.getProperty("version");
+                if (ObjectHelper.isNotEmpty(version)) {
+                    return version;
+                }
+            }
+        } catch (IOException e) {
+            LOG.debug("Could not read bundled Kamelets catalog metadata, using 
default version", e);
+        }
+
         return KameletCatalogHelper.KAMELETS_VERSION;
     }
 
diff --git 
a/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/util/VersionHelperTest.java
 
b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/util/VersionHelperTest.java
new file mode 100644
index 000000000000..b5ecf33e0e45
--- /dev/null
+++ 
b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/util/VersionHelperTest.java
@@ -0,0 +1,113 @@
+/*
+ * 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.main.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
+
+import org.apache.camel.util.ObjectHelper;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.slf4j.LoggerFactory;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class VersionHelperTest {
+
+    @TempDir
+    Path directory;
+
+    @Test
+    void detectsKameletsVersionFromCatalogMetadata() throws Exception {
+        assertEquals("4.22.1.vendor-00001", 
detectVersion("version=4.22.1.vendor-00001\n"));
+    }
+
+    @Test
+    void preservesSnapshotKameletsVersion() throws Exception {
+        assertEquals("4.24.0-SNAPSHOT", 
detectVersion("version=4.24.0-SNAPSHOT\n"));
+    }
+
+    @Test
+    void fallsBackWhenCatalogMetadataIsAbsent() throws Exception {
+        assertEquals(KameletCatalogHelper.KAMELETS_VERSION, 
detectVersion(null));
+    }
+
+    @Test
+    void fallsBackWhenCatalogMetadataHasNoVersion() throws Exception {
+        assertEquals(KameletCatalogHelper.KAMELETS_VERSION, 
detectVersion("artifactId=camel-kamelets\n"));
+    }
+
+    @Test
+    void fallsBackWhenCatalogVersionIsEmpty() throws Exception {
+        assertEquals(KameletCatalogHelper.KAMELETS_VERSION, 
detectVersion("version=\n"));
+    }
+
+    @Test
+    void fallsBackWhenCatalogMetadataCannotBeRead() throws Exception {
+        assertEquals(KameletCatalogHelper.KAMELETS_VERSION, 
detectVersion("version=4.24.0\n", true));
+    }
+
+    private String detectVersion(String metadata) throws Exception {
+        return detectVersion(metadata, false);
+    }
+
+    private String detectVersion(String metadata, boolean unreadable) throws 
Exception {
+        // A renamed JAR prevents filename-based detection, as with a nested 
launcher dependency.
+        Path catalog = directory.resolve("catalog.jar");
+        writeCatalog(catalog, metadata);
+        URL[] urls = {
+                
VersionHelper.class.getProtectionDomain().getCodeSource().getLocation(),
+                
ObjectHelper.class.getProtectionDomain().getCodeSource().getLocation(),
+                
LoggerFactory.class.getProtectionDomain().getCodeSource().getLocation(),
+                catalog.toUri().toURL()
+        };
+        try (URLClassLoader loader = new URLClassLoader(urls, 
ClassLoader.getPlatformClassLoader()) {
+            @Override
+            public InputStream getResourceAsStream(String name) {
+                if (unreadable && 
"META-INF/maven/org.apache.camel.kamelets/camel-kamelets/pom.properties".equals(name))
 {
+                    return new InputStream() {
+                        @Override
+                        public int read() throws IOException {
+                            throw new IOException("Cannot read catalog 
metadata");
+                        }
+                    };
+                }
+                return super.getResourceAsStream(name);
+            }
+        }) {
+            Class<?> helper = loader.loadClass(VersionHelper.class.getName());
+            return (String) 
helper.getMethod("extractKameletsVersion").invoke(null);
+        }
+    }
+
+    private void writeCatalog(Path catalog, String metadata) throws 
IOException {
+        try (JarOutputStream jar = new 
JarOutputStream(Files.newOutputStream(catalog))) {
+            if (metadata != null) {
+                jar.putNextEntry(new 
JarEntry("META-INF/maven/org.apache.camel.kamelets/camel-kamelets/pom.properties"));
+                jar.write(metadata.getBytes(StandardCharsets.ISO_8859_1));
+                jar.closeEntry();
+            }
+        }
+    }
+}

Reply via email to