Search in sources :

Example 1 with Launcher

use of org.eclipse.lsp4j.jsonrpc.Launcher in project sonarlint-core by SonarSource.

the class ServerMainTest method startServer.

@BeforeClass
public static void startServer() throws Exception {
    System.out.println("Max memory: " + Runtime.getRuntime().maxMemory());
    System.setProperty(SonarLintTelemetry.DISABLE_PROPERTY_KEY, "true");
    serverSocket = new ServerSocket(0);
    int port = serverSocket.getLocalPort();
    client = new FakeLanguageClient();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Callable<LanguageServer> callable = () -> {
        Socket socket = serverSocket.accept();
        Launcher<LanguageServer> launcher = LSPLauncher.createClientLauncher(client, socket.getInputStream(), socket.getOutputStream());
        launcher.startListening();
        return launcher.getRemoteProxy();
    };
    Future<LanguageServer> future = executor.submit(callable);
    executor.shutdown();
    String js = new File("target/plugins/javascript.jar").getAbsoluteFile().toURI().toURL().toString();
    String php = new File("target/plugins/php.jar").getAbsoluteFile().toURI().toURL().toString();
    String py = new File("target/plugins/python.jar").getAbsoluteFile().toURI().toURL().toString();
    String ts = new File("target/plugins/typescript.jar").getAbsoluteFile().toURI().toURL().toString();
    Path fakeTypeScriptProjectPath = globalTemp.newFolder().toPath();
    Path packagejson = fakeTypeScriptProjectPath.resolve("package.json");
    FileUtils.write(packagejson.toFile(), "{" + "\"devDependencies\": {\n" + "    \"typescript\": \"2.6.1\"\n" + "  }" + "}", StandardCharsets.UTF_8);
    ProcessBuilder pb = new ProcessBuilder("npm" + (SystemUtils.IS_OS_WINDOWS ? ".cmd" : ""), "install").directory(fakeTypeScriptProjectPath.toFile()).inheritIO();
    Process process = pb.start();
    if (process.waitFor() != 0) {
        fail("Unable to run npm install");
    }
    try {
        ServerMain.main("" + port, js, php, py, ts);
    } catch (Exception e) {
        e.printStackTrace();
        future.get(1, TimeUnit.SECONDS);
        if (!future.isDone()) {
            future.cancel(true);
        }
        throw e;
    }
    lsProxy = future.get();
    InitializeParams initializeParams = new InitializeParams();
    initializeParams.setInitializationOptions(ImmutableMap.builder().put(TEST_FILE_PATTERN, "{**/test/**,**/*test*,**/*Test*}").put(TYPESCRIPT_LOCATION, fakeTypeScriptProjectPath.resolve("node_modules").toString()).put(DISABLE_TELEMETRY, true).put("telemetryStorage", "not/exists").put("productName", "SLCORE tests").put("productVersion", "0.1").build());
    lsProxy.initialize(initializeParams).get();
}
Also used : Path(java.nio.file.Path) ServerSocket(java.net.ServerSocket) InitializeParams(org.eclipse.lsp4j.InitializeParams) IOException(java.io.IOException) LanguageServer(org.eclipse.lsp4j.services.LanguageServer) ExecutorService(java.util.concurrent.ExecutorService) Launcher(org.eclipse.lsp4j.jsonrpc.Launcher) LSPLauncher(org.eclipse.lsp4j.launch.LSPLauncher) File(java.io.File) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) BeforeClass(org.junit.BeforeClass)

Example 2 with Launcher

use of org.eclipse.lsp4j.jsonrpc.Launcher in project ballerina by ballerina-lang.

the class BallerinaLangServerService method launchRPCServer.

