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;
}
}
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 {
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();
}
}