01: /******************************************************************************
02: * Copyright (C) Lars Ivar Almli. All rights reserved. *
03: * ---------------------------------------------------------------------------*
04: * This file is part of MActor. *
05: * *
06: * MActor is free software; you can redistribute it and/or modify *
07: * it under the terms of the GNU General Public License as published by *
08: * the Free Software Foundation; either version 2 of the License, or *
09: * (at your option) any later version. *
10: * *
11: * MActor is distributed in the hope that it will be useful, *
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14: * GNU General Public License for more details. *
15: * *
16: * You should have received a copy of the GNU General Public License *
17: * along with MActor; if not, write to the Free Software *
18: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
19: ******************************************************************************/package org.mactor.framework.commandexecutors.shell;
20:
21: import java.io.BufferedInputStream;
22: import java.io.BufferedReader;
23: import java.io.IOException;
24: import java.io.InputStreamReader;
25: import org.mactor.framework.MactorException;
26:
27: public class ExecUtil {
28: public static void executeCommand(String command)
29: throws MactorException {
30: try {
31: Runtime r = Runtime.getRuntime();
32: Process p = r.exec(command);
33: int exitCode = p.waitFor();
34: if (exitCode != 0) {
35: throw new MactorException("Command '" + command
36: + "' failed. Exit code: '" + exitCode + "'");
37: }
38: } catch (IOException ioe) {
39: throw new MactorException("Failed to execute command '"
40: + command + "'. Error:" + ioe.getMessage(), ioe);
41: } catch (InterruptedException ie) {
42: throw new MactorException("Failed to execute command '"
43: + command + "'. Error:" + ie.getMessage(), ie);
44: }
45: }
46:
47: public static String executeCommandWithOutput(String command)
48: throws MactorException {
49: StringBuffer sb = new StringBuffer();
50: try {
51: Runtime r = Runtime.getRuntime();
52: Process p = r.exec(command);
53: BufferedInputStream bis = new BufferedInputStream(p
54: .getInputStream());
55: BufferedReader br = new BufferedReader(
56: new InputStreamReader(bis));
57: char[] buffer = new char[1000];
58: for (;;) {
59: int count = br.read(buffer, 0, buffer.length);
60: if (count > 0) {
61: sb.append(buffer, 0, count);
62: } else {
63: break;
64: }
65: }
66: int exitCode = p.waitFor();
67: if (exitCode != 0) {
68: throw new MactorException("Command '" + command
69: + "' failed. Exit code: '" + exitCode + "'");
70: }
71: return sb.toString();
72: } catch (IOException ioe) {
73: throw new MactorException("Failed to execute command '"
74: + command + "'. Error:" + ioe.getMessage(), ioe);
75: } catch (InterruptedException ie) {
76: throw new MactorException("Failed to execute command '"
77: + command + "'. Error:" + ie.getMessage(), ie);
78: }
79: }
80: }
|