1 package org.simantics.modeling.scl;
4 import java.util.concurrent.ConcurrentHashMap;
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;
11 public class SCLSessionManager {
12 static ConcurrentHashMap<String, SCLRealm> CONNECTIONS =
13 new ConcurrentHashMap<String, SCLRealm>();
15 static ConcurrentHashMap<String, NodeSupport<String>> SUPPORTS =
16 new ConcurrentHashMap<String, NodeSupport<String>>();
18 public static SCLRealm sclRealmById(String id) {
19 // CONNECTIONS is ConcurrentHashMap so no synchronization is needed here
20 return CONNECTIONS.get(id);
23 public static synchronized NodeSupport<String> getOrCreateNodeSupport(String id) {
24 NodeSupport<String> result = SUPPORTS.get(id);
26 SCLRealm realm = getOrCreateSCLRealm(id);
27 result = new NodeSupport<String>(realm.getNodeManager());
28 SUPPORTS.put(id, result);
33 public static synchronized SCLRealm createRealm() {
34 String id = UUID.randomUUID().toString();
35 return createRealm(id);
38 public static synchronized SCLRealm getOrCreateSCLRealm(String id) {
39 SCLRealm session = sclRealmById(id);
41 return createRealm(id);
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);
53 public static synchronized void removeRealm(String id) {
54 SCLRealm realm = CONNECTIONS.remove(id);
57 // if node support has been created remove it as well
58 NodeSupport<String> support = SUPPORTS.remove(id);