annotate SMART/Java/ProgramLauncher.java @ 33:aa0420172fc6

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