From cc30cd02dfe97c07d4749a21fd0ed3569e4ff3dd Mon Sep 17 00:00:00 2001 From: Kalin Chan <73829904+kalinchan@users.noreply.github.com> Date: Wed, 26 Jun 2024 10:21:38 +0100 Subject: [PATCH] Merge pull request #6724 from pzygielo/debug-option-display-jdk11+ FISH-8728 Refine RE to allow host in jdwp agent address --- .../enterprise/admin/launcher/GFLauncher.java | 12 ++++---- .../launcher/GFLauncherExtractPortTest.java | 30 ++++++++++++++++++- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/nucleus/admin/launcher/src/main/java/com/sun/enterprise/admin/launcher/GFLauncher.java b/nucleus/admin/launcher/src/main/java/com/sun/enterprise/admin/launcher/GFLauncher.java index 39b54f47ed5..0e03b82fa34 100644 --- a/nucleus/admin/launcher/src/main/java/com/sun/enterprise/admin/launcher/GFLauncher.java +++ b/nucleus/admin/launcher/src/main/java/com/sun/enterprise/admin/launcher/GFLauncher.java @@ -37,7 +37,7 @@ * only if the new code is made subject to such option by the copyright * holder. */ -// Portions Copyright [2016-2023] [Payara Foundation and/or its affiliates] +// Portions Copyright [2016-2024] [Payara Foundation and/or its affiliates] package com.sun.enterprise.admin.launcher; @@ -382,15 +382,17 @@ static boolean isJdwpOption(String option) { return option.startsWith("-Xrunjdwp:") || option.startsWith("-agentlib:jdwp"); } + private static final String DEBUG_ADDRESS_PORT_GROUP = "port"; + private static final Pattern DEBUG_ADDRESS_PATTERN = Pattern.compile(".*address=(?(?.+):)?(?\\d*).*"); + static int extractDebugPort(String option) { - Pattern portRegex = Pattern.compile(".*address=(?\\d*).*"); - Matcher m = portRegex.matcher(option); + Matcher m = DEBUG_ADDRESS_PATTERN.matcher(option); if (!m.matches()) { return -1; } try { - String addressGroup = m.group("port"); - return Integer.parseInt(addressGroup); + String portGroup = m.group(DEBUG_ADDRESS_PORT_GROUP); + return Integer.parseInt(portGroup); } catch (NumberFormatException nfex) { return -1; } diff --git a/nucleus/admin/launcher/src/test/java/com/sun/enterprise/admin/launcher/GFLauncherExtractPortTest.java b/nucleus/admin/launcher/src/test/java/com/sun/enterprise/admin/launcher/GFLauncherExtractPortTest.java index e1775e1cd09..384d4656a79 100644 --- a/nucleus/admin/launcher/src/test/java/com/sun/enterprise/admin/launcher/GFLauncherExtractPortTest.java +++ b/nucleus/admin/launcher/src/test/java/com/sun/enterprise/admin/launcher/GFLauncherExtractPortTest.java @@ -1,7 +1,7 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright (c) 2019 Payara Foundation and/or its affiliates. All rights reserved. + * Copyright (c) 2019-2024 Payara Foundation and/or its affiliates. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development @@ -51,6 +51,34 @@ public void shouldExtractPortNumberFromDebugOption() { assertEquals(9876, port); } + @Test + public void shouldExtractPortNumberFromDebugOptionWithHost() { + int port = GFLauncher.extractDebugPort("-agentlib:jdwp=transport=dt_socket,server=y,address=*:9876,suspend=n"); + + assertEquals(9876, port); + } + + @Test + public void shouldExtractPortNumberFromDebugOptionWithHostName() { + int port = GFLauncher.extractDebugPort("-agentlib:jdwp=transport=dt_socket,server=y,address=localhost:9876,suspend=n"); + + assertEquals(9876, port); + } + + @Test + public void shouldExtractPortNumberFromDebugOptionWithIPv4Host() { + int port = GFLauncher.extractDebugPort("-agentlib:jdwp=transport=dt_socket,server=y,address=127.0.0.1:9876,suspend=n"); + + assertEquals(9876, port); + } + + @Test + public void shouldExtractPortNumberFromDebugOptionWithIPv6Host() { + int port = GFLauncher.extractDebugPort("-agentlib:jdwp=transport=dt_socket,server=y,address=[fd27:2024:0518::1]:9876,suspend=n"); + + assertEquals(9876, port); + } + @Test public void shouldNotFindPortNumberInOption() { int port = GFLauncher.extractDebugPort("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n");