forked from gorkem/vscode-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavaServerStarter.ts
More file actions
129 lines (117 loc) · 4.26 KB
/
javaServerStarter.ts
File metadata and controls
129 lines (117 loc) · 4.26 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
import * as path from 'path';
import * as net from 'net';
import * as glob from 'glob';
import * as os from 'os';
import isDocker = require('is-Docker');
import { StreamInfo, Executable, ExecutableOptions } from 'vscode-languageclient';
import { RequirementsData } from './requirements';
import { getJavaEncoding } from './settings';
import { getJavaConfiguration } from './utils';
declare var v8debug;
const DEBUG = (typeof v8debug === 'object') || startedInDebugMode();
export function prepareExecutable(requirements: RequirementsData, workspacePath, javaConfig): Executable {
const executable: Executable = Object.create(null);
const options: ExecutableOptions = Object.create(null);
options.env = process.env;
options.stdio = 'pipe';
executable.options = options;
executable.command = path.resolve(requirements.java_home + '/bin/java');
executable.args = prepareParams(requirements, javaConfig, workspacePath);
return executable;
}
export function awaitServerConnection(port): Thenable<StreamInfo> {
const addr = parseInt(port);
return new Promise((res, rej) => {
const server = net.createServer(stream => {
server.close();
console.log('JDT LS connection established on port ' + addr);
res({ reader: stream, writer: stream });
});
server.on('error', rej);
server.listen(addr, () => {
server.removeListener('error', rej);
console.log('Awaiting JDT LS connection on port ' + addr);
});
return server;
});
}
function prepareParams(requirements: RequirementsData, javaConfiguration, workspacePath): string[] {
const params: string[] = [];
if (DEBUG) {
params.push('-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1044,quiet=y');
// suspend=y is the default. Use this form if you need to debug the server startup code:
// params.push('-agentlib:jdwp=transport=dt_socket,server=y,address=1044');
}
if (requirements.java_version > 8) {
params.push('--add-modules=ALL-SYSTEM',
'--add-opens',
'java.base/java.util=ALL-UNNAMED',
'--add-opens',
'java.base/java.lang=ALL-UNNAMED');
}
params.push('-Declipse.application=org.eclipse.jdt.ls.core.id1',
'-Dosgi.bundles.defaultStartLevel=4',
'-Declipse.product=org.eclipse.jdt.ls.core.product');
if (DEBUG) {
params.push('-Dlog.level=ALL');
}
let vmargs = javaConfiguration.get('jdt.ls.vmargs', '');
if( getJavaConfiguration().inspect('jdt.ls.vmargs').defaultValue === vmargs
&& isDocker() ) {
vmargs = '-noverify -XX:+UseContainerSupport -XX:MaxRAMPercentage=80 -XX:+UseG1GC -XX:+UseStringDeduplication';
}
const encodingKey = '-Dfile.encoding=';
if (vmargs.indexOf(encodingKey) < 0) {
params.push(encodingKey + getJavaEncoding());
}
if (os.platform() == 'win32') {
const watchParentProcess = '-DwatchParentProcess=';
if (vmargs.indexOf(watchParentProcess) < 0) {
params.push(watchParentProcess + 'false');
}
}
parseVMargs(params, vmargs);
let server_home: string = path.resolve(__dirname, '../server');
const launchersFound: Array<string> = glob.sync('**/plugins/org.eclipse.equinox.launcher_*.jar', { cwd: server_home });
if (launchersFound.length) {
params.push('-jar'); params.push(path.resolve(server_home, launchersFound[0]));
} else {
return null;
}
// select configuration directory according to OS
let configDir = 'config_win';
if (process.platform === 'darwin') {
configDir = 'config_mac';
} else if (process.platform === 'linux') {
configDir = 'config_linux';
}
params.push('-configuration'); params.push(path.resolve(__dirname, '../server', configDir));
params.push('-data'); params.push(workspacePath);
return params;
}
function startedInDebugMode(): boolean {
const args = (process as any).execArgv;
if (args) {
return args.some((arg) => /^--debug=?/.test(arg) || /^--debug-brk=?/.test(arg) || /^--inspect-brk=?/.test(arg));
}
return false;
}
// exported for tests
export function parseVMargs(params: any[], vmargsLine: string) {
if (!vmargsLine) {
return;
}
const vmargs = vmargsLine.match(/(?:[^\s"]+|"[^"]*")+/g);
if (vmargs === null) {
return;
}
vmargs.forEach(arg => {
// remove all standalone double quotes
arg = arg.replace(/(\\)?"/g, function ($0, $1) { return ($1 ? $0 : ''); });
// unescape all escaped double quotes
arg = arg.replace(/(\\)"/g, '"');
if (params.indexOf(arg) < 0) {
params.push(arg);
}
});
}