|
19 | 19 |
|
20 | 20 | package com.cloud.utils.script; |
21 | 21 |
|
22 | | -import com.cloud.utils.PropertiesUtil; |
23 | | -import com.cloud.utils.concurrency.NamedThreadFactory; |
24 | | -import com.cloud.utils.script.OutputInterpreter.TimedOutLogger; |
25 | | -import org.apache.log4j.Logger; |
26 | | - |
27 | 22 | import java.io.BufferedReader; |
28 | 23 | import java.io.File; |
29 | 24 | import java.io.FileInputStream; |
|
43 | 38 | import java.util.concurrent.ScheduledFuture; |
44 | 39 | import java.util.concurrent.TimeUnit; |
45 | 40 |
|
| 41 | +import org.apache.commons.io.IOUtils; |
| 42 | +import org.apache.log4j.Logger; |
| 43 | + |
| 44 | +import com.cloud.utils.PropertiesUtil; |
| 45 | +import com.cloud.utils.concurrency.NamedThreadFactory; |
| 46 | +import com.cloud.utils.script.OutputInterpreter.TimedOutLogger; |
| 47 | + |
46 | 48 | public class Script implements Callable<String> { |
47 | 49 | private static final Logger s_logger = Logger.getLogger(Script.class); |
48 | 50 |
|
@@ -167,6 +169,16 @@ public String toString() { |
167 | 169 | return buildCommandLine(command); |
168 | 170 | } |
169 | 171 |
|
| 172 | + static String stackTraceAsString(Throwable throwable) { |
| 173 | + //TODO: a StringWriter is bit to heavy weight |
| 174 | + try(StringWriter out = new StringWriter(); PrintWriter writer = new PrintWriter(out);) { |
| 175 | + throwable.printStackTrace(writer); |
| 176 | + return out.toString(); |
| 177 | + } catch (IOException e) { |
| 178 | + return ""; |
| 179 | + } |
| 180 | + } |
| 181 | + |
170 | 182 | public String execute(OutputInterpreter interpreter) { |
171 | 183 | String[] command = _command.toArray(new String[_command.size()]); |
172 | 184 |
|
@@ -259,28 +271,15 @@ public String execute(OutputInterpreter interpreter) { |
259 | 271 | return error; |
260 | 272 | } catch (SecurityException ex) { |
261 | 273 | _logger.warn("Security Exception....not running as root?", ex); |
262 | | - StringWriter writer = new StringWriter(); |
263 | | - ex.printStackTrace(new PrintWriter(writer)); |
264 | | - return writer.toString(); |
| 274 | + return stackTraceAsString(ex); |
265 | 275 | } catch (Exception ex) { |
266 | 276 | _logger.warn("Exception: " + buildCommandLine(command), ex); |
267 | | - StringWriter writer = new StringWriter(); |
268 | | - ex.printStackTrace(new PrintWriter(writer)); |
269 | | - return writer.toString(); |
| 277 | + return stackTraceAsString(ex); |
270 | 278 | } finally { |
271 | 279 | if (_process != null) { |
272 | | - try { |
273 | | - _process.getErrorStream().close(); |
274 | | - } catch (IOException ex) { |
275 | | - } |
276 | | - try { |
277 | | - _process.getOutputStream().close(); |
278 | | - } catch (IOException ex) { |
279 | | - } |
280 | | - try { |
281 | | - _process.getInputStream().close(); |
282 | | - } catch (IOException ex) { |
283 | | - } |
| 280 | + IOUtils.closeQuietly(_process.getErrorStream()); |
| 281 | + IOUtils.closeQuietly(_process.getOutputStream()); |
| 282 | + IOUtils.closeQuietly(_process.getInputStream()); |
284 | 283 | _process.destroy(); |
285 | 284 | } |
286 | 285 | } |
@@ -318,23 +317,15 @@ public void run() { |
318 | 317 | try { |
319 | 318 | result = interpreter.interpret(reader); |
320 | 319 | } catch (IOException ex) { |
321 | | - StringWriter writer = new StringWriter(); |
322 | | - ex.printStackTrace(new PrintWriter(writer)); |
323 | | - result = writer.toString(); |
| 320 | + result = stackTraceAsString(ex); |
324 | 321 | } catch (Exception ex) { |
325 | | - StringWriter writer = new StringWriter(); |
326 | | - ex.printStackTrace(new PrintWriter(writer)); |
327 | | - result = writer.toString(); |
| 322 | + result = stackTraceAsString(ex); |
328 | 323 | } finally { |
329 | 324 | synchronized (this) { |
330 | 325 | done = true; |
331 | 326 | notifyAll(); |
332 | 327 | } |
333 | | - try { |
334 | | - reader.close(); |
335 | | - } catch (IOException ex) { |
336 | | - } |
337 | | - ; |
| 328 | + IOUtils.closeQuietly(reader); |
338 | 329 | } |
339 | 330 | } |
340 | 331 |
|
|
0 commit comments