]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/CommandSessionWithModules.java
(refs #7374) Created CommandSessionWithModules
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / commands / CommandSessionWithModules.java
1 package org.simantics.scl.compiler.commands;
2
3
4 import java.io.IOException;
5 import java.io.Reader;
6 import java.io.Writer;
7
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;
19
20 public class CommandSessionWithModules {
21     private static final Logger LOGGER = LoggerFactory.getLogger(CommandSessionWithModules.class);
22     
23     private MapModuleSourceRepository localModuleSourceRepository;
24     private ModuleRepository moduleRepository;
25     private CommandSession commandSession;
26     
27     private static final SCLReportingHandler DEFAULT_REPORTING_HANDLER = new AbstractSCLReportingHandler() {
28         @Override
29         public void print(String text) {
30             CommandSessionWithModules.LOGGER.info(text);
31         }
32     };
33     
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() {
39             @Override
40             public void notifyAboutUpdate() {
41                 commandSession.updateRuntimeEnvironment(true);
42             }
43         });
44     }
45     
46     public CommandSession getCommandSession() {
47         return commandSession;
48     }
49     
50     /**
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.
54      */
55     public String putModule(String moduleName, String moduleText) {
56         StringModuleSource moduleSource = new StringModuleSource(moduleName, moduleText);
57         synchronized(localModuleSourceRepository) {
58             localModuleSourceRepository.addModuleDescriptor(moduleSource);
59         }
60         Failable<Module> module = moduleRepository.getModule(moduleName);
61         if(module.didSucceed())
62             return null;
63         else
64             return ((Failure)module).toString(moduleText);
65     }
66     
67     /**
68      * Runs commands read from commandReader and writes responses to
69      * responseWriter.
70      */
71     public void runCommands(Reader commandReader, Writer responseWriter) {
72         SCLReportingHandler handler = new AbstractSCLReportingHandler() {
73             @Override
74             public void printCommand(String command) {
75                 // Don't echo commands
76             }
77             @Override
78             public void print(String text) {
79                 try {
80                     responseWriter.write(text + "\n");
81                 } catch (IOException e) {
82                     CommandSessionWithModules.LOGGER.error("Writing reponse failed.", e);
83                 }
84             }
85         };
86         commandSession.execute(commandReader, handler);
87     }
88 }