- Timestamp:
- 2017-05-01T20:07:17+12:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/gli/src/org/greenstone/gatherer/util/SafeProcess.java
r31638 r31639 21 21 22 22 public class SafeProcess { 23 //public static int DEBUG = 0; 23 24 24 25 public static final int STDERR = 0; … … 99 100 public void setSplitStdErrorNewLines(boolean split) { 100 101 splitStdErrorNewLines = split; 101 }102 103 // logger and DebugStream print commands are synchronized, therefore thread safe.104 public static void log(String msg) {105 //logger.info(msg);106 107 System.err.println(msg);108 109 //DebugStream.println(msg);110 }111 112 public static void log(String msg, Exception e) { // Print stack trace on the exception113 //logger.error(msg, e);114 115 System.err.println(msg);116 e.printStackTrace();117 118 //DebugStream.println(msg);119 //DebugStream.printStackTrace(e);120 }121 122 public static void log(Exception e) {123 //logger.error(e);124 125 e.printStackTrace();126 127 //DebugStream.printStackTrace(e);128 }129 130 public static void log(String msg, Exception e, boolean printStackTrace) {131 if(printStackTrace) {132 log(msg, e);133 } else {134 log(msg);135 }136 102 } 137 103 … … 184 150 this.exitValue = process.waitFor(); // can throw an InterruptedException if process did not terminate 185 151 } catch(InterruptedException ie) { 186 187 152 log("*** Process interrupted (InterruptedException). Expected to be a Cancel operation."); 188 153 // don't print stacktrace: an interrupt here is not an error, it's expected to be a cancel action 154 if(exceptionHandler != null) { 155 exceptionHandler.gotException(ie); 156 } 189 157 190 158 // propagate interrupts to worker threads here … … 204 172 // to swallow the interrupt this time and not let it propagate by commenting out the next line? 205 173 //Thread.currentThread().interrupt(); // re-interrupt the thread 206 174 207 175 inputGobbler.interrupt(); 208 176 errorGobbler.interrupt(); 209 177 outputGobbler.interrupt(); 210 178 179 // even after the interrupts, we want to proceed to calling join() on all the worker threads 180 // in order to wait for each of them to die before attempting to destroy the process if it 181 // still hasn't terminated after all that. 211 182 } finally { 212 183 … … 358 329 359 330 } finally { 360 361 log("*** In finally of SafeProcess.runProcess(3 params)");331 //String cmd = (this.command == null) ? Arrays.toString(this.command_args) : this.command; 332 //log("*** In finally of SafeProcess.runProcess(3 params): " + cmd); 362 333 363 334 if( process != null ) { … … 440 411 // http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2 441 412 // http://mark.koli.ch/leaky-pipes-remember-to-close-your-streams-when-using-javas-runtimegetruntimeexec 413 414 //String cmd = (this.command == null) ? Arrays.toString(this.command_args) : this.command; 415 //log("*** In finally of SafeProcess.runProcess(2 params): " + cmd); 416 442 417 if( process != null ) { 443 418 log("*** Going to call process.destroy 1"); … … 446 421 log("*** Have called process.destroy 1"); 447 422 } 448 log("*** In finally of SafeProcess.runProcess(2 params)");449 423 } 450 424 … … 484 458 protected CustomProcessHandler(int src) { 485 459 this.source = src; // STDERR or STDOUT or STDIN 486 487 // modify threadname to prefix stream src (stdin/stderr/stdout) 488 // Useful for debugging if thread is named clearly 489 String stream; 490 switch(src) { 491 case SafeProcess.STDERR: 492 stream = "stderr"; 493 case SafeProcess.STDOUT: 494 stream = "stdout"; 495 default: 496 stream = "stdin"; 497 } 498 Thread.currentThread().setName(stream + Thread.currentThread().getName()); 460 } 461 462 public String getThreadNamePrefix() { 463 return SafeProcess.streamToString(this.source); 499 464 } 500 465 … … 510 475 protected LineByLineHandler(int src) { 511 476 this.source = src; // STDERR or STDOUT 477 } 478 479 public String getThreadNamePrefix() { 480 return SafeProcess.streamToString(this.source); 512 481 } 513 482 … … 530 499 private LineByLineHandler lineByLineHandler = null; 531 500 501 protected InputStreamGobbler() { 502 super("InputStreamGobbler"); 503 } 504 532 505 public InputStreamGobbler(InputStream is) 533 506 { 534 super("InputStreamGobbler"); //thread name507 this(); // sets thread name 535 508 this.is = is; 536 509 this.split_newlines = false; … … 539 512 public InputStreamGobbler(InputStream is, boolean split_newlines) 540 513 { 541 super("InputStreamGobbler"); //thread name514 this(); // sets thread name 542 515 this.is = is; 543 516 this.split_newlines = split_newlines; … … 546 519 public InputStreamGobbler(InputStream is, CustomProcessHandler customHandler) 547 520 { 548 super("InputStreamGobbler"); // thread name521 this(); // thread name 549 522 this.is = is; 550 523 this.customHandler = customHandler; 524 this.adjustThreadName(customHandler.getThreadNamePrefix()); 525 } 526 527 528 private void adjustThreadName(String prefix) { 529 this.setName(prefix + this.getName()); 551 530 } 552 531 553 532 public void setLineByLineHandler(LineByLineHandler lblHandler) { 554 533 this.lineByLineHandler = lblHandler; 534 this.adjustThreadName(lblHandler.getThreadNamePrefix()); 555 535 } 556 536 … … 579 559 lineByLineHandler.gotException(ioe); 580 560 } else { 581 log("Exception when reading from a process' stdout/stderr stream: ", ioe);561 log("Exception when reading process stream with " + this.getName() + ": ", ioe); 582 562 } 583 563 } finally { 584 log("*********");585 564 if(this.isInterrupted()) { 586 log(" We've been asked to stop.");565 log("@@@ Successfully interrupted " + this.getName() + "."); 587 566 } 588 567 SafeProcess.closeResource(br); 589 log("*** In finally of " + this.getName());590 log("*********");591 568 } 592 569 } … … 619 596 private CustomProcessHandler customHandler = null; 620 597 598 protected OutputStreamGobbler() { 599 super("stdinOutputStreamGobbler"); // thread name 600 } 601 621 602 public OutputStreamGobbler(OutputStream os) { 622 super("OutputStreamGobbler"); //thread name603 this(); // set thread name 623 604 this.os = os; 624 605 } … … 626 607 public OutputStreamGobbler(OutputStream os, String inputstr) 627 608 { 628 super("OutputStreamGobbler"); //thread name609 this(); // set thread name 629 610 this.os = os; 630 611 this.inputstr = inputstr; … … 632 613 633 614 public OutputStreamGobbler(OutputStream os, CustomProcessHandler customHandler) { 634 super("OutputStreamGobbler"); //thread name615 this(); // set thread name 635 616 this.os = os; 636 617 this.customHandler = customHandler; … … 692 673 } // end static inner class OutputStreamGobbler 693 674 675 //**************** Static methods **************// 676 677 678 // logger and DebugStream print commands are synchronized, therefore thread safe. 679 public static void log(String msg) { 680 //logger.info(msg); 681 682 System.err.println(msg); 683 684 //DebugStream.println(msg); 685 } 686 687 public static void log(String msg, Exception e) { // Print stack trace on the exception 688 //logger.error(msg, e); 689 690 System.err.println(msg); 691 e.printStackTrace(); 692 693 //DebugStream.println(msg); 694 //DebugStream.printStackTrace(e); 695 } 696 697 public static void log(Exception e) { 698 //logger.error(e); 699 700 e.printStackTrace(); 701 702 //DebugStream.printStackTrace(e); 703 } 704 705 public static void log(String msg, Exception e, boolean printStackTrace) { 706 if(printStackTrace) { 707 log(msg, e); 708 } else { 709 log(msg); 710 } 711 } 712 713 public static String streamToString(int src) { 714 String stream; 715 switch(src) { 716 case STDERR: 717 stream = "stderr"; 718 break; 719 case STDOUT: 720 stream = "stdout"; 721 break; 722 default: 723 stream = "stdin"; 724 } 725 return stream; 726 } 694 727 695 728 //**************** Useful static methods. Copied from GLI's Utility.java ******************
Note:
See TracChangeset
for help on using the changeset viewer.