$darkmode
DENOPTIM
ProcessHandler.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2019 Vishwesh Venkatraman <vishwesh.venkatraman@ntnu.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.task;
20
21import java.io.IOException;
22import java.util.ArrayList;
23import java.util.Arrays;
24import java.util.List;
25
26import denoptim.exception.DENOPTIMException;
27
32public class ProcessHandler
33{
34 private int exitCode;
35 private transient Process proc = null;
36
40 private String standardOutput;
41
45 private String errorOutput;
46
50 @SuppressWarnings("unused")
51 private String id = null;
52
53 private String cmdStr = null;
54
55
56//------------------------------------------------------------------------------
57
58 public ProcessHandler(String cmdStr, String id)
59 {
60 this.cmdStr = cmdStr;
61 this.id = id;
62 }
63
64 //------------------------------------------------------------------------------
65
78 {
79 try
80 {
81 ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", cmdStr);
82 proc = pb.start();
83
84 Runtime.getRuntime().addShutdownHook(new Thread()
85 {
86 @Override
87 public void run()
88 {
89 proc.destroy();
90 }
91 });
92
93 // Any error message?
94 StreamGobbler errorGobbler =
95 new StreamGobbler(proc.getErrorStream(), "ERR");
96
97 // Any output?
98 StreamGobbler outputGobbler =
99 new StreamGobbler(proc.getInputStream(), "OUT");
100
101 errorGobbler.start();
102 outputGobbler.start();
103
104 errorGobbler.join();
105 outputGobbler.join();
106
107 exitCode = proc.waitFor();
108
109 standardOutput = outputGobbler.getMessages();
110 outputGobbler.sb.setLength(0);
111 errorOutput = errorGobbler.getMessages();
112 errorGobbler.sb.setLength(0);
113
114 }
115 catch(IllegalArgumentException iae)
116 {
117 if (proc!=null)
118 proc.destroy();
119 throw new DENOPTIMException(iae);
120 }
121 catch(NullPointerException npe)
122 {
123 if (proc!=null)
124 proc.destroy();
125 throw new DENOPTIMException(npe);
126 }
127 catch(IOException ioe)
128 {
129 if (proc!=null)
130 proc.destroy();
131 throw new DENOPTIMException(ioe);
132 }
133 catch(Exception e)
134 {
135 if (proc!=null)
136 proc.destroy();
137 throw new DENOPTIMException(e);
138 }
139 finally
140 {
141 if (proc != null)
142 {
143 try {
144 proc.getOutputStream().close();
145 proc.getInputStream().close();
146 proc.getErrorStream().close();
147 proc.destroy();
148 } catch (IOException e) {
149 e.printStackTrace();
150 }
151 }
152 }
153 }
154
155//------------------------------------------------------------------------------
156
168 public void runProcess() throws DENOPTIMException
169 {
170 try
171 {
172 List<String> command = new ArrayList<String>(Arrays.asList(
173 cmdStr.split("\\s+")));
174 ProcessBuilder pb = new ProcessBuilder(command);
175 //NB: this leads to an IOException with the following message:
176 // CreateProcess error=2, The system cannot find the file specified
177 //ProcessBuilder pb = new ProcessBuilder(cmdStr);
178 proc = pb.start();
179
180 Runtime.getRuntime().addShutdownHook(new Thread()
181 {
182 @Override
183 public void run()
184 {
185 proc.destroy();
186 }
187 });
188
189 // Any error message?
190 StreamGobbler errorGobbler =
191 new StreamGobbler(proc.getErrorStream(), "ERR");
192
193 // Any output?
194 StreamGobbler outputGobbler =
195 new StreamGobbler(proc.getInputStream(), "OUT");
196
197 errorGobbler.start();
198 outputGobbler.start();
199
200 errorGobbler.join();
201 outputGobbler.join();
202
203 exitCode = proc.waitFor();
204
205 standardOutput = outputGobbler.getMessages();
206 outputGobbler.sb.setLength(0);
207 errorOutput = errorGobbler.getMessages();
208 errorGobbler.sb.setLength(0);
209
210 }
211 catch(IllegalArgumentException iae)
212 {
213 if (proc!=null)
214 proc.destroy();
215 throw new DENOPTIMException(iae);
216 }
217 catch(NullPointerException npe)
218 {
219 if (proc!=null)
220 proc.destroy();
221 throw new DENOPTIMException(npe);
222 }
223 catch(IOException ioe)
224 {
225 if (proc!=null)
226 proc.destroy();
227 throw new DENOPTIMException(ioe);
228 }
229 catch(Exception e)
230 {
231 if (proc!=null)
232 proc.destroy();
233 throw new DENOPTIMException(e);
234 }
235 finally
236 {
237 if (proc != null)
238 {
239 try {
240 proc.getOutputStream().close();
241 proc.getInputStream().close();
242 proc.getErrorStream().close();
243 proc.destroy();
244 } catch (IOException e) {
245 e.printStackTrace();
246 }
247 }
248 }
249 }
250
251//------------------------------------------------------------------------------
252
257 public String getStandardOutput()
258 {
259 return standardOutput;
260 }
261
262//------------------------------------------------------------------------------
263
268 public String getErrorOutput()
269 {
270 return errorOutput;
271 }
272
273//------------------------------------------------------------------------------
274
281 public int getExitCode()
282 {
283 return exitCode;
284 }
285
286//------------------------------------------------------------------------------
287
288 public void stopProcess()
289 {
290 try
291 {
292 if (proc != null)
293 {
294 proc.getOutputStream().close();
295 proc.getInputStream().close();
296 proc.getErrorStream().close();
297 proc.destroy();
298 }
299 }
300 catch (Throwable t)
301 {
302 // do nothing
303 }
304 }
305
306//------------------------------------------------------------------------------
307
308}
void runProcess()
Run the process associated with the command.
String errorOutput
Content of the error output for the process.
int getExitCode()
Get the exit code returned by the sub-process.
String getErrorOutput()
Get the content of the error output for the process.
void runProcessInBASH()
Run the process associated with the command from BASH http://www.javaworld.com/javaworld/jw-12-2000/j...
String id
A user-assigned id for this task.
String standardOutput
Content of the standard output for the process.
String getStandardOutput()
Get the content of the standard output for the process.
See http://www.javaworld.com/jw-12-2000/jw-1229-traps.html?page=4.