]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLFunctions.java
Merge "Multiple reader thread support for db client"
[simantics/platform.git] / bundles / org.simantics.scl.db / src / org / simantics / scl / db / SCLFunctions.java
index 618d6a91fbe793e5d982656f19a063f49e297627..f3d4c34306d71a7bce5c9779ddee01added2ec60 100644 (file)
@@ -36,20 +36,25 @@ import org.simantics.scl.osgi.SCLOsgi;
 import org.simantics.scl.runtime.SCLContext;
 import org.simantics.scl.runtime.function.Function;
 import org.simantics.scl.runtime.function.Function1;
+import org.simantics.scl.runtime.reporting.SCLReportingHandler;
 import org.simantics.scl.runtime.tuple.Tuple;
 import org.simantics.scl.runtime.tuple.Tuple0;
 import org.simantics.utils.DataContainer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 @SuppressWarnings({"rawtypes", "unchecked"})
 public class SCLFunctions {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(SCLFunctions.class);
+
     public static final String GRAPH = "graph";
 
     public static <T> T safeExec(final Function f) {
         try {
             return (T)f.apply(Tuple0.INSTANCE);
         } catch (Throwable t) {
-            t.printStackTrace();
+            LOGGER.error("safeExec caught exception", t);
             return null;
         }
     }
@@ -94,18 +99,22 @@ public class SCLFunctions {
     
     public static void asyncWrite(final Function f) throws DatabaseException {
         SCLContext context = SCLContext.createDerivedContext();
-        Simantics.getSession().asyncRequest(new WriteRequest() {
-            @Override
-            public void perform(WriteGraph graph) throws DatabaseException {
-                SCLContext.push(context);
-                context.put(GRAPH, graph);
-                try {
-                    f.apply(Tuple0.INSTANCE);
-                } finally {
-                    SCLContext.pop();
+        if (Simantics.peekSession() != null) {
+            Simantics.getSession().asyncRequest(new WriteRequest() {
+                @Override
+                public void perform(WriteGraph graph) throws DatabaseException {
+                    SCLContext.push(context);
+                    context.put(GRAPH, graph);
+                    try {
+                        f.apply(Tuple0.INSTANCE);
+                    } finally {
+                        SCLContext.pop();
+                    }
                 }
-            }
-        });
+            });
+        } else {
+            LOGGER.warn("No session available for asynchronous write requests");
+        }
     }
     
     public static <T> T syncWrite(final Function f) throws DatabaseException {
@@ -114,15 +123,18 @@ public class SCLFunctions {
         if (graph != null) {
             return (T)f.apply(Tuple0.INSTANCE);
         } else {
+            final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
             return Simantics.getSession().syncRequest(new WriteResultRequest<T>() {
                 @Override
                 public T perform(WriteGraph graph) throws DatabaseException {
                     SCLContext.push(context);
+                    SCLReportingHandler oldPrinter = (SCLReportingHandler)context.put(SCLReportingHandler.REPORTING_HANDLER, printer);
                     ReadGraph oldGraph = (ReadGraph)context.put(GRAPH, graph);
                     try {
                         return (T)f.apply(Tuple0.INSTANCE);
                     } finally {
                         context.put(GRAPH, oldGraph);
+                        context.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
                         SCLContext.pop();
                     }
                 }
@@ -287,7 +299,7 @@ public class SCLFunctions {
     }
     
     public static void subqueryL(ReadGraph graph, Function query, Function executeCallback, Function1<Throwable, Tuple> exceptionCallback, Function1<Tuple0, Boolean> isDisposedCallback) throws DatabaseException {
-        graph.asyncRequest(new Subquery(query), new SyncListenerAdapter<Object>() {
+        graph.syncRequest(new Subquery(query), new SyncListenerAdapter<Object>() {
             @Override
             public void execute(ReadGraph graph, Object result) throws DatabaseException {
                 Simantics.applySCLRead(graph, executeCallback, result);