KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > util > ProcessUtil


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
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
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.util;
20
21 import gcc.*;
22 import gcc.properties.*;
23 import java.io.*;
24
25 public class ProcessUtil
26     //implements gcc.bootstrap.BootstrapObject
27
{
28     /*
29     public static final Attribute[] attributes =
30     {
31         // Bootstrap classes must have inline static attributes.
32     }
33     ;
34     */

35
36     //public static final Component component = new Component(ProcessUtil.class);
37

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

50
51         return new ProcessUtil();
52     }
53
54     // private data
55

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

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

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