@@ -54,25 +54,68 @@ public static String capture(InputStream is)
54
54
return buff .toString ();
55
55
}
56
56
57
- public static Process executeCommand (String command ) throws IOException
57
+ public static ProcessState executeCommand (String command ) throws IOException
58
58
{
59
59
Process pr = executeCommandProcess (command );
60
+ InputStreamPumpState inputState = new InputStreamPumpState (pr .getInputStream ());
61
+ InputStreamPumpState errorState = new InputStreamPumpState (pr .getErrorStream ());
60
62
61
- int ev = waitForExitValue (pr );
63
+ int ev = waitForExitValue (pr , inputState , errorState );
64
+ inputState .pump ();
65
+ errorState .pump ();
62
66
if (ev != 0 ) {
63
- String stdout = capture (pr .getInputStream ());
64
- String stderr = capture (pr .getErrorStream ());
65
67
throw new IOException ("unexpected command exit value: " + ev +
66
68
"\n command: " + command + "\n " +
67
- "\n stdout:\n " + stdout +
68
- "\n stderr:\n " + stderr + "\n " );
69
+ "\n stdout:\n " + inputState . buffer . toString () +
70
+ "\n stderr:\n " + errorState . buffer . toString () + "\n " );
69
71
}
70
- return pr ;
72
+ return new ProcessState (pr , inputState , errorState );
73
+ }
74
+
75
+ static class ProcessState {
76
+
77
+ private final Process process ;
78
+ private final InputStreamPumpState inputState ;
79
+ private final InputStreamPumpState errorState ;
80
+
81
+ ProcessState (Process process , InputStreamPumpState inputState ,
82
+ InputStreamPumpState errorState ) {
83
+ this .process = process ;
84
+ this .inputState = inputState ;
85
+ this .errorState = errorState ;
86
+ }
87
+
88
+ private String output () {
89
+ return inputState .buffer .toString ();
90
+ }
91
+
92
+ }
93
+
94
+ private static class InputStreamPumpState {
95
+
96
+ private final BufferedReader reader ;
97
+ private final StringBuilder buffer ;
98
+
99
+ private InputStreamPumpState (InputStream in ) {
100
+ this .reader = new BufferedReader (new InputStreamReader (in ));
101
+ this .buffer = new StringBuilder ();
102
+ }
103
+
104
+ void pump () throws IOException {
105
+ String line ;
106
+ while ((line = reader .readLine ()) != null ) {
107
+ buffer .append (line ).append ("\n " );
108
+ }
109
+ }
110
+
71
111
}
72
112
73
- private static int waitForExitValue (Process pr ) {
113
+ private static int waitForExitValue (Process pr , InputStreamPumpState inputState ,
114
+ InputStreamPumpState errorState ) throws IOException {
74
115
while (true ) {
75
116
try {
117
+ inputState .pump ();
118
+ errorState .pump ();
76
119
pr .waitFor ();
77
120
break ;
78
121
} catch (InterruptedException ignored ) {}
@@ -83,6 +126,10 @@ private static int waitForExitValue(Process pr) {
83
126
public static Process executeCommandIgnoringErrors (String command ) throws IOException
84
127
{
85
128
Process pr = executeCommandProcess (command );
129
+ InputStreamPumpState inputState = new InputStreamPumpState (pr .getInputStream ());
130
+ InputStreamPumpState errorState = new InputStreamPumpState (pr .getErrorStream ());
131
+ inputState .pump ();
132
+ errorState .pump ();
86
133
boolean exited = false ;
87
134
try {
88
135
exited = pr .waitFor (30 , TimeUnit .SECONDS );
@@ -118,7 +165,7 @@ public static boolean isRabbitMqCtlCommandAvailable(String command) throws IOExc
118
165
return exitValue == 0 ;
119
166
}
120
167
121
- public static Process rabbitmqctl (String command ) throws IOException {
168
+ public static ProcessState rabbitmqctl (String command ) throws IOException {
122
169
return executeCommand (rabbitmqctlCommand () +
123
170
rabbitmqctlNodenameArgument () +
124
171
" " + command );
@@ -142,7 +189,7 @@ public static void clearResourceAlarm(String source) throws IOException {
142
189
rabbitmqctl ("eval 'rabbit_alarm:clear_alarm({resource_limit, " + source + ", node()}).'" );
143
190
}
144
191
145
- public static Process invokeMakeTarget (String command ) throws IOException {
192
+ public static ProcessState invokeMakeTarget (String command ) throws IOException {
146
193
File rabbitmqctl = new File (rabbitmqctlCommand ());
147
194
return executeCommand (makeCommand () +
148
195
" -C \' " + rabbitmqDir () + "\' " +
@@ -307,7 +354,7 @@ public String toString() {
307
354
}
308
355
309
356
public static List <ConnectionInfo > listConnections () throws IOException {
310
- String output = capture ( rabbitmqctl ("list_connections -q pid peer_port client_properties" ).getInputStream () );
357
+ String output = rabbitmqctl ("list_connections -q pid peer_port client_properties" ).output ( );
311
358
// output (header line presence depends on broker version):
312
359
// pid peer_port
313
360
0 commit comments