X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scl.rest%2Fsrc%2Forg%2Fsimantics%2Fscl%2Frest%2FSCLAPI.java;fp=bundles%2Forg.simantics.scl.rest%2Fsrc%2Forg%2Fsimantics%2Fscl%2Frest%2FSCLAPI.java;h=cb86ff861a8ba28c8a17d0b198083bae157447ab;hb=db00b51c6ab9c63883de134e669b5da5be0f1bd5;hp=0000000000000000000000000000000000000000;hpb=23fa194c0cf85db6c435f342600d30ddc0f8c620;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scl.rest/src/org/simantics/scl/rest/SCLAPI.java b/bundles/org.simantics.scl.rest/src/org/simantics/scl/rest/SCLAPI.java new file mode 100644 index 000000000..cb86ff861 --- /dev/null +++ b/bundles/org.simantics.scl.rest/src/org/simantics/scl/rest/SCLAPI.java @@ -0,0 +1,62 @@ +package org.simantics.scl.rest; + +import java.io.Reader; +import java.io.Writer; +import java.util.concurrent.ConcurrentHashMap; + +import org.simantics.scl.compiler.commands.CommandSessionWithModules; +import org.simantics.scl.osgi.SCLOsgi; + +public class SCLAPI { + + private static SCLAPI INSTANCE; + + private ConcurrentHashMap commandSessions; + + private SCLAPI() { + this.commandSessions = new ConcurrentHashMap<>(); + } + + public static SCLAPI getInstance() { + if (INSTANCE == null) { + synchronized (SCLAPI.class) { + if (INSTANCE == null) { + INSTANCE = new SCLAPI(); + } + } + } + return INSTANCE; + } + + public CommandSessionWithModules getOrCreateCommandSession(String sessionId) { + return commandSessions.computeIfAbsent(sessionId, key -> new CommandSessionWithModules(SCLOsgi.MODULE_REPOSITORY)); + } + + public void execute(String sessionId, Reader reader, Writer writer) { + CommandSessionWithModules session = commandSessions.get(sessionId); + if (session == null) + throw new IllegalArgumentException("CommandSession for sessionId " + sessionId + " does not exist!"); + session.runCommands(reader, writer); + } + + public void deleteCommandSession(String sessionId) { + commandSessions.computeIfPresent(sessionId, (key, session) -> { + // session could be flushed or closed here to release possible resources? + return null; + }); + } + + public Object variableValue(String sessionId, String variableName) { + CommandSessionWithModules session = commandSessions.get(sessionId); + if (session == null) + throw new IllegalArgumentException("CommandSession for sessionId " + sessionId + " does not exist!"); + return session.getCommandSession().getVariableValue(variableName); + } + + public String putModule(String sessionId, String moduleName, String moduleText) { + CommandSessionWithModules session = commandSessions.get(sessionId); + if (session == null) + throw new IllegalArgumentException("CommandSession for sessionId " + sessionId + " does not exist!"); + return session.putModule(moduleName, moduleText); + } +}