]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/SCLContext.java
(refs #7607) Fixed handling of SCLContext in asynchronous requests
[simantics/platform.git] / bundles / org.simantics.scl.runtime / src / org / simantics / scl / runtime / SCLContext.java
index 63fa2f1e28dc6b1240feb08ac2dd57ca233507cf..a9444bcf909f97bd77fde8c47b16ec97996bb0cb 100644 (file)
@@ -1,60 +1,76 @@
-package org.simantics.scl.runtime;\r
-\r
-import gnu.trove.map.hash.THashMap;\r
-\r
-import java.util.Map;\r
-\r
-public class SCLContext extends THashMap<String,Object> {\r
-    private static ThreadLocal<SCLContext> CONTEXT = new ThreadLocal<SCLContext>();\r
-    private static ThreadLocal<OldContextNode> OLD_CONTEXT = new ThreadLocal<OldContextNode>();\r
-    \r
-    private static class OldContextNode {\r
-        final SCLContext context;\r
-        final OldContextNode next;\r
-        \r
-        public OldContextNode(SCLContext context, OldContextNode next) {\r
-            this.context = context;\r
-            this.next = next;\r
-        }\r
-    }\r
-    \r
-    public static SCLContext getCurrent() {\r
-        SCLContext context = CONTEXT.get();\r
-        if(context == null) {\r
-            context = new SCLContext();\r
-            CONTEXT.set(context);\r
-        }\r
-        return context;\r
-    }\r
-    \r
-    public static void push(SCLContext newContext) {\r
-        SCLContext oldContext = CONTEXT.get();\r
-        if(oldContext != null)\r
-            OLD_CONTEXT.set(new OldContextNode(oldContext, OLD_CONTEXT.get()));\r
-        CONTEXT.set(newContext);\r
-    }\r
-    \r
-    public static void pop() {\r
-        OldContextNode node = OLD_CONTEXT.get();\r
-        if(node == null)\r
-            CONTEXT.set(null);\r
-        else {\r
-            CONTEXT.set(node.context);\r
-            OLD_CONTEXT.set(node.next);\r
-        }\r
-    }\r
-    \r
-    /*\r
-    @Override\r
-    public Object get(Object key) {\r
-        Object result = super.get(key);\r
-        System.out.println(this + ": " + key + " -> " + result);\r
-        return result;\r
-    }\r
-    \r
-    @Override\r
-    public Object put(String key, Object value) {\r
-        System.out.println(this + ": " + key + " <- " + value);\r
-        return super.put(key, value);\r
-    }*/\r
-}\r
+package org.simantics.scl.runtime;
+
+import org.simantics.scl.runtime.reporting.SCLReportingHandler;
+
+import gnu.trove.map.hash.THashMap;
+
+public class SCLContext extends THashMap<String,Object> {
+    private static ThreadLocal<SCLContext> CONTEXT = new ThreadLocal<SCLContext>();
+    private static ThreadLocal<OldContextNode> OLD_CONTEXT = new ThreadLocal<OldContextNode>();
+    
+    private static class OldContextNode {
+        final SCLContext context;
+        final OldContextNode next;
+        
+        public OldContextNode(SCLContext context, OldContextNode next) {
+            this.context = context;
+            this.next = next;
+        }
+    }
+    
+    public static SCLContext getCurrent() {
+        SCLContext context = CONTEXT.get();
+        if(context == null) {
+            context = new SCLContext();
+            CONTEXT.set(context);
+        }
+        return context;
+    }
+    
+    public static void push(SCLContext newContext) {
+        SCLContext oldContext = CONTEXT.get();
+        if(oldContext != null)
+            OLD_CONTEXT.set(new OldContextNode(oldContext, OLD_CONTEXT.get()));
+        CONTEXT.set(newContext);
+    }
+    
+    /**
+     * Creates a new context based on some properties of the current context.
+     * The new context is safe for use in parallel threads.
+     */
+    public static SCLContext createDerivedContext() {
+        SCLContext newContext = new SCLContext();
+        
+        SCLContext baseContext = CONTEXT.get();
+        if(baseContext != null) {
+            Object reportingHandler = baseContext.get(SCLReportingHandler.REPORTING_HANDLER);
+            if(reportingHandler != null)
+                newContext.put(SCLReportingHandler.REPORTING_HANDLER, reportingHandler);
+        }
+        return newContext;
+    }
+    
+    public static void pop() {
+        OldContextNode node = OLD_CONTEXT.get();
+        if(node == null)
+            CONTEXT.set(null);
+        else {
+            CONTEXT.set(node.context);
+            OLD_CONTEXT.set(node.next);
+        }
+    }
+    
+    /*
+    @Override
+    public Object get(Object key) {
+        Object result = super.get(key);
+        System.out.println(this + ": " + key + " -> " + result);
+        return result;
+    }
+    
+    @Override
+    public Object put(String key, Object value) {
+        System.out.println(this + ": " + key + " <- " + value);
+        return super.put(key, value);
+    }*/
+}