|
| 1 | +/****************************************************************************** |
| 2 | + * Copyright (c) 2019 TypeFox and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0, |
| 7 | + * or the Eclipse Distribution License v. 1.0 which is available at |
| 8 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 9 | + * |
| 10 | + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
| 11 | + ******************************************************************************/ |
| 12 | +package org.eclipse.lsp4j.websocket; |
| 13 | + |
| 14 | +import java.util.Collection; |
| 15 | + |
| 16 | +import javax.websocket.Session; |
| 17 | + |
| 18 | +import org.eclipse.lsp4j.jsonrpc.Endpoint; |
| 19 | +import org.eclipse.lsp4j.jsonrpc.Launcher; |
| 20 | +import org.eclipse.lsp4j.jsonrpc.MessageConsumer; |
| 21 | +import org.eclipse.lsp4j.jsonrpc.RemoteEndpoint; |
| 22 | +import org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler; |
| 23 | +import org.eclipse.lsp4j.jsonrpc.services.ServiceEndpoints; |
| 24 | + |
| 25 | +/** |
| 26 | + * JSON-RPC launcher builder for use in {@link WebSocketEndpoint}. |
| 27 | + * |
| 28 | + * @param <T> remote service interface type |
| 29 | + */ |
| 30 | +public class WebSocketLauncherBuilder<T> extends Launcher.Builder<T> { |
| 31 | + |
| 32 | + protected Session session; |
| 33 | + |
| 34 | + public Collection<Object> getLocalServices() { |
| 35 | + return localServices; |
| 36 | + } |
| 37 | + |
| 38 | + public WebSocketLauncherBuilder<T> setSession(Session session) { |
| 39 | + this.session = session; |
| 40 | + return this; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public Launcher<T> create() { |
| 45 | + if (localServices == null) |
| 46 | + throw new IllegalStateException("Local service must be configured."); |
| 47 | + if (remoteInterfaces == null) |
| 48 | + throw new IllegalStateException("Remote interface must be configured."); |
| 49 | + |
| 50 | + MessageJsonHandler jsonHandler = createJsonHandler(); |
| 51 | + RemoteEndpoint remoteEndpoint = createRemoteEndpoint(jsonHandler); |
| 52 | + addMessageHandlers(jsonHandler, remoteEndpoint); |
| 53 | + T remoteProxy = createProxy(remoteEndpoint); |
| 54 | + return createLauncher(null, remoteProxy, remoteEndpoint, null); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected RemoteEndpoint createRemoteEndpoint(MessageJsonHandler jsonHandler) { |
| 59 | + MessageConsumer outgoingMessageStream = new WebSocketMessageConsumer(session, jsonHandler); |
| 60 | + outgoingMessageStream = wrapMessageConsumer(outgoingMessageStream); |
| 61 | + Endpoint localEndpoint = ServiceEndpoints.toEndpoint(localServices); |
| 62 | + RemoteEndpoint remoteEndpoint; |
| 63 | + if (exceptionHandler == null) |
| 64 | + remoteEndpoint = new RemoteEndpoint(outgoingMessageStream, localEndpoint); |
| 65 | + else |
| 66 | + remoteEndpoint = new RemoteEndpoint(outgoingMessageStream, localEndpoint, exceptionHandler); |
| 67 | + jsonHandler.setMethodProvider(remoteEndpoint); |
| 68 | + return remoteEndpoint; |
| 69 | + } |
| 70 | + |
| 71 | + protected void addMessageHandlers(MessageJsonHandler jsonHandler, RemoteEndpoint remoteEndpoint) { |
| 72 | + MessageConsumer messageConsumer = wrapMessageConsumer(remoteEndpoint); |
| 73 | + session.addMessageHandler(new WebSocketMessageHandler(messageConsumer, jsonHandler, remoteEndpoint)); |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments