Skip to content

Commit 336d090

Browse files
lhchavezZabot
authored andcommitted
Add a System property to allow binding to a specific interface/port
This change adds support to read the value of the `com.microsoft.java.debug.serverAddress` system property when constructing the JavaDebugServer. This allows callers to specify a specific interface to bind to (e.g. `localhost:0`), a specific port (e.g. `:12345`), or both (e.g. `localhost:12345`).
1 parent 7943067 commit 336d090

File tree

1 file changed

+28
-1
lines changed
  • com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal

1 file changed

+28
-1
lines changed

com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaDebugServer.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
package com.microsoft.java.debug.plugin.internal;
1313

1414
import java.io.IOException;
15+
import java.net.InetAddress;
1516
import java.net.ServerSocket;
1617
import java.net.Socket;
18+
import java.net.UnknownHostException;
1719
import java.util.concurrent.ExecutorService;
1820
import java.util.concurrent.SynchronousQueue;
1921
import java.util.concurrent.ThreadPoolExecutor;
@@ -33,8 +35,33 @@ public class JavaDebugServer implements IDebugServer {
3335
private ExecutorService executor = null;
3436

3537
private JavaDebugServer() {
38+
int port = 0;
39+
InetAddress bindAddr = null;
40+
String serverAddress = System.getProperty("com.microsoft.java.debug.serverAddress");
41+
if (serverAddress != null) {
42+
int portIndex = serverAddress.lastIndexOf(':');
43+
if (portIndex == -1) {
44+
logger.log(Level.SEVERE, String.format("Malformed server address \"%s\": missing port", serverAddress));
45+
return;
46+
}
47+
try {
48+
port = Integer.parseInt(serverAddress.substring(portIndex + 1));
49+
} catch (NumberFormatException e) {
50+
logger.log(Level.SEVERE, String.format("Malformed server address \"%s\": %s", serverAddress, e.toString()), e);
51+
return;
52+
}
53+
54+
if (portIndex > 0) {
55+
try {
56+
bindAddr = InetAddress.getByName(serverAddress.substring(0, portIndex));
57+
} catch (UnknownHostException e) {
58+
logger.log(Level.SEVERE, String.format("Invalid server address \"%s\": %s", serverAddress, e.toString()), e);
59+
return;
60+
}
61+
}
62+
}
3663
try {
37-
this.serverSocket = new ServerSocket(0, 1);
64+
this.serverSocket = new ServerSocket(port, 1, bindAddr);
3865
} catch (IOException e) {
3966
logger.log(Level.SEVERE, String.format("Failed to create Java Debug Server: %s", e.toString()), e);
4067
}

0 commit comments

Comments
 (0)