3 * This class is intended to be used with the SystemCommandExecutor class to let
4 * users execute system commands from Java applications.
6 * This class is based on work that was shared in a JavaWorld article named
7 * "When System.exec() won't". That article is available at this url:
9 * http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
11 * Documentation for this class is available at this URL:
13 * http://devdaily.com/java/java-processbuilder-process-system-exec
16 * Copyright 2010 alvin j. alexander, devdaily.com.
18 * This program is free software: you can redistribute it and/or modify it under
19 * the terms of the GNU Lesser Public License as published by the Free Software
20 * Foundation, either version 3 of the License, or (at your option) any later
23 * This program is distributed in the hope that it will be useful, but WITHOUT
24 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
25 * FOR A PARTICULAR PURPOSE. See the GNU Lesser Public License for more details.
27 * You should have received a copy of the GNU Lesser Public License along with
28 * this program. If not, see <http://www.gnu.org/licenses/>.
30 * Please ee the following page for the LGPL license:
31 * http://www.gnu.org/licenses/lgpl.txt
35 package org.forester.util;
37 import java.io.BufferedReader;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.io.InputStreamReader;
41 import java.io.OutputStream;
42 import java.io.PrintWriter;
44 class ThreadedStreamHandler extends Thread {
46 InputStream inputStream;
48 OutputStream outputStream;
49 PrintWriter printWriter;
50 StringBuilder outputBuffer = new StringBuilder( 65536 );
51 private boolean sudoIsRequested = false;
54 * A simple constructor for when the sudo command is not necessary.
55 * This constructor will just run the command you provide, without
56 * running sudo before the command, and without expecting a password.
61 ThreadedStreamHandler( final InputStream inputStream ) {
62 this.inputStream = inputStream;
66 * Use this constructor when you want to invoke the 'sudo' command.
67 * The outputStream must not be null. If it is, you'll regret it. :)
69 * TODO this currently hangs if the admin password given for the sudo command is wrong.
74 * @param adminPassword
76 ThreadedStreamHandler( final InputStream inputStream, final OutputStream outputStream, final String adminPassword ) {
77 this.inputStream = inputStream;
78 this.outputStream = outputStream;
79 printWriter = new PrintWriter( outputStream );
80 this.adminPassword = adminPassword;
81 sudoIsRequested = true;
84 ThreadedStreamHandler( final InputStream inputStream, final OutputStream outputStream ) {
85 this.inputStream = inputStream;
86 this.outputStream = outputStream;
87 printWriter = new PrintWriter( outputStream );
88 sudoIsRequested = false;
91 private void doSleep( final long millis ) {
93 Thread.sleep( millis );
95 catch ( final InterruptedException e ) {
100 public StringBuilder getOutputBuffer() {
106 // on mac os x 10.5.x, when i run a 'sudo' command, i need to write
107 // the admin password out immediately; that's why this code is
109 if ( sudoIsRequested ) {
111 printWriter.println( adminPassword );
114 BufferedReader bufferedReader = null;
115 final String newline = ForesterUtil.LINE_SEPARATOR;
117 bufferedReader = new BufferedReader( new InputStreamReader( inputStream ) );
119 while ( ( line = bufferedReader.readLine() ) != null ) {
120 // outputBuffer.append( line + "\n" ); // CMZ change
121 outputBuffer.append( line );
122 outputBuffer.append( newline );
125 catch ( final IOException ioe ) {
126 // TODO handle this better
127 ioe.printStackTrace();
129 catch ( final Throwable t ) {
130 // TODO handle this better
135 bufferedReader.close();
137 catch ( final IOException e ) {