1
package gnu.dtools.ritopt;
7
* $Id: SimpleProcess.java,v 1.1 2004/05/20 20:45:47 mortenalver Exp $
13
* A SimpleProcess is used to execute a shell process, and redirect an
14
* input stream to the processes' standard input, as well as redirect
15
* the processes' standard output/error to an output stream. The processes
16
* is multithreaded to prevent deadlock.<p>
18
* The example below demonstrates the use of this class.
20
* class ExecuteProcess {
21
* public static void main( String args[] ) {
22
* if ( args.length > 0 ) {
23
* String processName = args[ 0 ];
25
* SimpleProcess process
26
* = new SimpleProcess( Runtime.getRuntime.exec(
29
* int exitStatus = process.waitFor();
30
* System.out.println( "The process ran successfully"
31
* + " with an exit status of "
32
* + exitStatus + "." );
34
* catch ( Exception e ) {
35
* System.out.println( "The process was not successful. "
36
* + " Reason: " + e.getMessage() );
40
* System.err.println( "Please specify a command" );
49
* Copyright (C) Damian Ryan Eads, 2001. All Rights Reserved.
51
* ritopt is free software; you can redistribute it and/or modify
52
* it under the terms of the GNU General Public License as published by
53
* the Free Software Foundation; either version 2 of the License, or
54
* (at your option) any later version.
56
* ritopt is distributed in the hope that it will be useful,
57
* but WITHOUT ANY WARRANTY; without even the implied warranty of
58
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
59
* GNU General Public License for more details.
61
* You should have received a copy of the GNU General Public License
62
* along with ritopt; if not, write to the Free Software
63
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
69
public class SimpleProcess extends Process {
75
private Process process;
78
* The input stream that the processes' standard input will use.
81
private InputStream processInput;
84
* The print stream to redirect to.
87
private PrintStream yourOutput;
90
* The print stream to redirect to.
93
private PrintStream yourError;
99
private StreamPrinter in, out, error;
102
* Constructs a SimpleProcess, redirecting System.in to the its standard
103
* input, System.out to its standard output, and System.err to its standard
107
public SimpleProcess( Process process ) throws IOException {
108
this( process, System.in, System.out, System.err );
112
* Constructs a SimpleProcess, initializing it with the streams passed.
114
* @param process The target process.
115
* @param processInput The stream that is redirected to the
116
* processes' standard input.
117
* @param processOutput The stream to redirect the processes's
119
* @param processError The stream to redirect the processes's
123
public SimpleProcess( Process process, InputStream processInput,
124
PrintStream yourOutput, PrintStream yourError )
127
this.process = process;
128
this.processInput = processInput;
129
this.yourOutput = yourOutput;
130
this.yourError = yourError;
134
* Returns the standard input of this process.
136
* @return The standard input of this process.
139
public OutputStream getOutputStream() {
140
return process.getOutputStream();
144
* Returns the standard output of this process.
146
* @return The standard output of this process.
149
public InputStream getInputStream() {
150
return process.getInputStream();
154
* Returns the standard error of this process.
156
* @return The standard error of this process.
159
public InputStream getErrorStream() {
160
return process.getErrorStream();
164
* Begin redirecting the streams passed. This method should be invoked
165
* immediately after execution of a simple process to prevent thread
168
* @return The exit status of the target process.
171
public int waitFor() throws InterruptedException {
172
int retval = waitForImpl();
180
* Contains the implementation of wait for.
182
* @return The exit status of the target process.
185
private int waitForImpl() throws InterruptedException {
187
in = new StreamPrinter( processInput,
188
new PrintStream( process.getOutputStream() ) );
190
out = new StreamPrinter( process.getInputStream(), yourOutput );
191
error = new StreamPrinter( process.getErrorStream(), yourError );
197
return process.waitFor();
201
* Returns the target processes' exit value.
203
* @return This processes' exit value.
206
public int exitValue() {
207
return process.exitValue();
211
* Destroys the target process.
214
public void destroy() throws IllegalThreadStateException {
217
} /** SimpleProcess **/
1
package gnu.dtools.ritopt;
7
* $Id: SimpleProcess.java 322 2004-05-20 20:45:47Z mortenalver $
13
* A SimpleProcess is used to execute a shell process, and redirect an
14
* input stream to the processes' standard input, as well as redirect
15
* the processes' standard output/error to an output stream. The processes
16
* is multithreaded to prevent deadlock.<p>
18
* The example below demonstrates the use of this class.
20
* class ExecuteProcess {
21
* public static void main( String args[] ) {
22
* if ( args.length > 0 ) {
23
* String processName = args[ 0 ];
25
* SimpleProcess process
26
* = new SimpleProcess( Runtime.getRuntime.exec(
29
* int exitStatus = process.waitFor();
30
* System.out.println( "The process ran successfully"
31
* + " with an exit status of "
32
* + exitStatus + "." );
34
* catch ( Exception e ) {
35
* System.out.println( "The process was not successful. "
36
* + " Reason: " + e.getMessage() );
40
* System.err.println( "Please specify a command" );
49
* Copyright (C) Damian Ryan Eads, 2001. All Rights Reserved.
51
* ritopt is free software; you can redistribute it and/or modify
52
* it under the terms of the GNU General Public License as published by
53
* the Free Software Foundation; either version 2 of the License, or
54
* (at your option) any later version.
56
* ritopt is distributed in the hope that it will be useful,
57
* but WITHOUT ANY WARRANTY; without even the implied warranty of
58
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
59
* GNU General Public License for more details.
61
* You should have received a copy of the GNU General Public License
62
* along with ritopt; if not, write to the Free Software
63
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
69
public class SimpleProcess extends Process {
75
private Process process;
78
* The input stream that the processes' standard input will use.
81
private InputStream processInput;
84
* The print stream to redirect to.
87
private PrintStream yourOutput;
90
* The print stream to redirect to.
93
private PrintStream yourError;
99
private StreamPrinter in, out, error;
102
* Constructs a SimpleProcess, redirecting System.in to the its standard
103
* input, System.out to its standard output, and System.err to its standard
107
public SimpleProcess( Process process ) throws IOException {
108
this( process, System.in, System.out, System.err );
112
* Constructs a SimpleProcess, initializing it with the streams passed.
114
* @param process The target process.
115
* @param processInput The stream that is redirected to the
116
* processes' standard input.
117
* @param processOutput The stream to redirect the processes's
119
* @param processError The stream to redirect the processes's
123
public SimpleProcess( Process process, InputStream processInput,
124
PrintStream yourOutput, PrintStream yourError )
127
this.process = process;
128
this.processInput = processInput;
129
this.yourOutput = yourOutput;
130
this.yourError = yourError;
134
* Returns the standard input of this process.
136
* @return The standard input of this process.
139
public OutputStream getOutputStream() {
140
return process.getOutputStream();
144
* Returns the standard output of this process.
146
* @return The standard output of this process.
149
public InputStream getInputStream() {
150
return process.getInputStream();
154
* Returns the standard error of this process.
156
* @return The standard error of this process.
159
public InputStream getErrorStream() {
160
return process.getErrorStream();
164
* Begin redirecting the streams passed. This method should be invoked
165
* immediately after execution of a simple process to prevent thread
168
* @return The exit status of the target process.
171
public int waitFor() throws InterruptedException {
172
int retval = waitForImpl();
180
* Contains the implementation of wait for.
182
* @return The exit status of the target process.
185
private int waitForImpl() throws InterruptedException {
187
in = new StreamPrinter( processInput,
188
new PrintStream( process.getOutputStream() ) );
190
out = new StreamPrinter( process.getInputStream(), yourOutput );
191
error = new StreamPrinter( process.getErrorStream(), yourError );
197
return process.waitFor();
201
* Returns the target processes' exit value.
203
* @return This processes' exit value.
206
public int exitValue() {
207
return process.exitValue();
211
* Destroys the target process.
214
public void destroy() throws IllegalThreadStateException {
217
} /** SimpleProcess **/