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 cd4a897c2efd CAMEL-27476: camel-kamelet-main - the Kamelets of
--kamelets-version win over the camel-kamelets jar of the launcher (#30682)
cd4a897c2efd is described below
commit cd4a897c2efd764d97f5db85b09770fb8e112c35
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon Nov 23 20:15:55 2026 +0100
CAMEL-27476: camel-kamelet-main - the Kamelets of --kamelets-version win
over the camel-kamelets jar of the launcher (#30682)
The launcher bundles a camel-kamelets jar (4.22.0 via CamelJBang //DEPS,
4.23.0 in camel-launcher). camel run --kamelets-version=X downloads
camel-kamelets X into DependencyDownloaderClassLoader, which is parent first,
so a Kamelet present in both jars was loaded from the launcher's version.
DependencyDownloaderClassLoader.getResource now looks up kamelets/
resources in the downloaded jars first; all other resources stay parent first.
Without --kamelets-version nothing changes.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../download/DependencyDownloaderClassLoader.java | 21 ++++
.../DependencyDownloaderClassLoaderTest.java | 115 +++++++++++++++++++++
2 files changed, 136 insertions(+)
diff --git
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderClassLoader.java
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderClassLoader.java
index 119248c2b5cf..ce9631f62d43 100644
---
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderClassLoader.java
+++
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderClassLoader.java
@@ -30,6 +30,11 @@ public class DependencyDownloaderClassLoader extends
URLClassLoader {
private static final URL[] EMPTY_URL_ARRAY = new URL[0];
+ /**
+ * The folder of the Kamelets in the camel-kamelets jar.
+ */
+ private static final String KAMELETS_DIR = "kamelets/";
+
public DependencyDownloaderClassLoader(ClassLoader parent) {
super(EMPTY_URL_ARRAY, parent);
}
@@ -42,6 +47,22 @@ public class DependencyDownloaderClassLoader extends
URLClassLoader {
}
}
+ /**
+ * A Kamelet is looked up in the downloaded jars first: the launcher may
bundle a camel-kamelets jar of another
+ * version, and camel-kamelets is only downloaded when the version asked
for (--kamelets-version) is not that one
+ * (CAMEL-27476). Any other resource is looked up in the parent first, as
usual.
+ */
+ @Override
+ public URL getResource(String name) {
+ if (name != null && name.startsWith(KAMELETS_DIR)) {
+ URL url = findResource(name);
+ if (url != null) {
+ return url;
+ }
+ }
+ return super.getResource(name);
+ }
+
public List<String> getDownloaded() {
return Arrays.stream(getURLs()).map(u ->
FileUtil.stripPath(u.getFile())).collect(Collectors.toList());
}
diff --git
a/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/download/DependencyDownloaderClassLoaderTest.java
b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/download/DependencyDownloaderClassLoaderTest.java
new file mode 100644
index 000000000000..0376e5684008
--- /dev/null
+++
b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/download/DependencyDownloaderClassLoaderTest.java
@@ -0,0 +1,115 @@
+/*
+ * 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.download;
+
+import java.io.InputStream;
+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.Map;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.spi.Resource;
+import org.apache.camel.support.PluginHelper;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * CAMEL-27476: the launcher bundles a camel-kamelets jar; the one downloaded
for --kamelets-version must win.
+ */
+class DependencyDownloaderClassLoaderTest {
+
+ @TempDir
+ Path dir;
+
+ @Test
+ void kameletFromDownloadedJarWins() throws Exception {
+ try (URLClassLoader launcher = launcher();
+ DependencyDownloaderClassLoader cl = new
DependencyDownloaderClassLoader(launcher)) {
+ cl.addFile(jar("camel-kamelets-4.24.0.jar", Map.of(
+ "kamelets/postgresql-sink.kamelet.yaml", "4.24",
+ "other.properties", "4.24")).toFile());
+
+ assertEquals("4.24",
read(cl.getResource("kamelets/postgresql-sink.kamelet.yaml")));
+ // a Kamelet only the launcher has is still found
+ assertEquals("4.22",
read(cl.getResource("kamelets/old-sink.kamelet.yaml")));
+ // any other resource is parent first, as before
+ assertEquals("4.22", read(cl.getResource("other.properties")));
+ }
+ }
+
+ @Test
+ void kameletResourceOfCamelContext() throws Exception {
+ try (URLClassLoader launcher = launcher();
+ DependencyDownloaderClassLoader cl = new
DependencyDownloaderClassLoader(launcher);
+ CamelContext context = new DefaultCamelContext()) {
+ cl.addFile(jar("camel-kamelets-4.24.0.jar", Map.of(
+ "kamelets/postgresql-sink.kamelet.yaml",
"4.24")).toFile());
+ context.setApplicationContextClassLoader(cl);
+
+ Resource resource = PluginHelper.getResourceLoader(context)
+
.resolveResource("classpath:kamelets/postgresql-sink.kamelet.yaml");
+ try (InputStream is = resource.getInputStream()) {
+ assertEquals("4.24", new String(is.readAllBytes(),
StandardCharsets.UTF_8));
+ }
+ }
+ }
+
+ @Test
+ void launcherKameletWhenNothingDownloaded() throws Exception {
+ try (URLClassLoader launcher = launcher();
+ DependencyDownloaderClassLoader cl = new
DependencyDownloaderClassLoader(launcher)) {
+ assertEquals("4.22",
read(cl.getResource("kamelets/postgresql-sink.kamelet.yaml")));
+ }
+ }
+
+ private URLClassLoader launcher() throws Exception {
+ Path jar = jar("camel-kamelets-4.22.0.jar", Map.of(
+ "kamelets/postgresql-sink.kamelet.yaml", "4.22",
+ "kamelets/old-sink.kamelet.yaml", "4.22",
+ "other.properties", "4.22"));
+ // no parent: only the launcher jar, so the test classpath does not
interfere
+ return new URLClassLoader(new URL[] { jar.toUri().toURL() }, null);
+ }
+
+ private Path jar(String name, Map<String, String> entries) throws
Exception {
+ Path jar = dir.resolve(name);
+ try (OutputStream os = Files.newOutputStream(jar);
+ JarOutputStream jos = new JarOutputStream(os)) {
+ for (Map.Entry<String, String> e : entries.entrySet()) {
+ jos.putNextEntry(new JarEntry(e.getKey()));
+ jos.write(e.getValue().getBytes(StandardCharsets.UTF_8));
+ jos.closeEntry();
+ }
+ }
+ return jar;
+ }
+
+ private static String read(URL url) throws Exception {
+ try (InputStream is = url.openStream()) {
+ return new String(is.readAllBytes(), StandardCharsets.UTF_8);
+ }
+ }
+}