KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > util > ProcessUtil


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.util;
19
20 import java.io.BufferedInputStream JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.PrintStream JavaDoc;
25
26 import org.apache.geronimo.interop.SystemException;
27
28
29 public class ProcessUtil
30         //implements org.apache.geronimo.interop.bootstrap.BootstrapObject
31
{
32     /*
33     public static final Attribute[] attributes =
34     {
35         // Bootstrap classes must have inline static attributes.
36     }
37     ;
38     */

39
40     //public static final Component component = new Component(ProcessUtil.class);
41

42     public static ProcessUtil getInstance() {
43         /*
44         if (component == null || SystemProperties.bootstrap())
45         {
46             return new ProcessUtil();
47         }
48         else
49         {
50             return (ProcessUtil)component.getInstance();
51         }
52         */

53
54         return new ProcessUtil();
55     }
56
57     // private data
58

59     private String JavaDoc _cmd;
60
61     private boolean _echo;
62
63     private PrintStream JavaDoc _echoStream;
64
65     private int _exitValue;
66
67     private byte[] _errorBytes;
68
69     private byte[] _inputBytes;
70
71     // internal methods
72

73     protected ProcessUtil() {
74         // Used by getInstance in bootstrap mode.
75
// Prevents direct instantiation of class.
76
}
77
78     // public methods
79

80     public void setEcho(boolean echo) {
81         _echo = echo;
82         if (_echo) {
83             _echoStream = System.out;
84         } else {
85             _echoStream = null;
86         }
87     }
88
89     public void setEcho(PrintStream JavaDoc stream) {
90         _echo = stream != null;
91         _echoStream = stream;
92     }
93
94     public void run(String JavaDoc cmd) {
95         run(cmd, null, null);
96     }
97
98     public void run(String JavaDoc cmd, String JavaDoc[] env, String JavaDoc dir) {
99         _cmd = cmd;
100         Process JavaDoc process;
101         try {
102             if (_echo) {
103                 _echoStream.println(cmd);
104             }
105             File JavaDoc dirFile = dir == null ? null : new File JavaDoc(dir);
106             process = Runtime.getRuntime().exec(cmd, env, dirFile);
107         } catch (Exception JavaDoc ex) {
108             throw new SystemException(ex);
109         }
110         run(process);
111     }
112
113     public void run(String JavaDoc[] cmd, String JavaDoc[] env, String JavaDoc dir) {
114         _cmd = cmd.length == 0 ? "" : cmd[0];
115         for (int i = 1; i < cmd.length; i++) {
116             _cmd += " " + cmd[i];
117         }
118         Process JavaDoc process;
119         try {
120             if (_echo) {
121                 _echoStream.println(cmd);
122             }
123             File JavaDoc dirFile = dir == null ? null : new File JavaDoc(dir);
124             process = Runtime.getRuntime().exec(cmd, env, dirFile);
125         } catch (Exception JavaDoc ex) {
126             throw new SystemException(ex);
127         }
128         run(process);
129     }
130
131     public void run(Process JavaDoc process) {
132         try {
133             GetBytesThread errorThread = new GetBytesThread(process.getErrorStream());
134             GetBytesThread inputThread = new GetBytesThread(process.getInputStream());
135             errorThread.start();
136             inputThread.start();
137             process.waitFor();
138             errorThread.join();
139             inputThread.join();
140             _errorBytes = errorThread.getBytes();
141             _inputBytes = inputThread.getBytes();
142             _exitValue = process.exitValue();
143         } catch (Exception JavaDoc ex) {
144             throw new SystemException(ex);
145         }
146     }
147
148     public void checkStatus() {
149         if (_exitValue != 0) {
150             // TODO: I18N
151
String JavaDoc result = getResult();
152             throw new SystemException("Command Failed: " + _cmd
153                                       + "\nExit Status: " + _exitValue
154                                       + (result.length() == 0 ? "" : ("\nOutput: " + getResult())));
155         }
156     }
157
158     public int exitValue() {
159         return _exitValue;
160     }
161
162     public String JavaDoc getResult() {
163         return new String JavaDoc(getResultBytes());
164     }
165
166     public String JavaDoc getError() {
167         return new String JavaDoc(getErrorBytes());
168     }
169
170     public String JavaDoc getInput() {
171         return new String JavaDoc(getInputBytes());
172     }
173
174     public byte[] getResultBytes() {
175         byte[] bytes = new byte[_errorBytes.length + _inputBytes.length];
176         System.arraycopy(_errorBytes, 0, bytes, 0, _errorBytes.length);
177         System.arraycopy(_inputBytes, 0, bytes, _errorBytes.length, _inputBytes.length);
178         return bytes;
179     }
180
181     public byte[] getErrorBytes() {
182         return _errorBytes;
183     }
184
185     public byte[] getInputBytes() {
186         return _inputBytes;
187     }
188
189     private class GetBytesThread extends Thread JavaDoc {
190         InputStream JavaDoc _input;
191
192         ByteArrayOutputStream JavaDoc _bytes;
193
194         GetBytesThread(InputStream JavaDoc input) {
195             _input = new BufferedInputStream JavaDoc(input);
196             _bytes = new ByteArrayOutputStream JavaDoc();
197             setDaemon(true);
198         }
199
200         public void run() {
201             try {
202                 int c;
203                 while ((c = _input.read()) != -1) {
204                     _bytes.write(c);
205                     if (_echo) {
206                         _echoStream.print((char) c);
207                         if (c == '\n') {
208                             _echoStream.flush();
209                         }
210                     }
211                 }
212             } catch (Exception JavaDoc ex) {
213                 throw new SystemException(ex);
214             }
215         }
216
217         public byte[] getBytes() {
218             return _bytes.toByteArray();
219         }
220     }
221 }
222
Popular Tags