1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package name.angoca.zemucan.main;
30
31 import name.angoca.zemucan.AbstractZemucanException;
32 import name.angoca.zemucan.interfaze.InterfaceCore;
33 import name.angoca.zemucan.tools.configurator.Configurator;
34 import name.angoca.zemucan.ui.api.AbstractInterfaceController;
35 import name.angoca.zemucan.ui.api.InputReaderException;
36 import name.angoca.zemucan.ui.api.SynchronizedLoad;
37 import name.angoca.zemucan.ui.impl.jline.ImplementationJlineInterfaceController;
38 import name.angoca.zemucan.ui.impl.system.ImplementationSystemInterfaceController;
39
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 public final class Main {
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 class BackgroundGrammarLoader extends Thread {
92
93
94
95 private final SynchronizedLoad synchronizeLoad;
96
97
98
99
100
101
102
103
104 public BackgroundGrammarLoader(
105 final SynchronizedLoad
106 super("GrammarLoaderThread");
107 this.synchronizeLoad = synchronizedLoad;
108 }
109
110
111
112
113
114 @Override
115 public void run() {
116
117 if (!this.synchronizeLoad.isAlreadyLoaded()) {
118 synchronized (this) {
119 try {
120 this.wait();
121 } catch (final InterruptedException exception) {
122 Main.LOGGER.error(exception.getMessage());
123 Main.LOGGER.info(exception.getStackTrace().toString());
124 }
125 }
126 }
127
128 try {
129 InterfaceCore.initializeGraph();
130 } catch (final AbstractZemucanException exception) {
131 Main.LOGGER.error(exception.getMessage());
132 Main.LOGGER.info(exception.getStackTrace().toString());
133 exception.printStackTrace();
134 System.exit(Main.INITIALIZING_ERROR);
135 }
136 }
137 }
138
139
140
141
142 static final int ID_DEFAULT_CONTROLLER = Main.ID_JLINE;
143
144
145
146 static final int ID_JLINE = 1;
147
148
149
150
151 static final int ID_SYSTEM = 0;
152
153
154
155 public static final int INITIALIZING_ERROR = -1;
156
157
158
159
160 private static final String INTERFACE_CONTROLLER_TYPE_PROPERTY = "InterfaceControllerType";
161
162
163
164 private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
165
166
167
168 static final String PROMPT_PROPERTY = "Prompt";
169
170
171
172 private static final String PROMPT_VALUE = "DB2>";
173
174
175
176
177 private static final String SEPARATED_THREAD = "SeparatedThreadForGrammar";
178
179
180
181 private static final Object TRUE = "true";
182
183
184
185 private static final String VALUE_DEFAULT_CONTROLLER = Main.VALUE_JLINE;
186
187
188
189 static final String VALUE_JLINE = "jline";
190
191
192
193
194 static final String VALUE_SYSTEM = "system";
195
196
197
198
199
200
201
202
203 static int getInterfaceControllerType(final String[]
204 assert args != null;
205 boolean assertsEnabled = false;
206
207 assert assertsEnabled = true;
208 if (assertsEnabled) {
209 for (final String string : args) {
210 assert string != null;
211 }
212 }
213
214 int type = Main.ID_DEFAULT_CONTROLLER;
215 if (args.length > 0) {
216 type = Main.processInterfaceControllerType(args[0]);
217 } else {
218 final String prompt = Configurator.getInstance().getProperty(
219 Main.INTERFACE_CONTROLLER_TYPE_PROPERTY);
220 type = Main.processInterfaceControllerType(prompt);
221 }
222 return type;
223 }
224
225
226
227
228
229
230
231
232
233 public static void main(final String[]
234 throws AbstractZemucanException {
235 Main.LOGGER.debug("Application Start {}",
236 System.currentTimeMillis());
237
238 new Main(args);
239 }
240
241
242
243
244
245
246
247
248
249
250
251 static AbstractInterfaceController
252 throws InputReaderException {
253 assert (type == Main.ID_JLINE) || (type == Main.ID_SYSTEM);
254
255 String prompt = Configurator.getInstance().getProperty(
256 Main.PROMPT_PROPERTY);
257 if (prompt == null) {
258 prompt = Main.PROMPT_VALUE;
259 }
260 AbstractInterfaceController controller = null;
261 if (type == Main.ID_SYSTEM) {
262 controller = new ImplementationSystemInterfaceController(prompt);
263 } else if (type == Main.ID_JLINE) {
264 controller = new ImplementationJlineInterfaceController(prompt);
265 } else {
266
267 assert false;
268 }
269
270 assert controller != null;
271
272 return controller;
273 }
274
275
276
277
278
279
280
281
282
283 private static int processInterfaceControllerType(final String
284 assert text != null;
285
286 int type = Main.ID_DEFAULT_CONTROLLER;
287 if (text.equals(Main.VALUE_SYSTEM)) {
288 type = Main.ID_SYSTEM;
289 } else if (text.equals(Main.VALUE_JLINE)) {
290 type = Main.ID_JLINE;
291 } else {
292 Main.LOGGER.error("Unsupported interface controller type value. "
293 + "Using default '" + Main.VALUE_DEFAULT_CONTROLLER + "'.");
294 }
295 return type;
296 }
297
298
299
300
301
302
303
304 Main(final String[]
305 assert args != null;
306 boolean assertsEnabled = false;
307
308 assert assertsEnabled = true;
309 if (assertsEnabled) {
310 for (final String string : args) {
311 assert string != null;
312 }
313 }
314
315
316 if ((args.length == 1)
317 && (args[0].equals("-h") || args[0].equals("--help") || args[0]
318 .equals("/h"))) {
319 this.showHelp();
320 } else {
321
322 try {
323 final int interfaceControllerType = Main
324 .getInterfaceControllerType(args);
325 final AbstractInterfaceController controller = Main
326 .prepare(interfaceControllerType);
327
328 SynchronizedLoad synchronizedLoad = null;
329 Main.BackgroundGrammarLoader thread = null;
330
331
332 final String separatedThread = Configurator.getInstance()
333 .getProperty(Main.SEPARATED_THREAD);
334 if ((separatedThread != null)
335 && separatedThread.equals(Main.TRUE)) {
336 synchronizedLoad = new SynchronizedLoad();
337 thread = new BackgroundGrammarLoader(synchronizedLoad);
338 thread.setPriority(Thread.MIN_PRIORITY);
339 thread.start();
340 }
341 controller.start(thread, synchronizedLoad);
342 } catch (final AbstractZemucanException exception) {
343 Main.LOGGER.error(exception.getMessage());
344 Main.LOGGER.info(exception.getStackTrace().toString());
345 throw exception;
346 }
347 }
348 }
349
350
351
352
353 private void showHelp() {
354 final String help = "Call the application with one of those flags: '"
355 + Main.VALUE_JLINE + "', '" + Main.VALUE_SYSTEM
356 + "'. If you don't select a flag, the default will be '"
357 + Main.VALUE_DEFAULT_CONTROLLER + "'.\nFor example:\n"
358 + "sa.sh jline (For linux)\nsa.bat jline (For Windows)";
359 Main.LOGGER.error(help);
360 }
361 }