Lines 450-461
Link Here
|
450 |
} |
450 |
} |
451 |
|
451 |
|
452 |
private class OutputGrabber extends Object implements SafeRunnable { |
452 |
private class OutputGrabber extends Object implements SafeRunnable { |
453 |
|
453 |
|
454 |
private static final int LINE_LENGTH = 80; |
454 |
private BufferedReader stdout; |
455 |
private static final int BUFF_LENGTH = 512; |
455 |
private BufferedReader stderr; |
456 |
|
|
|
457 |
private InputStreamReader stdout; |
458 |
private InputStreamReader stderr; |
459 |
|
456 |
|
460 |
// Variables indicating EOF of command's output streams. |
457 |
// Variables indicating EOF of command's output streams. |
461 |
// They are needed only for the OpenVMS & OS/2 patch. |
458 |
// They are needed only for the OpenVMS & OS/2 patch. |
Lines 465-477
Link Here
|
465 |
private boolean shouldStop = false; |
462 |
private boolean shouldStop = false; |
466 |
private boolean stopped = false; |
463 |
private boolean stopped = false; |
467 |
private boolean finished = false; |
464 |
private boolean finished = false; |
468 |
private StringBuffer outBuffer = new StringBuffer(LINE_LENGTH); |
|
|
469 |
private StringBuffer errBuffer = new StringBuffer(LINE_LENGTH); |
470 |
private char[] buff = new char[BUFF_LENGTH]; |
471 |
|
465 |
|
472 |
public OutputGrabber(InputStream stdout, InputStream stderr) { |
466 |
public OutputGrabber(InputStream stdout, InputStream stderr) { |
473 |
this.stdout = new InputStreamReader(stdout); |
467 |
this.stdout = new BufferedReader(new InputStreamReader(stdout)); |
474 |
this.stderr = new InputStreamReader(stderr); |
468 |
this.stderr = new BufferedReader(new InputStreamReader(stderr)); |
475 |
} |
469 |
} |
476 |
|
470 |
|
477 |
/** Stop the grabber */ |
471 |
/** Stop the grabber */ |
Lines 489-508
Link Here
|
489 |
|
483 |
|
490 |
/** Whether the grabber is stopped. If yes, should be flushed and garbage-collected. */ |
484 |
/** Whether the grabber is stopped. If yes, should be flushed and garbage-collected. */ |
491 |
public boolean isStopped() { |
485 |
public boolean isStopped() { |
492 |
try { |
486 |
if (shouldStop && eof_stdout && eof_stderr) { |
493 |
//if (shouldStop && !stdout.ready() && !stderr.ready()) stopped = true; |
|
|
494 |
// If the OS is OpenVMS or OS/2, we want to stop only if EOF has been reached on both output streams |
495 |
if (osType != Utilities.OS_VMS && osType != Utilities.OS_OS2) { |
496 |
if (shouldStop && !stdout.ready() && !stderr.ready()) stopped = true; |
497 |
} else { |
498 |
if (shouldStop && eof_stdout && eof_stderr) { |
499 |
stopped = true; |
500 |
} else { |
501 |
stopped = false; |
502 |
} |
503 |
} |
504 |
} catch (IOException ioexc) { |
505 |
stopped = true; |
487 |
stopped = true; |
|
|
488 |
} else { |
489 |
stopped = false; |
506 |
} |
490 |
} |
507 |
return stopped; |
491 |
return stopped; |
508 |
} |
492 |
} |
Lines 513-609
Link Here
|
513 |
wait(); |
497 |
wait(); |
514 |
} |
498 |
} |
515 |
} |
499 |
} |
516 |
} |
500 |
} |
517 |
|
|
|
518 |
/** Whether there is some output to grab */ |
519 |
public boolean hasOutput() { |
520 |
boolean has; |
521 |
try { |
522 |
//has = stdout.ready() || stderr.ready(); |
523 |
// If the OS is OpenVMS or OS/2, just assume there is output available |
524 |
if (osType != Utilities.OS_VMS && osType != Utilities.OS_OS2) { |
525 |
has = stdout.ready() || stderr.ready(); |
526 |
} else { |
527 |
has = true; |
528 |
} |
529 |
} catch (IOException ioexc) { |
530 |
has = false; |
531 |
} |
532 |
return has; |
533 |
} |
534 |
|
501 |
|
535 |
/** Run the grabbing. It grabbs some of the output, needs to be invoked periodically |
502 |
/** Run the grabbing. It grabbs some of the output, needs to be invoked periodically |
536 |
intil it's not stopped. */ |
503 |
intil it's not stopped. */ |
537 |
public void run() { |
504 |
public void run() { |
538 |
int n = 0; |
505 |
final BufferedReader err = stderr; |
|
|
506 |
final BufferedReader out = stdout; |
507 |
|
508 |
Thread errThread = new Thread(new Runnable() { |
509 |
public void run() { |
510 |
try { |
511 |
String line; |
512 |
while ((line = err.readLine()) != null) { |
513 |
stderrNextLine(line); |
514 |
} |
515 |
} catch (IOException ie) {} |
516 |
} |
517 |
} |
518 |
); |
519 |
|
520 |
Thread outThread = new Thread(new Runnable() { |
521 |
public void run() { |
522 |
try { |
523 |
String line; |
524 |
while ((line = out.readLine()) != null) { |
525 |
stdoutNextLine(line); |
526 |
} |
527 |
} catch (IOException ie) {} |
528 |
} |
529 |
} |
530 |
); |
531 |
outThread.start(); |
532 |
errThread.start(); |
539 |
try { |
533 |
try { |
540 |
//if (stdout.ready() && (n = stdout.read(buff, 0, BUFF_LENGTH)) > -1) { |
534 |
outThread.join(); |
541 |
// For OpenVMS and OS/2, we need to see EOF before we're sure we've grabbed all output |
535 |
} catch (InterruptedException ie) { |
542 |
if (((osType == Utilities.OS_VMS || osType == Utilities.OS_OS2) && !eof_stdout) || stdout.ready()) { |
536 |
} finally { |
543 |
n = stdout.read(buff, 0, BUFF_LENGTH); |
537 |
stopped = true; |
544 |
} |
538 |
eof_stdout = true; |
545 |
if (n > -1) { |
539 |
} |
546 |
for (int i = 0; i < n; i++) { |
540 |
try { |
547 |
if (buff[i] == '\n') { |
541 |
errThread.join(); |
548 |
try { |
542 |
} catch (InterruptedException ie) { |
549 |
stdoutNextLine(outBuffer.toString()); |
543 |
} finally { |
550 |
} finally { |
|
|
551 |
outBuffer.delete(0, outBuffer.length()); |
552 |
} |
553 |
} else { |
554 |
if (buff[i] != 13) { |
555 |
outBuffer.append(buff[i]); |
556 |
} |
557 |
} |
558 |
} |
559 |
//if (n > 0) System.out.println("IMMEDIATE OUT("+n+") = '"+new String(buff, 0, n)+"'"); |
560 |
if (n > 0 && isImmediateOut) { |
561 |
synchronized (stdOutLock) { |
562 |
Iterator it = stdImmediateOutListeners.iterator(); |
563 |
while(it.hasNext()) { |
564 |
((TextOutputListener) it.next()).outputLine(new String(buff, 0, n)); |
565 |
} |
566 |
} |
567 |
} |
568 |
} else { |
569 |
stopped = true; |
570 |
eof_stdout = true; |
571 |
} |
572 |
//if (stderr.ready() && (n = stderr.read(buff, 0, BUFF_LENGTH)) > -1) { |
573 |
n = 0; |
574 |
// For OpenVMS and OS/2, we need to see EOF before we're sure we've grabbed all output |
575 |
if (((osType == Utilities.OS_VMS || osType == Utilities.OS_OS2) && !eof_stderr) || stderr.ready()) { |
576 |
n = stderr.read(buff, 0, BUFF_LENGTH); |
577 |
} |
578 |
if (n > -1) { |
579 |
for (int i = 0; i < n; i++) { |
580 |
if (buff[i] == '\n') { |
581 |
try { |
582 |
stderrNextLine(errBuffer.toString()); |
583 |
} finally { |
584 |
errBuffer.delete(0, errBuffer.length()); |
585 |
} |
586 |
} else { |
587 |
if (buff[i] != 13) { |
588 |
errBuffer.append(buff[i]); |
589 |
} |
590 |
} |
591 |
} |
592 |
//if (n > 0) System.out.println("IMMEDIATE ERR("+n+") = '"+new String(buff, 0, n)+"'"); |
593 |
if (n > 0 && isImmediateErr) { |
594 |
synchronized (stdErrLock) { |
595 |
Iterator it = stdImmediateErrListeners.iterator(); |
596 |
while(it.hasNext()) { |
597 |
((TextOutputListener) it.next()).outputLine(new String(buff, 0, n)); |
598 |
} |
599 |
} |
600 |
} |
601 |
} else { |
602 |
stopped = true; |
603 |
eof_stderr = true; |
604 |
} |
605 |
} catch (IOException ioexc) { |
606 |
stopped = true; |
544 |
stopped = true; |
|
|
545 |
eof_stderr = true; |
607 |
} |
546 |
} |
608 |
} |
547 |
} |
609 |
|
548 |
|
Lines 616-632
Link Here
|
616 |
try { |
555 |
try { |
617 |
stderr.close(); |
556 |
stderr.close(); |
618 |
} catch (IOException ioexc1) {} |
557 |
} catch (IOException ioexc1) {} |
619 |
// Grab any remaining output |
558 |
|
620 |
run(); |
559 |
finished = true; |
621 |
// Flush it |
560 |
|
622 |
try { |
561 |
synchronized (this) { |
623 |
if (outBuffer.length() > 0) stdoutNextLine(outBuffer.toString()); |
562 |
notifyAll(); |
624 |
if (errBuffer.length() > 0) stderrNextLine(errBuffer.toString()); |
|
|
625 |
} finally { |
626 |
finished = true; |
627 |
synchronized (this) { |
628 |
notifyAll(); |
629 |
} |
630 |
} |
563 |
} |
631 |
} |
564 |
} |
632 |
|
565 |
|
Lines 663-673
Link Here
|
663 |
OutputGrabber output = (OutputGrabber) outputGrabbers.get(i); |
596 |
OutputGrabber output = (OutputGrabber) outputGrabbers.get(i); |
664 |
//System.out.println(" output("+i+"): isStopped() = "+output.isStopped()+", hasOutput() = "+output.hasOutput()); |
597 |
//System.out.println(" output("+i+"): isStopped() = "+output.isStopped()+", hasOutput() = "+output.hasOutput()); |
665 |
if (!output.isStopped()) { |
598 |
if (!output.isStopped()) { |
666 |
if (output.hasOutput()) { |
599 |
output.run(); |
667 |
output.run(); |
600 |
processed = true; |
668 |
processed = true; |
601 |
outputProducers.add(output); |
669 |
outputProducers.add(output); |
|
|
670 |
} |
671 |
} else { |
602 |
} else { |
672 |
output.flush(); |
603 |
output.flush(); |
673 |
outputGrabbers.remove(i); |
604 |
outputGrabbers.remove(i); |