KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > console > CmdLineThread


1 package org.sapia.console;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.File JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.io.InputStreamReader JavaDoc;
8 import java.io.PipedInputStream JavaDoc;
9 import java.io.PipedOutputStream JavaDoc;
10
11
12 /**
13  * An instance of this class can be used to invoke a command-line that spawns an external process.
14  * <p>
15  * Usage:
16  * <pre>
17  * CmdLine cmd = new CmdLine();
18  * cmd.addArg("java");
19  * CmdLineThread th = new CmdLineThread(cmd);
20  * th.start();
21  * BufferedReader br = new BufferedReader(new InputStreamReader(th.getInputStream()));
22  * String line;
23  * try{
24  * while((line = br.readLine()) != null){
25  * System.out.println(line);
26  * }
27  * }finally{
28  * br.close();
29  * }
30  * </pre>
31  *
32  * @author Yanick Duchesne
33  * <dl>
34  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
35  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
36  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
37  * </dl>
38  */

39 public class CmdLineThread extends Thread JavaDoc {
40   private CmdLine _cmd;
41   private boolean _ready;
42   private boolean _closed;
43   private PipedInputStream JavaDoc _stdout;
44   private PipedInputStream JavaDoc _stderr;
45   private File JavaDoc _procDir;
46   private String JavaDoc[] _env;
47   private IOException JavaDoc _exc;
48   private Process JavaDoc _proc;
49
50   /**
51    * Constructor for CmdLineThread.
52    */

53   public CmdLineThread(CmdLine cmd) {
54     super("CmdLineThread");
55     _cmd = cmd;
56   }
57
58   public CmdLineThread(CmdLine cmd, String JavaDoc[] env) {
59     super("CmdLineThread");
60     _cmd = cmd;
61     _env = env;
62   }
63
64   public CmdLineThread(CmdLine cmd, File JavaDoc processDir, String JavaDoc[] env) {
65     this(cmd);
66     _procDir = processDir;
67     _env = env;
68   }
69
70   public Process JavaDoc getProcess() {
71     return _proc;
72   }
73
74   /**
75    * Returns the stream that corresponds to the started process' stdout.
76    *
77    * @return an <code>InputStream</code>
78    */

79   public synchronized InputStream JavaDoc getInputStream() throws IOException JavaDoc {
80     if (_exc != null) {
81       throw _exc;
82     }
83
84     while (!_ready) {
85       try {
86         wait();
87       } catch (InterruptedException JavaDoc e) {
88         throw new IOException JavaDoc(e.getMessage());
89       }
90     }
91
92     return new ProcessInputStream(_stdout, this);
93   }
94
95   /**
96    * Returns the stream that corresponds to the started process' stderr.
97    *
98    * @return an <code>InputStream</code>
99    */

100   public synchronized InputStream JavaDoc getErrStream() throws IOException JavaDoc {
101     if (_exc != null) {
102       throw _exc;
103     }
104
105     while (!_ready) {
106       try {
107         wait();
108       } catch (InterruptedException JavaDoc e) {
109         throw new IOException JavaDoc(e.getMessage());
110       }
111     }
112
113     return new ProcessInputStream(_stderr, this);
114   }
115
116   synchronized void close() {
117     _closed = true;
118   }
119
120   synchronized boolean isClosed() {
121     return _closed;
122   }
123
124   boolean hasException() {
125     return _exc != null;
126   }
127
128   IOException JavaDoc getException() {
129     return _exc;
130   }
131
132   /**
133    * @see java.lang.Thread#run()
134    */

135   public void run() {
136     PipedOutputStream JavaDoc perr = null;
137     PipedOutputStream JavaDoc pout = null;
138
139     try {
140       Process JavaDoc proc;
141
142       if (_procDir == null) {
143         proc = Runtime.getRuntime().exec(_cmd.toArray(), _env);
144       } else {
145         proc = Runtime.getRuntime().exec(_cmd.toArray(), _env, _procDir);
146       }
147
148       _proc = proc;
149
150       InputStream JavaDoc stderr = proc.getErrorStream();
151       InputStream JavaDoc stdout = proc.getInputStream();
152       _stdout = new PipedInputStream JavaDoc();
153       _stderr = new PipedInputStream JavaDoc();
154       pout = new PipedOutputStream JavaDoc(_stdout);
155       perr = new PipedOutputStream JavaDoc(_stderr);
156
157       streamsCreated();
158
159       int avail;
160       byte[] buf;
161
162       while (!isClosed()) {
163         if ((avail = stderr.available()) > 0) {
164           stderr.read(buf = new byte[avail]);
165           perr.write(buf);
166           perr.flush();
167         }
168
169         if ((avail = stdout.available()) > 0) {
170           stdout.read(buf = new byte[avail]);
171           pout.write(buf);
172           pout.flush();
173         }
174
175         Thread.sleep(200);
176       }
177     } catch (IOException JavaDoc e) {
178       _exc = e;
179       streamsCreated();
180     } catch (InterruptedException JavaDoc e) {
181       _exc = new IOException JavaDoc(e.getMessage());
182       streamsCreated();
183     } finally {
184       if (perr != null) {
185         try {
186           perr.close();
187         } catch (IOException JavaDoc e) {
188         }
189       }
190
191       if (pout != null) {
192         try {
193           pout.close();
194         } catch (IOException JavaDoc e) {
195         }
196       }
197     }
198   }
199
200   private synchronized void streamsCreated() {
201     _ready = true;
202     notify();
203   }
204
205   public static void main(String JavaDoc[] args) {
206     try {
207       String JavaDoc cmdStr = "\"/opt/IBMJava2-141/bin/../bin/../jre/bin/java\" -Xms32M -Dcorus.process.java.main=org.sapia.corus.examples.NoopApplication -Dcorus.server.host=127.0.0.1 -Dcorus.server.port=33000 -Dcorus.server.domain=yanick -Dcorus.distribution.name=demo -Dcorus.distribution.version=1.0 -Dcorus.process.dir\"/opt/dev/java/sapia/corus/deploy/yanick_33000/demo/1.0/processes/10829977791640\" -Dcorus.process.id=10829977791640 -Dcorus.process.poll.interval=10 -Dcorus.process.status.interval=30 -Dcorus.process.profile=test -Duser.dir=/opt/dev/java/sapia/corus/deploy/yanick_33000/demo/1.0/common -cp /opt/dev/java/sapia/corus/lib/sapia_corus_starter.jar:/opt/dev/java/sapia/corus/dist/sapia_corus_starter.jar:/opt/dev/java/sapia/corus/deploy/yanick_33000/demo/1.0/common/lib/demo.jar:/opt/dev/java/sapia/corus/deploy/yanick_33000/demo/1.0/common/jdom-1_0b9.jar:/opt/dev/java/sapia/corus/deploy/yanick_33000/demo/1.0/common/lib/log4j-1_2_5.jar:/opt/dev/java/sapia/corus/deploy/yanick_33000/demo/1.0/common/lib/Piccolo-1_0_3.jar:/opt/dev/java/sapia/corus/deploy/yanick_33000/demo/1.0/common/lib/sapia_corus_iop.jar:/opt/dev/java/sapia/corus/deploy/yanick_33000/demo/1.0/common/lib/sapia_utils-1_2.jar org.sapia.corus.starter.Starter";
208       CmdLine cmd = CmdLine.parse(cmdStr);
209
210       /*
211       CmdLine cmd = new CmdLine();
212       cmd.addArg("java");
213       */

214       ExecHandle handle = cmd.exec();
215
216       BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
217             handle.getInputStream()));
218       String JavaDoc line;
219
220       while ((line = br.readLine()) != null) {
221         System.out.println(line);
222       }
223
224       br.close();
225     } catch (Exception JavaDoc e) {
226       e.printStackTrace();
227     }
228   }
229
230   /*////////////////////////////////////////////////////////////////////
231                             INNER CLASSES
232   ////////////////////////////////////////////////////////////////////*/

