Skip to content

Commit

Permalink
CAMEL-19949: camel-jbang - Allow to set a custom remote debugging port
Browse files Browse the repository at this point in the history
  • Loading branch information
essobedo committed Oct 4, 2023
1 parent 5df9099 commit a5be69b
Showing 1 changed file with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = "<true|false|port>",
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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -808,6 +814,27 @@ private Properties loadProfileProperties() throws Exception {
return answer;
}

/**
* Parses the JVM debug port from the given value.
* <p/>
* 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.equalsIgnoreCase("true")) {
return 4004;
} else if (value.equalsIgnoreCase("false")) {
return 0;
}
return Integer.parseInt(value);
}

protected int runCamelVersion(KameletMain main) throws Exception {
List<String> cmds = new ArrayList<>(spec.commandLine().getParseResult().originalArgs());

Expand All @@ -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) {
Expand Down Expand Up @@ -1375,4 +1402,10 @@ protected void doConsumeParameters(Stack<String> args, Run cmd) {
}
}

static class DebugConsumer extends ParameterConsumer<Run> {
@Override
protected void doConsumeParameters(Stack<String> args, Run cmd) {
cmd.jvmDebugPort = parseJvmDebugPort(args.pop());
}
}
}

0 comments on commit a5be69b

Please sign in to comment.