This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27476-kamelets-version in repository https://gitbox.apache.org/repos/asf/camel.git
commit aa2a4007d5d445b4822ec0ce3fb92c78152a8de7 Author: Claus Ibsen <[email protected]> AuthorDate: Mon Nov 23 19:56:43 2026 +0100 CAMEL-27476: camel-kamelet-main - the Kamelets of --kamelets-version win over the camel-kamelets jar of the launcher The launcher bundles camel-kamelets (CamelJBang.java //DEPS, camel-launcher), and the jar of --kamelets-version is only added to the class loader of the downloads, which asks its parent first. So a Kamelet in both jars came from the launcher's version. A kamelets/ resource is now looked up in the downloaded jars first. A background run passes --kamelets-version to its JVM, so it gets the same fix. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[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); + } + } +}
