1 package org.simantics.scl.rest;
5 import java.util.concurrent.ConcurrentHashMap;
7 import org.simantics.scl.compiler.commands.CommandSessionWithModules;
8 import org.simantics.scl.osgi.SCLOsgi;
12 private static SCLAPI INSTANCE;
14 private ConcurrentHashMap<String, CommandSessionWithModules> commandSessions;
17 this.commandSessions = new ConcurrentHashMap<>();
20 public static SCLAPI getInstance() {
21 if (INSTANCE == null) {
22 synchronized (SCLAPI.class) {
23 if (INSTANCE == null) {
24 INSTANCE = new SCLAPI();
31 public CommandSessionWithModules getOrCreateCommandSession(String sessionId) {
32 return commandSessions.computeIfAbsent(sessionId, key -> new CommandSessionWithModules(SCLOsgi.MODULE_REPOSITORY));
35 public void execute(String sessionId, Reader reader, Writer writer) {
36 CommandSessionWithModules session = commandSessions.get(sessionId);
38 throw new IllegalArgumentException("CommandSession for sessionId " + sessionId + " does not exist!");
39 session.runCommands(reader, writer);
42 public void deleteCommandSession(String sessionId) {
43 commandSessions.computeIfPresent(sessionId, (key, session) -> {
44 // session could be flushed or closed here to release possible resources?
49 public Object variableValue(String sessionId, String variableName) {
50 CommandSessionWithModules session = commandSessions.get(sessionId);
52 throw new IllegalArgumentException("CommandSession for sessionId " + sessionId + " does not exist!");
53 return session.getCommandSession().getVariableValue(variableName);
56 public String putModule(String sessionId, String moduleName, String moduleText) {
57 CommandSessionWithModules session = commandSessions.get(sessionId);
59 throw new IllegalArgumentException("CommandSession for sessionId " + sessionId + " does not exist!");
60 return session.putModule(moduleName, moduleText);