|
| 1 | +From a97603787acc1430b7e129ec3da9dea9b549efb0 Mon Sep 17 00:00:00 2001 |
| 2 | +From: lhchavez <lhchavez@lhchavez.com> |
| 3 | +Date: Tue, 8 Jun 2021 17:03:38 +0000 |
| 4 | +Subject: [PATCH] Add a System property to allow binding to a specific |
| 5 | + interface/port |
| 6 | + |
| 7 | +This change adds support to read the value of the |
| 8 | +`com.microsoft.java.debug.serverAddress` system property when |
| 9 | +constructing the JavaDebugServer. This allows callers to specify a |
| 10 | +specific interface to bind to (e.g. `localhost:0`), a specific port |
| 11 | +(e.g. `:12345`), or both (e.g. `localhost:12345`). |
| 12 | +--- |
| 13 | + .../plugin/internal/JavaDebugServer.java | 29 ++++++++++++++++++- |
| 14 | + 1 file changed, 28 insertions(+), 1 deletion(-) |
| 15 | + |
| 16 | +diff --git a/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaDebugServer.java b/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaDebugServer.java |
| 17 | +index 32ab195b..12c2955c 100644 |
| 18 | +--- a/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaDebugServer.java |
| 19 | ++++ b/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaDebugServer.java |
| 20 | +@@ -12,8 +12,10 @@ |
| 21 | + package com.microsoft.java.debug.plugin.internal; |
| 22 | + |
| 23 | + import java.io.IOException; |
| 24 | ++import java.net.InetAddress; |
| 25 | + import java.net.ServerSocket; |
| 26 | + import java.net.Socket; |
| 27 | ++import java.net.UnknownHostException; |
| 28 | + import java.util.concurrent.ExecutorService; |
| 29 | + import java.util.concurrent.SynchronousQueue; |
| 30 | + import java.util.concurrent.ThreadPoolExecutor; |
| 31 | +@@ -33,8 +35,33 @@ |
| 32 | + private ExecutorService executor = null; |
| 33 | + |
| 34 | + private JavaDebugServer() { |
| 35 | ++ int port = 0; |
| 36 | ++ InetAddress bindAddr = null; |
| 37 | ++ String serverAddress = System.getProperty("com.microsoft.java.debug.serverAddress"); |
| 38 | ++ if (serverAddress != null) { |
| 39 | ++ int portIndex = serverAddress.lastIndexOf(':'); |
| 40 | ++ if (portIndex == -1) { |
| 41 | ++ logger.log(Level.SEVERE, String.format("Malformed server address \"%s\": missing port", serverAddress)); |
| 42 | ++ return; |
| 43 | ++ } |
| 44 | ++ try { |
| 45 | ++ port = Integer.parseInt(serverAddress.substring(portIndex + 1)); |
| 46 | ++ } catch (NumberFormatException e) { |
| 47 | ++ logger.log(Level.SEVERE, String.format("Malformed server address \"%s\": %s", serverAddress, e.toString()), e); |
| 48 | ++ return; |
| 49 | ++ } |
| 50 | ++ |
| 51 | ++ if (portIndex > 0) { |
| 52 | ++ try { |
| 53 | ++ bindAddr = InetAddress.getByName(serverAddress.substring(0, portIndex)); |
| 54 | ++ } catch (UnknownHostException e) { |
| 55 | ++ logger.log(Level.SEVERE, String.format("Invalid server address \"%s\": %s", serverAddress, e.toString()), e); |
| 56 | ++ return; |
| 57 | ++ } |
| 58 | ++ } |
| 59 | ++ } |
| 60 | + try { |
| 61 | +- this.serverSocket = new ServerSocket(0, 1); |
| 62 | ++ this.serverSocket = new ServerSocket(port, 1, bindAddr); |
| 63 | + } catch (IOException e) { |
| 64 | + logger.log(Level.SEVERE, String.format("Failed to create Java Debug Server: %s", e.toString()), e); |
| 65 | + } |
0 commit comments