org.eclipse.lsp4j.jsonrpc.Launcher类的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(15.9k)|赞(0)|评价(0)|浏览(323)

本文整理了Java中org.eclipse.lsp4j.jsonrpc.Launcher类的一些代码示例,展示了Launcher类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Launcher类的具体详情如下:
包路径:org.eclipse.lsp4j.jsonrpc.Launcher
类名称:Launcher

Launcher介绍

[英]This is the entry point for applications that use LSP4J. A Launcher does all the wiring that is necessary to connect your endpoint via an input stream and an output stream.
[中]这是使用LSP4J的应用程序的入口点。启动器完成通过输入流和输出流连接端点所需的所有连接。

代码示例

代码示例来源:origin: org.eclipse.che.core/che-core-api-languageserver

  1. @Override
  2. public LanguageServer get(LanguageClient client, InputStream in, OutputStream out) {
  3. Launcher<LanguageServer> launcher = createLauncher(client, LanguageServer.class, in, out);
  4. LOG.debug("Created launcher for language server");
  5. launcher.startListening();
  6. LOG.debug("Started listening");
  7. LanguageServer remoteProxy = launcher.getRemoteProxy();
  8. LOG.debug("Got remote proxy");
  9. return remoteProxy;
  10. }

代码示例来源:origin: eclipse/eclipse.jdt.ls

  1. private void startConnection() throws IOException {
  2. Launcher<JavaLanguageClient> launcher;
  3. ExecutorService executorService = Executors.newCachedThreadPool();
  4. protocol = new JDTLanguageServer(projectsManager, preferenceManager);
  5. if (JDTEnvironmentUtils.inSocketStreamDebugMode()) {
  6. String host = JDTEnvironmentUtils.getClientHost();
  7. Integer port = JDTEnvironmentUtils.getClientPort();
  8. InetSocketAddress inetSocketAddress = new InetSocketAddress(host, port);
  9. AsynchronousServerSocketChannel serverSocket = AsynchronousServerSocketChannel.open().bind(inetSocketAddress);
  10. try {
  11. AsynchronousSocketChannel socketChannel = serverSocket.accept().get();
  12. InputStream in = Channels.newInputStream(socketChannel);
  13. OutputStream out = Channels.newOutputStream(socketChannel);
  14. Function<MessageConsumer, MessageConsumer> messageConsumer = it -> it;
  15. launcher = Launcher.createIoLauncher(protocol, JavaLanguageClient.class, in, out, executorService, messageConsumer);
  16. } catch (InterruptedException | ExecutionException e) {
  17. throw new RuntimeException("Error when opening a socket channel at " + host + ":" + port + ".", e);
  18. }
  19. } else {
  20. ConnectionStreamFactory connectionFactory = new ConnectionStreamFactory();
  21. InputStream in = connectionFactory.getInputStream();
  22. OutputStream out = connectionFactory.getOutputStream();
  23. Function<MessageConsumer, MessageConsumer> wrapper = new ParentProcessWatcher(this.languageServer);
  24. launcher = Launcher.createLauncher(protocol, JavaLanguageClient.class, in, out, executorService, wrapper);
  25. }
  26. protocol.connectClient(launcher.getRemoteProxy());
  27. launcher.startListening();
  28. }

代码示例来源:origin: openhab/openhab-core

  1. private void handleConnection(final Socket client) {
  2. logger.debug("Client {} connected", client.getRemoteSocketAddress());
  3. try {
  4. LanguageServerImpl languageServer = injector.getInstance(LanguageServerImpl.class);
  5. Launcher<LanguageClient> launcher = LSPLauncher.createServerLauncher(languageServer,
  6. client.getInputStream(), client.getOutputStream());
  7. languageServer.connect(launcher.getRemoteProxy());
  8. Future<?> future = launcher.startListening();
  9. future.get();
  10. } catch (IOException e) {
  11. logger.warn("Error communicating with LSP client {}", client.getRemoteSocketAddress());
  12. } catch (InterruptedException e) {
  13. // go on, let the thread finish
  14. } catch (ExecutionException e) {
  15. logger.error("Error running the Language Server", e);
  16. }
  17. logger.debug("Client {} disconnected", client.getRemoteSocketAddress());
  18. }

