View Javadoc
1 /* Reattore HTTP Server 2 3 Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz> 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2 of the License, or 8 (at your option) any later version. 9 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 License 16 along with this program; if not, write to the Free Software 17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 19 $Id: Spawner.java,v 1.2 2003/03/03 05:14:39 michaelh Exp $ 20 */ 21 package juju.reattore.util; 22 23 import java.io.*; 24 25 import java.util.*; 26 27 import org.apache.commons.logging.*; 28 29 /*** A native process wrapper that provides higher level functionality 30 over Runtime.exec() 31 */ 32 public class Spawner 33 implements Runnable { 34 35 private static Log log = LogFactory.getLog(Spawner.class); 36 private String command; 37 private File dir; 38 private BufferedReader stdout; 39 private BufferedReader stderr; 40 private List output; 41 private Thread reader; 42 private Process proc; 43 private boolean running; 44 private boolean done; 45 private int exitValue; 46 47 /*** Internal method. 48 */ 49 public void run() { 50 synchronized (this) { 51 running = true; 52 notifyAll(); 53 } 54 55 try { 56 do { 57 try { 58 readStreams(); 59 60 synchronized (this) { 61 wait(500); 62 } 63 } 64 catch (InterruptedException ex) { 65 /* Ignore - normal operation */ 66 } 67 } 68 while (!done); 69 70 readStreams(); 71 } 72 catch (IOException ex) { 73 /* PENDING */ 74 } 75 } 76 77 private void readStreams() throws IOException { 78 readStream(stdout, output); 79 readStream(stderr, output); 80 } 81 82 private void readStream(BufferedReader in, List out) 83 throws IOException { 84 while (in.ready()) { 85 String got = in.readLine(); 86 87 synchronized (out) { 88 out.add(got); 89 out.notifyAll(); 90 } 91 } 92 } 93 94 /*** The Runtime.exec command to run. 95 96 @param command The command. 97 */ 98 public void setCommand(String command) { 99 this.command = command; 100 } 101 102 /*** The working directory to start in. null means unset. 103 104 @param dir Working directory. 105 */ 106 public void setDir(String dir) { 107 if (dir != null) { 108 this.dir = new File(dir); 109 } 110 else { 111 this.dir = null; 112 } 113 } 114 115 private void setup() { 116 output = new ArrayList(); 117 118 running = false; 119 done = false; 120 } 121 122 /*** Starts the process running in the background and waits for it 123 to start. 124 125 @throws Exception on error. 126 */ 127 public void spawn() 128 throws Exception { 129 130 setup(); 131 132 proc = Runtime.getRuntime().exec(command, new String[] { }, dir); 133 134 stdout = new BufferedReader(new InputStreamReader(proc.getInputStream())); 135 stderr = new BufferedReader(new InputStreamReader(proc.getErrorStream())); 136 137 reader = new Thread(this); 138 reader.start(); 139 140 waitRunning(); 141 } 142 143 private void waitRunning() throws Exception { 144 synchronized (this) { 145 while (running == false) { 146 wait(); 147 } 148 } 149 } 150 151 /*** Waits for the process to terminate. 152 153 @throws Exception on error. 154 */ 155 public void waitDone() 156 throws Exception { 157 158 if (done) { 159 } 160 else { 161 exitValue = proc.waitFor(); 162 done = true; 163 164 reader.interrupt(); 165 reader.join(); 166 167 stdout.close(); 168 stderr.close(); 169 } 170 } 171 172 /*** The exit code of the process, if present. Not valid until the 173 process completes. 174 175 @return The exit value. 176 */ 177 public int exitValue() { 178 return exitValue; 179 } 180 181 /*** Set if the process is finished. 182 183 @return true if finished. 184 */ 185 public boolean isDone() { 186 return done; 187 } 188 189 /*** Kills the process if it is not yet finished. 190 191 @throws Exception on error. 192 */ 193 public void destroy() 194 throws Exception { 195 196 if (done) { 197 } 198 else { 199 proc.destroy(); 200 waitDone(); 201 } 202 } 203 204 /*** The processes stdout and stderr, combined, as a list of 205 strings. The controller calls notifyAll() when lines are 206 added. 207 208 @return The processes output. 209 */ 210 public List getOutput() { 211 return output; 212 } 213 }

This page was automatically generated by Maven