~ubuntu-branches/ubuntu/natty/jabref/natty

« back to all changes in this revision

Viewing changes to src/java/gnu/dtools/ritopt/SimpleProcess.java

  • Committer: Bazaar Package Importer
  • Author(s): gregor herrmann
  • Date: 2007-11-16 18:38:18 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071116183818-20x934jdsju14618
Tags: 2.3-2
* Add /usr/lib/jvm/java-7-icedtea to wrapper script. Doesn't work on
  Debian yet but helps Ubuntu users (TODO: add dependency on
  icedtea-java7-jre (not yet in Debian)).
* Reformat wrapper script.
* debian/rules: re-arrange targets and their dependencies.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package gnu.dtools.ritopt;
2
 
 
3
 
/**
4
 
 * SimpleProcess.java
5
 
 *
6
 
 * Version:
7
 
 *   $Id: SimpleProcess.java,v 1.1 2004/05/20 20:45:47 mortenalver Exp $
8
 
 */
9
 
 
10
 
import java.io.*;
11
 
 
12
 
/**
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>
17
 
 *
18
 
 * The example below demonstrates the use of this class.
19
 
 * <pre>
20
 
 *  class ExecuteProcess {
21
 
 *       public static void main( String args[] ) {
22
 
 *           if ( args.length > 0 ) {
23
 
 *               String processName = args[ 0 ];
24
 
 *               try {
25
 
 *                   SimpleProcess process
26
 
 *                      = new SimpleProcess( Runtime.getRuntime.exec(
27
 
 *                                                            processName ) );
28
 
 *                                          );
29
 
 *                   int exitStatus = process.waitFor();
30
 
 *                   System.out.println( "The process ran successfully" 
31
 
 *                                       + " with an exit status of "
32
 
 *                                       + exitStatus + "." );
33
 
 *               }
34
 
 *               catch ( Exception e ) {
35
 
 *                   System.out.println( "The process was not successful. "
36
 
 *                                       + " Reason: " + e.getMessage() );
37
 
 *               }
38
 
 *           }
39
 
 *           else {
40
 
 *               System.err.println( "Please specify a command" );
41
 
 *           }
42
 
 *       }
43
 
 *  }
44
 
 * </pre>
45
 
 *
46
 
 * <hr>
47
 
 *
48
 
 * <pre>
49
 
 * Copyright (C) Damian Ryan Eads, 2001. All Rights Reserved.
50
 
 *
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.
55
 
 
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.
60
 
 *
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
64
 
 * </pre>
65
 
 *
66
 
 * @author Damian Eads
67
 
 */
68
 
 
69
 
public class SimpleProcess extends Process {
70
 
 
71
 
    /**
72
 
     * The target process.
73
 
     */
74
 
 
75
 
    private Process process;
76
 
 
77
 
    /**
78
 
     * The input stream that the processes' standard input will use.
79
 
     */
80
 
 
81
 
    private InputStream processInput;
82
 
 
83
 
    /**
84
 
     * The print stream to redirect to.
85
 
     */
86
 
 
87
 
    private PrintStream yourOutput;
88
 
 
89
 
    /**
90
 
     * The print stream to redirect to.
91
 
     */
92
 
 
93
 
    private PrintStream yourError;
94
 
 
95
 
    /**
96
 
     * The StreamPrinters.
97
 
     */
98
 
 
99
 
    private StreamPrinter in, out, error;
100
 
 
101
 
    /**
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
104
 
     * error.
105
 
     */
106
 
 
107
 
    public SimpleProcess( Process process ) throws IOException {
108
 
        this( process, System.in, System.out, System.err );
109
 
    }
110
 
 
111
 
    /**
112
 
     * Constructs a SimpleProcess, initializing it with the streams passed.
113
 
     *
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
118
 
     *                      standard output.
119
 
     * @param processError  The stream to redirect the processes's
120
 
     *                      standard input.
121
 
     */
122
 
 
123
 
    public SimpleProcess( Process process, InputStream processInput,
124
 
                          PrintStream yourOutput, PrintStream yourError )
125
 
                         throws IOException {
126
 
        super();
127
 
        this.process = process;
128
 
        this.processInput = processInput;
129
 
        this.yourOutput = yourOutput;
130
 
        this.yourError = yourError;
131
 
    }
132
 
 
133
 
    /**
134
 
     * Returns the standard input of this process.
135
 
     *
136
 
     * @return The standard input of this process.
137
 
     */
138
 
 
139
 
