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.executer.impl;
30
31 import java.io.BufferedReader;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 import java.io.Reader;
36 import java.util.StringTokenizer;
37
38 import name.angoca.zemucan.AbstractZemucanException;
39 import name.angoca.zemucan.ParameterNullException;
40 import name.angoca.zemucan.executer.api.AbstractExecuter;
41 import name.angoca.zemucan.executer.api.ExecutingCommandException;
42 import name.angoca.zemucan.executer.api.ExecutionState;
43 import name.angoca.zemucan.tools.configurator.Configurator;
44 import name.angoca.zemucan.tools.messages.executer.Messages;
45 import name.angoca.zemucan.ui.api.OutputWriter;
46
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
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
76
77
78
79
80
81
82
83
84
85 public final class ImplementationExecuter extends AbstractExecuter {
86
87
88
89 static final String DB2_NATIVE_PROPERTY = "DB2Native";
90
91
92
93
94 static final String DB2_PARAMS = "DB2params";
95
96
97
98 private static final String EXIT_PROPERTY = "Exit";
99
100
101
102
103 static final String EXIT_VALUE = "exit";
104
105
106
107
108 private static ImplementationExecuter instance;
109
110
111
112
113 private static final Logger LOGGER = LoggerFactory
114 .getLogger(ImplementationExecuter.class);
115
116
117
118
119 public static void destroyInstance() {
120 if (ImplementationExecuter.instance != null) {
121 ImplementationExecuter.instance.tokens = null;
122 ImplementationExecuter.instance = null;
123 }
124
125 assert ImplementationExecuter.instance == null;
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139 public static ImplementationExecuter
140 if (ImplementationExecuter.instance == null) {
141 synchronized (ImplementationExecuter.class) {
142 ImplementationExecuter.instance = new ImplementationExecuter();
143 }
144 }
145
146 assert ImplementationExecuter.instance != null;
147 return ImplementationExecuter.instance;
148 }
149
150
151
152
153 private boolean assertsEnabled;
154
155
156
157
158 private String tokens;
159
160
161
162
163 private ImplementationExecuter() {
164 this.assertsEnabled = false;
165
166 assert this.assertsEnabled = true;
167
168 this.tokens = Configurator.getInstance().getProperty(
169 ImplementationExecuter.EXIT_PROPERTY);
170 if ((this.tokens == null) || this.tokens.equals("")) {
171 this.tokens = ImplementationExecuter.EXIT_VALUE;
172 }
173 }
174
175
176
177
178
179
180
181
182
183
184
185
186
187 private void catchError(final String
188 final IOException
189 assert command != null;
190 assert exception != null;
191
192
193 final String message = exception.getMessage();
194 final String error1 = "Cannot run program";
195 final CharSequence error2 = new String("CreateProcess error=2");
196 if (message.startsWith(error1) && message.contains(error2)) {
197
198
199 throw new CommandNotFoundException(new StringTokenizer(command)
200 .nextToken());
201 }
202 }
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218 private void closeObjects(final InputStream
219 final Reader
220 throws ExecutingCommandException {
221
222 try {
223 if (bfStream != null) {
224 bfStream.close();
225 }
226 } catch (final IOException exception2) {
227
228 throw new ExecutingCommandException(exception2);
229 } finally {
230 try {
231 if (inputStream != null) {
232 inputStream.close();
233 }
234 } catch (final IOException exception3) {
235
236 throw new ExecutingCommandException(exception3);
237 } finally {
238 if (reader != null) {
239 this.closeStream(reader);
240 }
241 }
242 }
243 }
244
245
246
247
248
249
250
251
252
253 private void closeStream(final Reader reader)
254 throws ExecutingCommandException {
255 try {
256 reader.close();
257 } catch (final IOException exception4) {
258
259 throw new ExecutingCommandException(exception4);
260 }
261 }
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276 @Override
277 public ExecutionState
278 final OutputWriter
279 if (command == null) {
280 throw new ParameterNullException("command");
281 }
282 if (writer == null) {
283 throw new ParameterNullException("writer");
284 }
285
286
287
288 ExecutionState state = ExecutionState.INITIATED;
289
290 ImplementationExecuter.LOGGER.info(Messages
291 .getString("ImplementationExecuter.ToExecute"),
292 command);
293
294 if (this.isAnExitToken(command)) {
295 state = ExecutionState.APPLICATION_EXIT;
296 } else {
297 state = ExecutionState.EXECUTED;
298 if (!command.equals("")) {
299
300
301 final String[] commands = this.getCommandsToExecute(command);
302
303 Process process;
304 try {
305 process = Runtime.getRuntime().exec(commands);
306 } catch (final IOException exception) {
307
308 this.catchError(command, exception);
309 throw new ExecutingCommandException(exception);
310 }
311
312
313
314 this.printStream(writer, process.getInputStream());
315 this.printStream(writer, process.getErrorStream());
316
317 int value;
318 try {
319 value = process.waitFor();
320 } catch (final InterruptedException exception) {
321 throw new ExecutingCommandException(exception);
322 }
323
324 ImplementationExecuter.LOGGER.info(Messages
325 .getString("ImplementationExecuter.ErrorCode")
326 + value);
327 process.destroy();
328 }
329 }
330
331 ImplementationExecuter.LOGGER.debug("{} -> {}", command, state);
332
333 assert state != null;
334 return state;
335 }
336
337
338
339
340
341
342
343
344
345
346
347
348 private String[] executeNativeDB2Command(final String db2params,
349 final String db2Command, final boolean thereAreDB2Params) {
350 String[] commands;
351
352 if (thereAreDB2Params) {
353 ImplementationExecuter.LOGGER
354 .debug("db2 command natively with params");
355 commands = new String[] { "db2", db2params, db2Command };
356 } else {
357
358 ImplementationExecuter.LOGGER
359 .debug("db2 command natively without params");
360 commands = new String[] { "db2", db2Command };
361 }
362 return commands;
363 }
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378 private String[] executeOtherCommand(final String db2params,
379 final boolean isADB2Command, final String db2Command,
380 final boolean thereAreDB2Params) {
381 String[] commands;
382
383
384
385 final String os = System.getProperty("os.name");
386 ImplementationExecuter.LOGGER.info(Messages
387 .getString("ImplementationExecuter.Executing"), os);
388
389 if (os.startsWith("Linux")) {
390 commands = this.getLinuxCommand(isADB2Command, thereAreDB2Params,
391 db2params, db2Command);
392 } else
393
394 if (os.startsWith("Windows")) {
395 commands = this.getWindowsCommand(isADB2Command, thereAreDB2Params,
396 db2params, db2Command);
397 } else {
398
399
400 ImplementationExecuter.LOGGER.warn(Messages
401 .getString("ImplementationExecuter.NoShell"));
402 commands = new String[] { db2Command };
403 }
404 return commands;
405 }
406
407
408
409
410
411
412
413
414 private String
415 assert command != null;
416
417 String newCommand = command;
418 final String db2 = "db2 ";
419 if (command.startsWith(db2)) {
420 newCommand = command.substring(db2.length());
421 }
422
423 assert newCommand != null;
424 return newCommand;
425 }
426
427
428
429
430
431
432
433
434
435
436
437 String[]
438 throws ParameterNullException {
439 if (command == null) {
440 throw new ParameterNullException("command");
441 }
442
443
444 final boolean db2Native = Boolean.parseBoolean(Configurator
445 .getInstance().getProperty(
446 ImplementationExecuter.DB2_NATIVE_PROPERTY));
447 final String db2params = System
448 .getProperty(ImplementationExecuter.DB2_PARAMS);
449
450 final boolean isADB2Command = command.startsWith("db2 ");
451
452 final String db2Command = this.extractDB2Part(command);
453
454 boolean thereAreDB2Params = false;
455 if (!command.equals(db2Command)) {
456
457 thereAreDB2Params = (db2params != null) && !db2params.equals("");
458 }
459
460 String[] commands = null;
461
462
463
464 if (db2Native && isADB2Command) {
465 commands = this.executeNativeDB2Command(db2params, db2Command,
466 thereAreDB2Params);
467 } else {
468 commands = this.executeOtherCommand(db2params, isADB2Command,
469 db2Command, thereAreDB2Params);
470 }
471
472 assert commands != null;
473 if (this.assertsEnabled) {
474 for (final String retCommand : commands) {
475 assert retCommand != null;
476 }
477 }
478 return commands;
479 }
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494 private String[]
495 final boolean thereAreDB2Params, final String
496 final String
497 assert db2Command != null;
498
499 String[] commands;
500
501 if (isADB2Command) {
502
503 if (thereAreDB2Params) {
504 ImplementationExecuter.LOGGER
505 .debug("Linux and db2 command with params");
506 commands = new String[] { "db2", db2params, db2Command };
507 } else {
508
509 ImplementationExecuter.LOGGER
510 .debug("Linux and db2 command without params");
511 commands = new String[] { "db2", db2Command };
512 }
513 } else {
514
515 ImplementationExecuter.LOGGER.debug("Linux and NOT db2 command");
516 commands = new String[] { db2Command };
517 }
518
519 assert commands != null;
520 if (this.assertsEnabled) {
521 for (final String retCommand : commands) {
522 assert retCommand != null;
523 }
524 }
525 return commands;
526 }
527
528
529
530
531
532
533 String
534 assert this.tokens != null;
535 return this.tokens;
536 }
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551 private String[]
552 final boolean thereAreDB2Params, final String
553 final String
554 assert db2Command != null;
555
556 String[] commands;
557 if (isADB2Command) {
558 if (thereAreDB2Params) {
559 ImplementationExecuter.LOGGER
560 .debug("Windows and db2 command with params");
561 commands = new String[] { "cmd.exe", "/c", "db2", db2params,
562 db2Command };
563 } else {
564 ImplementationExecuter.LOGGER
565 .debug("Windows and db2 command without params");
566 commands = new String[] { "cmd.exe", "/c", "db2", db2Command };
567 }
568 } else {
569 ImplementationExecuter.LOGGER.debug("Windows and NOT db2 command");
570 commands = new String[] { "cmd.exe", "/c", db2Command };
571 }
572
573 assert commands != null;
574 if (this.assertsEnabled) {
575 for (final String retCommand : commands) {
576 assert retCommand != null;
577 }
578 }
579 return commands;
580 }
581
582
583
584
585
586
587
588
589 private boolean isAnExitToken(final String
590 assert command != null;
591
592 boolean exit = false;
593 final StringTokenizer pharseTokens = new StringTokenizer(this.tokens);
594 while (pharseTokens.hasMoreElements() && !exit) {
595 if (command.compareToIgnoreCase(pharseTokens.nextToken()) == 0) {
596 exit = true;
597 }
598 }
599 return exit;
600 }
601
602
603
604
605
606
607
608
609
610
611
612
613 private void printStream(final OutputWriter
614 final InputStream
615 assert writer != null;
616 assert inputStream != null;
617
618 final Reader reader = new InputStreamReader(inputStream);
619 final BufferedReader bfStream = new BufferedReader(reader);
620
621 String current;
622
623 try {
624 current = bfStream.readLine();
625 while (current != null) {
626 writer.writeLine(current);
627 current = bfStream.readLine();
628 }
629 } catch (final IOException exception1) {
630
631 throw new ExecutingCommandException(exception1);
632 } finally {
633 this.closeObjects(inputStream, reader, bfStream);
634 }
635 }
636 }