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

davsclaus pushed a commit to branch CAMEL-27070-kamelets-index
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 2c9727f3fb67d7ab970105cbe2268522160bd46a
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon Nov 16 16:59:56 2026 +0100

    CAMEL-27070: camel-jbang - list the Kamelets from the index in the 
camel-kamelets JAR
    
    camel-kamelets ships META-INF/kamelets-index.json 
(apache/camel-kamelets#3137) with the name, type, support level and
    description of each Kamelet. When the camel-kamelets JAR of the Kamelets 
version is on the class path and has the
    index, camel catalog kamelet, the Kamelet names for export and for "did you 
mean" in camel catalog doc, and the MCP
    kamelet list tool read it instead of downloading and loading the Kamelets 
catalog. For another version, or a JAR
    without the index (4.23.0 and older), they load the catalog as before.
    
    camel catalog kamelet: 830 -> 190 ms, with the same output for the 259 
Kamelets.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../core/commands/catalog/CatalogKamelet.java      |   8 +-
 .../dsl/jbang/core/commands/mcp/KameletTools.java  |  20 +++-
 .../jbang/run/catalog/KameletCatalogHelper.java    |  90 +++++++++++++++
 .../run/catalog/KameletCatalogHelperTest.java      | 121 +++++++++++++++++++++
 4 files changed, 228 insertions(+), 11 deletions(-)

diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/catalog/CatalogKamelet.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/catalog/CatalogKamelet.java
index 09350861c4d7..f120512a8c0e 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/catalog/CatalogKamelet.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/catalog/CatalogKamelet.java
@@ -21,7 +21,6 @@ import java.util.Arrays;
 import java.util.Comparator;
 import java.util.List;
 import java.util.Locale;
-import java.util.Map;
 import java.util.stream.Collectors;
 
 import com.github.freva.asciitable.AsciiTable;
@@ -144,12 +143,7 @@ public class CatalogKamelet extends CamelCommand {
      * The Kamelets of the catalog of the Kamelets version.
      */
     protected List<KameletModel> loadKamelets() throws Exception {
-        Map<String, Object> kamelets = 
KameletCatalogHelper.loadKamelets(kameletsVersion, mavenResolver.repos());
-        List<KameletModel> models = new ArrayList<>();
-        for (Object o : kamelets.values()) {
-            models.add(KameletCatalogHelper.createModel(o, false));
-        }
-        return models;
+        return KameletCatalogHelper.loadKameletSummaries(kameletsVersion, 
mavenResolver.repos());
     }
 
     private static JsonObject toJson(KameletModel r) {
diff --git 
a/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/KameletTools.java
 
b/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/KameletTools.java
index de194e99180d..d75f524c43e1 100644
--- 
a/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/KameletTools.java
+++ 
b/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/KameletTools.java
@@ -67,6 +67,21 @@ public class KameletTools {
         return existing != null ? existing : loaded;
     }
 
+    /**
+     * The Kamelets of {@code version} for a list: from the index in the 
camel-kamelets JAR when it has one, otherwise
+     * from the cached catalog.
+     */
+    private List<KameletModel> loadKameletSummaries(String version) throws 
Exception {
+        List<KameletModel> answer = KameletCatalogHelper.loadIndex(version);
+        if (answer == null) {
+            answer = new ArrayList<>();
+            for (Object o : loadKamelets(version).values()) {
+                answer.add(KameletCatalogHelper.createModel(o, false));
+            }
+        }
+        return answer;
+    }
+
     /**
      * Tool to list available Kamelets.
      */
@@ -89,12 +104,9 @@ public class KameletTools {
             String version = kameletsVersion != null && 
!kameletsVersion.isBlank()
                     ? kameletsVersion : VersionHelper.extractKameletsVersion();
 
-            Map<String, Object> kamelets = loadKamelets(version);
-
             // Build the full filtered list first, then sort, then limit — 
never limit before sorting
             List<KameletInfo> all = new ArrayList<>();
-            for (Object o : kamelets.values()) {
-                KameletModel km = KameletCatalogHelper.createModel(o, false);
+            for (KameletModel km : loadKameletSummaries(version)) {
 
                 // filter by type
                 if (type != null && !type.isBlank()
diff --git 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/catalog/KameletCatalogHelper.java
 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/catalog/KameletCatalogHelper.java
index 3e1e84e76c15..03f0047823ea 100644
--- 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/catalog/KameletCatalogHelper.java
+++ 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/catalog/KameletCatalogHelper.java
@@ -17,9 +17,13 @@
 package org.apache.camel.dsl.jbang.run.catalog;
 
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.lang.reflect.Method;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Enumeration;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -29,6 +33,9 @@ import 
org.apache.camel.main.download.DependencyDownloaderClassLoader;
 import org.apache.camel.main.download.MavenDependencyDownloader;
 import org.apache.camel.support.ObjectHelper;
 import org.apache.camel.util.StringHelper;
+import org.apache.camel.util.json.JsonArray;
+import org.apache.camel.util.json.JsonObject;
+import org.apache.camel.util.json.Jsoner;
 
 public final class KameletCatalogHelper {
 
@@ -38,11 +45,94 @@ public final class KameletCatalogHelper {
     private KameletCatalogHelper() {
     }
 
+    // the index of the Kamelets in the camel-kamelets JAR (camel-kamelets 
4.24 and newer)
+    static final String INDEX = "META-INF/kamelets-index.json";
+
     public static List<String> findKameletNames(String version, String 
repositories) throws Exception {
+        List<KameletModel> index = loadIndex(version);
+        if (index != null) {
+            List<String> answer = new ArrayList<>(index.size());
+            for (KameletModel km : index) {
+                answer.add(km.name);
+            }
+            return answer;
+        }
         Map<String, Object> kamelets = loadKamelets(version, repositories);
         return new ArrayList<>(kamelets.keySet());
     }
 
+    /**
+     * The Kamelets of the version, with only their name, type, support level 
and description. When the camel-kamelets
+     * JAR of this version is on the class path and has the index of its 
Kamelets, they come from that index, which
+     * takes a few milliseconds; otherwise from the Kamelets catalog, which is 
downloaded and loaded (half a second or
+     * more).
+     */
+    public static List<KameletModel> loadKameletSummaries(String version, 
String repositories) throws Exception {
+        List<KameletModel> answer = loadIndex(version);
+        if (answer == null) {
+            answer = new ArrayList<>();
+            for (Object o : loadKamelets(version, repositories).values()) {
+                answer.add(createModel(o, false));
+            }
+        }
+        return answer;
+    }
+
+    /**
+     * The Kamelets in the index of the camel-kamelets JAR of the version on 
the class path, with only their name, type,
+     * support level and description, or null when there is no such JAR or it 
has no index.
+     */
+    public static List<KameletModel> loadIndex(String version) {
+        return loadIndex(KameletCatalogHelper.class.getClassLoader(), version);
+    }
+
+    /**
+     * The Kamelets in the index of the camel-kamelets JAR of the version, or 
null when the class loader has no such
+     * JAR, or the JAR has no index (camel-kamelets 4.23 and older), or the 
index cannot be read.
+     */
+    static List<KameletModel> loadIndex(ClassLoader cl, String version) {
+        if (version == null || cl == null) {
+            return null;
+        }
+        // the JAR of this version only: another version on the class path has 
other Kamelets
+        String jar = "camel-kamelets-" + version + ".jar!/";
+        try {
+            Enumeration<URL> urls = cl.getResources(INDEX);
+            while (urls.hasMoreElements()) {
+                URL url = urls.nextElement();
+                if (url.toString().endsWith(jar + INDEX)) {
+                    try (InputStream is = url.openStream()) {
+                        return parseIndex(is);
+                    }
+                }
+            }
+        } catch (Exception e) {
+            // the catalog has the same Kamelets
+        }
+        return null;
+    }
+
+    static List<KameletModel> parseIndex(InputStream is) throws Exception {
+        JsonObject root = (JsonObject) Jsoner.deserialize(new 
InputStreamReader(is, StandardCharsets.UTF_8));
+        JsonArray kamelets = root.getCollection("kamelets");
+        if (kamelets == null) {
+            return null;
+        }
+        List<KameletModel> answer = new ArrayList<>(kamelets.size());
+        for (Object o : kamelets) {
+            JsonObject jo = (JsonObject) o;
+            KameletModel km = new KameletModel();
+            km.name = jo.getString("name");
+            km.type = jo.getString("type");
+            km.supportLevel = jo.getString("supportLevel");
+            km.description = jo.getString("description");
+            if (km.name != null) {
+                answer.add(km);
+            }
+        }
+        return answer;
+    }
+
     public static KameletModel createModel(Object kamelet, boolean all) throws 
Exception {
         KameletModel km = new KameletModel();
         km.name = getName(kamelet);
diff --git 
a/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/catalog/KameletCatalogHelperTest.java
 
b/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/catalog/KameletCatalogHelperTest.java
index b0ef5fa8b400..cbba2b6f1de6 100644
--- 
a/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/catalog/KameletCatalogHelperTest.java
+++ 
b/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/catalog/KameletCatalogHelperTest.java
@@ -16,14 +16,26 @@
  */
 package org.apache.camel.dsl.jbang.run.catalog;
 
+import java.io.OutputStream;
+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.ArrayList;
+import java.util.List;
 import java.util.Map;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
 
 import org.apache.camel.dsl.jbang.run.common.VersionHelper;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.junit.jupiter.api.Assumptions.assumeFalse;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
 
 class KameletCatalogHelperTest {
 
@@ -41,4 +53,113 @@ class KameletCatalogHelperTest {
         // the callers share it
         assertThatThrownBy(() -> 
first.remove("timer-source")).isInstanceOf(UnsupportedOperationException.class);
     }
+
+    private static final String INDEX = """
+            {
+              "kamelets" : [ {
+                "name" : "aws-ddb-sink",
+                "type" : "sink",
+                "supportLevel" : "Stable",
+                "title" : "AWS DynamoDB Sink",
+                "description" : "Send data to Amazon DynamoDB. The sent data 
inserts, updates, or deletes an item.",
+                "group" : "AWS DynamoDB Streams",
+                "namespace" : "AWS",
+                "provider" : "Apache Software Foundation"
+              }, {
+                "name" : "timer-source",
+                "type" : "source",
+                "supportLevel" : "Stable",
+                "title" : "Timer Source",
+                "description" : "Produces periodic messages with a custom 
payload.",
+                "group" : "Timer",
+                "namespace" : "Scheduling",
+                "provider" : "Apache Software Foundation"
+              } ]
+            }
+            """;
+
+    @Test
+    void indexOfTheCamelKameletsJarOfTheVersion(@TempDir Path dir) throws 
Exception {
+        try (URLClassLoader cl = classLoader(jar(dir, 
"camel-kamelets-9.9.9.jar", INDEX))) {
+            List<KameletModel> kamelets = KameletCatalogHelper.loadIndex(cl, 
"9.9.9");
+
+            assertThat(kamelets).extracting(k -> 
k.name).containsExactly("aws-ddb-sink", "timer-source");
+            KameletModel ddb = kamelets.get(0);
+            assertThat(ddb.type).isEqualTo("sink");
+            assertThat(ddb.supportLevel).isEqualTo("Stable");
+            // the whole description, as the catalog has it: the list shows 
its first sentence
+            assertThat(ddb.description)
+                    .isEqualTo("Send data to Amazon DynamoDB. The sent data 
inserts, updates, or deletes an item.");
+        }
+    }
+
+    @Test
+    void noIndexForAnotherVersion(@TempDir Path dir) throws Exception {
+        try (URLClassLoader cl = classLoader(jar(dir, 
"camel-kamelets-9.9.9.jar", INDEX))) {
+            // the Kamelets of another version may differ: the catalog of that 
version is loaded instead
+            assertThat(KameletCatalogHelper.loadIndex(cl, "9.9.8")).isNull();
+            assertThat(KameletCatalogHelper.loadIndex(cl, null)).isNull();
+        }
+    }
+
+    @Test
+    void noIndexInTheCamelKameletsJar(@TempDir Path dir) throws Exception {
+        // camel-kamelets 4.23 and older have no index
+        try (URLClassLoader cl = classLoader(jar(dir, 
"camel-kamelets-9.9.9.jar", null))) {
+            assertThat(KameletCatalogHelper.loadIndex(cl, "9.9.9")).isNull();
+        }
+    }
+
+    @Test
+    void indexOfAnotherJarIsNotUsed(@TempDir Path dir) throws Exception {
+        try (URLClassLoader cl = classLoader(jar(dir, 
"camel-kamelets-catalog-9.9.9.jar", INDEX))) {
+            assertThat(KameletCatalogHelper.loadIndex(cl, "9.9.9")).isNull();
+        }
+    }
+
+    @Test
+    void brokenIndexIsNotUsed(@TempDir Path dir) throws Exception {
+        try (URLClassLoader cl = classLoader(jar(dir, 
"camel-kamelets-9.9.9.jar", "{ \"kamelets\" : [ "))) {
+            assertThat(KameletCatalogHelper.loadIndex(cl, "9.9.9")).isNull();
+        }
+    }
+
+    @Test
+    void indexHasTheKameletsOfTheCatalog() throws Exception {
+        String version = VersionHelper.extractKameletsVersion();
+        List<KameletModel> index = KameletCatalogHelper.loadIndex(version);
+        // the index is in camel-kamelets 4.24 and newer
+        assumeTrue(index != null, "the camel-kamelets JAR " + version + " has 
no index");
+
+        List<String> fromIndex = new ArrayList<>();
+        for (KameletModel km : index) {
+            fromIndex.add(km.name + "|" + km.type + "|" + km.supportLevel + 
"|" + km.description);
+        }
+        List<String> fromCatalog = new ArrayList<>();
+        for (Object o : KameletCatalogHelper.loadKamelets(version, 
null).values()) {
+            KameletModel km = KameletCatalogHelper.createModel(o, false);
+            fromCatalog.add(km.name + "|" + km.type + "|" + km.supportLevel + 
"|" + km.description);
+        }
+        assertThat(fromIndex).containsExactlyInAnyOrderElementsOf(fromCatalog);
+    }
+
+    private static Path jar(Path dir, String name, String index) throws 
Exception {
+        Path jar = dir.resolve(name);
+        try (OutputStream os = Files.newOutputStream(jar); JarOutputStream out 
= new JarOutputStream(os)) {
+            out.putNextEntry(new 
JarEntry("kamelets/timer-source.kamelet.yaml"));
+            out.write("kind: Kamelet\n".getBytes(StandardCharsets.UTF_8));
+            out.closeEntry();
+            if (index != null) {
+                out.putNextEntry(new JarEntry(KameletCatalogHelper.INDEX));
+                out.write(index.getBytes(StandardCharsets.UTF_8));
+                out.closeEntry();
+            }
+        }
+        return jar;
+    }
+
+    private static URLClassLoader classLoader(Path jar) throws Exception {
+        // no parent: the camel-kamelets JAR of the build is not seen
+        return new URLClassLoader(new URL[] { jar.toUri().toURL() }, null);
+    }
 }

Reply via email to