    public OutputStream getOutputStream() {
140
 
        return process.getOutputStream();
141
 
    }
142
 
 
143
 
    /**
144
 
     * Returns the standard output of this process.
145
 
     *
146
 
     * @return The standard output of this process.
147
 
     */
148
 
 
149
 
    public InputStream getInputStream() {
150
 
        return process.getInputStream();
151
 
    }
152
 
 
153
 
    /**
154
 
     * Returns the standard error of this process.
155
 
     *
156
 
     * @return The standard error of this process.
157
 
     */
158
 
 
159
 
    public InputStream getErrorStream() {
160
 
        return process.getErrorStream();
161
 
    }
162
 
 
163
 
    /**
164
 
     * Begin redirecting the streams passed. This method should be invoked
165
 
     * immediately after execution of a simple process to prevent thread
166
 
     * deadlock.
167
 
     *
168
 
     * @return The exit status of the target process.
169
 
     */
170
 
 
171
 
    public int waitFor() throws InterruptedException {
172
 
        int retval = waitForImpl();
173
 
        if ( in != null ) {
174
 
            in.stop();
175
 
        }
176
 
        return retval;
177
 
    }
178
 
 
179
 
    /**
180
 
     * Contains the implementation of wait for.
181
 
     *
182
 
     * @return The exit status of the target process.
183
 
     */
184
 
 
185
 
    private int waitForImpl() throws InterruptedException {
186
 
        process = process;
187
 
        in = new StreamPrinter( processInput,
188
 
                                new PrintStream( process.getOutputStream() ) );
189
 
        in.setFlush( true );
190
 
        out = new StreamPrinter( process.getInputStream(), yourOutput );
191
 
        error = new StreamPrinter( process.getErrorStream(), yourError );
192
 
        in.start();
193
 
        out.start();
194
 
        error.start();
195
 
        out.join();
196
 
        error.join();
197
 
        return process.waitFor();
198
 
    }
199
 
 
200
 
    /**
201
 
     * Returns the target processes' exit value.
202
 
     *
203
 
     * @return This processes' exit value.
204
 
     */
205
 
 
206
 
    public int exitValue() {
207
 
        return process.exitValue();
208
 
    }
209
 
 
210
 
    /**
211
 
     * Destroys the target process.
212
 
     */
213
 
 
214
 
    public void destroy() throws IllegalThreadStateException {
215
 
        process.destroy();
216
 
    }
217
 
} /** SimpleProcess **/
 
1
package gnu.dtools.ritopt;
 
2
 
 
3
/**
 
4
 * SimpleProcess.java
 
5
 *
 
6
 * Version:
 
7
 *   $Id: SimpleProcess.java 322 2004-05-20 20:45:47Z mortenalver $
 
8
 */
 
9
 
 
10
import java.io.*;
 
11
 
 
12
/**
 
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>
 
17
 *
 
18
 * The example below demonstrates the use of this class.
 
19
 * <pre>
 
20
 *  class ExecuteProcess {
 
21
 *       public static void main( String args[] ) {
 
22
 *           if ( args.length > 0 ) {
 
23
 *               String processName = args[ 0 ];
 
24
 *               try {
 
25
 *                   SimpleProcess process
 
26
 *                      = new SimpleProcess( Runtime.getRuntime.exec(
 
27
 *                                                            processName ) );
 
28
 *                                          );
 
29
 *                   int exitStatus = process.waitFor();
 
30
 *                   System.out.println( "The process ran successfully" 
 
31
 *                                       + " with an exit status of "
 
32
 *                                       + exitStatus + "." );
 
33
 *               }
 
34
 *               catch ( Exception e ) {
 
35
 *                   System.out.println( "The process was not successful. "
 
36
 *                                       + " Reason: " + e.getMessage() );
 
37
 *               }
 
38
 *           }
 
39
 *           else {
 
40
 *               System.err.println( "Please specify a command" );
 
41
 *           }
 
42
 *       }
 
43
 *  }
 
44
 * </pre>
 
45
 *
 
46
 * <hr>
 
47
 *
 
48
 * <pre>
 
49
 * Copyright (C) Damian Ryan Eads, 2001. All Rights Reserved.
 
50
 *
 
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.
 
55
 
 
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.
 
60
 *
 
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
 
64
 * </pre>
 
65
 *
 
66
 * @author Damian Eads
 
67
 */
 
