Control 'standalone' launch via spring boot properties

This commit is contained in:
Kris De Volder
2018-10-03 12:43:11 -07:00
parent 502ee0429e
commit 2a9323ad36
4 changed files with 68 additions and 13 deletions

View File

@@ -29,6 +29,11 @@
<version>${reactor-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@@ -35,8 +35,10 @@ import org.eclipse.lsp4j.services.LanguageServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.ide.vscode.commons.languageserver.config.LanguageServerProperties;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
import org.springframework.ide.vscode.commons.util.Log;
import org.springframework.util.Assert;
/**
* A CommandLineRunner that launches a language server. This meant to be used as a Spring bean
@@ -63,7 +65,7 @@ public class LanguageServerRunner implements CommandLineRunner {
log.error("", e);
}
},
"LanguageServerApp lifecycle"
"LanguageServerApp-lifecycle"
).start();
}
@@ -71,25 +73,23 @@ public class LanguageServerRunner implements CommandLineRunner {
* System property that is set when the app launches. This makes it easy for the JVM process to recognized
* as a languageserver by using (for example) JMX to read the system properties.
*/
public static final String STS4_LANGUAGESERVER_NAME = "sts4.languageserver.name";
public static final String STANDALONE_STARTUP = "standalone-startup"; //TODO: turn into spring boot property
private static final int SERVER_STANDALONE_PORT = 5007; //TODO: turn into spring boot property
public static final String SYSPROP_LANGUAGESERVER_NAME = "sts4.languageserver.name";
private LanguageServerProperties properties;
private final String name;
private final Provider<SimpleLanguageServer> languageServerFactory;
public LanguageServerRunner(String name, Provider<SimpleLanguageServer> languageServerFactory) {
public LanguageServerRunner(String name, LanguageServerProperties properties, Provider<SimpleLanguageServer> languageServerFactory) {
super();
this.name = name;
this.properties = properties;
this.languageServerFactory = languageServerFactory;
}
public void start() throws Exception {
System.setProperty(STS4_LANGUAGESERVER_NAME, name); //makes it easy to recognize language server processes.
System.setProperty(SYSPROP_LANGUAGESERVER_NAME, name); //makes it easy to recognize language server processes.
LanguageServerRunner app = this;
if (System.getProperty(STANDALONE_STARTUP, "false").equals("true")) {
if (properties.isStandalone()) {
app.startAsServer();
} else {
app.startAsClient();
@@ -161,7 +161,8 @@ public class LanguageServerRunner implements CommandLineRunner {
* https://github.com/itemis/xtext-languageserver-example/blob/master/org.xtext.example.mydsl.ide/src/org/xtext/example/mydsl/ide/RunServer.java
*/
public void startAsServer() throws Exception {
log.info("Starting LS as standlone server port = {}", SERVER_STANDALONE_PORT);
int serverPort = properties.getStandalonePort();
log.info("Starting LS as standlone server port = {}", serverPort);
Function<MessageConsumer, MessageConsumer> wrapper = consumer -> {
MessageConsumer result = consumer;
@@ -170,7 +171,7 @@ public class LanguageServerRunner implements CommandLineRunner {
SimpleLanguageServer languageServer = createServer();
Launcher<STS4LanguageClient> launcher = createSocketLauncher(languageServer, STS4LanguageClient.class,
new InetSocketAddress("localhost", SERVER_STANDALONE_PORT), createServerThreads(), wrapper);
new InetSocketAddress("localhost", serverPort), createServerThreads(), wrapper);
languageServer.connect(launcher.getRemoteProxy());
launcher.startListening().get();

View File

@@ -0,0 +1,35 @@
package org.springframework.ide.vscode.commons.languageserver.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("languageserver")
public class LanguageServerProperties {
/**
* Enables 'standalone' launch mode. In standalone mode the language server
* creates a server socket and waits for a client to connect on that socket.
*/
private boolean standalone = true;
/**
* The port on which a standalone language server listens. This setting
* is ignored if the server is not launched in standalone mode.
*/
private int standalonePort = 5007;
public boolean isStandalone() {
return standalone;
}
public void setStandalone(boolean standalone) {
this.standalone = standalone;
}
public int getStandalonePort() {
return standalonePort;
}
public void setStandalonePort(int standalonePort) {
this.standalonePort = standalonePort;
}
}

View File

@@ -1,21 +1,35 @@
/*******************************************************************************
* Copyright (c) 2018 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.ide.vscode.languageserver.starter;
import javax.inject.Provider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ide.vscode.commons.languageserver.LanguageServerRunner;
import org.springframework.ide.vscode.commons.languageserver.config.LanguageServerProperties;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
@Configuration
@EnableConfigurationProperties(LanguageServerProperties.class)
public class LanguageServerAutoconf {
@Bean public LanguageServerRunner serverApp(
@Qualifier("serverName") String serverName,
@Qualifier("serverName") String serverName,
LanguageServerProperties properties,
Provider<SimpleLanguageServer> languageServerFactory
) {
return new LanguageServerRunner(serverName, languageServerFactory);
return new LanguageServerRunner(serverName, properties, languageServerFactory);
}
}