Simplify lifecycle by using constructor injection (#1479)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp 2020-05-17 19:51:19 +02:00 committed by GitHub
parent 6e407d01e6
commit a34eb14668
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 46 deletions

View File

@ -13,15 +13,14 @@
package org.openhab.core.model.lsp.internal;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.xtext.ide.server.UriExtensions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -36,17 +35,16 @@ import org.slf4j.LoggerFactory;
*/
public class MappingUriExtensions extends UriExtensions {
private static final Charset PATH_ENCODING = StandardCharsets.UTF_8;
private final Logger logger = LoggerFactory.getLogger(MappingUriExtensions.class);
private final String serverLocation;
private String clientLocation = null;
private final String rawConfigFolder;
private final String serverLocation;
private @Nullable String clientLocation;
public MappingUriExtensions(String configFolder) {
this.rawConfigFolder = configFolder;
serverLocation = calcServerLocation(configFolder);
this.serverLocation = calcServerLocation(configFolder);
logger.debug("The language server is using '{}' as its workspace", serverLocation);
}
@ -79,23 +77,20 @@ public class MappingUriExtensions extends UriExtensions {
@Override
public URI toUri(String pathWithScheme) {
try {
String decodedPathWithScheme = URLDecoder.decode(pathWithScheme, PATH_ENCODING.toString());
String decodedPathWithScheme = URLDecoder.decode(pathWithScheme, StandardCharsets.UTF_8);
if (clientLocation != null && decodedPathWithScheme.startsWith(clientLocation)) {
return map(decodedPathWithScheme);
}
clientLocation = guessClientPath(decodedPathWithScheme);
if (clientLocation != null) {
logger.debug("Identified client workspace as '{}'", clientLocation);
return map(decodedPathWithScheme);
}
clientLocation = pathWithScheme;
} catch (UnsupportedEncodingException e) {
logger.error("Charset {} is not supported. You're seriously in trouble.", PATH_ENCODING);
if (clientLocation != null && decodedPathWithScheme.startsWith(clientLocation)) {
return map(decodedPathWithScheme);
}
clientLocation = guessClientPath(decodedPathWithScheme);
if (clientLocation != null) {
logger.debug("Identified client workspace as '{}'", clientLocation);
return map(decodedPathWithScheme);
}
clientLocation = pathWithScheme;
logger.debug("Path mapping could not be done for '{}', leaving it untouched", pathWithScheme);
java.net.URI javaNetUri = java.net.URI.create(pathWithScheme);
return URI.createURI(toPathAsInXtext212(javaNetUri));
@ -139,7 +134,7 @@ public class MappingUriExtensions extends UriExtensions {
* @param pathWithScheme the filename as coming from the client
* @return the substring which needs to be replaced with the runtime's config folder path
*/
protected String guessClientPath(String pathWithScheme) {
protected @Nullable String guessClientPath(String pathWithScheme) {
if (isPointingToConfigFolder(pathWithScheme)) {
return removeTrailingSlash(pathWithScheme);
} else if (isFolder(pathWithScheme)) {

View File

@ -20,6 +20,8 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.lsp4j.jsonrpc.Launcher;
import org.eclipse.lsp4j.launch.LSPLauncher;
import org.eclipse.lsp4j.services.LanguageClient;
@ -48,6 +50,7 @@ import com.google.inject.Injector;
Constants.SERVICE_PID + "=org.openhab.lsp", ConfigurableService.SERVICE_PROPERTY_DESCRIPTION_URI + "=misc:lsp",
ConfigurableService.SERVICE_PROPERTY_LABEL + "=Language Server (LSP)",
ConfigurableService.SERVICE_PROPERTY_CATEGORY + "=misc" })
@NonNullByDefault
public class ModelServer {
public static final String CONFIGURATION_PID = "org.openhab.lsp";
@ -56,11 +59,16 @@ public class ModelServer {
private final ExecutorService pool = ThreadPoolManager.getPool("lsp");
private final Logger logger = LoggerFactory.getLogger(ModelServer.class);
private ServerSocket socket;
private ScriptServiceUtil scriptServiceUtil;
private ScriptEngine scriptEngine;
private Injector injector;
private @Nullable ServerSocket socket;
private final Injector injector;
@Activate
public ModelServer(final @Reference ScriptServiceUtil scriptServiceUtil,
final @Reference ScriptEngine scriptEngine) {
this.injector = Guice.createInjector(new RuntimeServerModule(scriptServiceUtil, scriptEngine));
}
@Activate
public void activate(Map<String, Object> config) {
@ -72,7 +80,6 @@ public class ModelServer {
config.get(KEY_PORT), DEFAULT_PORT);
}
final int serverPort = port;
injector = Guice.createInjector(new RuntimeServerModule(scriptServiceUtil, scriptEngine));
pool.submit(() -> listen(serverPort));
}
@ -125,22 +132,4 @@ public class ModelServer {
}
logger.debug("Client {} disconnected", client.getRemoteSocketAddress());
}
@Reference
public void setScriptServiceUtil(ScriptServiceUtil scriptServiceUtil) {
this.scriptServiceUtil = scriptServiceUtil;
}
public void unsetScriptServiceUtil(ScriptServiceUtil scriptServiceUtil) {
this.scriptServiceUtil = null;
}
@Reference
public void setScriptEngine(ScriptEngine scriptEngine) {
this.scriptEngine = scriptEngine;
}
public void unsetScriptEngine(ScriptEngine scriptEngine) {
this.scriptEngine = null;
}
}