KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > io > RuntimeCommand


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.io;
25
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.springframework.util.Assert;
32 import org.springframework.util.StringUtils;
33
34 /**
35  * @author Felix Gnass [fgnass at neteye dot de]
36  * @since 6.5
37  */

38 public class RuntimeCommand {
39
40     private String JavaDoc[] commandLine;
41
42     private Process JavaDoc process;
43
44     private OutputStream JavaDoc stdErr = new ByteArrayOutputStream JavaDoc();
45
46     private OutputStream JavaDoc stdOut = new ByteArrayOutputStream JavaDoc();
47
48     private OutputStream JavaDoc stdIn;
49     
50     private StreamCopyThread stdErrCopyThread;
51     
52     private StreamCopyThread stdOutCopyThread;
53
54     private Integer JavaDoc exitCode;
55
56     public RuntimeCommand(String JavaDoc executable) {
57         this(new String JavaDoc[] {executable});
58     }
59
60     public RuntimeCommand(String JavaDoc executable, String JavaDoc arg) {
61         this(new String JavaDoc[] {executable, arg});
62     }
63
64     public RuntimeCommand(List JavaDoc commandLine) {
65         this(StringUtils.toStringArray(commandLine));
66     }
67
68     public RuntimeCommand(String JavaDoc[] commandLine) {
69         this.commandLine = commandLine;
70     }
71
72     public RuntimeCommand setStdOutStream(OutputStream JavaDoc stdOut) {
73         if (stdOut == null) {
74             stdOut = new NullOutputStream();
75         }
76         this.stdOut = stdOut;
77         return this;
78     }
79
80     public RuntimeCommand setStdErrStream(OutputStream JavaDoc stdErr) {
81         if (stdErr == null) {
82             stdErr = new NullOutputStream();
83         }
84         this.stdErr = stdErr;
85         return this;
86     }
87
88     public RuntimeCommand exec() throws IOException JavaDoc {
89         process = Runtime.getRuntime().exec(commandLine);
90         
91         stdErrCopyThread = new StreamCopyThread(process.getErrorStream(), stdErr);
92         stdErrCopyThread.start();
93         
94         stdOutCopyThread = new StreamCopyThread(process.getInputStream(), stdOut);
95         stdOutCopyThread.start();
96         
97         stdIn = process.getOutputStream();
98         return this;
99     }
100
101     public OutputStream JavaDoc getStdIn() {
102         Assert.notNull(stdIn, "exec() must be called first");
103         return stdIn;
104     }
105
106     public int getExitCode() {
107         Assert.notNull(process, "exec() must be called first");
108         if (exitCode == null) {
109             try {
110                 IOUtils.closeStream(stdIn);
111                 stdErrCopyThread.join();
112                 stdOutCopyThread.join();
113                 exitCode = new Integer JavaDoc(process.waitFor());
114             }
115             catch (InterruptedException JavaDoc e) {
116                 return -1;
117             }
118         }
119         return exitCode.intValue();
120     }
121
122     public String JavaDoc getOutput() {
123         getExitCode();
124         return stdOut.toString();
125     }
126
127     public String JavaDoc getErrors() {
128         getExitCode();
129         return stdErr.toString();
130     }
131
132     public String JavaDoc getResult() throws IOException JavaDoc {
133         if (getExitCode() == 0) {
134             return stdOut.toString();
135         }
136         else {
137             throw new IOException JavaDoc(stdErr.toString());
138         }
139     }
140
141 }
142
Popular Tags