This repository was archived by the owner on Mar 16, 2026. It is now read-only.
forked from microsoft/java-debug
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJavaDebugServer.java
More file actions
146 lines (130 loc) · 5.38 KB
/
Copy pathJavaDebugServer.java
File metadata and controls
146 lines (130 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*******************************************************************************
* Copyright (c) 2017 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*******************************************************************************/
package com.microsoft.java.debug.plugin.internal;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.microsoft.java.debug.core.Configuration;
import com.microsoft.java.debug.core.adapter.ProtocolServer;
public class JavaDebugServer implements IDebugServer {
private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);
private static JavaDebugServer singletonInstance;
private ServerSocket serverSocket = null;
private boolean isStarted = false;
private ExecutorService executor = null;
private JavaDebugServer() {
try {
this.serverSocket = new ServerSocket(0, 1);
} catch (IOException e) {
logger.log(Level.SEVERE, String.format("Failed to create Java Debug Server: %s", e.toString()), e);
}
}
/**
* Gets the single instance of JavaDebugServer.
* @return the JavaDebugServer instance
*/
public static synchronized IDebugServer getInstance() {
if (singletonInstance == null) {
singletonInstance = new JavaDebugServer();
}
return singletonInstance;
}
/**
* Gets the server port.
*/
@Override
public synchronized int getPort() {
if (this.serverSocket != null) {
return this.serverSocket.getLocalPort();
}
return -1;
}
/**
* Starts the server if it's not started yet.
*/
@Override
public synchronized void start() {
if (this.serverSocket != null && !this.isStarted) {
this.isStarted = true;
this.executor = new ThreadPoolExecutor(0, 100, 30L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
// Execute eventLoop in a new thread.
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
// Allow server socket to service multiple clients at the same time.
// When a request comes in, create a connection thread to process it.
// Then the server goes back to listen for new connection request.
Socket connection = serverSocket.accept();
executor.submit(createConnectionTask(connection));
} catch (IOException e) {
logger.log(Level.SEVERE, String.format("Setup socket connection exception: %s", e.toString()), e);
closeServerSocket();
// If exception occurs when waiting for new client connection, shut down the connection pool
// to make sure no new tasks are accepted. But the previously submitted tasks will continue to run.
shutdownConnectionPool(false);
return;
}
}
}
}, "Java Debug Server").start();
}
}
@Override
public synchronized void stop() {
closeServerSocket();
shutdownConnectionPool(true);
}
private synchronized void closeServerSocket() {
if (serverSocket != null) {
try {
logger.info("Close debugserver socket port " + serverSocket.getLocalPort());
serverSocket.close();
} catch (IOException e) {
logger.log(Level.SEVERE, String.format("Close ServerSocket exception: %s", e.toString()), e);
}
}
serverSocket = null;
}
private synchronized void shutdownConnectionPool(boolean now) {
if (this.executor != null) {
if (now) {
this.executor.shutdownNow();
} else {
this.executor.shutdown();
}
}
}
private Runnable createConnectionTask(Socket connection) {
return new Runnable() {
@Override
public void run() {
try {
ProtocolServer protocolServer = new ProtocolServer(connection.getInputStream(), connection.getOutputStream(),
JdtProviderContextFactory.createProviderContext());
// protocol server will dispatch request and send response in a while-loop.
protocolServer.run();
} catch (IOException e) {
logger.log(Level.SEVERE, String.format("Socket connection exception: %s", e.toString()), e);
} finally {
logger.info("Debug connection closed");
}
}
};
}
}