From 9ef209da74b16a6d28fee3748d55711e5e55a292 Mon Sep 17 00:00:00 2001 From: BoykoAlex Date: Mon, 14 Feb 2022 16:00:40 -0500 Subject: [PATCH] vscode-spring-boot: Setup vm args for auto connect to launched app --- .../vscode-spring-boot/lib/Main.ts | 4 ++ .../lib/debug-config-provider.ts | 55 +++++++++++++++++++ .../vscode-spring-boot/package.json | 3 +- 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 vscode-extensions/vscode-spring-boot/lib/debug-config-provider.ts diff --git a/vscode-extensions/vscode-spring-boot/lib/Main.ts b/vscode-extensions/vscode-spring-boot/lib/Main.ts index ad61ca6d4..ffa5f7661 100644 --- a/vscode-extensions/vscode-spring-boot/lib/Main.ts +++ b/vscode-extensions/vscode-spring-boot/lib/Main.ts @@ -7,6 +7,7 @@ import * as commons from '@pivotal-tools/commons-vscode'; import * as liveHoverUi from './live-hover-connect-ui'; import {LanguageClient} from "vscode-languageclient/node"; +import { startDebugSupport } from './debug-config-provider'; const PROPERTIES_LANGUAGE_ID = "spring-boot-properties"; const YAML_LANGUAGE_ID = "spring-boot-properties-yaml"; @@ -78,6 +79,9 @@ export function activate(context: VSCode.ExtensionContext): Thenable { liveHoverUi.activate(client, options, context); return client; diff --git a/vscode-extensions/vscode-spring-boot/lib/debug-config-provider.ts b/vscode-extensions/vscode-spring-boot/lib/debug-config-provider.ts new file mode 100644 index 000000000..4dd4cfff5 --- /dev/null +++ b/vscode-extensions/vscode-spring-boot/lib/debug-config-provider.ts @@ -0,0 +1,55 @@ +import { CancellationToken, DebugConfiguration, DebugConfigurationProvider, ProviderResult, WorkspaceFolder } from "vscode"; +import * as path from "path"; +import * as VSCode from "vscode"; +import { Disposable } from "vscode"; + +const JMX_VM_ARG = '-Dspring.jmx.enabled=' +const ADMIN_VM_ARG = '-Dspring.application.admin.enabled=' +const BOOT_PROJECT_ARG = '-Dspring.boot.project.name='; + +class SpringBootDebugConfigProvider implements DebugConfigurationProvider { + + resolveDebugConfigurationWithSubstitutedVariables(folder: WorkspaceFolder | undefined, debugConfiguration: DebugConfiguration, token?: CancellationToken): ProviderResult { + if (isAutoConnect() && this.isActuatorOnClasspath(debugConfiguration)) { + if (debugConfiguration.vmArgs) { + if (debugConfiguration.vmArgs.indexOf(JMX_VM_ARG) < 0) { + debugConfiguration.vmArgs += ` ${JMX_VM_ARG}true`; + } + if (debugConfiguration.vmArgs.indexOf(ADMIN_VM_ARG) < 0) { + debugConfiguration.vmArgs += ` ${ADMIN_VM_ARG}true`; + } + if (debugConfiguration.vmArgs.indexOf(BOOT_PROJECT_ARG) < 0) { + debugConfiguration.vmArgs += ` ${BOOT_PROJECT_ARG}${debugConfiguration.projectName}`; + } + } else { + debugConfiguration.vmArgs = `${JMX_VM_ARG}true ${ADMIN_VM_ARG}true ${BOOT_PROJECT_ARG}${debugConfiguration.projectName}`; + } + } + return debugConfiguration; + } + + private isActuatorOnClasspath(debugConfiguration: DebugConfiguration): boolean { + if (Array.isArray(debugConfiguration.classPaths)) { + return !!debugConfiguration.classPaths.find(this.isActuatorJarFile); + } + return false; + } + + private isActuatorJarFile(f: string): boolean { + const fileName = path.basename(f || ""); + if (/^spring-boot-actuator-\d+\.\d+\.\d+(.*)?.jar$/.test(fileName)) { + return true; + } + return false; + } + +} + +export function startDebugSupport(): Disposable { + // VSCode.debug.onDidStartDebugSession(handleDebugSessionStarted); + return VSCode.debug.registerDebugConfigurationProvider('java', new SpringBootDebugConfigProvider(), VSCode.DebugConfigurationProviderTriggerKind.Initial); +} + +function isAutoConnect(): boolean { + return VSCode.workspace.getConfiguration("boot-java.live-information.automatic-tracking")?.get('on'); +} diff --git a/vscode-extensions/vscode-spring-boot/package.json b/vscode-extensions/vscode-spring-boot/package.json index d8fda52bb..2646b1215 100644 --- a/vscode-extensions/vscode-spring-boot/package.json +++ b/vscode-extensions/vscode-spring-boot/package.json @@ -29,7 +29,8 @@ "onLanguage:spring-boot-properties", "onLanguage:spring-boot-properties-yaml", "onLanguage:java", - "onLanguage:xml" + "onLanguage:xml", + "onDebugResolve:java" ], "contributes": { "javaExtensions": [