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 05b8d041a2b8 CAMEL-26964: camel-jbang - camel run and camel export go
on without a stored plugin built for another Camel version (#29990)
05b8d041a2b8 is described below
commit 05b8d041a2b8eea76074a2348d12370b50fb2222
Author: Federico Mariani <[email protected]>
AuthorDate: Sun Nov 15 16:34:47 2026 +0100
CAMEL-26964: camel-jbang - camel run and camel export go on without a
stored plugin built for another Camel version (#29990)
CAMEL-26931 left out a stored plugin that cannot be loaded with a warning,
but caught RuntimeException only. A plugin
built for another Camel version fails with a LinkageError instead, such as
a NoClassDefFoundError for a class that
moved, and it failed camel run and camel export.
PluginHelper.getActivePlugins now also catches LinkageError: a
contributing plugin is left out with the reason (it does not link with this
Camel version), and the plugin's own
command fails with that reason as a CliException. camel plugin add reports
such a release as not loadable instead of
failing.
CAMEL-26964: camel-jbang - a plugin whose exporter or run customizer does
not link is left out too, and the message says how to fix it
The JVM links the classes of a plugin when they are first used, so a plugin
built for another Camel version may be
created and still fail when camel run or camel export asks it for its run
customizer or exporter. CliExportContext now
asks each plugin in a try, and leaves a plugin that fails with a
LinkageError out with the once-only warning.
The error and the warning for a plugin that does not link say to run camel
plugin add <name> to select a release for
this Camel version, or camel plugin delete <name>, as there is no camel
plugin update. The reason says the plugin may be
built for another version, and shows the cause of an error without a
message, such as ExceptionInInitializerError.
PluginHelperTest imports the classes it used fully qualified.
Co-Authored-By: Claude <[email protected]>
---
.../modules/ROOT/pages/camel-jbang-projects.adoc | 5 +-
.../dsl/jbang/core/commands/CliExportContext.java | 37 +++++--
.../dsl/jbang/core/commands/plugin/PluginAdd.java | 6 +-
.../camel/dsl/jbang/core/common/PluginHelper.java | 79 +++++++++++++-
.../jbang/core/commands/plugin/PluginAddTest.java | 23 +++++
.../dsl/jbang/core/common/PluginHelperTest.java | 114 ++++++++++++++++++---
.../common/UnlinkedContributionFakePlugins.java | 106 +++++++++++++++++++
.../camel-jbang-plugin/camel-jbang-plugin-unlinked | 19 ++++
8 files changed, 365 insertions(+), 24 deletions(-)
diff --git a/docs/user-manual/modules/ROOT/pages/camel-jbang-projects.adoc
b/docs/user-manual/modules/ROOT/pages/camel-jbang-projects.adoc
index 2505529eee5c..b4726c459479 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-jbang-projects.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-jbang-projects.adoc
@@ -282,7 +282,10 @@ camel plugin delete <plugin-name>
`camel plugin add` checks that a plugin it does not know (neither a plugin of
the Apache Camel project nor a known
community plugin) can be loaded before it stores it: a name such as
`kubernets` exits with code `3` and the plugins of a
similar name. Give `--gav` for a plugin of your own. A stored plugin that
cannot be loaded no longer fails `camel run`
-and `camel export`: they warn and go on without it.
+and `camel export`: they warn and go on without it. This includes a plugin
built for another Camel version, whose
+classes (or only those of its exporter or run customizer) do not link with
this Camel CLI: the warning says so, and the
+plugin's own command fails with the same reason. Run `camel plugin add
<plugin-name>` to select a release built for
+this Camel version, or remove the plugin with `camel plugin delete
<plugin-name>`.
=== Custom plugin configuration
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CliExportContext.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CliExportContext.java
index ba46041bd8d7..fb6735f12664 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CliExportContext.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CliExportContext.java
@@ -16,8 +16,8 @@
*/
package org.apache.camel.dsl.jbang.core.commands;
-import java.util.Collection;
-import java.util.LinkedHashMap;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -25,6 +25,8 @@ import
org.apache.camel.dsl.jbang.core.commands.ai.YamlLoadFailureReport;
import org.apache.camel.dsl.jbang.core.common.Plugin;
import org.apache.camel.dsl.jbang.core.common.PluginHelper;
import org.apache.camel.dsl.jbang.run.RunContext;
+import org.apache.camel.dsl.jbang.run.common.PluginExporter;
+import org.apache.camel.dsl.jbang.run.common.PluginRunCustomizer;
import org.apache.camel.dsl.jbang.run.common.Printer;
import org.apache.camel.dsl.jbang.run.export.ExportContext;
@@ -61,17 +63,34 @@ public final class CliExportContext {
T context, CamelJBangMain main, Printer printer, boolean yes,
String target) {
context.withPrinter(printer)
.withPlugins(repos -> {
- Map<String, Plugin> active = new
LinkedHashMap<>(PluginHelper.getActivePlugins(main, repos, target, true));
- // and the plugins built into the CLI, unless the plugin
configuration has the same plugin
+ List<PluginRunCustomizer> runCustomizers = new
ArrayList<>();
+ List<PluginExporter> exporters = new ArrayList<>();
+ Map<String, Plugin> active =
PluginHelper.getActivePlugins(main, repos, target, true);
+ active.forEach((command, plugin) -> {
+ Optional<PluginRunCustomizer> runCustomizer;
+ Optional<PluginExporter> exporter;
+ try {
+ runCustomizer = plugin.getRunCustomizer();
+ exporter = plugin.getExporter();
+ } catch (LinkageError e) {
+ // the JVM links the classes of a plugin when they
are first used: one built for another
+ // Camel version may be created, and fail here as
its run customizer or exporter refers to
+ // a class that has moved. It is left out as one
that cannot be loaded (CAMEL-26964)
+ PluginHelper.warnNotLinked(main.getOut(), command,
target, e);
+ return;
+ }
+ runCustomizer.ifPresent(runCustomizers::add);
+ exporter.ifPresent(exporters::add);
+ });
+ // and the plugins built into the CLI (they link with it),
unless the plugin configuration has the
+ // same plugin
for (Plugin embedded : main.getEmbeddedPlugins(target)) {
if (active.values().stream().noneMatch(p ->
p.getClass().equals(embedded.getClass()))) {
- active.put(embedded.getClass().getName(),
embedded);
+
embedded.getRunCustomizer().ifPresent(runCustomizers::add);
+ embedded.getExporter().ifPresent(exporters::add);
}
}
- Collection<Plugin> plugins = active.values();
- return new ExportContext.ActivePlugins(
-
plugins.stream().map(Plugin::getRunCustomizer).flatMap(Optional::stream).toList(),
-
plugins.stream().map(Plugin::getExporter).flatMap(Optional::stream).toList());
+ return new
ExportContext.ActivePlugins(List.copyOf(runCustomizers),
List.copyOf(exporters));
})
.withStartFailureListener(YamlLoadFailureReport::logIfYamlLoadFailure)
.withConfirm(message ->
CommandHelper.confirmOperation(message, yes));
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/plugin/PluginAdd.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/plugin/PluginAdd.java
index 9e7a78f8902d..038996976616 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/plugin/PluginAdd.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/plugin/PluginAdd.java
@@ -203,7 +203,7 @@ public class PluginAdd extends PluginBaseCommand {
try {
// quietly: the answer says why it is not added
return downloadPlugin(null, new StringPrinter()).isPresent();
- } catch (RuntimeException e) {
+ } catch (RuntimeException | LinkageError e) {
return false;
}
}
@@ -300,6 +300,10 @@ public class PluginAdd extends PluginBaseCommand {
} catch (Exception e) {
printer().printErr("Cannot download " + releaseGav, e);
return false;
+ } catch (LinkageError e) {
+ // built for another Camel version (CAMEL-26964)
+ printer().printErr("Cannot load " + releaseGav + ": " +
PluginHelper.notLinked(cliVersion(), e));
+ return false;
}
if (resolved.isEmpty()) {
printer().printErr("Cannot download " + releaseGav);
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/PluginHelper.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/PluginHelper.java
index 08316cf2e175..e505452fa302 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/PluginHelper.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/PluginHelper.java
@@ -435,6 +435,16 @@ public final class PluginHelper {
}
warnNotLoaded(main.getOut(), name, target, e.getMessage());
continue;
+ } catch (LinkageError e) {
+ // a plugin built for another Camel version, whose classes
refer to a class that has moved or is
+ // gone: a NoClassDefFoundError when it is created
(CAMEL-26964)
+ String reason = notLinked(version, e);
+ if (!contributor) {
+ throw CliException.failure("Plugin " + name + " cannot
be loaded (" + reason + "): "
+ + addOrDelete(name));
+ }
+ warnNotLoaded(main.getOut(), name, target, reason,
addOrDelete(name));
+ continue;
}
if (res.plugin().isPresent()) {
activePlugins.put(command, res.plugin().get());
@@ -459,10 +469,77 @@ public final class PluginHelper {
// than once
private static final Map<Printer, Set<String>> WARNED_NOT_LOADED =
Collections.synchronizedMap(new WeakHashMap<>());
+ /**
+ * Why a plugin that fails with a linkage error cannot be loaded, such as
+ * {@code it does not link with Camel 4.24.0 and may be built for another
version: NoClassDefFoundError org/...}.
+ * The error may also come from elsewhere, such as a newer Java ({@link
UnsupportedClassVersionError}) or a static
+ * initializer of the plugin that fails ({@link
ExceptionInInitializerError}, without a message of its own: its
+ * cause says what failed).
+ */
+ public static String notLinked(String camelVersion, LinkageError e) {
+ String error = e.getClass().getSimpleName();
+ if (e.getMessage() != null) {
+ error += " " + e.getMessage();
+ } else if (e.getCause() != null) {
+ Throwable cause = e.getCause();
+ error += " caused by " + cause.getClass().getSimpleName()
+ + (cause.getMessage() != null ? " " + cause.getMessage()
: "");
+ }
+ return "it does not link with Camel " + camelVersion + " and may be
built for another version: " + error;
+ }
+
+ /**
+ * Warns, once, that a plugin is left out of camel run or camel export as
its run customizer or exporter does not
+ * link with this Camel version: the JVM links the classes of a plugin
when they are first used, so a plugin that
+ * could be created may still fail when asked for them (CAMEL-26964).
+ *
+ * @param printer where to warn
+ * @param command the command of the plugin, as the keys of {@link
#getActivePlugins(CamelJBangMain, String)}
+ * @param target the command that goes on without the plugin, such as run
or export
+ * @param e the error
+ */
+ public static void warnNotLinked(Printer printer, String command, String
target, LinkageError e) {
+ String name = nameOf(command);
+ warnNotLoaded(printer, name, target, notLinked(new
DefaultCamelCatalog().getCatalogVersion(), e),
+ addOrDelete(name));
+ }
+
+ // the name of the plugin of the given command in the plugin
configuration, which camel plugin add and camel plugin
+ // delete take
+ private static String nameOf(String command) {
+ try {
+ JsonObject config = getPluginConfig();
+ JsonObject plugins = config != null ? config.getMap("plugins") :
null;
+ if (plugins != null) {
+ for (String pluginKey : plugins.keySet()) {
+ JsonObject properties = plugins.getMap(pluginKey);
+ String name = properties.getOrDefault("name",
pluginKey).toString();
+ if (command.equals(properties.getOrDefault("command",
name).toString())) {
+ return name;
+ }
+ }
+ }
+ } catch (RuntimeException e) {
+ // corrupt plugin config: warned about by getActivePlugins
+ }
+ return command;
+ }
+
+ // what to do about a plugin built for another Camel version: camel plugin
add selects the release built for this
+ // one, as compatibilityCheck says
+ private static String addOrDelete(String name) {
+ return "run camel plugin add " + name + " to select a release for this
Camel version, or remove it with"
+ + " camel plugin delete " + name;
+ }
+
private static void warnNotLoaded(Printer printer, String name, String
target, String reason) {
+ warnNotLoaded(printer, name, target, reason, "remove it with camel
plugin delete " + name);
+ }
+
+ private static void warnNotLoaded(Printer printer, String name, String
target, String reason, String remedy) {
if (WARNED_NOT_LOADED.computeIfAbsent(printer, p ->
ConcurrentHashMap.newKeySet()).add(name)) {
printer.printWarn("Plugin " + name + " cannot be loaded (" +
reason + "), so camel " + target
- + " goes on without it: remove it with camel
plugin delete " + name);
+ + " goes on without it: " + remedy);
}
}
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/plugin/PluginAddTest.java
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/plugin/PluginAddTest.java
index feeff1e03148..57b4be1c6e87 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/plugin/PluginAddTest.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/plugin/PluginAddTest.java
@@ -214,6 +214,24 @@ class PluginAddTest extends CamelCommandBaseTestSupport {
Assertions.assertEquals(before,
PluginHelper.getOrCreatePluginConfig().toJson());
}
+ @Test
+ public void shouldNotChangeConfigWhenSelectedReleaseDoesNotLink() throws
Exception {
+ downloader.release("1.6.2", "4.22.2", null);
+ String before = PluginHelper.getOrCreatePluginConfig().toJson();
+
+ TestPluginAdd command = newFixtureCommand();
+ command.doesNotLink = true;
+ // before, the NoClassDefFoundError failed camel plugin add
(CAMEL-26964)
+ Assertions.assertEquals(1, command.doCall());
+
+ Assertions.assertTrue(
+ printer.getStderr().contains("Cannot load
org.example.plugins:camel-jbang-plugin-fixture:1.6.2: it does not"
+ + " link with Camel 4.22.0 and
may be built for another version:"
+ + " NoClassDefFoundError
org/apache/camel/dsl/jbang/core/common/Printer"),
+ printer.getStderr());
+ Assertions.assertEquals(before,
PluginHelper.getOrCreatePluginConfig().toJson());
+ }
+
@Test
public void shouldUseLatestWhenNoReleaseMatches() throws Exception {
downloader.release("2.0.0", "4.23.0", "[4.23.0,4.24.0)")
@@ -375,6 +393,7 @@ class PluginAddTest extends CamelCommandBaseTestSupport {
private final List<String> downloaded = new ArrayList<>();
private String cli = "4.22.0";
private boolean downloadFails;
+ private boolean doesNotLink;
TestPluginAdd(CamelJBangMain main, DependencyDownloader downloader) {
super(main);
@@ -394,6 +413,10 @@ class PluginAddTest extends CamelCommandBaseTestSupport {
@Override
Optional<Plugin> downloadPlugin(String dependency, Printer printer) {
downloaded.add(dependency);
+ if (doesNotLink) {
+ // a release built for another Camel version, whose classes
refer to one that moved
+ throw new
NoClassDefFoundError("org/apache/camel/dsl/jbang/core/common/Printer");
+ }
return downloadFails ? Optional.empty() : Optional.of(new
CachedFakePlugin());
}
}
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/common/PluginHelperTest.java
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/common/PluginHelperTest.java
index 6660c74c0c90..1bdfc09aa8c9 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/common/PluginHelperTest.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/common/PluginHelperTest.java
@@ -18,17 +18,24 @@ package org.apache.camel.dsl.jbang.core.common;
import java.net.URI;
import java.net.URL;
+import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
import java.util.Map;
import org.aesh.command.Command;
import org.aesh.command.CommandDefinition;
import org.aesh.command.CommandResult;
import org.aesh.command.invocation.CommandInvocation;
+import org.apache.camel.catalog.DefaultCamelCatalog;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
+import org.apache.camel.dsl.jbang.core.commands.CliExportContext;
import org.apache.camel.dsl.jbang.run.common.StringPrinter;
+import org.apache.camel.dsl.jbang.run.export.ExportContext;
import org.apache.camel.util.json.JsonObject;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
@@ -213,7 +220,7 @@ public class PluginHelperTest {
Path jar = tempDir.resolve("fake-plugin.jar");
FakePluginJar.write(jar, "fake");
- String camelVersion = new
org.apache.camel.catalog.DefaultCamelCatalog().getCatalogVersion();
+ String camelVersion = new DefaultCamelCatalog().getCatalogVersion();
writeConfig(buildEntry("fake", camelVersion, jar,
Files.getLastModifiedTime(jar).toMillis()));
CamelJBangMain main = new CamelJBangMain();
@@ -229,7 +236,7 @@ public class PluginHelperTest {
Path jar = tempDir.resolve("fake-plugin.jar");
FakePluginJar.write(jar, "fake");
- String camelVersion = new
org.apache.camel.catalog.DefaultCamelCatalog().getCatalogVersion();
+ String camelVersion = new DefaultCamelCatalog().getCatalogVersion();
long staleMtime = Files.getLastModifiedTime(jar).toMillis() - 1000;
writeConfig(buildEntry("fake", camelVersion, jar, staleMtime));
@@ -246,7 +253,7 @@ public class PluginHelperTest {
public void aPluginThatCannotBeLoadedDoesNotFailCamelRun() throws
Exception {
Path jar = tempDir.resolve("fake-plugin.jar");
FakePluginJar.write(jar, "fake");
- String camelVersion = new
org.apache.camel.catalog.DefaultCamelCatalog().getCatalogVersion();
+ String camelVersion = new DefaultCamelCatalog().getCatalogVersion();
long staleMtime = Files.getLastModifiedTime(jar).toMillis() - 1000;
// without appliesTo the plugin contributes to camel run and camel
export, and it cannot be resolved
writeConfig(buildEntry("fake", camelVersion, jar, staleMtime));
@@ -265,12 +272,84 @@ public class PluginHelperTest {
assertThrows(CliException.class, () ->
PluginHelper.getActivePlugins(main, null, "fake"));
}
+ @Test
+ public void aPluginThatDoesNotLinkDoesNotFailCamelRun() throws Exception {
+ // a plugin built for another Camel version, whose classes refer to
one that moved: creating it fails with a
+ // NoClassDefFoundError, which is not a RuntimeException. It is found
on the classpath of the tests
+ //
(META-INF/services/org/apache/camel/camel-jbang-plugin/camel-jbang-plugin-unlinked).
+ String camelVersion = new DefaultCamelCatalog().getCatalogVersion();
+ writeConfig(buildEntryWithoutResolvedBlock("unlinked", camelVersion));
+ StringPrinter printer = new StringPrinter();
+ QuitCapture main = new QuitCapture();
+ main.withPrinter(printer);
+
+ // before, the error failed camel run (CAMEL-26964): it is left out
with a warning
+ Map<String, Plugin> plugins = assertDoesNotThrow(() ->
PluginHelper.getActivePlugins(main, null, "run", true));
+
+ assertTrue(plugins.isEmpty());
+ String reason = "it does not link with Camel " + camelVersion + " and
may be built for another version: "
+ + "NoClassDefFoundError ";
+ String remedy = "run camel plugin add unlinked to select a release for
this Camel version, or remove it with"
+ + " camel plugin delete unlinked";
+ assertTrue(printer.getStderr().contains("Plugin unlinked cannot be
loaded (" + reason), printer.getStderr());
+ assertTrue(printer.getStderr().contains("so camel run goes on without
it: " + remedy), printer.getStderr());
+
+ // its own command fails with the reason, rather than with the error
+ CliException e = assertThrows(CliException.class, () ->
PluginHelper.getActivePlugins(main, null, "unlinked"));
+ assertTrue(e.getMessage().startsWith("Plugin unlinked cannot be loaded
(" + reason), e.getMessage());
+ assertTrue(e.getMessage().endsWith("): " + remedy), e.getMessage());
+ }
+
+ @Test
+ public void
aPluginWhoseExporterOrRunCustomizerDoesNotLinkDoesNotFailCamelExport() throws
Exception {
+ // plugins built for another Camel version that link, while their
exporter or run customizer refers to a class
+ // that moved: the JVM links that class when it is first used, as
camel run and camel export ask for it
+ String camelVersion = new DefaultCamelCatalog().getCatalogVersion();
+ JsonObject exporter = cachedEntry("exporter",
UnlinkedContributionFakePlugins.UnlinkedExporterPlugin.class,
+ camelVersion);
+ JsonObject customizer = cachedEntry("customizer",
UnlinkedContributionFakePlugins.UnlinkedRunCustomizerPlugin.class,
+ camelVersion);
+ // stored with a command of its own: the warning names the plugin,
which camel plugin delete takes
+ customizer.put("command", "customize");
+ writeConfig(exporter, customizer);
+ StringPrinter printer = new StringPrinter();
+ CamelJBangMain main = new CamelJBangMain().withPrinter(printer);
+
+ // before, the NoClassDefFoundError failed camel export (CAMEL-26964):
both are left out with a warning
+ ExportContext.ActivePlugins plugins
+ = assertDoesNotThrow(() -> CliExportContext.create(main,
printer, true).getPlugins(null));
+
+ // the run customizer of the first one links, but the plugin is left
out as a whole
+ assertTrue(plugins.runCustomizers().isEmpty(),
plugins.runCustomizers().toString());
+ assertTrue(plugins.exporters().isEmpty(),
plugins.exporters().toString());
+ String reason = "it does not link with Camel " + camelVersion + " and
may be built for another version: "
+ + "NoClassDefFoundError ";
+ for (String name : List.of("exporter", "customizer")) {
+ assertTrue(printer.getStderr().contains("Plugin " + name + "
cannot be loaded (" + reason), printer.getStderr());
+ assertTrue(printer.getStderr().contains("so camel export goes on
without it: run camel plugin add " + name
+ + " to select a release
for this Camel version, or remove it with"
+ + " camel plugin delete "
+ name),
+ printer.getStderr());
+ }
+ }
+
+ @Test
+ public void aStaticInitializerThatFailsIsReportedWithItsCause() {
+ // an ExceptionInInitializerError is a LinkageError without a message
of its own: what failed is its cause,
+ // often a bug in the plugin rather than another Camel version
+ String reason = PluginHelper.notLinked("4.24.0", new
ExceptionInInitializerError(new IllegalStateException("boom")));
+
+ assertEquals("it does not link with Camel 4.24.0 and may be built for
another version: ExceptionInInitializerError"
+ + " caused by IllegalStateException boom",
+ reason);
+ }
+
@Test
public void testCachedPluginThatDoesNotLinkIsResolvedAgain() throws
Exception {
Path jar = tempDir.resolve("unlinked-plugin.jar");
FakePluginJar.write(jar, "fake", UnlinkedFakePlugin.class.getName());
- String camelVersion = new
org.apache.camel.catalog.DefaultCamelCatalog().getCatalogVersion();
+ String camelVersion = new DefaultCamelCatalog().getCatalogVersion();
JsonObject entry = buildEntry("fake", camelVersion, jar,
Files.getLastModifiedTime(jar).toMillis());
((JsonObject) entry.get("resolved")).put("className",
UnlinkedFakePlugin.class.getName());
writeConfig(entry);
@@ -306,7 +385,7 @@ public class PluginHelperTest {
entry.put("name", "fake");
entry.put("command", "fake");
- try (java.net.URLClassLoader cl = new java.net.URLClassLoader(new
java.net.URL[] { jar.toUri().toURL() })) {
+ try (URLClassLoader cl = new URLClassLoader(new URL[] {
jar.toUri().toURL() })) {
boolean written = PluginHelper.writeCache(entry, "9.9.9", null,
null, FakePluginJar.PLUGIN_CLASS, cl, "fake",
"9.9.9");
assertTrue(written);
@@ -319,8 +398,8 @@ public class PluginHelperTest {
assertNotNull(resolved.get("cachedAt"));
Object cp = resolved.get("classpath");
- assertTrue(cp instanceof java.util.Collection);
- java.util.Collection<?> classpath = (java.util.Collection<?>) cp;
+ assertTrue(cp instanceof Collection);
+ Collection<?> classpath = (Collection<?>) cp;
assertEquals(1, classpath.size());
Map<?, ?> jarEntry = (Map<?, ?>) classpath.iterator().next();
assertEquals(jar.toAbsolutePath().toString(), jarEntry.get("path"));
@@ -340,7 +419,7 @@ public class PluginHelperTest {
// both halves — only the presence of the `resolved` block in the
config differs.
Path jar = tempDir.resolve("fake-plugin.jar");
FakePluginJar.write(jar, "fake");
- String camelVersion = new
org.apache.camel.catalog.DefaultCamelCatalog().getCatalogVersion();
+ String camelVersion = new DefaultCamelCatalog().getCatalogVersion();
// BEFORE CAMEL-23335: entry has no `resolved` block. resolvePlugin
falls through loadFromCache,
// misses FACTORY_FINDER (no service registered for "fake"), reaches
downloadPlugin with no usable
@@ -388,7 +467,7 @@ public class PluginHelperTest {
// a plugin release selected for an older CLI; loading it uses the
cache only, so there is no download
Path jar = tempDir.resolve("fake-plugin.jar");
FakePluginJar.write(jar, "compat");
- String camelVersion = new
org.apache.camel.catalog.DefaultCamelCatalog().getCatalogVersion();
+ String camelVersion = new DefaultCamelCatalog().getCatalogVersion();
JsonObject entry = buildEntry("compat", camelVersion, jar,
Files.getLastModifiedTime(jar).toMillis());
entry.put("camelVersions", "[4.0.0,4.1.0)");
entry.put("camelVersionsSource", "inferred");
@@ -609,9 +688,11 @@ public class PluginHelperTest {
assertEquals(dirPath.resolve(PluginHelper.PLUGIN_CONFIG), result);
}
- private void writeConfig(JsonObject pluginEntry) throws Exception {
+ private void writeConfig(JsonObject... pluginEntries) throws Exception {
JsonObject plugins = new JsonObject();
- plugins.put(pluginEntry.getString("name"), pluginEntry);
+ for (JsonObject pluginEntry : pluginEntries) {
+ plugins.put(pluginEntry.getString("name"), pluginEntry);
+ }
JsonObject config = new JsonObject();
config.put("plugins", plugins);
Path userConfig =
CommandLineHelper.getHomeDir().resolve(PluginHelper.PLUGIN_CONFIG);
@@ -627,6 +708,15 @@ public class PluginHelperTest {
return entry;
}
+ // a plugin entry whose cache points to a jar with the given plugin class
+ private JsonObject cachedEntry(String name, Class<? extends Plugin>
pluginClass, String camelVersion) throws Exception {
+ Path jar = tempDir.resolve(name + "-plugin.jar");
+ FakePluginJar.write(jar, name, pluginClass.getName());
+ JsonObject entry = buildEntry(name, camelVersion, jar,
Files.getLastModifiedTime(jar).toMillis());
+ ((JsonObject) entry.get("resolved")).put("className",
pluginClass.getName());
+ return entry;
+ }
+
private static JsonObject buildEntry(String name, String camelVersion,
Path jar, long jarMtime) throws Exception {
JsonObject entry = new JsonObject();
entry.put("name", name);
@@ -642,7 +732,7 @@ public class PluginHelperTest {
JsonObject resolved = new JsonObject();
resolved.put("camelVersion", camelVersion);
resolved.put("className", FakePluginJar.PLUGIN_CLASS);
- java.util.List<JsonObject> cp = new java.util.ArrayList<>();
+ List<JsonObject> cp = new ArrayList<>();
cp.add(jarEntry);
resolved.put("classpath", cp);
entry.put("resolved", resolved);
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/common/UnlinkedContributionFakePlugins.java
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/common/UnlinkedContributionFakePlugins.java
new file mode 100644
index 000000000000..654b1ef24de5
--- /dev/null
+++
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/common/UnlinkedContributionFakePlugins.java
@@ -0,0 +1,106 @@
+/*
+ * 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.dsl.jbang.core.common;
+
+import java.nio.file.Path;
+import java.util.List;
+import java.util.Optional;
+
+import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
+import org.apache.camel.dsl.jbang.run.common.PluginExporter;
+import org.apache.camel.dsl.jbang.run.common.PluginRunCustomizer;
+import org.apache.camel.dsl.jbang.run.common.Printer;
+import org.apache.camel.main.KameletMain;
+
+/**
+ * Plugins that link with the CLI that loads them, while their exporter or run
customizer does not, such as plugins
+ * built for another Camel version whose exporter refers to a class that has
moved: the JVM links a class when it is
+ * first used, so creating the plugin works, and asking it for its exporter or
run customizer fails.
+ */
+public final class UnlinkedContributionFakePlugins {
+
+ private UnlinkedContributionFakePlugins() {
+ }
+
+ /**
+ * Its run customizer links, its exporter does not.
+ */
+ public static class UnlinkedExporterPlugin implements Plugin {
+
+ @Override
+ public void customize(CommandTree commandLine, CamelJBangMain main) {
+ // no-op
+ }
+
+ @Override
+ public Optional<PluginRunCustomizer> getRunCustomizer() {
+ return Optional.of((main, files) -> {
+ });
+ }
+
+ @Override
+ public Optional<PluginExporter> getExporter() {
+ return Optional.of(new UnlinkedExporter());
+ }
+ }
+
+ /**
+ * Its run customizer does not link.
+ */
+ public static class UnlinkedRunCustomizerPlugin implements Plugin {
+
+ @Override
+ public void customize(CommandTree commandLine, CamelJBangMain main) {
+ // no-op
+ }
+
+ @Override
+ public Optional<PluginRunCustomizer> getRunCustomizer() {
+ return Optional.of(new UnlinkedRunCustomizer());
+ }
+ }
+
+ static class UnlinkedExporter implements PluginExporter {
+
+ static {
+ // as the JVM fails when the class links: the error, not wrapped
+ if (Boolean.TRUE) {
+ throw new
NoClassDefFoundError("org/apache/camel/dsl/jbang/core/common/Printer");
+ }
+ }
+
+ @Override
+ public void addSourceFiles(Path buildDir, String packageName, Printer
printer) {
+ // no-op
+ }
+ }
+
+ static class UnlinkedRunCustomizer implements PluginRunCustomizer {
+
+ static {
+ // as the JVM fails when the class links: the error, not wrapped
+ if (Boolean.TRUE) {
+ throw new
NoClassDefFoundError("org/apache/camel/dsl/jbang/core/common/Printer");
+ }
+ }
+
+ @Override
+ public void beforeRun(KameletMain main, List<String> files) {
+ // no-op
+ }
+ }
+}
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/test/resources/META-INF/services/org/apache/camel/camel-jbang-plugin/camel-jbang-plugin-unlinked
b/dsl/camel-jbang/camel-jbang-core/src/test/resources/META-INF/services/org/apache/camel/camel-jbang-plugin/camel-jbang-plugin-unlinked
new file mode 100644
index 000000000000..5daf28da57a4
--- /dev/null
+++
b/dsl/camel-jbang/camel-jbang-core/src/test/resources/META-INF/services/org/apache/camel/camel-jbang-plugin/camel-jbang-plugin-unlinked
@@ -0,0 +1,19 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+# a plugin on the classpath that does not link with this CLI (PluginHelperTest)
+class=org.apache.camel.dsl.jbang.core.common.UnlinkedFakePlugin