233   static final class ProcessInputStream extends InputStream JavaDoc {
234     private InputStream JavaDoc _is;
235     private CmdLineThread _cmdThread;
236
237     ProcessInputStream(InputStream JavaDoc is, CmdLineThread cmdThread) {
238       _is = is;
239       _cmdThread = cmdThread;
240     }
241
242     /**
243      * @see java.io.InputStream#available()
244      */

245     public int available() throws IOException JavaDoc {
246       checkException();
247
248       return _is.available();
249     }
250
251     /**
252      * @see java.io.InputStream#close()
253      */

254     public void close() throws IOException JavaDoc {
255       checkException();
256
257       try {
258         if (_is != null) {
259           _is.close();
260         }
261       } finally {
262         _cmdThread.close();
263       }
264     }
265
266     /**
267      * @see java.io.InputStream#mark(int)
268      */

269     public synchronized void mark(int arg0) {
270       _is.mark(arg0);
271     }
272
273     /**
274      * @see java.io.InputStream#markSupported()
275      */

276     public boolean markSupported() {
277       return _is.markSupported();
278     }
279
280     /**
281      * @see java.io.InputStream#read()
282      */

283     public int read() throws IOException JavaDoc {
284       checkException();
285
286       return _is.read();
287     }
288
289     /**
290      * @see java.io.InputStream#read(byte[], int, int)
291      */

292     public int read(byte[] arg0, int arg1, int arg2) throws IOException JavaDoc {
293       checkException();
294
295       return _is.read(arg0, arg1, arg2);
296     }
297
298     /**
299      * @see java.io.InputStream#read(byte[])
300      */

301     public int read(byte[] arg0) throws IOException JavaDoc {
302       checkException();
303
304       return _is.read(arg0);
305     }
306
307     /**
308      * @see java.io.InputStream#reset()
309      */

310     public synchronized void reset() throws IOException JavaDoc {
311       checkException();
312       _is.reset();
313     }
314
315     /**
316      * @see java.io.InputStream#skip(long)
317      */

318     public long skip(long arg0) throws IOException JavaDoc {
319       checkException();
320
321       return _is.skip(arg0);
322     }
323
324     private void checkException() throws IOException JavaDoc {
325       if (_cmdThread.hasException()) {
326         throw _cmdThread.getException();
327       }
328     }
329   }
330 }
331
Popular Tags