]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/scl/SCLSessionManager.java
Some fixes for resource cleaning spreadsheets in simupedia
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / scl / SCLSessionManager.java
1 package org.simantics.modeling.scl;
2
3 import java.util.UUID;
4 import java.util.concurrent.ConcurrentHashMap;
5
6 import org.simantics.db.layer0.variable.NodeSupport;
7 import org.simantics.scl.compiler.commands.CommandSession;
8 import org.simantics.scl.osgi.SCLOsgi;
9 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
10
11 public class SCLSessionManager {
12     static ConcurrentHashMap<String, SCLRealm> CONNECTIONS =
13             new ConcurrentHashMap<String, SCLRealm>(); 
14     
15     static ConcurrentHashMap<String, NodeSupport<String>> SUPPORTS =
16             new ConcurrentHashMap<String, NodeSupport<String>>(); 
17     
18     public static SCLRealm sclRealmById(String id) {
19         // CONNECTIONS is ConcurrentHashMap so no synchronization is needed here
20         return CONNECTIONS.get(id);
21     }
22     
23     public static synchronized NodeSupport<String> getOrCreateNodeSupport(String id) {
24         NodeSupport<String> result = SUPPORTS.get(id);
25         if(result == null) {
26             SCLRealm realm = getOrCreateSCLRealm(id);
27             result = new NodeSupport<String>(realm.getNodeManager());
28             SUPPORTS.put(id, result);
29         }
30         return result;
31     }
32     
33     public static synchronized SCLRealm createRealm() {
34         String id = UUID.randomUUID().toString();
35         return createRealm(id);
36     }
37     
38     public static synchronized SCLRealm getOrCreateSCLRealm(String id) {
39         SCLRealm session = sclRealmById(id);
40         if(session == null)
41             return createRealm(id);
42         else
43             return session;
44     }
45     
46     private static SCLRealm createRealm(String id) {
47         CommandSession connection = new CommandSession(SCLOsgi.MODULE_REPOSITORY, SCLReportingHandler.DEFAULT);
48         SCLRealm realm = new SCLRealm(connection, id);
49         CONNECTIONS.put(id, realm);
50         return realm;
51     }
52     
53     public static synchronized void removeRealm(String id) {
54         SCLRealm realm = CONNECTIONS.remove(id);
55         if (realm != null)
56             realm.close();
57         // if node support has been created remove it as well
58         NodeSupport<String> support = SUPPORTS.remove(id);
59         if (support != null)
60             support.dispose();
61     }
62 }