From d35d5a469e85e5d1092fcb443ae571d5efddd82a Mon Sep 17 00:00:00 2001 From: Nicolas Filotto Date: Wed, 4 Oct 2023 20:36:29 +0200 Subject: [PATCH] CAMEL-19949: camel-jbang - Allow to set a custom remote debugging port (#11645) --- .../camel/dsl/jbang/core/commands/Run.java | 59 ++++++++++++++++--- 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java index ad0e0856925cb..5446fe8ee0c2e 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java @@ -16,7 +16,7 @@ */ package org.apache.camel.dsl.jbang.core.commands; -import java.awt.*; +import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.UnsupportedFlavorException; @@ -166,8 +166,10 @@ public class Run extends CamelCommand { description = "Whether to allow automatic downloading JAR dependencies (over the internet)") boolean download = true; - @Option(names = { "--jvm-debug" }, defaultValue = "false", description = "To enable JVM remote debug on localhost:4004") - boolean jvmDebug; + @Option(names = { "--jvm-debug" }, parameterConsumer = DebugConsumer.class, paramLabel = "", + description = "To enable JVM remote debugging on port 4004 by default. The supported values are true to " + + "enable the remote debugging, false to disable the remote debugging or a number to use a custom port") + int jvmDebugPort; @Option(names = { "--name" }, defaultValue = "CamelJBang", description = "The name of the Camel application") String name; @@ -300,6 +302,10 @@ protected Integer runScript(String file) throws Exception { return run(); } + private boolean isDebugMode() { + return jvmDebugPort > 0; + } + private void writeSetting(KameletMain main, Properties existing, String key, String value) { String val = existing != null ? existing.getProperty(key, value) : value; if (val != null) { @@ -707,7 +713,7 @@ private int run() throws Exception { } // okay we have validated all input and are ready to run - if (camelVersion != null || jvmDebug) { + if (camelVersion != null || isDebugMode()) { boolean custom = false; if (camelVersion != null) { // run in another JVM with different camel version (foreground or background) @@ -794,7 +800,7 @@ private Properties loadProfileProperties() throws Exception { openapi = answer.getProperty("camel.jbang.open-api", openapi); download = "true".equals(answer.getProperty("camel.jbang.download", download ? "true" : "false")); background = "true".equals(answer.getProperty("camel.jbang.background", background ? "true" : "false")); - jvmDebug = "true".equals(answer.getProperty("camel.jbang.jvmDebug", jvmDebug ? "true" : "false")); + jvmDebugPort = parseJvmDebugPort(answer.getProperty("camel.jbang.jvmDebug", Integer.toString(jvmDebugPort))); camelVersion = answer.getProperty("camel.jbang.camel-version", camelVersion); kameletsVersion = answer.getProperty("camel.jbang.kameletsVersion", kameletsVersion); gav = answer.getProperty("camel.jbang.gav", gav); @@ -808,6 +814,27 @@ private Properties loadProfileProperties() throws Exception { return answer; } + /** + * Parses the JVM debug port from the given value. + *

+ * The value can be {@code true} to indicate a default port which is {@code 4004}, {@code false} to indicate no + * debug, or a number corresponding to a custom port. + * + * @param value the value to parse. + * + * @return the JVM debug port corresponding to the given value. + */ + private static int parseJvmDebugPort(String value) { + if (value == null) { + return 0; + } else if (value.equals("true")) { + return 4004; + } else if (value.equals("false")) { + return 0; + } + return Integer.parseInt(value); + } + protected int runCamelVersion(KameletMain main) throws Exception { List cmds = new ArrayList<>(spec.commandLine().getParseResult().originalArgs()); @@ -831,9 +858,9 @@ protected int runCamelVersion(KameletMain main) throws Exception { if (kameletsVersion != null) { jbangArgs.add("-Dcamel-kamelets.version=" + kameletsVersion); } - if (jvmDebug) { - jbangArgs.add("--debug"); // jbang --debug - cmds.remove("--jvm-debug"); + if (isDebugMode()) { + jbangArgs.add("--debug=" + jvmDebugPort); // jbang --debug=port + cmds.removeIf(arg -> arg.startsWith("--jvm-debug")); } if (repos != null) { @@ -1375,4 +1402,20 @@ protected void doConsumeParameters(Stack args, Run cmd) { } } + static class DebugConsumer extends ParameterConsumer { + private static final Pattern DEBUG_ARG_VALUE_PATTERN = Pattern.compile("\\d+|true|false"); + + @Override + protected void doConsumeParameters(Stack args, Run cmd) { + String arg = args.peek(); + if (DEBUG_ARG_VALUE_PATTERN.asPredicate().test(arg)) { + // The value matches with the expected format so let's assume that it is a debug argument value + args.pop(); + } else { + // Here we assume that the value is not a debug argument value so let's simply enable the debug mode + arg = "true"; + } + cmd.jvmDebugPort = parseJvmDebugPort(arg); + } + } }