]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLScriptRunnerApplication.java
acfce7de990adc16a50aa66384b99d608263051e
[simantics/platform.git] / bundles / org.simantics.scl.db / src / org / simantics / scl / db / SCLScriptRunnerApplication.java
1 package org.simantics.scl.db;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6
7 import org.eclipse.core.runtime.IProgressMonitor;
8 import org.eclipse.core.runtime.NullProgressMonitor;
9 import org.eclipse.equinox.app.IApplication;
10 import org.eclipse.equinox.app.IApplicationContext;
11 import org.simantics.Simantics;
12 import org.simantics.application.arguments.Arguments;
13 import org.simantics.application.arguments.IArgumentFactory;
14 import org.simantics.application.arguments.IArguments;
15 import org.simantics.application.arguments.SimanticsArguments;
16 import org.simantics.scl.compiler.commands.CommandSession;
17 import org.simantics.scl.osgi.SCLOsgi;
18 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
19
20 public class SCLScriptRunnerApplication implements IApplication {
21
22         @Override
23         public Object start(IApplicationContext context) throws Exception {
24                 try {
25             String[] args = (String[])context.getArguments().get("application.args");
26             IProgressMonitor progress = new NullProgressMonitor();
27             Simantics.startUpHeadless(parseArguments(args), progress);
28             runSCLFromStdin();
29             Simantics.shutdown(progress);
30             return IApplication.EXIT_OK;
31         } catch(Throwable t) {
32             t.printStackTrace();
33             throw (Exception)t;
34         } finally {
35             System.exit(0);
36         }
37         }
38         
39         private void runSCLFromStdin() throws Exception {
40                 BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
41                 CommandSession commandSession = new CommandSession(SCLOsgi.MODULE_REPOSITORY,
42                         SCLReportingHandler.DEFAULT_WITHOUT_ECHO);
43                 try {
44                         while(true) {
45                                 String line = input.readLine();
46                                 if(line == null)
47                                         break;
48                                 if(line.isEmpty())
49                                         continue;
50                                 commandSession.execute(line);
51                         }
52                 } catch(IOException e) {
53                         e.printStackTrace();
54                 }               
55         }
56
57         private IArguments parseArguments(String[] args) {
58                 IArgumentFactory<?>[] accepted = {
59                                 SimanticsArguments.RECOVERY_POLICY_FIX_ERRORS,
60                                 SimanticsArguments.ONTOLOGY_RECOVERY_POLICY_REINSTALL,
61                                 SimanticsArguments.SERVER,
62                                 SimanticsArguments.LOCAL_SERVER_PORT,
63                 };
64                 IArguments result = Arguments.parse(args, accepted);
65                 return result;
66         }
67         
68         @Override
69         public void stop() {
70         }
71
72 }