summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Fraser <george@fivetran.com>2019-01-05 17:20:27 -0800
committerGeorge Fraser <george@fivetran.com>2019-01-05 17:20:27 -0800
commit9369e3f09dd64beba7e8e10973cbdd565f8c544a (patch)
treec25ca4bf46827a411541964be4f8474582a883b0
parentec011d2dfa3f510f763561e94f50f1f509981f46 (diff)
downloadjava-language-server-9369e3f09dd64beba7e8e10973cbdd565f8c544a.zip
VisualVM config
-rw-r--r--lib/extension.ts99
-rw-r--r--pom.xml18
2 files changed, 116 insertions, 1 deletions
diff --git a/lib/extension.ts b/lib/extension.ts
index cf6ff19..4f73acf 100644
--- a/lib/extension.ts
+++ b/lib/extension.ts
@@ -2,6 +2,7 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as Path from "path";
+import * as FS from "fs";
import { window, workspace, ExtensionContext, commands, tasks, Task, TaskExecution, ShellExecution, Uri, TaskDefinition, languages, IndentAction, Progress, ProgressLocation } from 'vscode';
import {LanguageClient, LanguageClientOptions, ServerOptions, NotificationType} from "vscode-languageclient";
@@ -9,7 +10,7 @@ import {LanguageClient, LanguageClientOptions, ServerOptions, NotificationType}
/** Called when extension is activated */
export function activate(context: ExtensionContext) {
console.log('Activating Java');
-
+
// Options to control the language client
let clientOptions: LanguageClientOptions = {
// Register the server for java documents
@@ -213,4 +214,100 @@ function platformSpecificLauncher(): string[] {
}
throw `unsupported platform: ${process.platform}`;
+}
+
+// Alternative server options if you want to use visualvm
+function visualVmConfig(context: ExtensionContext): ServerOptions {
+ let javaExecutablePath = findJavaExecutable('java');
+
+ if (javaExecutablePath == null) {
+ window.showErrorMessage("Couldn't locate java in $JAVA_HOME or $PATH");
+
+ throw "Gave up";
+ }
+
+ let classes = Path.resolve(context.extensionPath, "target", "classes");
+ let cpTxt = Path.resolve(context.extensionPath, "target", "cp.txt");
+ let cpContents = FS.readFileSync(cpTxt, "utf-8");
+
+ let args = [
+ '-cp', classes + ":" + cpContents,
+ '-Xverify:none', // helps VisualVM avoid 'error 62'
+ '-Xdebug',
+ // '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005',
+ 'org.javacs.Main'
+ ];
+
+ console.log(javaExecutablePath + ' ' + args.join(' '));
+
+ // Start the child java process
+ return {
+ command: javaExecutablePath,
+ args: args,
+ options: { cwd: context.extensionPath }
+ }
+}
+
+function findJavaExecutable(binname: string) {
+ binname = correctBinname(binname);
+
+ // First search java.home setting
+ let userJavaHome = workspace.getConfiguration('java').get('home') as string;
+
+ if (userJavaHome != null) {
+ console.log('Looking for java in settings java.home ' + userJavaHome + '...');
+
+ let candidate = findJavaExecutableInJavaHome(userJavaHome, binname);
+
+ if (candidate != null)
+ return candidate;
+ }
+
+ // Then search each JAVA_HOME
+ let envJavaHome = process.env['JAVA_HOME'];
+
+ if (envJavaHome) {
+ console.log('Looking for java in environment variable JAVA_HOME ' + envJavaHome + '...');
+
+ let candidate = findJavaExecutableInJavaHome(envJavaHome, binname);
+
+ if (candidate != null)
+ return candidate;
+ }
+
+ // Then search PATH parts
+ if (process.env['PATH']) {
+ console.log('Looking for java in PATH');
+
+ let pathparts = process.env['PATH'].split(Path.delimiter);
+ for (let i = 0; i < pathparts.length; i++) {
+ let binpath = Path.join(pathparts[i], binname);
+ if (FS.existsSync(binpath)) {
+ return binpath;
+ }
+ }
+ }
+
+ // Else return the binary name directly (this will likely always fail downstream)
+ return null;
+}
+
+function correctBinname(binname: string) {
+ if (process.platform === 'win32')
+ return binname + '.exe';
+ else
+ return binname;
+}
+
+function findJavaExecutableInJavaHome(javaHome: string, binname: string) {
+ let workspaces = javaHome.split(Path.delimiter);
+
+ for (let i = 0; i < workspaces.length; i++) {
+ let binpath = Path.join(workspaces[i], 'bin', binname);
+
+ if (FS.existsSync(binpath))
+ return binpath;
+ }
+
+ return null;
} \ No newline at end of file
diff --git a/pom.xml b/pom.xml
index bc2e6fe..aab5256 100644
--- a/pom.xml
+++ b/pom.xml
@@ -68,6 +68,24 @@
</annotationProcessorPaths>
</configuration>
</plugin>
+ <!-- Generate target/cp.txt so visualVmConfig() in extension.ts can read it -->
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-dependency-plugin</artifactId>
+ <version>2.9</version>
+ <executions>
+ <execution>
+ <id>build-classpath</id>
+ <phase>generate-sources</phase>
+ <goals>
+ <goal>build-classpath</goal>
+ </goals>
+ <configuration>
+ <outputFile>${project.build.directory}/cp.txt</outputFile>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
</plugins>
</build>
</project>