]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLFunctions.java
Async utilities for SCL
[simantics/platform.git] / bundles / org.simantics.scl.db / src / org / simantics / scl / db / SCLFunctions.java
index 618d6a91fbe793e5d982656f19a063f49e297627..23c1ad508ef09d68b10d0ac22649c7d3004d17cb 100644 (file)
@@ -39,17 +39,21 @@ import org.simantics.scl.runtime.function.Function1;
 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 +98,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 {