private <T> Launcher<T> launchRPCServer(Object localService, Class<T> remoteInterface) {
    Consumer<GsonBuilder> configureGson = (gsonBuilder) -> {
    };
    Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap();
    supportedMethods.putAll(ServiceEndpoints.getSupportedMethods(remoteInterface));
    if (localService instanceof JsonRpcMethodProvider) {
        JsonRpcMethodProvider rpcMethodProvider = (JsonRpcMethodProvider) localService;
        supportedMethods.putAll(rpcMethodProvider.supportedMethods());
    } else {
        supportedMethods.putAll(ServiceEndpoints.getSupportedMethods(localService.getClass()));
    }
    MessageJsonHandler jsonHandler = new MessageJsonHandler(supportedMethods, configureGson);
    MessageConsumer outGoingMessageStream = new WSRPCMessageConsumer(this, jsonHandler);
    RemoteEndpoint serverEndpoint = new RemoteEndpoint(outGoingMessageStream, ServiceEndpoints.toEndpoint(localService));
    jsonHandler.setMethodProvider(serverEndpoint);
    final MessageConsumer messageConsumer = serverEndpoint;
    final MessageProducer reader = new WSRPCMessageProducer(this, jsonHandler);
    final T remoteProxy = ServiceEndpoints.toServiceObject(serverEndpoint, remoteInterface);
    return new Launcher<T>() {

        public Future<?> startListening() {
            return ConcurrentMessageProcessor.startProcessing(reader, messageConsumer, Executors.newCachedThreadPool());
        }

        public T getRemoteProxy() {
            return remoteProxy;
        }
    };
}
Also used : ConcurrentMessageProcessor(org.eclipse.lsp4j.jsonrpc.json.ConcurrentMessageProcessor) LanguageClient(org.eclipse.lsp4j.services.LanguageClient) OnError(javax.websocket.OnError) CloseReason(javax.websocket.CloseReason) OnMessage(javax.websocket.OnMessage) LoggerFactory(org.slf4j.LoggerFactory) Constants(org.ballerinalang.composer.service.ballerina.langserver.Constants) MessageProducer(org.eclipse.lsp4j.jsonrpc.MessageProducer) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) OnOpen(javax.websocket.OnOpen) ComposerService(org.ballerinalang.composer.server.spi.ComposerService) Future(java.util.concurrent.Future) OnClose(javax.websocket.OnClose) Map(java.util.Map) LinkedList(java.util.LinkedList) Launcher(org.eclipse.lsp4j.jsonrpc.Launcher) ServiceType(org.ballerinalang.composer.server.spi.ServiceType) Logger(org.slf4j.Logger) Session(javax.websocket.Session) ServiceEndpoints(org.eclipse.lsp4j.jsonrpc.services.ServiceEndpoints) MessageJsonHandler(org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler) IOException(java.io.IOException) ServerEndpoint(javax.websocket.server.ServerEndpoint) MessageConsumer(org.eclipse.lsp4j.jsonrpc.MessageConsumer) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) List(java.util.List) ServerConstants(org.ballerinalang.composer.server.core.ServerConstants) JsonRpcMethod(org.eclipse.lsp4j.jsonrpc.json.JsonRpcMethod) ServiceInfo(org.ballerinalang.composer.server.spi.ServiceInfo) JsonRpcMethodProvider(org.eclipse.lsp4j.jsonrpc.json.JsonRpcMethodProvider) BallerinaLanguageServer(org.ballerinalang.langserver.BallerinaLanguageServer) RemoteEndpoint(org.eclipse.lsp4j.jsonrpc.RemoteEndpoint) JsonRpcMethodProvider(org.eclipse.lsp4j.jsonrpc.json.JsonRpcMethodProvider) MessageConsumer(org.eclipse.lsp4j.jsonrpc.MessageConsumer) MessageJsonHandler(org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler) GsonBuilder(com.google.gson.GsonBuilder) RemoteEndpoint(org.eclipse.lsp4j.jsonrpc.RemoteEndpoint) LinkedHashMap(java.util.LinkedHashMap) JsonRpcMethod(org.eclipse.lsp4j.jsonrpc.json.JsonRpcMethod) Launcher(org.eclipse.lsp4j.jsonrpc.Launcher) MessageProducer(org.eclipse.lsp4j.jsonrpc.MessageProducer)

