1
1
/*
2
- Copyright 2017 The Kubernetes Authors.
2
+ Copyright 2017, 2018 The Kubernetes Authors.
3
3
Licensed under the Apache License, Version 2.0 (the "License");
4
4
you may not use this file except in compliance with the License.
5
5
You may obtain a copy of the License at
18
18
import java .io .IOException ;
19
19
import java .io .InputStream ;
20
20
import java .io .OutputStream ;
21
+ import java .util .HashMap ;
22
+ import java .util .Map ;
21
23
import java .util .concurrent .TimeUnit ;
22
24
import org .apache .commons .lang3 .StringUtils ;
25
+ import org .slf4j .Logger ;
26
+ import org .slf4j .LoggerFactory ;
23
27
24
28
public class Exec {
29
+ private static final Logger log = LoggerFactory .getLogger (Exec .class );
30
+
25
31
private ApiClient apiClient ;
26
32
27
33
/** Simple Exec API constructor, uses default configuration */
@@ -178,10 +184,34 @@ public Process exec(
178
184
return exec ;
179
185
}
180
186
187
+ static int parseExitCode (InputStream inputStream ) {
188
+ int exitCode = 0 ;
189
+ try {
190
+ int available = inputStream .available ();
191
+ if (available > 0 ) {
192
+ byte [] b = new byte [available ];
193
+ inputStream .read (b );
194
+ String result = new String (b , "UTF-8" );
195
+ int idx = result .lastIndexOf (':' );
196
+ if (idx > 0 ) {
197
+ try {
198
+ exitCode = Integer .parseInt (result .substring (idx + 1 ).trim ());
199
+ } catch (NumberFormatException nfe ) {
200
+ log .error ("Error parsing exit code from status channel response" , nfe );
201
+ }
202
+ }
203
+ }
204
+ } catch (IOException io ) {
205
+ log .error ("Error parsing exit code from status channel response" , io );
206
+ }
207
+
208
+ return exitCode ;
209
+ }
210
+
181
211
private static class ExecProcess extends Process {
182
212
private final WebSocketStreamHandler streamHandler ;
183
213
private volatile int statusCode ;
184
- private volatile boolean isDestroyed = false ;
214
+ private final Map < Integer , InputStream > input = new HashMap <>() ;
185
215
186
216
public ExecProcess () throws IOException {
187
217
this .statusCode = -1 ;
@@ -190,25 +220,7 @@ public ExecProcess() throws IOException {
190
220
@ Override
191
221
public void close () {
192
222
if (statusCode == -1 ) {
193
- InputStream inputStream = getInputStream (3 );
194
- int exitCode = 0 ;
195
- try {
196
- int available = inputStream .available ();
197
- if (available > 0 ) {
198
- byte [] b = new byte [available ];
199
- inputStream .read (b );
200
- String result = new String (b , "UTF-8" );
201
- int idx = result .lastIndexOf (':' );
202
- if (idx > 0 ) {
203
- try {
204
- exitCode = Integer .parseInt (result .substring (idx + 1 ).trim ());
205
- } catch (NumberFormatException nfe ) {
206
- // no-op
207
- }
208
- }
209
- }
210
- } catch (IOException e ) {
211
- }
223
+ int exitCode = parseExitCode (ExecProcess .this .getInputStream (3 ));
212
224
213
225
// notify of process completion
214
226
synchronized (ExecProcess .this ) {
@@ -217,7 +229,7 @@ public void close() {
217
229
}
218
230
}
219
231
220
- if ( isDestroyed ) super .close ();
232
+ super .close ();
221
233
}
222
234
};
223
235
}
@@ -233,12 +245,19 @@ public OutputStream getOutputStream() {
233
245
234
246
@ Override
235
247
public InputStream getInputStream () {
236
- return streamHandler . getInputStream (1 );
248
+ return getInputStream (1 );
237
249
}
238
250
239
251
@ Override
240
252
public InputStream getErrorStream () {
241
- return streamHandler .getInputStream (2 );
253
+ return getInputStream (2 );
254
+ }
255
+
256
+ private synchronized InputStream getInputStream (int stream ) {
257
+ if (!input .containsKey (stream )) {
258
+ input .put (stream , streamHandler .getInputStream (stream ));
259
+ }
260
+ return input .get (stream );
242
261
}
243
262
244
263
@ Override
@@ -265,8 +284,14 @@ public int exitValue() {
265
284
266
285
@ Override
267
286
public void destroy () {
268
- isDestroyed = true ;
269
287
streamHandler .close ();
288
+ for (InputStream in : input .values ()) {
289
+ try {
290
+ in .close ();
291
+ } catch (IOException ex ) {
292
+ log .error ("Error on close" , ex );
293
+ }
294
+ }
270
295
}
271
296
}
272
297
}
0 commit comments