Skip to content

Commit

Permalink
Merge pull request #91 from sailingKieler/master
Browse files Browse the repository at this point in the history
Fixes of #89 & #90, and a bit of simplification in the example project
  • Loading branch information
dhuebner authored Feb 29, 2024
2 parents 6e3ab67 + f17ff50 commit 9654b98
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 108 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,23 @@
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer;
import org.eclipse.sprotty.IDiagramServer;
import org.eclipse.sprotty.examples.circlegraph.CircleGraphModule;
import org.eclipse.sprotty.examples.circlegraph.LayoutSelectionAction;
import org.eclipse.sprotty.layout.ElkLayoutEngine;
import org.eclipse.sprotty.server.json.ActionTypeAdapter;
import org.eclipse.sprotty.server.websocket.DiagramServerEndpoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.inject.Guice;
import com.google.inject.Injector;

public class ExampleLauncher {

private static final Logger LOG = LoggerFactory.getLogger(ExampleLauncher.class);
private static final Logger LOG = LoggerFactory.getLogger(ExampleLauncher.class);

public static void main(String[] args) {
try {
Expand All @@ -56,15 +62,33 @@ public void launch() throws Exception {
webAppContext.setWelcomeFiles(new String[] { "index.html" });
server.setHandler(webAppContext);

final ServerEndpointConfig.Configurator configurator = new ServerEndpointConfig.Configurator() {

@SuppressWarnings("unchecked")
@Override
public <T extends Object> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
DiagramServerEndpoint endpoint = ((LoggingServerEndpoint) super.getEndpointInstance(endpointClass));
endpoint.setGson(getGson());
endpoint.setDiagramServerProvider(circleGraphInjector.getInstance(IDiagramServer.Provider.class));
return (T) endpoint;
}
};

JakartaWebSocketServletContainerInitializer.configure(webAppContext, (servletContext, serverContainer) -> {
serverContainer.addEndpoint(ServerEndpointConfig.Builder
.create(LoggingServerEndpoint.class, "/circlegraph")
.configurator(new ExampleEndpointConfigurator(circleGraphInjector))
.configurator(configurator)
.build());
});

server.start();
server.join();
}


private Gson getGson() {
final ActionTypeAdapter.Factory factory = new ActionTypeAdapter.Factory();
factory.addActionKind(LayoutSelectionAction.KIND, LayoutSelectionAction.class);

return new GsonBuilder().registerTypeAdapterFactory(factory).create();
}
}
1 change: 1 addition & 0 deletions gradle/manifest-gen.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jar.bundle.bnd (
'Bundle-Vendor': 'Eclipse Sprotty',
'Bundle-RequiredExecutionEnvironment': 'JavaSE-11',
'Import-Package': "com.google.common.*;version=\"$versions.guava\", *",
'-exportcontents': 'org.eclipse.sprotty*',
"-savemanifest": "build/tmp/bnd/MANIFEST.MF",
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.TypeAdapter;
import com.google.gson.internal.bind.JsonTreeWriter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

/**
Expand Down Expand Up @@ -116,57 +115,9 @@ protected Field findField(Class<?> type, String propertyName) throws NoSuchField
}

protected JsonElement toTree(JsonReader in) throws IOException {
JsonTreeWriter writer = new JsonTreeWriter();
transfer(in, writer);
return writer.get();
return JsonParser.parseReader(in);
}

protected void transfer(JsonReader in, JsonWriter out) throws IOException {
JsonToken token = in.peek();
switch (token) {
case BEGIN_ARRAY:
in.beginArray();
out.beginArray();
while (in.hasNext()) {
transfer(in, out);
}
out.endArray();
in.endArray();
break;

case BEGIN_OBJECT:
in.beginObject();
out.beginObject();
while (in.hasNext()) {
out.name(in.nextName());
transfer(in, out);
}
out.endObject();
in.endObject();
break;

case STRING:
out.value(in.nextString());
break;

case NUMBER:
out.value(in.nextDouble());
break;

case BOOLEAN:
out.value(in.nextBoolean());
break;

case NULL:
in.nextNull();
out.nullValue();
break;

default:
throw new IllegalStateException();
}
}

@Override
public void write(JsonWriter out, T value) throws IOException {
if (value == null) {
Expand Down

0 comments on commit 9654b98

Please sign in to comment.