1 26 27 package net.sourceforge.groboutils.util.thread.v1; 28 29 30 import java.io.File ; 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.io.OutputStream ; 34 import java.io.PipedInputStream ; 35 import java.io.PipedOutputStream ; 36 37 import java.lang.reflect.Method ; 38 39 40 59 public class BackgroundProcess 60 { 61 private Process proc; 62 private OutputStream stdIn; 63 private PipedInputStream outReader; 64 private PipedOutputStream outWriter; 65 private PipedInputStream errReader; 66 private PipedOutputStream errWriter; 67 68 private IOThreadRunner outThread; 69 private IOThreadRunner errThread; 70 71 72 77 public BackgroundProcess( String command ) 78 throws IOException 79 { 80 setupProcess( exec( command, null, null ) ); 81 } 82 83 84 87 public BackgroundProcess( String [] cmdarray ) 88 throws IOException 89 { 90 setupProcess( exec( cmdarray, null, null ) ); 91 } 92 93 94 97 public BackgroundProcess( String [] cmdarray, String [] envp, File dir ) 98 throws IOException 99 { 100 setupProcess( exec( cmdarray, envp, dir ) ); 101 } 102 103 104 107 public BackgroundProcess( String command, String [] envp, File dir ) 108 throws IOException 109 { 110 setupProcess( exec( command, envp, dir ) ); 111 } 112 113 114 117 public BackgroundProcess( String [] cmdarray, String [] envp ) 118 throws IOException 119 { 120 setupProcess( exec( cmdarray, envp, null ) ); 121 } 122 123 124 127 public BackgroundProcess( String command, String [] envp ) 128 throws IOException 129 { 130 setupProcess( exec( command, envp, null ) ); 131 } 132 133 134 137 public BackgroundProcess( Process p ) 138 throws IOException 139 { 140 if (p == null) 141 { 142 throw new IllegalArgumentException ("no null args"); 143 } 144 setupProcess( p ); 145 } 146 147 148 151 public OutputStream getStdIn() 152 { 153 return this.stdIn; 154 } 155 156 157 161 public InputStream getStdOut() 162 { 163 return this.outReader; 164 } 165 166 167 171 public InputStream getStdErr() 172 { 173 return this.errReader; 174 } 175 176 177 180 public void destroy() 181 { 182 this.proc.destroy(); 183 } 184 185 186 189 public int exitValue() 190 { 191 return this.proc.exitValue(); 192 } 193 194 195 198 public int waitFor() 199 throws InterruptedException 200 { 201 return this.proc.waitFor(); 202 } 203 204 205 208 209 212 protected void setupProcess( Process p ) 213 throws IOException 214 { 215 this.proc = p; 216 217 this.stdIn = p.getOutputStream(); 218 219 this.outReader = new PipedInputStream (); 220 this.outWriter = new PipedOutputStream ( this.outReader ); 221 222 this.errReader = new PipedInputStream (); 223 this.errWriter = new PipedOutputStream ( this.errReader ); 224 225 this.outThread = 226 new IOThreadRunner( p.getInputStream(), this.outWriter ); 227 this.outThread.setCloseInputOnStop( true ); 228 this.outThread.setCloseOutputOnStop( true ); 229 this.outThread.getThread().setSleepTime( 0 ); 230 231 this.errThread = 232 new IOThreadRunner( p.getErrorStream(), this.errWriter ); 233 this.errThread.setCloseInputOnStop( true ); 234 this.errThread.setCloseOutputOnStop( true ); 235 this.errThread.getThread().setSleepTime( 0 ); 236 237 238 this.outThread.start(); 239 this.errThread.start(); 240 } 241 242 243 244 247 protected Process exec( String cmd, String [] env, File dir ) 248 throws IOException 249 { 250 Runtime r = Runtime.getRuntime(); 251 Process p; 252 if (dir == null) 253 { 254 if (env == null) 255 { 256 p = r.exec( cmd ); 257 } 258 else 259 { 260 p = r.exec( cmd, env ); 261 } 262 } 263 else 264 { 265 p = reflectExec( cmd, env, dir ); 266 if (p == null) 267 { 268 p = r.exec( cmd, setPWD( env, dir ) ); 269 } 270 } 271 return p; 272 } 273 274 275 278 protected static Process exec( String [] cmd, String [] env, File dir ) 279 throws IOException 280 { 281 Runtime r = Runtime.getRuntime(); 282 Process p; 283 if (dir == null) 284 { 285 if (env == null) 286 { 287 p = r.exec( cmd ); 288 } 289 else 290 { 291 p = r.exec( cmd, env ); 292 } 293 } 294 else 295 { 296 p = reflectExec( cmd, env, dir ); 297 if (p == null) 298 { 299 p = r.exec( cmd, setPWD( env, dir ) ); 300 } 301 } 302 return p; 303 } 304 305 306 313 protected static Process reflectExec( Object cmd, String [] env, File dir ) 314 throws IOException 315 { 316 try 317 { 318 Runtime r = Runtime.getRuntime(); 319 Method m = r.getClass().getMethod( "exec", 320 new Class [] { cmd.getClass(), String [].class, 321 File .class } 322 ); 323 if (m == null) 324 { 325 return null; 326 } 327 return (Process )m.invoke( r, new Object [] { cmd, env, dir } ); 328 } 329 catch (java.lang.reflect.InvocationTargetException ite) 330 { 331 Throwable t = ite.getTargetException(); 332 if (t instanceof IOException ) 333 { 334 throw (IOException )t; 335 } 336 if (t instanceof RuntimeException ) 337 { 338 throw (RuntimeException )t; 339 } 340 if (t instanceof Error ) 341 { 342 throw (Error )t; 343 } 344 t.printStackTrace(); 346 } 347 catch (IllegalAccessException e) 348 { 349 } 351 catch (NoSuchMethodException e) 352 { 353 } 355 catch (IllegalArgumentException e) 356 { 357 } 359 364 369 return null; 370 } 371 372 373 376 protected static String [] setPWD( String env[], File dir ) 377 { 378 String tmp[]; 379 String pwd = "PWD="+dir.getAbsolutePath(); 380 if (env == null) 381 { 382 tmp = new String [] { pwd }; 383 } 384 else 385 { 386 int len = env.length; 387 tmp = new String [ len ]; 388 System.arraycopy( env, 0, tmp, 0, len ); 389 String s[] = new String [ len + 1 ]; 390 for (int i = 0; i < len; ++i) 391 { 392 if (tmp[i].startsWith( "PWD=" )) 393 { 394 tmp[i] = pwd; 395 s = null; 396 break; 397 } 398 else 399 { 400 s[i] = tmp[i]; 401 } 402 } 403 if (s != null) 404 { 405 s[ env.length ] = pwd; 406 tmp = s; 407 } 408 } 409 return tmp; 410 } 411 } 412 | Popular Tags |