1 10 package org.mmbase.util.externalprocess; 11 12 import java.io.InputStream ; 13 import java.io.OutputStream ; 14 15 import org.mmbase.util.logging.Logger; 16 import org.mmbase.util.logging.Logging; 17 18 34 public class ProcessClosure { 35 36 private static final Logger log = Logging.getLoggerInstance(ProcessClosure.class); 37 38 41 protected String name; 42 43 46 protected Process process; 47 48 51 protected InputStream input; 52 55 protected OutputStream output; 56 59 protected OutputStream error; 60 61 64 protected StreamCopyThread inputWriter; 65 68 protected StreamCopyThread outputReader; 69 72 protected StreamCopyThread errorReader; 73 74 85 public ProcessClosure( 86 String name, 87 Process process, 88 InputStream inputStream, 89 OutputStream outputStream, 90 OutputStream errorStream) { 91 this.name = name; 92 this.process = process; 93 this.input = inputStream; 94 this.output = outputStream; 95 this.error = errorStream; 96 } 97 98 101 public void readNonBlocking() { 102 log.debug(name + " read Non Blocking"); 103 104 ThreadGroup group = new ThreadGroup (name + " ThreadGroup"); 105 106 109 InputStream stdout = process.getInputStream(); 111 InputStream stderr = process.getErrorStream(); 112 113 outputReader = new StreamCopyThread(group, name + "OutputReader", stdout, output, false); 114 errorReader = new StreamCopyThread(group, name + "ErrorReader", stderr, error, false); 115 116 outputReader.start(); 117 errorReader.start(); 118 } 119 120 123 public void writeNonBlocking() { 124 log.debug(name + " write Non Blocking"); 125 if (input != null) { 127 OutputStream stdin = process.getOutputStream(); 128 129 inputWriter = new StreamCopyThread(name + "InputWriter", input, stdin, true); 130 131 inputWriter.start(); 132 } 133 } 134 135 139 public void readBlocking() { 140 log.debug(name + " read Blocking"); 141 readNonBlocking(); 142 143 log.debug(name + " wait for process"); 144 waitForProcess(); 145 waitForReaders(); 146 147 process = null; 150 outputReader = null; 151 errorReader = null; 152 log.debug(name + " read done"); 153 } 154 155 159 public void writeBlocking() { 160 log.debug(name + " write Blocking"); 161 162 readNonBlocking(); 165 writeNonBlocking(); 166 167 log.debug(name + " wait for process"); 168 waitForWriter(); 169 waitForProcess(); 170 waitForReaders(); 171 172 process = null; 175 outputReader = null; 176 errorReader = null; 177 inputWriter = null; 178 log.debug(name + " write done"); 179 } 180 181 184 protected void waitForProcess() { 185 boolean finished = false; 186 while (!finished) { 187 try { 188 process.waitFor(); 189 } catch (InterruptedException e) { 190 } 192 try { 193 process.exitValue(); 194 finished = true; 195 } catch (IllegalThreadStateException e) { 196 } 198 } 199 } 200 201 204 protected void waitForReaders() { 205 if (!outputReader.finished()) { 207 outputReader.waitFor(); 208 } 209 210 if (!errorReader.finished()) { 211 errorReader.waitFor(); 212 } 213 } 214 215 218 protected void waitForWriter() { 219 if (!inputWriter.finished()) { 221 inputWriter.waitFor(); 222 } 223 } 224 225 230 public boolean isAlive() { 231 if (process != null) { 232 if (outputReader.isAlive() || errorReader.isAlive()) { 233 return true; 234 } else { 235 process = null; 236 outputReader = null; 237 errorReader = null; 238 } 239 } 240 return false; 241 } 242 243 246 public void terminate() { 247 if (process != null) { 248 process.destroy(); 249 process = null; 250 } 251 } 252 253 } 254 | Popular Tags |