Example 3 with Launcher

use of org.eclipse.lsp4j.jsonrpc.Launcher in project sts4 by spring-projects.

the class LaunguageServerApp method startAsServer.

/**
 * starts up the language server and let it listen for connections from the outside
 * instead of connecting itself to an existing port or channel.
 *
 * This is meant for development only, to reduce turnaround times while working
 * on the language server from within an IDE, so that you can start the language
 * server right away in debug mode and let the vscode extension connect to that
 * instance instead of vice versa.
 *
 * Source of inspiration:
 * 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 IOException, InterruptedException {
    Log.info("Starting LS as standlone server");
    Function<MessageConsumer, MessageConsumer> wrapper = consumer -> {
        MessageConsumer result = consumer;
        return result;
    };
    SimpleLanguageServer languageServer = createServer();
    Launcher<STS4LanguageClient> launcher = createSocketLauncher(languageServer, STS4LanguageClient.class, new InetSocketAddress("localhost", SERVER_STANDALONE_PORT), createServerThreads(), wrapper);
    languageServer.connect(launcher.getRemoteProxy());
    Future<?> future = launcher.startListening();
    while (!future.isDone()) {
        Thread.sleep(10_000l);
    }
}
Also used : OutputStream(java.io.OutputStream) PrintStream(java.io.PrintStream) AsynchronousServerSocketChannel(java.nio.channels.AsynchronousServerSocketChannel) LanguageClientAware(org.eclipse.lsp4j.services.LanguageClientAware) Socket(java.net.Socket) Launcher(org.eclipse.lsp4j.jsonrpc.Launcher) LanguageClient(org.eclipse.lsp4j.services.LanguageClient) SocketAddress(java.net.SocketAddress) Provider(javax.inject.Provider) Log(org.springframework.ide.vscode.commons.util.Log) Channels(java.nio.channels.Channels) IOException(java.io.IOException) LanguageServer(org.eclipse.lsp4j.services.LanguageServer) InetSocketAddress(java.net.InetSocketAddress) Function(java.util.function.Function) Executors(java.util.concurrent.Executors) MessageConsumer(org.eclipse.lsp4j.jsonrpc.MessageConsumer) ExecutionException(java.util.concurrent.ExecutionException) Future(java.util.concurrent.Future) LoggingFormat(org.springframework.ide.vscode.commons.languageserver.util.LoggingFormat) ExecutorService(java.util.concurrent.ExecutorService) InputStream(java.io.InputStream) AsynchronousSocketChannel(java.nio.channels.AsynchronousSocketChannel) SimpleLanguageServer(org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer) MessageConsumer(org.eclipse.lsp4j.jsonrpc.MessageConsumer) SimpleLanguageServer(org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer) InetSocketAddress(java.net.InetSocketAddress)

Aggregations

IOException (java.io.IOException)3 Launcher (org.eclipse.lsp4j.jsonrpc.Launcher)3 Socket (java.net.Socket)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorService (java.util.concurrent.ExecutorService)2 Executors (java.util.concurrent.Executors)2 Future (java.util.concurrent.Future)2 MessageConsumer (org.eclipse.lsp4j.jsonrpc.MessageConsumer)2 LanguageClient (org.eclipse.lsp4j.services.LanguageClient)2 LanguageServer (org.eclipse.lsp4j.services.LanguageServer)2 GsonBuilder (com.google.gson.GsonBuilder)1 File (java.io.File)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocket (java.net.ServerSocket)1 SocketAddress (java.net.SocketAddress)1 AsynchronousServerSocketChannel (java.nio.channels.AsynchronousServerSocketChannel)1 AsynchronousSocketChannel (java.nio.channels.AsynchronousSocketChannel)1