From a34eb146687b640224906a9ac18b7d70ec0dc876 Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Sun, 17 May 2020 19:51:19 +0200 Subject: [PATCH] Simplify lifecycle by using constructor injection (#1479) Signed-off-by: Christoph Weitkamp --- .../lsp/internal/MappingUriExtensions.java | 41 ++++++++----------- .../core/model/lsp/internal/ModelServer.java | 35 ++++++---------- 2 files changed, 30 insertions(+), 46 deletions(-) diff --git a/bundles/org.openhab.core.model.lsp/src/main/java/org/openhab/core/model/lsp/internal/MappingUriExtensions.java b/bundles/org.openhab.core.model.lsp/src/main/java/org/openhab/core/model/lsp/internal/MappingUriExtensions.java index ddfe180b1..0f7974033 100644 --- a/bundles/org.openhab.core.model.lsp/src/main/java/org/openhab/core/model/lsp/internal/MappingUriExtensions.java +++ b/bundles/org.openhab.core.model.lsp/src/main/java/org/openhab/core/model/lsp/internal/MappingUriExtensions.java @@ -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)) { diff --git a/bundles/org.openhab.core.model.lsp/src/main/java/org/openhab/core/model/lsp/internal/ModelServer.java b/bundles/org.openhab.core.model.lsp/src/main/java/org/openhab/core/model/lsp/internal/ModelServer.java index d5e8e6ee5..730357437 100644 --- a/bundles/org.openhab.core.model.lsp/src/main/java/org/openhab/core/model/lsp/internal/ModelServer.java +++ b/bundles/org.openhab.core.model.lsp/src/main/java/org/openhab/core/model/lsp/internal/ModelServer.java @@ -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 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; - } }