001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.installer;
021:
022: import java.io.BufferedReader;
023: import java.io.File;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.InputStreamReader;
027: import java.io.OutputStream;
028: import java.io.Reader;
029: import java.io.StringReader;
030:
031: /**
032: * RunCommand execute the command text and returns the
033: * status of execution and output and error messages
034: * associated with the execution
035: */
036: public class RunCommand {
037: private Runtime runTime;
038: protected boolean interrupted;
039: protected Process this Process;
040: protected String lineSeparator;
041: protected String cmdString;
042:
043: private BufferedReader inputReader = null;
044: private BufferedReader errorReader = null;
045: protected OutputStream out = null;
046: private int exitValue;
047: private String in;
048: private String err;
049:
050: public RunCommand() {
051: this Process = null;
052: runTime = Runtime.getRuntime();
053: interrupted = false;
054: lineSeparator = System.getProperty("line.separator", "\n");
055: }
056:
057: /**
058: * Execute the command line in a separate thread
059: * The command parameter is a fully qualified path
060: * of the command
061: * @see java.lang.Runtime.exec(String)
062: */
063: public void execute(String command) {
064: execute(command, null, null);
065: }
066:
067: /**
068: * @see java.lang.Runtime.exec(String,String[],File)
069: */
070: public void execute(String command, String[] envp, File dir) {
071: cmdString = command;
072: try {
073: this Process = runTime.exec(command, envp, dir);
074: //initIOStreams();
075: //System.out.println("process = " + thisProcess);
076: } catch (Exception ex) {
077: System.out.println(ex.getLocalizedMessage());
078: ex.printStackTrace();
079: }
080: }
081:
082: /**
083: * @see java.lang.Runtime.exec(String[])
084: */
085: public void execute(String[] cmdArray) {
086: cmdString = Util.arrayToString(cmdArray, " ");
087: try {
088: this Process = runTime.exec(cmdArray);
089: //initIOStreams();
090: //System.out.println("process = " + thisProcess);
091: } catch (Exception ex) {
092: System.out.println(ex.getLocalizedMessage());
093: ex.printStackTrace();
094: }
095: }
096:
097: /* @see java.lang.Runtime.exec(String[],String[],File)
098: */
099: public void execute(String[] cmdArray, String[] envp, File dir) {
100: cmdString = Util.arrayToString(cmdArray, " ");
101: try {
102: this Process = runTime.exec(cmdArray, envp, dir);
103: //initIOStreams();
104: } catch (Throwable t) {
105: System.out.println(t.getLocalizedMessage());
106: t.printStackTrace();
107: }
108: }
109:
110: /**
111: * Creates the I/O streams
112: */
113: /*public void initIOStreams(){
114: interrupted = false;
115: try {
116: InputStream in = thisProcess.getInputStream();
117: InputStream err = thisProcess.getErrorStream();
118: out = thisProcess.getOutputStream();
119: inputReader = new BufferedReader(new InputStreamReader(in));
120: errorReader = new BufferedReader(new InputStreamReader(err));
121: } catch (Exception ex) {
122: System.out.println(ex.getLocalizedMessage());
123: ex.printStackTrace();
124: }
125: }*/
126:
127: /**
128: * Interrupt the command line process
129: */
130: public void interrupt() {
131: interrupted = true;
132: //System.out.println("Runcommand interrupted");
133: if (System.getProperty("os.name").startsWith("SunOS")) {
134: try {
135: SolarisRoutines.killProcess(cmdString);
136: } catch (Exception ex) {
137: ex.printStackTrace();
138: }
139: }
140: this Process.destroy();
141: this Process = null;
142: //System.out.println("Process destroyed thisProcess -> " + thisProcess);
143: }
144:
145: /**
146: * Gets the return status of the command line process
147: */
148: public int getReturnStatus() {
149: if (interrupted) {
150: return (-1);
151: }
152: if (this Process != null) {
153: return exitValue;
154: }
155:
156: return (-3);
157: }
158:
159: /**
160: * Gets the return status of the command line process
161: */
162: public int waitFor() {
163: if (interrupted) {
164: return (-1);
165: }
166: try {
167: StreamAccumulator inAccumulator = new StreamAccumulator(
168: this Process.getInputStream());
169: StreamAccumulator errAccumulator = new StreamAccumulator(
170: this Process.getErrorStream());
171:
172: inAccumulator.start();
173: errAccumulator.start();
174:
175: exitValue = this Process.waitFor();
176: inAccumulator.join();
177: errAccumulator.join();
178:
179: in = inAccumulator.result();
180: err = errAccumulator.result();
181:
182: inputReader = new BufferedReader(new StringReader(in));
183: errorReader = new BufferedReader(new StringReader(err));
184: } catch (Throwable t) {
185: System.out.println(t.getLocalizedMessage());
186: t.printStackTrace();
187: //System.out.println(ex.getMessage());
188: return (-2);
189: }
190: //System.out.println("WaitFor completed: thisProcess -> " + thisProcess);
191: if (this Process != null) {
192: return exitValue;
193: }
194:
195: return (-3);
196: }
197:
198: /**
199: * Checks whether the command line process is still running
200: */
201: public boolean isRunning() {
202: try {
203: this Process.exitValue();
204: } catch (IllegalThreadStateException ie) {
205: return (true);
206: } catch (Exception ie) {
207: return (false);
208: }
209: return (false);
210: }
211:
212: /*public BufferedReader getErrorReader() {
213: return errorReader;
214: }
215:
216: public BufferedReader getInputReader() {
217: return inputReader;
218: }
219:
220: public OutputStream getOutputStream() {
221: return out;
222: }*/
223:
224: /*public void flush(){
225: try {
226: if (!interrupted) {
227: int c;
228: if (inputReader.ready()) {
229: while ((c = inputReader.read()) != -1) {
230: }
231: }
232: if ((errorReader != null) && (errorReader.ready())) {
233: while ((c = errorReader.read()) != -1) {
234: }
235: }
236: }
237: }catch(IOException ex) {
238: System.out.println(ex.getLocalizedMessage());
239: ex.printStackTrace();
240: }
241: }*/
242:
243: /**
244: * Gets a line from the standard output of the command line process
245: */
246: public String getOutputLine() {
247: String ret = null;
248: try {
249: ret = inputReader.readLine();
250: if (ret != null) {
251: ret = ret + "\n";
252: }
253: } catch (IOException ex) {
254: System.out.println(ex.getLocalizedMessage());
255: ex.printStackTrace();
256: }
257: return ret;
258: }
259:
260: /*public void flush(){
261: String line=null;
262: if (!interrupted) {
263: while((line = getErrorLine()) != null);
264: while((line = getOutputLine()) != null);
265: }
266: }*/
267:
268: /**
269: * Gets a line from the standard error of the command line process
270: */
271: public String getErrorLine() {
272: String ret = null;
273: try {
274: ret = errorReader.readLine();
275: if (ret != null) {
276: ret = ret + "\n";
277: }
278: } catch (IOException ex) {
279: System.out.println(ex.getLocalizedMessage());
280: ex.printStackTrace();
281: }
282: return ret;
283: }
284:
285: /**
286: * Returns debug info about the command line process like
287: * output,error & return status
288: */
289: public String print() {
290: StringBuffer sb = new StringBuffer(1024);
291: sb.append("\n");
292: sb
293: .append("------------------------------ Command Print -----------------------\n");
294: sb.append("Command: \n");
295: sb.append(cmdString + "\n");
296: sb.append("Command Output:\n");
297: sb.append(in + "\n");
298: sb.append("Command Error:\n");
299: sb.append(err + "\n");
300: sb.append("Return Status: " + exitValue + "\n");
301: sb
302: .append("------------------------------------------------------------------------\n");
303: return sb.toString();
304: }
305:
306: private static class StreamAccumulator extends Thread {
307: private final InputStream is;
308: private final StringBuffer sb = new StringBuffer();
309: private Throwable throwable = null;
310:
311: public String result() throws Throwable {
312: if (throwable != null) {
313: throw throwable;
314: }
315: return sb.toString();
316: }
317:
318: StreamAccumulator(InputStream is) {
319: this .is = is;
320: }
321:
322: public void run() {
323: try {
324: Reader r = new InputStreamReader(is);
325: char[] buf = new char[4096];
326: int n;
327: //Method read blocks till end of stream is encountered so it ends
328: //after external process ends.
329: while ((n = r.read(buf)) > 0) {
330: sb.append(buf, 0, n);
331: }
332: } catch (Throwable t) {
333: throwable = t;
334: }
335: }
336: }
337: }
|