KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > client > util > ProcessMonitor


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.agent.client.util;
21
22 import java.io.OutputStream JavaDoc;
23
24 /**
25  * Connects to the input / output streams of a {@link Process}, displays any
26  * ouput on the current System input / output streams and waits for the process
27  * to finish. An exit code is then returned. for it
28  *
29  * @author Lee David Painter <a HREF="mailto: lee@3sp.com">&lt;lee@3sp.com&gt;</a>
30  */

31 public class ProcessMonitor {
32
33     // Private instance variables
34

35     private String JavaDoc name;
36     private Process JavaDoc process;
37
38     private IOStreamConnector stdout;
39     private IOStreamConnector stderr;
40
41     /**
42      * Constructor.
43      *
44      * @param name name of monitor
45      * @param process process to connect to
46      */

47     public ProcessMonitor(String JavaDoc name, Process JavaDoc process) {
48
49         this.process = process;
50         this.name = name;
51     }
52
53     /**
54      * Get the name of this monitor.
55      *
56      * @return monitor name
57      */

58     public String JavaDoc getName() {
59         return name;
60     }
61
62     /**
63      * Watch and wait until the process has completed. Any output from the
64      * process will be written to the streams provided and the exit code will be
65      * returned when the process terminates.
66      *
67      * @param out an OutputStream to receive data from the stdout stream
68      * @param err an OutputStream to receive data from the stderr stream
69      * @return exit code
70      * @throws java.lang.InterruptedException
71      */

72     public int watch(OutputStream JavaDoc out, OutputStream JavaDoc err) throws InterruptedException JavaDoc {
73
74         stdout = new IOStreamConnector();
75         stdout.connect(process.getInputStream(), out);
76
77         stderr = new IOStreamConnector();
78         stderr.connect(process.getErrorStream(), err);
79
80         return process.waitFor();
81     }
82
83     /**
84      * Terminate the process
85      */

86     public void kill() {
87         process.destroy();
88     }
89
90 }
Popular Tags