]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.server/src/org/simantics/document/server/handler/EventHandler.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / handler / EventHandler.java
1 package org.simantics.document.server.handler;
2
3 import org.simantics.Simantics;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.exception.DatabaseException;
6 import org.simantics.db.request.Read;
7 import org.simantics.document.server.io.CommandContext;
8 import org.simantics.document.server.io.CommandResult;
9 import org.simantics.document.server.serverResponse.Error;
10 import org.simantics.document.server.serverResponse.IDelayedResponse;
11 import org.simantics.document.server.serverResponse.ServerResponse;
12
13 public abstract class EventHandler extends AbstractEventHandler {
14         
15     protected abstract ServerResponse handle(ReadGraph graph, CommandContext parameters) throws DatabaseException;
16
17     public CommandResult handle(final CommandContext parameters) {
18         try {
19             ServerResponse response = Simantics.getSession().syncRequest(new Read<ServerResponse>() {
20                 @Override
21                 public ServerResponse perform(ReadGraph graph) throws DatabaseException {
22                     return handle(graph, parameters);
23                 }
24             });
25
26             if(response instanceof IDelayedResponse && !(((IDelayedResponse)response).hasRun())) {
27                 try {
28                     synchronized(response) {
29                         response.wait(20000);
30                     }
31                 } catch (InterruptedException e) {
32                     e.printStackTrace();
33                 }
34             }
35             
36             return response;
37         } catch (DatabaseException e) {
38             e.printStackTrace();
39             return new Error(e.getMessage()); // Return some error
40         }
41     }
42
43 }