CrossRun.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.List;
  7. public class CrossRun {
  8. private static final List<String> messages = Collections.synchronizedList(new ArrayList<String>());
  9. private static volatile boolean failed = false;
  10. private static void error(String message) {
  11. System.out.println("ERROR: " + message);
  12. System.exit(1);
  13. }
  14. public static void main(String[] args) {
  15. int sep = -1;
  16. for (int i = 0; i < args.length; i++) {
  17. if (args[i].equals("--")) {
  18. sep = i;
  19. break;
  20. }
  21. }
  22. if (sep == -1) {
  23. error("Expected exactly one '--' as separator for the two process command lines.");
  24. }
  25. String[] params1 = new String[sep];
  26. System.arraycopy(args, 0, params1, 0, params1.length);
  27. String[] params2 = new String[args.length - sep - 1];
  28. System.arraycopy(args, sep + 1, params2, 0, params2.length);
  29. long startTime = System.currentTimeMillis();
  30. try {
  31. runProcesses(params1, params2);
  32. } catch (IOException e) {
  33. error(e.getMessage());
  34. }
  35. System.out.println("Completed in " + (System.currentTimeMillis() - startTime) + " ms.");
  36. }
  37. private static void runProcesses(String[] params1, String[] params2) throws IOException {
  38. Process process1 = new ProcessBuilder(params1).start();
  39. Process process2 = new ProcessBuilder(params2).start();
  40. Thread readProcess1WriteProcess2Thread =
  41. new Thread(new StreamProxyRunner("process1", "process2", process1.getInputStream(), process2.getOutputStream()));
  42. Thread readProcess2WriteProcess1Thread =
  43. new Thread(new StreamProxyRunner("process2", "process1", process2.getInputStream(), process1.getOutputStream()));
  44. readProcess1WriteProcess2Thread.start();
  45. readProcess2WriteProcess1Thread.start();
  46. int processExitCode1 = -1;
  47. try {
  48. processExitCode1 = process1.waitFor();
  49. } catch (InterruptedException e) {
  50. error(e.getMessage());
  51. }
  52. int processExitCode2 = -1;
  53. try {
  54. processExitCode2 = process2.waitFor();
  55. } catch (InterruptedException e) {
  56. error(e.getMessage());
  57. }
  58. try {
  59. readProcess1WriteProcess2Thread.join();
  60. } catch (InterruptedException e) {
  61. error(e.getMessage());
  62. }
  63. try {
  64. readProcess2WriteProcess1Thread.join();
  65. } catch (InterruptedException e) {
  66. error(e.getMessage());
  67. }
  68. if (processExitCode1 != 0) {
  69. messages.add("The process 1 returned with exit code " + processExitCode1 + ".");
  70. }
  71. if (processExitCode2 != 0) {
  72. messages.add("The process 2 returned with exit code " + processExitCode2 + ".");
  73. }
  74. for (String message : messages) {
  75. System.out.println("* " + message);
  76. }
  77. if (failed) {
  78. System.exit(1);
  79. }
  80. }
  81. @SuppressWarnings("ClassCanBeRecord")
  82. private static final class StreamProxyRunner implements Runnable {
  83. private final String processName1;
  84. private final String processName2;
  85. private final InputStream inputStream;
  86. private final OutputStream outputStream;
  87. private StreamProxyRunner(final String processName1,
  88. final String processName2,
  89. final InputStream inputStream,
  90. final OutputStream outputStream) {
  91. this.processName1 = processName1;
  92. this.processName2 = processName2;
  93. this.inputStream = inputStream;
  94. this.outputStream = outputStream;
  95. }
  96. @Override
  97. public void run() {
  98. byte[] buffer = new byte[65536];
  99. while (true) {
  100. int size;
  101. try {
  102. size = inputStream.read(buffer);
  103. } catch (IOException e) {
  104. messages.add("Unexpected exception " + e.getClass().getSimpleName() + " while reading from the output of the " + processName1 + " process: " + e.getMessage());
  105. failed = true;
  106. break;
  107. }
  108. if (size < 0) {
  109. break;
  110. }
  111. try {
  112. outputStream.write(buffer, 0, size);
  113. outputStream.flush();
  114. } catch (IOException e) {
  115. messages.add("Unexpected exception " + e.getClass().getSimpleName() + " while writing to the input of the " + processName2 + " process: " + e.getMessage());
  116. failed = true;
  117. break;
  118. }
  119. }
  120. try {
  121. inputStream.close();
  122. } catch (IOException e) {
  123. // No operations.
  124. }
  125. try {
  126. outputStream.close();
  127. } catch (IOException e) {
  128. // No operations.
  129. }
  130. }
  131. }
  132. }