KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > process > Exec


1 /*
2  * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.process;
6
7 import org.apache.commons.io.IOUtils;
8
9 import java.io.ByteArrayInputStream JavaDoc;
10 import java.io.File JavaDoc;
11 import java.io.FileOutputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.io.OutputStream JavaDoc;
15 import java.util.Arrays JavaDoc;
16
17 public class Exec {
18
19   public static Result execute(String JavaDoc cmd[]) throws Exception JavaDoc {
20     return execute(cmd, null, null, null);
21   }
22
23   public static Result execute(String JavaDoc cmd[], String JavaDoc outputLog) throws Exception JavaDoc {
24     return execute(cmd, outputLog, null, null);
25   }
26
27   public static Result execute(String JavaDoc cmd[], String JavaDoc outputLog, byte[] input) throws Exception JavaDoc {
28     return execute(cmd, outputLog, input, null);
29   }
30
31   public static Result execute(String JavaDoc cmd[], String JavaDoc outputLog, byte[] input, File JavaDoc workingDir) throws Exception JavaDoc {
32     Process JavaDoc process = Runtime.getRuntime().exec(cmd, null, workingDir);
33
34     Thread JavaDoc inputThread = new InputPumper(input == null ? new byte[] {} : input, process.getOutputStream());
35
36     StreamCollector stderr = null;
37     StreamCollector stdout = null;
38
39     FileOutputStream JavaDoc fileOutput = null;
40     StreamAppender outputLogger = null;
41
42     String JavaDoc errString = null;
43     String JavaDoc outString = null;
44
45     try {
46       if (outputLog != null) {
47         errString = "stderr output redirected to file " + outputLog;
48         outString = "stdout output redirected to file " + outputLog;
49         fileOutput = new FileOutputStream JavaDoc(outputLog);
50         outputLogger = new StreamAppender(fileOutput);
51         outputLogger.writeInput(process.getErrorStream(), process.getInputStream());
52       } else {
53         stderr = new StreamCollector(process.getErrorStream());
54         stdout = new StreamCollector(process.getInputStream());
55         stderr.start();
56         stdout.start();
57       }
58
59       inputThread.start();
60
61       final int exitCode = process.waitFor();
62
63       if (inputThread != null) inputThread.join();
64
65       if (outputLogger != null) {
66         outputLogger.finish();
67       }
68
69       if (stderr != null) {
70         stderr.join();
71         errString = stderr.toString();
72       }
73
74       if (stdout != null) {
75         stdout.join();
76         outString = stdout.toString();
77       }
78
79       return new Result(cmd, outString, errString, exitCode);
80     } finally {
81       closeQuietly(fileOutput);
82     }
83   }
84
85   private static void closeQuietly(OutputStream JavaDoc output) {
86     if (output != null) {
87       try {
88         output.close();
89       } catch (IOException JavaDoc ioe) {
90         // quiet
91
}
92     }
93   }
94
95   private static class InputPumper extends Thread JavaDoc {
96     private final InputStream JavaDoc data;
97     private final OutputStream JavaDoc output;
98
99     InputPumper(byte[] input, OutputStream JavaDoc output) {
100       this.output = output;
101       this.data = new ByteArrayInputStream JavaDoc(input);
102     }
103
104     public void run() {
105       try {
106         IOUtils.copy(data, output);
107       } catch (IOException JavaDoc e) {
108         e.printStackTrace();
109       } finally {
110         closeQuietly(output);
111       }
112     }
113   }
114
115   public static class Result {
116     private final String JavaDoc stderr;
117     private final String JavaDoc stdout;
118     private final int exitCode;
119     private final String JavaDoc[] cmd;
120
121     private Result(String JavaDoc[] cmd, String JavaDoc stdout, String JavaDoc stderr, int exitCode) {
122       this.cmd = cmd;
123       this.stdout = stdout;
124       this.stderr = stderr;
125       this.exitCode = exitCode;
126     }
127
128     public String JavaDoc getStderr() {
129       return stderr;
130     }
131
132     public String JavaDoc getStdout() {
133       return stdout;
134     }
135
136     public int getExitCode() {
137       return exitCode;
138     }
139
140     public String JavaDoc toString() {
141       return "Command: " + Arrays.asList(cmd) + "\n" + "exit code: " + exitCode + "\n" + "stdout: " + stdout + "\n"
142           + "stderr: " + stderr + "\n";
143     }
144
145   }
146
147 }
148
Popular Tags