1 package org.sapia.console; 2 3 import java.io.BufferedReader ; 4 import java.io.File ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.io.InputStreamReader ; 8 import java.io.PipedInputStream ; 9 import java.io.PipedOutputStream ; 10 11 12 39 public class CmdLineThread extends Thread { 40 private CmdLine _cmd; 41 private boolean _ready; 42 private boolean _closed; 43 private PipedInputStream _stdout; 44 private PipedInputStream _stderr; 45 private File _procDir; 46 private String [] _env; 47 private IOException _exc; 48 private Process _proc; 49 50 53 public CmdLineThread(CmdLine cmd) { 54 super("CmdLineThread"); 55 _cmd = cmd; 56 } 57 58 public CmdLineThread(CmdLine cmd, String [] env) { 59 super("CmdLineThread"); 60 _cmd = cmd; 61 _env = env; 62 } 63 64 public CmdLineThread(CmdLine cmd, File processDir, String [] env) { 65 this(cmd); 66 _procDir = processDir; 67 _env = env; 68 } 69 70 public Process getProcess() { 71 return _proc; 72 } 73 74 79 public synchronized InputStream getInputStream() throws IOException { 80 if (_exc != null) { 81 throw _exc; 82 } 83 84 while (!_ready) { 85 try { 86 wait(); 87 } catch (InterruptedException e) { 88 throw new IOException (e.getMessage()); 89 } 90 } 91 92 return new ProcessInputStream(_stdout, this); 93 } 94 95 100 public synchronized InputStream getErrStream() throws IOException { 101 if (_exc != null) { 102 throw _exc; 103 } 104 105 while (!_ready) { 106 try { 107 wait(); 108 } catch (InterruptedException e) { 109 throw new IOException (e.getMessage()); 110 } 111 } 112 113 return new ProcessInputStream(_stderr, this); 114 } 115 116 synchronized void close() { 117 _closed = true; 118 } 119 120 synchronized boolean isClosed() { 121 return _closed; 122 } 123 124 boolean hasException() { 125 return _exc != null; 126 } 127 128 IOException getException() { 129 return _exc; 130 } 131 132 135 public void run() { 136 PipedOutputStream perr = null; 137 PipedOutputStream pout = null; 138 139 try { 140 Process proc; 141 142 if (_procDir == null) { 143 proc = Runtime.getRuntime().exec(_cmd.toArray(), _env); 144 } else { 145 proc = Runtime.getRuntime().exec(_cmd.toArray(), _env, _procDir); 146 } 147 148 _proc = proc; 149 150 InputStream stderr = proc.getErrorStream(); 151 InputStream stdout = proc.getInputStream(); 152 _stdout = new PipedInputStream (); 153 _stderr = new PipedInputStream (); 154 pout = new PipedOutputStream (_stdout); 155 perr = new PipedOutputStream (_stderr); 156 157 streamsCreated(); 158 159 int avail; 160 byte[] buf; 161 162 while (!isClosed()) { 163 if ((avail = stderr.available()) > 0) { 164 stderr.read(buf = new byte[avail]); 165 perr.write(buf); 166 perr.flush(); 167 } 168 169 if ((avail = stdout.available()) > 0) { 170 stdout.read(buf = new byte[avail]); 171 pout.write(buf); 172 pout.flush(); 173 } 174 175 Thread.sleep(200); 176 } 177 } catch (IOException e) { 178 _exc = e; 179 streamsCreated(); 180 } catch (InterruptedException e) { 181 _exc = new IOException (e.getMessage()); 182 streamsCreated(); 183 } finally { 184 if (perr != null) { 185 try { 186 perr.close(); 187 } catch (IOException e) { 188 } 189 } 190 191 if (pout != null) { 192 try { 193 pout.close(); 194 } catch (IOException e) { 195 } 196 } 197 } 198 } 199 200 private synchronized void streamsCreated() { 201 _ready = true; 202 notify(); 203 } 204 205 public static void main(String [] args) { 206 try { 207 String cmdStr = "\"/opt/IBMJava2-141/bin/../bin/../jre/bin/java\" -Xms32M -Dcorus.process.java.main=org.sapia.corus.examples.NoopApplication -Dcorus.server.host=127.0.0.1 -Dcorus.server.port=33000 -Dcorus.server.domain=yanick -Dcorus.distribution.name=demo -Dcorus.distribution.version=1.0 -Dcorus.process.dir\"/opt/dev/java/sapia/corus/deploy/yanick_33000/demo/1.0/processes/10829977791640\" -Dcorus.process.id=10829977791640 -Dcorus.process.poll.interval=10 -Dcorus.process.status.interval=30 -Dcorus.process.profile=test -Duser.dir=/opt/dev/java/sapia/corus/deploy/yanick_33000/demo/1.0/common -cp /opt/dev/java/sapia/corus/lib/sapia_corus_starter.jar:/opt/dev/java/sapia/corus/dist/sapia_corus_starter.jar:/opt/dev/java/sapia/corus/deploy/yanick_33000/demo/1.0/common/lib/demo.jar:/opt/dev/java/sapia/corus/deploy/yanick_33000/demo/1.0/common/jdom-1_0b9.jar:/opt/dev/java/sapia/corus/deploy/yanick_33000/demo/1.0/common/lib/log4j-1_2_5.jar:/opt/dev/java/sapia/corus/deploy/yanick_33000/demo/1.0/common/lib/Piccolo-1_0_3.jar:/opt/dev/java/sapia/corus/deploy/yanick_33000/demo/1.0/common/lib/sapia_corus_iop.jar:/opt/dev/java/sapia/corus/deploy/yanick_33000/demo/1.0/common/lib/sapia_utils-1_2.jar org.sapia.corus.starter.Starter"; 208 CmdLine cmd = CmdLine.parse(cmdStr); 209 210 214 ExecHandle handle = cmd.exec(); 215 216 BufferedReader br = new BufferedReader (new InputStreamReader ( 217 handle.getInputStream())); 218 String line; 219 220 while ((line = br.readLine()) != null) { 221 System.out.println(line); 222 } 223 224 br.close(); 225 } catch (Exception e) { 226 e.printStackTrace(); 227 } 228 } 229 230 233 static final class ProcessInputStream extends InputStream { 234 private InputStream _is; 235 private CmdLineThread _cmdThread; 236 237 ProcessInputStream(InputStream is, CmdLineThread cmdThread) { 238 _is = is; 239 _cmdThread = cmdThread; 240 } 241 242 245 public int available() throws IOException { 246 checkException(); 247 248 return _is.available(); 249 } 250 251 254 public void close() throws IOException { 255 checkException(); 256 257 try { 258 if (_is != null) { 259 _is.close(); 260 } 261 } finally { 262 _cmdThread.close(); 263 } 264 } 265 266 269 public synchronized void mark(int arg0) { 270 _is.mark(arg0); 271 } 272 273 276 public boolean markSupported() { 277 return _is.markSupported(); 278 } 279 280 283 public int read() throws IOException { 284 checkException(); 285 286 return _is.read(); 287 } 288 289 292 public int read(byte[] arg0, int arg1, int arg2) throws IOException { 293 checkException(); 294 295 return _is.read(arg0, arg1, arg2); 296 } 297 298 301 public int read(byte[] arg0) throws IOException { 302 checkException(); 303 304 return _is.read(arg0); 305 } 306 307 310 public synchronized void reset() throws IOException { 311 checkException(); 312 _is.reset(); 313 } 314 315 318 public long skip(long arg0) throws IOException { 319 checkException(); 320 321 return _is.skip(arg0); 322 } 323 324 private void checkException() throws IOException { 325 if (_cmdThread.hasException()) { 326 throw _cmdThread.getException(); 327 } 328 } 329 } 330 } 331 | Popular Tags |