annotate SMART/Java/Sav/ProgramLauncher.java @ 6:769e306b7933

Change the repository level.
author yufei-luo
date Fri, 18 Jan 2013 04:54:14 -0500
parents
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
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 public ProgramLauncher (LinkedList <String> c, JTextArea la, JLabel mf, JProgressBar pb, JLabel ef) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 command = new String[c.size()];
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 logArea = la;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 messageField = mf;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 progressBar = pb;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 etaField = ef;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 exitValue = -1;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 c.toArray(command);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 public ProgramLauncher (String[] c, JTextArea la, JLabel mf, JProgressBar pb, JLabel ef) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 command = c;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 logArea = la;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 messageField = mf;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 progressBar = pb;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 etaField = ef;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 exitValue = -1;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 @Override
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 public Boolean doInBackground() {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 ProcessBuilder pb = new ProcessBuilder(command);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 Process process = null;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 BufferedReader outputReader = null;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 pb = pb.redirectErrorStream(true);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 Map<String, String> env = pb.environment();
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 env.put("PYTHONPATH", System.getProperty("user.dir") + java.io.File.separator + "Python");
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 env.put("SMARTPATH", System.getProperty("user.dir") + java.io.File.separator + "Python");
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 env.put("SMARTMYSQLPATH", Global.mysqlCommand);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 env.put("SMARTRPATH", Global.rCommand);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 String commandJoined = Arrays.toString(command);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 try {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 publish("=== Starting command '" + commandJoined.trim() + "' ===\n");
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 process = pb.start();
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 BufferedInputStream outputStream = new BufferedInputStream(process.getInputStream());
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 InputStream is = process.getInputStream();
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 InputStreamReader isr = new InputStreamReader(is);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 outputReader = new BufferedReader(isr);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 catch (Exception exception) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 publish("!Process cannot be started (command is '" + commandJoined + "')!\n");
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 exception.printStackTrace();
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 return Boolean.FALSE;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 if (outputReader == null) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 publish("!Problem in the output of the command!\n");
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 return Boolean.FALSE;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 else {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 try {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 String line;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 while ((line = outputReader.readLine()) != null) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 publish(line + "\n");
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 catch (IOException e) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 e.printStackTrace();
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 publish("!Cannot get the output of the command!\n");
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 return Boolean.FALSE;
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 try {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 process.waitFor();
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 catch (InterruptedException e) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 e.printStackTrace();
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 publish("!Cannot wait for the end of the command!\n");
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 return Boolean.FALSE;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 try {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 exitValue = process.exitValue();
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 System.out.println(exitValue);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 catch (IllegalThreadStateException e) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 e.printStackTrace();
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 publish("!Cannot get the exit value of the command!\n");
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 return Boolean.FALSE;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 if (exitValue != 0) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 publish("!Problem during the execution of the command '" + commandJoined + "'!\n");
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 return Boolean.FALSE;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 publish("=== Ending command '" + commandJoined.trim() + "' ===\n");
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 return Boolean.TRUE;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 @Override
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 protected void process(List<String> chunks) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 String message = "";
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 String text = logArea.getText();
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 for (String chunk: chunks) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 text += chunk;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 for (String lineSeparatedByCarriageReturn: text.split("\n")) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 for (String line: lineSeparatedByCarriageReturn.split("\r")) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 boolean progressLine = false;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 if (line.matches(".*\\[=*\\s*\\]\\s*\\d*/\\d*\\s*")) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 String[] ratioElements = line.split("\\]")[1].trim().split("/");
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 int current = Integer.parseInt(ratioElements[0].trim());
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 int aim = Integer.parseInt(ratioElements[1].trim());
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 messageField.setText(line.split("\\[")[0].trim());
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 progressBar.setValue(current * 100 / aim);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 etaField.setText("");
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 progressLine = true;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 else if (line.matches(".*\\[=*\\s*\\]\\s*\\d*/\\d*\\s*ETA:\\s*.*")) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 String[] ratioElements = line.split("\\]")[1].split("E")[0].trim().split("/");
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 int current = Integer.parseInt(ratioElements[0].trim());
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 int aim = Integer.parseInt(ratioElements[1].trim());
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 String eta = line.split("ETA:")[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("ETA: " + eta);
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*\\s*completed in.*")) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 String nbElements = line.split("\\]")[1].split("completed")[0].trim();
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 String timeSpent = line.split("completed in")[1].trim();
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 message += line.split("\\[")[0].trim() + ": " + nbElements + " elements completed in " + timeSpent + "\n";
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 messageField.setText(line.split("\\[")[0].trim());
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 progressLine = true;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175 if (! progressLine) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176 message += line + "\n";
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 String lines[] = message.split("\n");
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181 String toBeWritten = "";
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182 for (int i = Math.max(0, lines.length - Global.logAreaSize); i < lines.length; i++) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183 toBeWritten += lines[i] + "\n";
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185 logArea.setText(toBeWritten);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188 public int getExitValue() {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 return exitValue;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191 }