This is an automated email from the ASF dual-hosted git repository.
bamaer pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/hop.git
The following commit(s) were added to refs/heads/main by this push:
new 2338a7af61 One configuration plugin that fails to load no longer stops
the Hop commands, fixes #9542 (#9543)
2338a7af61 is described below
commit 2338a7af6134998d8bc3720766c50c561ac841b4
Author: Hans Van Akelyen <[email protected]>
AuthorDate: Tue Nov 17 16:19:49 2026 +0100
One configuration plugin that fails to load no longer stops the Hop
commands, fixes #9542 (#9543)
---
engine/src/main/java/org/apache/hop/hop/Hop.java | 40 ++++-
.../org/apache/hop/hop/HopAddMixinPluginsTest.java | 168 +++++++++++++++++++++
2 files changed, 205 insertions(+), 3 deletions(-)
diff --git a/engine/src/main/java/org/apache/hop/hop/Hop.java
b/engine/src/main/java/org/apache/hop/hop/Hop.java
index 0204a148d6..0d54050767 100644
--- a/engine/src/main/java/org/apache/hop/hop/Hop.java
+++ b/engine/src/main/java/org/apache/hop/hop/Hop.java
@@ -18,6 +18,7 @@
package org.apache.hop.hop;
import java.util.List;
+import java.util.Map;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
@@ -44,6 +45,8 @@ import org.apache.hop.metadata.util.HopMetadataUtil;
import org.apache.hop.metadata.util.SharedMetadata;
import picocli.CommandLine;
import picocli.CommandLine.Command;
+import picocli.CommandLine.Model.CommandSpec;
+import picocli.CommandLine.Model.OptionSpec;
@Getter
@Setter
@@ -255,10 +258,41 @@ public class Hop {
for (IPlugin configPlugin : configPlugins) {
// Load only the plugins of the "run" category
if (category.equals(configPlugin.getCategory())) {
- IConfigOptions configOptions =
- PluginRegistry.getInstance().loadClass(configPlugin,
IConfigOptions.class);
- cmd.addMixin(configPlugin.getIds()[0], configOptions);
+ // One plugin that can't be loaded or mixed in must not take the whole
command line down.
+ // Skip it: its options are unavailable, every other plugin keeps
working.
+ //
+ try {
+ IConfigOptions configOptions =
+ PluginRegistry.getInstance().loadClass(configPlugin,
IConfigOptions.class);
+ addMixin(cmd, configPlugin.getIds()[0], configOptions);
+ } catch (Exception | LinkageError e) {
+ LogChannel.GENERAL.logError(
+ "Configuration plugin '"
+ + configPlugin.getIds()[0]
+ + "' is skipped, its command line options are not available:
"
+ + e.getMessage(),
+ e);
+ }
+ }
+ }
+ }
+
+ /**
+ * Picocli registers a mixin before it adds the mixin's options, so an
option name that is already
+ * taken would leave a half-added mixin behind. Check the names first, then
add.
+ */
+ private static void addMixin(CommandLine cmd, String name, IConfigOptions
configOptions)
+ throws HopPluginException {
+ CommandSpec mixin = CommandSpec.forAnnotatedObject(configOptions,
cmd.getFactory());
+ Map<String, OptionSpec> existingOptions =
cmd.getCommandSpec().optionsMap();
+ for (OptionSpec option : mixin.options()) {
+ for (String optionName : option.names()) {
+ if (existingOptions.containsKey(optionName)) {
+ throw new HopPluginException(
+ "option '" + optionName + "' is already declared by another
plugin or the command");
+ }
}
}
+ cmd.getCommandSpec().addMixin(name, mixin);
}
}
diff --git
a/engine/src/test/java/org/apache/hop/hop/HopAddMixinPluginsTest.java
b/engine/src/test/java/org/apache/hop/hop/HopAddMixinPluginsTest.java
new file mode 100644
index 0000000000..c334bacc0b
--- /dev/null
+++ b/engine/src/test/java/org/apache/hop/hop/HopAddMixinPluginsTest.java
@@ -0,0 +1,168 @@
+/*
+ * 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.hop.hop;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.hop.core.config.plugin.ConfigPluginType;
+import org.apache.hop.core.config.plugin.IConfigOptions;
+import org.apache.hop.core.logging.HopLogStore;
+import org.apache.hop.core.logging.ILogChannel;
+import org.apache.hop.core.plugins.IPlugin;
+import org.apache.hop.core.plugins.Plugin;
+import org.apache.hop.core.plugins.PluginRegistry;
+import org.apache.hop.core.variables.IVariables;
+import org.apache.hop.metadata.api.IHasHopMetadataProvider;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import picocli.CommandLine;
+import picocli.CommandLine.Command;
+import picocli.CommandLine.Option;
+
+/**
+ * A configuration plugin that can't be loaded or mixed in is skipped. The
command line still starts
+ * and the options of the other plugins still work.
+ */
+class HopAddMixinPluginsTest {
+
+ private static final String CATEGORY = "add-mixin-plugins-test";
+
+ private final List<IPlugin> registered = new ArrayList<>();
+
+ @BeforeAll
+ static void setUpClass() {
+ // The skipped plugins are logged, as on the command line.
+ HopLogStore.init();
+ }
+
+ @AfterEach
+ void tearDown() {
+ for (IPlugin plugin : registered) {
+ PluginRegistry.getInstance().removePlugin(ConfigPluginType.class,
plugin);
+ }
+ }
+
+ @Test
+ void aPluginWhoseClassIsMissingIsSkipped() throws Exception {
+ register("a-working", WorkingOptions.class.getName());
+ register("b-missing", "org.apache.hop.hop.DoesNotExistOptions");
+
+ assertWorkingOptionStillParses();
+ }
+
+ @Test
+ void aPluginThatFailsToBeCreatedIsSkipped() throws Exception {
+ register("a-working", WorkingOptions.class.getName());
+ register("b-failing", FailingOptions.class.getName());
+
+ assertWorkingOptionStillParses();
+ }
+
+ @Test
+ void aPluginPicocliRefusesIsSkipped() throws Exception {
+ register("a-working", WorkingOptions.class.getName());
+ register("b-no-options", NoOptions.class.getName());
+
+ assertWorkingOptionStillParses();
+ }
+
+ @Test
+ void aPluginWithADuplicateOptionIsSkipped() throws Exception {
+ register("a-working", WorkingOptions.class.getName());
+ register("b-duplicate", DuplicateOptions.class.getName());
+
+ CommandLine cmd = assertWorkingOptionStillParses();
+ assertFalse(cmd.getMixins().containsKey("b-duplicate"));
+ }
+
+ private CommandLine assertWorkingOptionStillParses() throws Exception {
+ CommandLine cmd = new CommandLine(new TestCommand());
+ Hop.addMixinPlugins(cmd, CATEGORY);
+
+ assertTrue(cmd.getMixins().containsKey("a-working"), "mixins: " +
cmd.getMixins().keySet());
+ assertEquals(1, cmd.getMixins().size(), "mixins: " +
cmd.getMixins().keySet());
+
+ cmd.parseArgs("--working-option", "value");
+ WorkingOptions options = (WorkingOptions) cmd.getMixins().get("a-working");
+ assertEquals("value", options.workingOption);
+ return cmd;
+ }
+
+ private void register(String id, String className) throws Exception {
+ Map<Class<?>, String> classMap = new HashMap<>();
+ classMap.put(IConfigOptions.class, className);
+ Plugin plugin =
+ new Plugin(
+ new String[] {id},
+ ConfigPluginType.class,
+ IConfigOptions.class,
+ CATEGORY,
+ id,
+ id,
+ null,
+ false,
+ true,
+ classMap,
+ new ArrayList<>(),
+ null,
+ new String[0],
+ null,
+ false);
+ PluginRegistry.getInstance().registerPlugin(ConfigPluginType.class,
plugin);
+ registered.add(plugin);
+ }
+
+ @Command(name = "test")
+ public static class TestCommand {}
+
+ public static class WorkingOptions implements IConfigOptions {
+ @Option(names = "--working-option")
+ String workingOption;
+
+ @Override
+ public boolean handleOption(
+ ILogChannel log, IHasHopMetadataProvider metadataProvider, IVariables
variables) {
+ return false;
+ }
+ }
+
+ /** Declares the same option name as {@link WorkingOptions}. */
+ public static class DuplicateOptions extends WorkingOptions {}
+
+ /** Declares nothing picocli accepts as a mixin. */
+ public static class NoOptions implements IConfigOptions {
+ @Override
+ public boolean handleOption(
+ ILogChannel log, IHasHopMetadataProvider metadataProvider, IVariables
variables) {
+ return false;
+ }
+ }
+
+ public static class FailingOptions extends WorkingOptions {
+ public FailingOptions() {
+ throw new IllegalStateException("broken plugin");
+ }
+ }
+}