01: package com.jat.core.installer.util;
02:
03: import java.io.BufferedReader;
04: import java.io.IOException;
05: import java.io.InputStream;
06: import java.io.InputStreamReader;
07:
08: import com.jat.core.log.LogManager;
09:
10: /**
11: * <p>Title: JAT</p>
12: * <p>Description:</p>
13: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
14: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
15: * @author stf
16: * @version 1.1
17: * @since 1.2
18: */
19:
20: class StreamGobbler extends Thread {
21: InputStream is;
22: String type;
23:
24: StreamGobbler(InputStream is, String type) {
25: this .is = is;
26: this .type = type;
27: }
28:
29: public void run() {
30: try {
31: InputStreamReader isr = new InputStreamReader(is);
32: BufferedReader br = new BufferedReader(isr);
33: String line = null;
34: while ((line = br.readLine()) != null) {
35: LogManager.sendDebug(type + ">" + line);
36: }
37: } catch (IOException ioe) {
38: ioe.printStackTrace();
39: }
40: }
41: }
42:
43: public class CommandExec {
44: public static void execute(String args[]) throws Exception {
45: if (args.length < 1) {
46: System.exit(1);
47: }
48:
49: try {
50: Runtime rt = Runtime.getRuntime();
51: String debug = "Executing ";
52: for (int i = 0; i < args.length; i++) {
53: debug = debug + args[i] + " ";
54: }
55: debug += "\n";
56: LogManager.sendDebug(debug);
57: Process proc = rt.exec(args);
58: // any error message?
59: StreamGobbler errorGobbler = new StreamGobbler(proc
60: .getErrorStream(), "ERROR");
61:
62: // any output?
63: StreamGobbler outputGobbler = new StreamGobbler(proc
64: .getInputStream(), "OUTPUT");
65:
66: // kick them off
67: errorGobbler.start();
68: outputGobbler.start();
69:
70: // any error???
71: int exitVal = proc.waitFor();
72: LogManager.sendDebug("ExitValue: " + exitVal);
73: if (exitVal != 0) {
74: LogManager.sendError("Installer error: exitVal '"
75: + exitVal + "'" + " execute command '" + debug
76: + "'");
77: throw new Exception(
78: "Installer error executing command '" + args[0]
79: + "'. Exit value: " + exitVal);
80: }
81: } catch (Throwable t) {
82: throw new Exception(t);
83: }
84: }
85: }
|