001: /*
002: Copyright (C) 2004 David Bucciarelli (davibu@interfree.it)
003:
004: This program is free software; you can redistribute it and/or
005: modify it under the terms of the GNU General Public License
006: as published by the Free Software Foundation; either version 2
007: of the License, or (at your option) any later version.
008:
009: This program is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: GNU General Public License for more details.
013:
014: You should have received a copy of the GNU General Public License
015: along with this program; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: */
018:
019: package org.homedns.dade.jcgrid.util;
020:
021: import java.io.*;
022:
023: import org.apache.log4j.*;
024:
025: public class ConsoleCmd extends Object {
026: private final static String className = ConsoleCmd.class.getName();
027: private static Logger log = Logger.getLogger(className);
028: private static Logger logDetail = Logger.getLogger("DETAIL."
029: + className);
030:
031: //--------------------------------------------------------------------------
032:
033: private class WritterThread extends Thread {
034: private OutputStream os;
035: private byte[] data;
036:
037: public WritterThread(OutputStream osVal, byte[] dataVal) {
038: os = osVal;
039: data = dataVal;
040: }
041:
042: public void run() {
043: try {
044: if (data != null) {
045: os.write(data);
046: os.flush();
047: }
048:
049: os.close();
050: } catch (Throwable ex) {
051: log.warn("Exception in ConsoleCmd.WritterThread.run()",
052: ex);
053: }
054: }
055: }
056:
057: private class ReaderThread extends Thread {
058: private InputStream is;
059: private byte[] data;
060:
061: public ReaderThread(InputStream isVal) {
062: is = new BufferedInputStream(isVal);
063: }
064:
065: public void run() {
066: try {
067: data = ByteVector.readAll(is);
068: } catch (Throwable ex) {
069: log.warn("Exception in ConsoleCmd.ReaderThread.run()",
070: ex);
071: }
072: }
073:
074: public byte[] getData() {
075: return data;
076: }
077: }
078:
079: //--------------------------------------------------------------------------
080:
081: private String[] cmd;
082: private Process proc;
083: private byte[] stdin;
084: private byte[] stdout;
085: private byte[] stderr;
086: private File curDir;
087:
088: public ConsoleCmd(String[] cmdVal, byte[] stdinVal, String dir) {
089: stdin = stdinVal;
090: cmd = cmdVal;
091: proc = null;
092:
093: if (dir == null)
094: curDir = null;
095: else
096: curDir = new File(dir);
097: }
098:
099: public ConsoleCmd(String[] cmdVal, byte[] stdinVal) {
100: this (cmdVal, stdinVal, null);
101: }
102:
103: public ConsoleCmd(String[] cmdVal) {
104: this (cmdVal, null, null);
105: }
106:
107: public String getCmdLine() {
108: StringBuffer sb = new StringBuffer();
109:
110: boolean first = true;
111: for (int i = 0; i < cmd.length; i++) {
112: if (first)
113: first = false;
114: else
115: sb.append(" ");
116:
117: sb.append(cmd[i]);
118: }
119:
120: return sb.toString();
121: }
122:
123: public byte[] getStdOut() {
124: return stdout;
125: }
126:
127: public byte[] getStdErr() {
128: return stderr;
129: }
130:
131: public int run() throws IOException, InterruptedException {
132: if (curDir == null)
133: proc = Runtime.getRuntime().exec(cmd);
134: else
135: proc = Runtime.getRuntime().exec(cmd, null, curDir);
136:
137: OutputStream si = proc.getOutputStream();
138: InputStream so = proc.getInputStream();
139: InputStream se = proc.getErrorStream();
140:
141: // Thread stdin
142:
143: WritterThread it = new WritterThread(si, stdin);
144: it.start();
145:
146: // Thread stdout
147:
148: ReaderThread ot = new ReaderThread(so);
149: ot.start();
150:
151: // Thread stderr
152:
153: ReaderThread et = new ReaderThread(se);
154: et.start();
155:
156: // Wait execution end
157:
158: int exitVal = proc.waitFor();
159: it.join();
160: ot.join();
161: et.join();
162:
163: stdout = ot.getData();
164: stderr = et.getData();
165:
166: return exitVal;
167: }
168: }
|