comparison SMART/Java/ProgramLauncher.java @ 6:769e306b7933

Change the repository level.
author yufei-luo
date Fri, 18 Jan 2013 04:54:14 -0500
parents
children
comparison
equal deleted inserted replaced
5:ea3082881bf8 6:769e306b7933
1 /**
2 *
3 * Copyright INRA-URGI 2009-2010
4 *
5 * This software is governed by the CeCILL license under French law and
6 * abiding by the rules of distribution of free software. You can use,
7 * modify and/ or redistribute the software under the terms of the CeCILL
8 * license as circulated by CEA, CNRS and INRIA at the following URL
9 * "http://www.cecill.info".
10 *
11 * As a counterpart to the access to the source code and rights to copy,
12 * modify and redistribute granted by the license, users are provided only
13 * with a limited warranty and the software's author, the holder of the
14 * economic rights, and the successive licensors have only limited
15 * liability.
16 *
17 * In this respect, the user's attention is drawn to the risks associated
18 * with loading, using, modifying and/or developing or reproducing the
19 * software by the user in light of its specific status of free software,
20 * that may mean that it is complicated to manipulate, and that also
21 * therefore means that it is reserved for developers and experienced
22 * professionals having in-depth computer knowledge. Users are therefore
23 * encouraged to load and test the software's suitability as regards their
24 * requirements in conditions enabling the security of their systems and/or
25 * data to be ensured and, more generally, to use and operate it in the
26 * same conditions as regards security.
27 *
28 * The fact that you are presently reading this means that you have had
29 * knowledge of the CeCILL license and that you accept its terms.
30 *
31 */
32 import java.util.*;
33 import java.io.*;
34 import javax.swing.SwingUtilities;
35 import javax.swing.*;
36 import java.util.concurrent.CountDownLatch;
37
38 public class ProgramLauncher extends SwingWorker<Boolean, String> {
39
40 String[] command;
41 JTextArea logArea;
42 JLabel messageField;
43 JProgressBar progressBar;
44 JLabel etaField;
45 int exitValue;
46 CountDownLatch latch;
47
48
49
50 public ProgramLauncher (LinkedList <String> c, JTextArea la, JLabel mf, JProgressBar pb, JLabel ef) {
51 command = new String[c.size()];
52 logArea = la;
53 messageField = mf;
54 progressBar = pb;
55 etaField = ef;
56 exitValue = -1;
57 c.toArray(command);
58 latch = new CountDownLatch(1);
59 }
60
61
62 public ProgramLauncher (String[] c, JTextArea la, JLabel mf, JProgressBar pb, JLabel ef) {
63 command = c;
64 logArea = la;
65 messageField = mf;
66 progressBar = pb;
67 etaField = ef;
68 exitValue = -1;
69 latch = new CountDownLatch(1);
70 }
71
72
73 @Override
74 public Boolean doInBackground() {
75 ProcessBuilder pb = new ProcessBuilder(command);
76 Process process = null;
77 BufferedReader outputReader = null;
78 pb = pb.redirectErrorStream(true);
79 Map<String, String> env = pb.environment();
80 env.put("PYTHONPATH", System.getProperty("user.dir"));
81 env.put("SMARTPATH", System.getProperty("user.dir") + java.io.File.separator + "SMART" + java.io.File.separator + "Java" + java.io.File.separator + "Python");
82 env.put("SMARTMYSQLPATH", Global.mysqlCommand);
83 env.put("SMARTRPATH", Global.rCommand);
84 String commandJoined = Arrays.toString(command);
85
86 try {
87 publish("=== Starting command '" + commandJoined.trim() + "' ===\n");
88 process = pb.start();
89
90 BufferedInputStream outputStream = new BufferedInputStream(process.getInputStream());
91 InputStream is = process.getInputStream();
92 InputStreamReader isr = new InputStreamReader(is);
93 outputReader = new BufferedReader(isr);
94 }
95 catch (Exception exception) {
96 publish("!Process cannot be started (command is '" + commandJoined + "')!\n");
97 exception.printStackTrace();
98 latch.countDown();
99 return Boolean.FALSE;
100 }
101 if (outputReader == null) {
102 publish("!Problem in the output of the command!\n");
103 latch.countDown();
104 return Boolean.FALSE;
105 }
106 else {
107 try {
108 String line;
109 while ((line = outputReader.readLine()) != null) {
110 publish(line + "\n");
111 }
112 }
113 catch (IOException e) {
114 e.printStackTrace();
115 publish("!Cannot get the output of the command!\n");
116 latch.countDown();
117 return Boolean.FALSE;
118 }
119 }
120 try {
121 process.waitFor();
122 }
123 catch (InterruptedException e) {
124 e.printStackTrace();
125 publish("!Cannot wait for the end of the command!\n");
126 latch.countDown();
127 return Boolean.FALSE;
128 }
129 try {
130 exitValue = process.exitValue();
131 }
132 catch (IllegalThreadStateException e) {
133 e.printStackTrace();
134 publish("!Cannot get the exit value of the command!\n");
135 latch.countDown();
136 return Boolean.FALSE;
137 }
138 if (exitValue != 0) {
139 publish("!Problem during the execution of the command '" + commandJoined + "'!\n");
140 latch.countDown();
141 return Boolean.FALSE;
142 }
143 publish("=== Ending command '" + commandJoined.trim() + "' ===\n");
144 latch.countDown();
145 return Boolean.TRUE;
146 }
147
148
149 @Override
150 protected void process(List<String> chunks) {
151 String message = "";
152 String text = logArea.getText();
153 for (String chunk: chunks) {
154 text += chunk;
155 }
156 for (String lineSeparatedByCarriageReturn: text.split("\n")) {
157 for (String line: lineSeparatedByCarriageReturn.split("\r")) {
158 boolean progressLine = false;
159 if (line.matches(".*\\[=*\\s*\\]\\s*\\d*/\\d*\\s*")) {
160 String[] ratioElements = line.split("\\]")[1].trim().split("/");
161 int current = Integer.parseInt(ratioElements[0].trim());
162 int aim = Integer.parseInt(ratioElements[1].trim());
163 messageField.setText(line.split("\\[")[0].trim());
164 progressBar.setValue(current * 100 / aim);
165 etaField.setText("");
166 progressLine = true;
167 }
168 else if (line.matches(".*\\[=*\\s*\\]\\s*\\d*/\\d*\\s*ETA:\\s*.*")) {
169 String[] ratioElements = line.split("\\]")[1].split("E")[0].trim().split("/");
170 int current = Integer.parseInt(ratioElements[0].trim());
171 int aim = Integer.parseInt(ratioElements[1].trim());
172 String eta = line.split("ETA:")[1].trim();
173 messageField.setText(line.split("\\[")[0].trim());
174 progressBar.setValue(current * 100 / aim);
175 etaField.setText("ETA: " + eta);
176 progressLine = true;
177 }
178 else if (line.matches(".*\\[=*\\s*\\]\\s*\\d*\\s*completed in.*")) {
179 String nbElements = line.split("\\]")[1].split("completed")[0].trim();
180 String timeSpent = line.split("completed in")[1].trim();
181 message += line.split("\\[")[0].trim() + ": " + nbElements + " elements completed in " + timeSpent + "\n";
182 messageField.setText(line.split("\\[")[0].trim());
183 progressLine = true;
184 }
185 if (! progressLine) {
186 message += line + "\n";
187 }
188 }
189 }
190 String lines[] = message.split("\n");
191 String toBeWritten = "";
192 for (int i = Math.max(0, lines.length - Global.logAreaSize); i < lines.length; i++) {
193 toBeWritten += lines[i] + "\n";
194 }
195 logArea.setText(toBeWritten);
196 }
197
198 public int getExitValue() {
199 try {
200 latch.await();
201 }
202 catch (InterruptedException e) {
203 logArea.append("Cannot wait for the end of the process!\n");
204 e.printStackTrace();
205 return -1;
206 }
207 return exitValue;
208 }
209 }