-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#203: Implemented WebSocket adapter for JSON-RPC
- Loading branch information
1 parent
781e226
commit 69a2b53
Showing
13 changed files
with
890 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>org.eclipse.lsp4j.websocket</name> | ||
<comment>Project org.eclipse.lsp4j.websocket created by Buildship.</comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.xtext.ui.shared.xtextBuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.buildship.core.gradleprojectbuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
<nature>org.eclipse.xtext.ui.shared.xtextNature</nature> | ||
<nature>org.eclipse.buildship.core.gradleprojectnature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/****************************************************************************** | ||
* Copyright (c) 2019 TypeFox and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, | ||
* or the Eclipse Distribution License v. 1.0 which is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause | ||
******************************************************************************/ | ||
|
||
ext.title = 'LSP4J WebSocket' | ||
description = 'WebSocket support for LSP4J' | ||
|
||
dependencies { | ||
compile project(":org.eclipse.lsp4j.jsonrpc") | ||
compile "javax.websocket:javax.websocket-api:${versions.websocket}" | ||
testCompile "junit:junit:$versions.junit" | ||
} | ||
|
||
jar.manifest { | ||
instruction 'Import-Package', '*' | ||
} |
51 changes: 51 additions & 0 deletions
51
org.eclipse.lsp4j.websocket/src/main/java/org/eclipse/lsp4j/websocket/WebSocketEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/****************************************************************************** | ||
* Copyright (c) 2019 TypeFox and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, | ||
* or the Eclipse Distribution License v. 1.0 which is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause | ||
******************************************************************************/ | ||
package org.eclipse.lsp4j.websocket; | ||
|
||
import java.util.Collection; | ||
|
||
import javax.websocket.Endpoint; | ||
import javax.websocket.EndpointConfig; | ||
import javax.websocket.Session; | ||
|
||
import org.eclipse.lsp4j.jsonrpc.Launcher; | ||
|
||
/** | ||
* WebSocket endpoint implementation that connects to a JSON-RPC service. | ||
* | ||
* @param <T> remote service interface type | ||
*/ | ||
public abstract class WebSocketEndpoint<T> extends Endpoint { | ||
|
||
@Override | ||
public void onOpen(Session session, EndpointConfig config) { | ||
WebSocketLauncherBuilder<T> builder = new WebSocketLauncherBuilder<T>(); | ||
builder.setSession(session); | ||
configure(builder); | ||
Launcher<T> launcher = builder.create(); | ||
connect(builder.getLocalServices(), launcher.getRemoteProxy()); | ||
} | ||
|
||
/** | ||
* Configure the JSON-RPC launcher. Implementations should set at least the | ||
* {@link Launcher.Builder#setLocalService(Object) local service} and the | ||
* {@link Launcher.Builder#setRemoteInterface(Class) remote interface}. | ||
*/ | ||
abstract protected void configure(Launcher.Builder<T> builder); | ||
|
||
/** | ||
* Override this in order to connect the local services to the remote service proxy. | ||
*/ | ||
protected void connect(Collection<Object> localServices, T remoteProxy) { | ||
} | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
...e.lsp4j.websocket/src/main/java/org/eclipse/lsp4j/websocket/WebSocketLauncherBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/****************************************************************************** | ||
* Copyright (c) 2019 TypeFox and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, | ||
* or the Eclipse Distribution License v. 1.0 which is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause | ||
******************************************************************************/ | ||
package org.eclipse.lsp4j.websocket; | ||
|
||
import java.util.Collection; | ||
|
||
import javax.websocket.Session; | ||
|
||
import org.eclipse.lsp4j.jsonrpc.Launcher; | ||
import org.eclipse.lsp4j.jsonrpc.MessageConsumer; | ||
import org.eclipse.lsp4j.jsonrpc.RemoteEndpoint; | ||
import org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler; | ||
import org.eclipse.lsp4j.jsonrpc.services.ServiceEndpoints; | ||
|
||
/** | ||
* JSON-RPC launcher builder for use in {@link WebSocketEndpoint}. | ||
* | ||
* @param <T> remote service interface type | ||
*/ | ||
public class WebSocketLauncherBuilder<T> extends Launcher.Builder<T> { | ||
|
||
protected Session session; | ||
|
||
public Collection<Object> getLocalServices() { | ||
return localServices; | ||
} | ||
|
||
public WebSocketLauncherBuilder<T> setSession(Session session) { | ||
this.session = session; | ||
return this; | ||
} | ||
|
||
@Override | ||
public Launcher<T> create() { | ||
if (localServices == null) | ||
throw new IllegalStateException("Local service must be configured."); | ||
if (remoteInterfaces == null) | ||
throw new IllegalStateException("Remote interface must be configured."); | ||
|
||
MessageJsonHandler jsonHandler = createJsonHandler(); | ||
RemoteEndpoint remoteEndpoint = createRemoteEndpoint(jsonHandler); | ||
addMessageHandlers(jsonHandler, remoteEndpoint); | ||
T remoteProxy = createProxy(remoteEndpoint); | ||
return createLauncher(null, remoteProxy, remoteEndpoint, null); | ||
} | ||
|
||
@Override | ||
protected RemoteEndpoint createRemoteEndpoint(MessageJsonHandler jsonHandler) { | ||
MessageConsumer outgoingMessageStream = new WebSocketMessageConsumer(session, jsonHandler); | ||
outgoingMessageStream = wrapMessageConsumer(outgoingMessageStream); | ||
RemoteEndpoint remoteEndpoint = new RemoteEndpoint(outgoingMessageStream, ServiceEndpoints.toEndpoint(localServices)); | ||
jsonHandler.setMethodProvider(remoteEndpoint); | ||
return remoteEndpoint; | ||
} | ||
|
||
protected void addMessageHandlers(MessageJsonHandler jsonHandler, RemoteEndpoint remoteEndpoint) { | ||
MessageConsumer messageConsumer = wrapMessageConsumer(remoteEndpoint); | ||
session.addMessageHandler(new WebSocketMessageHandler(messageConsumer, jsonHandler, remoteEndpoint)); | ||
} | ||
|
||
} |
Oops, something went wrong.