代码示例来源:origin: com.eclipsesource.glsp/glsp-server

  1. public void run() throws IOException, InterruptedException, ExecutionException {
  2. Injector injector = Guice.createInjector(module);
  3. GsonConfigurator gsonConf = injector.getInstance(GsonConfigurator.class);
  4. AsynchronousServerSocketChannel serverSocket = AsynchronousServerSocketChannel.open()
  5. .bind(new InetSocketAddress(host, port));
  6. ExecutorService threadPool = Executors.newCachedThreadPool();
  7. log.info("The graphical server launcher is ready to accept new client requests");
  8. while (true) {
  9. AsynchronousSocketChannel socketChannel = serverSocket.accept().get();
  10. InputStream in = Channels.newInputStream(socketChannel);
  11. OutputStream out = Channels.newOutputStream(socketChannel);
  12. Consumer<GsonBuilder> configureGson = (GsonBuilder builder) -> gsonConf.configureGsonBuilder(builder);
  13. Function<MessageConsumer, MessageConsumer> wrapper = (MessageConsumer it) -> {
  14. return it;
  15. };
  16. GLSPServer languageServer = injector.getInstance(GLSPServer.class);
  17. Launcher<GLSPClient> launcher = Launcher.createIoLauncher(languageServer,
  18. GLSPClient.class, in, out, threadPool, wrapper, configureGson);
  19. languageServer.connect(launcher.getRemoteProxy());
  20. launcher.startListening();
  21. log.info("Started language server for client " + socketChannel.getRemoteAddress());
  22. }
  23. }
  24. }

代码示例来源:origin: eclipse/lsp4j

  1. @Test public void testDone() throws Exception {
  2. A a = new A() {
  3. @Override
  4. public void say(Param p) {
  5. }
  6. };
  7. Launcher<A> launcher = Launcher.createLauncher(a, A.class, new ByteArrayInputStream("".getBytes()), new ByteArrayOutputStream());
  8. Future<?> startListening = launcher.startListening();
  9. startListening.get(TIMEOUT, TimeUnit.MILLISECONDS);
  10. Assert.assertTrue(startListening.isDone());
  11. Assert.assertFalse(startListening.isCancelled());
  12. }

代码示例来源:origin: eclipse/lsp4j

  1. @Test public void testDone() throws Exception {
  2. A a = new A() {
  3. @Override
  4. public void say(Param p) {
  5. }
  6. };
  7. Launcher<A> launcher = DebugLauncher.createLauncher(a, A.class, new ByteArrayInputStream("".getBytes()), new ByteArrayOutputStream());
  8. Future<?> startListening = launcher.startListening();
  9. startListening.get(TIMEOUT, TimeUnit.MILLISECONDS);
  10. Assert.assertTrue(startListening.isDone());
  11. Assert.assertFalse(startListening.isCancelled());
  12. }

代码示例来源:origin: eclipse/lsp4j

  1. @Test public void testCustomGson() throws Exception {
  2. A a = new A() {
  3. @Override
  4. public void say(Param p) {
  5. }
  6. };
  7. ByteArrayOutputStream out = new ByteArrayOutputStream();
  8. TypeAdapter<Param> typeAdapter = new TypeAdapter<Param>() {
  9. @Override
  10. public void write(JsonWriter out, Param value) throws IOException {
  11. out.beginObject();
  12. out.name("message");
  13. out.value("bar");
  14. out.endObject();
  15. }
  16. @Override
  17. public Param read(JsonReader in) throws IOException {
  18. return null;
  19. }
  20. };
  21. Launcher<A> launcher = DebugLauncher.createIoLauncher(a, A.class, new ByteArrayInputStream("".getBytes()), out,
  22. Executors.newCachedThreadPool(), c -> c,
  23. gsonBuilder -> {gsonBuilder.registerTypeAdapter(Param.class, typeAdapter);});
  24. A remoteProxy = launcher.getRemoteProxy();
  25. remoteProxy.say(new Param("foo"));
  26. Assert.assertEquals("Content-Length: 63\r\n\r\n" +
  27. "{\"type\":\"event\",\"seq\":1,\"event\":\"say\",\"body\":{\"message\":\"bar\"}}",
  28. out.toString());
  29. }

代码示例来源:origin: eclipse/lsp4j

  1. Launcher<A> launcher = Launcher.createIoLauncher(a, A.class, new ByteArrayInputStream("".getBytes()), out,
  2. Executors.newCachedThreadPool(), c -> c,
  3. gsonBuilder -> {gsonBuilder.registerTypeAdapter(Param.class, typeAdapter);});
  4. A remoteProxy = launcher.getRemoteProxy();

代码示例来源:origin: eclipse/lsp4j

  1. /**
  2. * Create a new Launcher for a given local service object, a given remote interface and an input and output stream.
  3. * Threads are started with the given executor service. The wrapper function is applied to the incoming and
  4. * outgoing message streams so additional message handling such as validation and tracing can be included.
  5. *
  6. * @param localService - the object that receives method calls from the remote service
  7. * @param remoteInterface - an interface on which RPC methods are looked up
  8. * @param in - input stream to listen for incoming messages
  9. * @param out - output stream to send outgoing messages
  10. * @param executorService - the executor service used to start threads
  11. * @param wrapper - a function for plugging in additional message consumers
  12. */
  13. static <T> Launcher<T> createLauncher(Object localService, Class<T> remoteInterface, InputStream in, OutputStream out,
  14. ExecutorService executorService, Function<MessageConsumer, MessageConsumer> wrapper) {
  15. return createIoLauncher(localService, remoteInterface, in, out, executorService, wrapper);
  16. }

代码示例来源:origin: palantir/language-servers

  1. @SuppressFBWarnings("DM_DEFAULT_ENCODING")
  2. public void launch() {
  3. Function<MessageConsumer, MessageConsumer> loggingMessageConsumerAdapter = consumer -> {
  4. return message -> {
  5. log.debug("Message received to client: {}", message);
  6. consumer.consume(message);
  7. };
  8. };
  9. Launcher<LanguageClient> serverLauncher = LSPLauncher.createServerLauncher(
  10. languageServer,
  11. inputStream,
  12. outputStream,
  13. Executors.newCachedThreadPool(),
  14. loggingMessageConsumerAdapter);
  15. if (languageServer instanceof LanguageClientAware) {
  16. LanguageClient client = serverLauncher.getRemoteProxy();
  17. ((LanguageClientAware) languageServer).connect(client);
  18. }
  19. serverLauncher.startListening();
  20. }
  21. }

代码示例来源:origin: eclipse/lsp4j

  1. @Test
  2. public void testResponse2() throws Exception {
  3. // create client message
  4. String requestMessage = "{\"jsonrpc\": \"2.0\",\n"
  5. + "\"id\": 42,\n"
  6. + "\"method\": \"askServer\",\n"
  7. + "\"params\": { \"value\": \"bar\" }\n"
  8. + "}";
  9. String clientMessage = getHeader(requestMessage.getBytes().length) + requestMessage;
  10. // create server side
  11. ByteArrayInputStream in = new ByteArrayInputStream(clientMessage.getBytes());
  12. ByteArrayOutputStream out = new ByteArrayOutputStream();
  13. MyServer server = new MyServerImpl();
  14. Launcher<MyClient> serverSideLauncher = Launcher.createLauncher(server, MyClient.class, in, out);
  15. serverSideLauncher.startListening().get(TIMEOUT, TimeUnit.MILLISECONDS);
  16. Assert.assertEquals("Content-Length: 50" + CRLF + CRLF
  17. + "{\"jsonrpc\":\"2.0\",\"id\":42,\"result\":{\"value\":\"bar\"}}",
  18. out.toString());
  19. }

