1 package org.simantics.scl.compiler.commands;
4 import java.io.IOException;
8 import org.simantics.scl.compiler.errors.Failable;
9 import org.simantics.scl.compiler.errors.Failure;
10 import org.simantics.scl.compiler.module.Module;
11 import org.simantics.scl.compiler.module.repository.ModuleRepository;
12 import org.simantics.scl.compiler.module.repository.UpdateListener;
13 import org.simantics.scl.compiler.source.StringModuleSource;
14 import org.simantics.scl.compiler.source.repository.MapModuleSourceRepository;
15 import org.simantics.scl.runtime.reporting.AbstractSCLReportingHandler;
16 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 public class CommandSessionWithModules {
21 private static final Logger LOGGER = LoggerFactory.getLogger(CommandSessionWithModules.class);
23 private MapModuleSourceRepository localModuleSourceRepository;
24 private ModuleRepository moduleRepository;
25 private CommandSession commandSession;
27 private static final SCLReportingHandler DEFAULT_REPORTING_HANDLER = new AbstractSCLReportingHandler() {
29 public void print(String text) {
30 CommandSessionWithModules.LOGGER.info(text);
34 public CommandSessionWithModules(ModuleRepository parentRepository) {
35 this.localModuleSourceRepository = new MapModuleSourceRepository();
36 this.moduleRepository = new ModuleRepository(parentRepository, localModuleSourceRepository);
37 this.commandSession = new CommandSession(moduleRepository, DEFAULT_REPORTING_HANDLER);
38 this.commandSession.setDependenciesListener(new UpdateListener() {
40 public void notifyAboutUpdate() {
41 commandSession.updateRuntimeEnvironment(true);
46 public CommandSession getCommandSession() {
47 return commandSession;
51 * Puts the given module to the local module repository. Returns null, if the
52 * compilation of the module succeeded or a string containing the compilation
53 * errors, if it failed.
55 public String putModule(String moduleName, String moduleText) {
56 StringModuleSource moduleSource = new StringModuleSource(moduleName, moduleText);
57 synchronized(localModuleSourceRepository) {
58 localModuleSourceRepository.addModuleDescriptor(moduleSource);
60 Failable<Module> module = moduleRepository.getModule(moduleName);
61 if(module.didSucceed())
64 return ((Failure)module).toString(moduleText);
68 * Runs commands read from commandReader and writes responses to
71 public void runCommands(Reader commandReader, Writer responseWriter) {
72 SCLReportingHandler handler = new AbstractSCLReportingHandler() {
74 public void printCommand(String command) {
75 // Don't echo commands
78 public void print(String text) {
80 responseWriter.write(text + "\n");
81 } catch (IOException e) {
82 CommandSessionWithModules.LOGGER.error("Writing reponse failed.", e);
86 commandSession.execute(commandReader, handler);