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