代码示例来源:origin: eclipse/lsp4j

  1. Launcher<Object> launcher = Launcher.createIoLauncher(Arrays.asList(a, b), Arrays.asList(A.class, B.class),
  2. classLoader, in, out, Executors.newCachedThreadPool(), c -> c, null);
  3. launcher.startListening().get(TIMEOUT, TimeUnit.MILLISECONDS);
  4. assertEquals("foo1", paramA[0]);
  5. assertEquals("bar1", paramB[0]);
  6. Object remoteProxy = launcher.getRemoteProxy();
  7. ((A) remoteProxy).say(new Param("foo2"));
  8. ((B) remoteProxy).ask(new Param("bar2"));

代码示例来源:origin: eclipse/lsp4j

  1. @Test
  2. public void testDebugServerCanBeLaunched() throws IOException {
  3. TestDebugServer testDebugServer = new TestDebugServer();
  4. Launcher<IDebugProtocolClient> launcher = DSPLauncher.createServerLauncher(testDebugServer,
  5. new PipedInputStream(), new PipedOutputStream());
  6. Future<Void> listening = launcher.startListening();
  7. listening.cancel(true);
  8. }
  9. }

代码示例来源:origin: eclipse/lsp4j

  1. @Test
  2. public void testNotification() throws IOException {
  3. OutputEventArguments p = new OutputEventArguments();
  4. p.setOutput("Hello World");
  5. client.expectedNotifications.put("output", p);
  6. serverLauncher.getRemoteProxy().output(p);
  7. client.joinOnEmpty();
  8. }

代码示例来源:origin: org.eclipse.lsp4j/org.eclipse.lsp4j.jsonrpc

  1. /**
  2. * Create a new Launcher for a given local service object, a given remote interface and an input and output stream.
  3. * Threads are started with the given executor service. The wrapper function is applied to the incoming and
  4. * outgoing message streams so additional message handling such as validation and tracing can be included.
  5. *
  6. * @param localService - the object that receives method calls from the remote service
  7. * @param remoteInterface - an interface on which RPC methods are looked up
  8. * @param in - input stream to listen for incoming messages
  9. * @param out - output stream to send outgoing messages
  10. * @param executorService - the executor service used to start threads
  11. * @param wrapper - a function for plugging in additional message consumers
  12. */
  13. static <T> Launcher<T> createLauncher(Object localService, Class<T> remoteInterface, InputStream in, OutputStream out,
  14. ExecutorService executorService, Function<MessageConsumer, MessageConsumer> wrapper) {
  15. return createIoLauncher(localService, remoteInterface, in, out, executorService, wrapper);
  16. }

代码示例来源:origin: spring-projects/sts4

  1. private Future<Void> runAsync(Connection connection) throws Exception {
  2. LanguageServer server = this.languageServer;
  3. ExecutorService executor = createServerThreads();
  4. Function<MessageConsumer, MessageConsumer> wrapper = (MessageConsumer consumer) -> {
  5. return (msg) -> {
  6. try {
  7. consumer.consume(msg);
  8. } catch (UnsupportedOperationException e) {
  9. //log a warning and ignore. We are getting some messages from vsCode the server doesn't know about
  10. log.warn("Unsupported message was ignored!", e);
  11. }
  12. };
  13. };
  14. Launcher<STS4LanguageClient> launcher = Launcher.createLauncher(server,
  15. STS4LanguageClient.class,
  16. connection.in,
  17. connection.out,
  18. executor,
  19. wrapper
  20. );
  21. if (server instanceof LanguageClientAware) {
  22. LanguageClient client = launcher.getRemoteProxy();
  23. ((LanguageClientAware) server).connect(client);
  24. }
  25. return launcher.startListening();
  26. }