68
 
 
69
public class SimpleProcess extends Process {
 
70
 
 
71
    /**
 
72
     * The target process.
 
73
     */
 
74
 
 
75
    private Process process;
 
76
 
 
77
    /**
 
78
     * The input stream that the processes' standard input will use.
 
79
     */
 
80
 
 
81
    private InputStream processInput;
 
82
 
 
83
    /**
 
84
     * The print stream to redirect to.
 
85
     */
 
86
 
 
87
    private PrintStream yourOutput;
 
88
 
 
89
    /**
 
90
     * The print stream to redirect to.
 
91
     */
 
92
 
 
93
    private PrintStream yourError;
 
94
 
 
95
    /**
 
96
     * The StreamPrinters.
 
97
     */
 
98
 
 
99
    private StreamPrinter in, out, error;
 
100
 
 
101
    /**
 
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
 
104
     * error.
 
105
     */
 
106
 
 
107
    public SimpleProcess( Process process ) throws IOException {
 
108
        this( process, System.in, System.out, System.err );
 
109
    }
 
110
 
 
111
    /**
 
112
     * Constructs a SimpleProcess, initializing it with the streams passed.
 
113
     *
 
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
 
118
     *                      standard output.
 
119
     * @param processError  The stream to redirect the processes's
 
120
     *                      standard input.
 
121
     */
 
122
 
 
123
    public SimpleProcess( Process process, InputStream processInput,
 
124
                          PrintStream yourOutput, PrintStream yourError )
 
125
                         throws IOException {
 
126
        super();
 
127
        this.process = process;
 
128
        this.processInput = processInput;
 
129
        this.yourOutput = yourOutput;
 
130
        this.yourError = yourError;
 
131
    }
 
132
 
 
133
    /**
 
134
     * Returns the standard input of this process.
 
135
     *
 
136
     * @return The standard input of this process.
 
137
     */
 
138
 
 
139
    public OutputStream getOutputStream() {
 
140
        return process.getOutputStream();
 
141
    }
 
142
 
 
143
    /**
 
144
     * Returns the standard output of this process.
 
145
     *
 
146
     * @return The standard output of this process.
 
147
     */
 
148
 
 
149
    public InputStream getInputStream() {
 
150
        return process.getInputStream();
 
151
    }
 
152
 
 
153
    /**
 
154
     * Returns the standard error of this process.
 
155
     *
 
156
     * @return The standard error of this process.
 
157
     */
 
158
 
 
159
    public InputStream getErrorStream() {
 
160
        return process.getErrorStream();
 
161
    }
 
162
 
 
163
    /**
 
164
     * Begin redirecting the streams passed. This method should be invoked
 
165
     * immediately after execution of a simple process to prevent thread
 
166
     * deadlock.
 
167
     *
 
168
     * @return The exit status of the target process.
 
169
     */
 
170
 
 
171
    public int waitFor() throws InterruptedException {
 
172
        int retval = waitForImpl();
 
173
        if ( in != null ) {
 
174
            in.stop();
 
175
        }
 
176
        return retval;
 
177
    }
 
178
 
 
179
    /**
 
180
     * Contains the implementation of wait for.
 
181
     *
 
182
     * @return The exit status of the target process.
 
183
     */
 
184
 
 
185
    private int waitForImpl() throws InterruptedException {
 
186
        process = process;
 
187
        in = new StreamPrinter( processInput,
 
188
                                new PrintStream( process.getOutputStream() ) );
 
189
        in.setFlush( true );
 
190
        out = new StreamPrinter( process.getInputStream(), yourOutput );
 
191
        error = new StreamPrinter( process.getErrorStream(), yourError );
 
192
        in.start();
 
193
        out.start();
 
194
        error.start();
 
195
        out.join();
 
196
        error.join();
 
197
        return process.waitFor();
 
198
    }
 
199
 
 
200
    /**
 
201
     * Returns the target processes' exit value.
 
202
     *
 
203
     * @return This processes' exit value.
 
204
     */
 
205
 
 
206
    public int exitValue() {
 
207
        return process.exitValue();
 
208
    }
 
209
 
 
210
    /**
 
211
     * Destroys the target process.
 
212
     */
 
213
 
 
214
    public void destroy() throws IllegalThreadStateException {
 
215
        process.destroy();
 
216
    }
 
217
} /** SimpleProcess **/