|
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 package com.symbian.smt.gui.builder; |
|
17 |
|
18 import java.io.BufferedReader; |
|
19 import java.io.IOException; |
|
20 import java.io.InputStreamReader; |
|
21 import java.io.OutputStream; |
|
22 import java.util.List; |
|
23 |
|
24 import org.eclipse.swt.widgets.Display; |
|
25 |
|
26 import com.symbian.smt.gui.Logger; |
|
27 import com.symbian.smt.gui.views.ConsoleOutput; |
|
28 |
|
29 public class SMTProcess { |
|
30 |
|
31 /** |
|
32 * Runs the command line base System Model Generator |
|
33 * |
|
34 * @param List |
|
35 * <String> Arguments for the CLI |
|
36 * @return int The exit code from the System Model Generator |
|
37 */ |
|
38 public int run(List<String> command) { |
|
39 int result = 0; |
|
40 |
|
41 // Reset the console |
|
42 Display.getDefault().asyncExec(new Runnable() { |
|
43 public void run() { |
|
44 ConsoleOutput.reset(); |
|
45 |
|
46 } |
|
47 }); |
|
48 |
|
49 // Print the command line string to the console output |
|
50 StringBuilder commandLineString = new StringBuilder(); |
|
51 |
|
52 for (String item : command) { |
|
53 commandLineString.append(item.concat(" ")); |
|
54 } |
|
55 |
|
56 writeToConsoleOutput("Executing '" + commandLineString.toString() + "'"); |
|
57 |
|
58 // First we check that Perl is available |
|
59 ProcessBuilder pbCheckPerl = new ProcessBuilder("perl", "-v"); |
|
60 pbCheckPerl.redirectErrorStream(true); |
|
61 |
|
62 try { |
|
63 pbCheckPerl.start(); |
|
64 } catch (IOException e) { |
|
65 writeToConsoleOutput("Error: Perl is not installed"); |
|
66 return 9009; // Same exit code as Windows produces for a program not |
|
67 // found |
|
68 } |
|
69 |
|
70 ProcessBuilder pb = new ProcessBuilder(command); |
|
71 |
|
72 // Redirect STDERR to STDOUT |
|
73 pb.redirectErrorStream(true); |
|
74 |
|
75 try { |
|
76 // Start the process |
|
77 final Process p = pb.start(); |
|
78 |
|
79 // Get and close the process STDIN |
|
80 OutputStream out = p.getOutputStream(); |
|
81 out.close(); |
|
82 |
|
83 // Create a reader to read from the process STDOUT |
|
84 BufferedReader inReader = new BufferedReader(new InputStreamReader( |
|
85 p.getInputStream())); |
|
86 |
|
87 // Print STDOUT to the command output view |
|
88 String line; |
|
89 while ((line = inReader.readLine()) != null) { |
|
90 writeToConsoleOutput(line); |
|
91 } |
|
92 |
|
93 // Close the process STDOUT pipe when finished |
|
94 inReader.close(); |
|
95 |
|
96 try { |
|
97 // Ensure the process has finished and get the exit code |
|
98 result = p.waitFor(); |
|
99 } catch (InterruptedException e) { |
|
100 Logger.log(e.getMessage(), e); |
|
101 } finally { |
|
102 // Destroy the process |
|
103 p.destroy(); |
|
104 } |
|
105 } catch (IOException e) { |
|
106 Logger.log(e.getMessage(), e); |
|
107 } |
|
108 |
|
109 return result; |
|
110 |
|
111 } |
|
112 |
|
113 private void writeToConsoleOutput(final String string) { |
|
114 Display.getDefault().asyncExec(new Runnable() { |
|
115 public void run() { |
|
116 ConsoleOutput.addText(string); |
|
117 } |
|
118 }); |
|
119 } |
|
120 } |