代码示例来源:origin: spring-projects/sts4

  1. /**
  2. * starts up the language server and let it listen for connections from the outside
  3. * instead of connecting itself to an existing port or channel.
  4. *
  5. * This is meant for development only, to reduce turnaround times while working
  6. * on the language server from within an IDE, so that you can start the language
  7. * server right away in debug mode and let the vscode extension connect to that
  8. * instance instead of vice versa.
  9. *
  10. * Source of inspiration:
  11. * https://github.com/itemis/xtext-languageserver-example/blob/master/org.xtext.example.mydsl.ide/src/org/xtext/example/mydsl/ide/RunServer.java
  12. */
  13. public void startAsServer() throws Exception {
  14. int serverPort = properties.getStandalonePort();
  15. log.info("Starting LS as standlone server port = {}", serverPort);
  16. Function<MessageConsumer, MessageConsumer> wrapper = consumer -> {
  17. MessageConsumer result = consumer;
  18. return result;
  19. };
  20. Launcher<STS4LanguageClient> launcher = createSocketLauncher(languageServer, STS4LanguageClient.class,
  21. new InetSocketAddress("localhost", serverPort), createServerThreads(), wrapper);
  22. languageServer.connect(launcher.getRemoteProxy());
  23. launcher.startListening().get();
  24. }

代码示例来源:origin: eclipse/lsp4j

  1. @Test
  2. public void testEither() throws Exception {
  3. // create client message
  4. String requestMessage = "{\"jsonrpc\": \"2.0\",\n"
  5. + "\"id\": 42,\n"
  6. + "\"method\": \"askServer\",\n"
  7. + "\"params\": { \"either\": \"bar\", \"value\": \"foo\" }\n"
  8. + "}";
  9. String clientMessage = getHeader(requestMessage.getBytes().length) + requestMessage;
  10. // create server side
  11. ByteArrayInputStream in = new ByteArrayInputStream(clientMessage.getBytes());
  12. ByteArrayOutputStream out = new ByteArrayOutputStream();
  13. MyServer server = new MyServerImpl();
  14. Launcher<MyClient> serverSideLauncher = Launcher.createLauncher(server, MyClient.class, in, out);
  15. serverSideLauncher.startListening().get(TIMEOUT, TimeUnit.MILLISECONDS);
  16. Assert.assertEquals("Content-Length: 65" + CRLF + CRLF
  17. + "{\"jsonrpc\":\"2.0\",\"id\":42,\"result\":{\"value\":\"foo\",\"either\":\"bar\"}}",
  18. out.toString());
  19. }

代码示例来源:origin: eclipse/lsp4j

  1. @Test
  2. public void testResponse() throws Exception {
  3. String clientMessage = "{\"type\":\"request\","
  4. + "\"seq\":1,\n"
  5. + "\"command\":\"askServer\",\n"
  6. + " \"arguments\": { value: \"bar\" }\n"
  7. + "}";
  8. String clientMessages = getHeader(clientMessage.getBytes().length) + clientMessage;
  9. // create server side
  10. ByteArrayInputStream in = new ByteArrayInputStream(clientMessages.getBytes());
  11. ByteArrayOutputStream out = new ByteArrayOutputStream();
  12. MyServer server = new MyServer() {
  13. @Override
  14. public CompletableFuture<MyParam> askServer(MyParam param) {
  15. return CompletableFuture.completedFuture(param);
  16. }
  17. };
  18. Launcher<MyClient> serverSideLauncher = DebugLauncher.createLauncher(server, MyClient.class, in, out);
  19. serverSideLauncher.startListening().get(TIMEOUT, TimeUnit.MILLISECONDS);
  20. Assert.assertEquals("Content-Length: 103\r\n\r\n" +
  21. "{\"type\":\"response\",\"seq\":1,\"request_seq\":1,\"command\":\"askServer\",\"success\":true,\"body\":{\"value\":\"bar\"}}",
  22. out.toString());
  23. }

代码示例来源:origin: eclipse/lsp4j

  1. @Test public void testNotification() throws IOException {
  2. MessageParams p = new MessageParams();
  3. p.setMessage("Hello World");
  4. p.setType(MessageType.Info);
  5. client.expectedNotifications.put("window/logMessage", p);
  6. serverLauncher.getRemoteProxy().logMessage(p);
  7. client.joinOnEmpty();
  8. }

相关文章