diff --git a/eclipse-language-servers/org.springframework.boot.ide.java.servers/plugin.xml b/eclipse-language-servers/org.springframework.boot.ide.java.servers/plugin.xml index 4066c2d39..e8a6c483e 100644 --- a/eclipse-language-servers/org.springframework.boot.ide.java.servers/plugin.xml +++ b/eclipse-language-servers/org.springframework.boot.ide.java.servers/plugin.xml @@ -5,7 +5,7 @@ diff --git a/eclipse-language-servers/org.springframework.boot.ide.java.servers/src/org/springframework/boot/ide/java/servers/DelegatingStreamConnectionProvider.java b/eclipse-language-servers/org.springframework.boot.ide.java.servers/src/org/springframework/boot/ide/java/servers/DelegatingStreamConnectionProvider.java new file mode 100644 index 000000000..d5b659eea --- /dev/null +++ b/eclipse-language-servers/org.springframework.boot.ide.java.servers/src/org/springframework/boot/ide/java/servers/DelegatingStreamConnectionProvider.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright (c) 2017 Pivotal, Inc. + * 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: + * Pivotal, Inc. - initial API and implementation + *******************************************************************************/ +package org.springframework.boot.ide.java.servers; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.eclipse.lsp4e.server.StreamConnectionProvider; + +/** + * if the system property "boot-java-ls-port" exists, delegate to the socket-based + * stream connection provider, otherwise use the standard process-based stream + * connection provider. + * + * This allows you to run the language server in server mode (listens on a port for + * a connection) and connect the IDE integration to that already running language + * server instead of starting a new process for it. + * + * @author Martin Lippert + */ +public class DelegatingStreamConnectionProvider implements StreamConnectionProvider { + + private StreamConnectionProvider provider; + + public DelegatingStreamConnectionProvider() { + String port = System.getProperty("boot-java-ls-port"); + + if (port != null) { + this.provider = new SpringBootJavaLanguageServerViaSocket(Integer.parseInt(port)); + } + else { + this.provider = new SpringBootJavaLanguageServer(); + } + } + + @Override + public void start() throws IOException { + this.provider.start(); + } + + @Override + public InputStream getInputStream() { + return this.provider.getInputStream(); + } + + @Override + public OutputStream getOutputStream() { + return this.provider.getOutputStream(); + } + + @Override + public void stop() { + this.provider.stop(); + } + +} diff --git a/eclipse-language-servers/org.springframework.boot.ide.java.servers/src/org/springframework/boot/ide/java/servers/SpringBootJavaLanguageServerViaSocket.java b/eclipse-language-servers/org.springframework.boot.ide.java.servers/src/org/springframework/boot/ide/java/servers/SpringBootJavaLanguageServerViaSocket.java new file mode 100644 index 000000000..518bba96d --- /dev/null +++ b/eclipse-language-servers/org.springframework.boot.ide.java.servers/src/org/springframework/boot/ide/java/servers/SpringBootJavaLanguageServerViaSocket.java @@ -0,0 +1,64 @@ +/******************************************************************************* + * Copyright (c) 2017 Pivotal, Inc. + * 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: + * Pivotal, Inc. - initial API and implementation + *******************************************************************************/ +package org.springframework.boot.ide.java.servers; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; + +import org.eclipse.lsp4e.server.StreamConnectionProvider; + +public class SpringBootJavaLanguageServerViaSocket implements StreamConnectionProvider { + + private OutputStream outputStream; + private InputStream inputStream; + private Socket socket; + private int port; + + public SpringBootJavaLanguageServerViaSocket(int port) { + this.port = port; + } + + @Override + public void start() throws IOException { + try { + socket = new Socket("localhost", port); + outputStream = socket.getOutputStream(); + inputStream = socket.getInputStream(); + } + catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public InputStream getInputStream() { + return inputStream; + } + + @Override + public OutputStream getOutputStream() { + return outputStream; + } + + @Override + public void stop() { + if (socket != null) { + try { + socket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + +}