Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes of #89 & #90, and a bit of simplification in the example project #91

Merged
merged 3 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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