33 */
44package com .github .dockerjava .core .command ;
55
6- import org .slf4j .Logger ;
7- import org .slf4j .LoggerFactory ;
8-
96import com .github .dockerjava .api .DockerClientException ;
107import com .github .dockerjava .api .model .PullResponseItem ;
118import com .github .dockerjava .core .async .ResultCallbackTemplate ;
9+ import org .slf4j .Logger ;
10+ import org .slf4j .LoggerFactory ;
11+
12+ import java .util .HashMap ;
13+ import java .util .Map ;
1214
1315/**
1416 *
@@ -19,14 +21,71 @@ public class PullImageResultCallback extends ResultCallbackTemplate<PullImageRes
1921
2022 private final static Logger LOGGER = LoggerFactory .getLogger (PullImageResultCallback .class );
2123
24+ private boolean isSwarm = false ;
25+ private Map <String , PullResponseItem > results = null ;
2226 private PullResponseItem latestItem = null ;
2327
2428 @ Override
2529 public void onNext (PullResponseItem item ) {
26- this .latestItem = item ;
30+ // only do it once
31+ if (results == null && latestItem == null ) {
32+ checkForDockerSwarmResponse (item );
33+ }
34+
35+ if (isSwarm ) {
36+ handleDockerSwarmResponse (item );
37+ } else {
38+ handleDockerClientResponse (item );
39+ }
2740 LOGGER .debug (item .toString ());
2841 }
2942
43+ public void checkForDockerSwarmResponse (PullResponseItem item ) {
44+ if (item .getStatus ().matches ("Pulling\\ s.+\\ .{3}$" )) {
45+ isSwarm = true ;
46+ LOGGER .debug ("Communicating with Docker Swarm." );
47+ }
48+ }
49+
50+ public void handleDockerSwarmResponse (PullResponseItem item ) {
51+ if (results == null ) {
52+ results = new HashMap <String , PullResponseItem >();
53+ }
54+ results .put (item .getId (), item );
55+ }
56+
57+ public void checkDockerSwarmPullSuccessful () {
58+ if (results .isEmpty ()) {
59+ throw new DockerClientException ("Could not pull image" );
60+ } else {
61+ boolean pullFailed = false ;
62+ StringBuilder sb = new StringBuilder ();
63+
64+ for (PullResponseItem pullResponseItem : results .values ()) {
65+ if (!pullResponseItem .isPullSuccessIndicated ()) {
66+ pullFailed = true ;
67+ sb .append ("[" + pullResponseItem .getId () + ";" + pullResponseItem .getError () + "]" );
68+ }
69+ }
70+
71+ if (pullFailed ) {
72+ throw new DockerClientException ("Could not pull image: " + sb .toString ());
73+ }
74+ }
75+ }
76+
77+ public void handleDockerClientResponse (PullResponseItem item ) {
78+ latestItem = item ;
79+ }
80+
81+ public void checkDockerClientPullSuccessful () {
82+ if (latestItem == null ) {
83+ throw new DockerClientException ("Could not pull image" );
84+ } else if (!latestItem .isPullSuccessIndicated ()) {
85+ throw new DockerClientException ("Could not pull image: " + latestItem .getError ());
86+ }
87+ }
88+
3089 /**
3190 * Awaits the image to be pulled successful.
3291 *
@@ -40,10 +99,10 @@ public void awaitSuccess() {
4099 throw new DockerClientException ("" , e );
41100 }
42101
43- if (latestItem == null ) {
44- throw new DockerClientException ( "Could not pull image" );
45- } else if (! latestItem . isPullSuccessIndicated ()) {
46- throw new DockerClientException ( "Could not pull image: " + latestItem . getError () );
102+ if (isSwarm ) {
103+ checkDockerSwarmPullSuccessful ( );
104+ } else {
105+ checkDockerClientPullSuccessful ( );
47106 }
48107 }
49108}
0 commit comments