forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPullImageResultCallback.java
More file actions
131 lines (110 loc) · 4.04 KB
/
Copy pathPullImageResultCallback.java
File metadata and controls
131 lines (110 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Created on 21.07.2015
*/
package com.github.dockerjava.core.command;
import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.model.PullResponseItem;
import com.github.dockerjava.core.async.ResultCallbackTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.CheckForNull;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
*
* @author Marcus Linke
*
* @deprecated use {@link com.github.dockerjava.api.command.PullImageResultCallback}
*/
@Deprecated
public class PullImageResultCallback extends ResultCallbackTemplate<PullImageResultCallback, PullResponseItem> {
private static final Logger LOGGER = LoggerFactory.getLogger(PullImageResultCallback.class);
private boolean isSwarm = false;
private Map<String, PullResponseItem> results = null;
@CheckForNull
private PullResponseItem latestItem = null;
@Override
public void onNext(PullResponseItem item) {
// only do it once
if (results == null && latestItem == null) {
checkForDockerSwarmResponse(item);
}
if (isSwarm) {
handleDockerSwarmResponse(item);
} else {
handleDockerClientResponse(item);
}
LOGGER.debug(item.toString());
}
private void checkForDockerSwarmResponse(PullResponseItem item) {
if (item.getStatus().matches("Pulling\\s.+\\.{3}$")) {
isSwarm = true;
LOGGER.debug("Communicating with Docker Swarm.");
}
}
private void handleDockerSwarmResponse(final PullResponseItem item) {
if (results == null) {
results = new HashMap<>();
}
// Swarm terminates a pull sometimes with an empty line.
// Therefore keep first success message
PullResponseItem currentItem = results.get(item.getId());
if (currentItem == null || !currentItem.isPullSuccessIndicated()) {
results.put(item.getId(), item);
}
}
private void handleDockerClientResponse(PullResponseItem item) {
latestItem = item;
}
private void checkDockerSwarmPullSuccessful() {
if (results.isEmpty()) {
throw new DockerClientException("Could not pull image through Docker Swarm");
} else {
boolean pullFailed = false;
StringBuilder sb = new StringBuilder();
for (PullResponseItem pullResponseItem : results.values()) {
if (!pullResponseItem.isPullSuccessIndicated()) {
pullFailed = true;
sb.append("[" + pullResponseItem.getId() + ":" + messageFromPullResult(pullResponseItem) + "]");
}
}
if (pullFailed) {
throw new DockerClientException("Could not pull image: " + sb.toString());
}
}
}
private void checkDockerClientPullSuccessful() {
if (latestItem == null) {
throw new DockerClientException("Could not pull image");
} else if (!latestItem.isPullSuccessIndicated()) {
throw new DockerClientException("Could not pull image: " + messageFromPullResult(latestItem));
}
}
private String messageFromPullResult(PullResponseItem pullResponseItem) {
return (pullResponseItem.getError() != null) ? pullResponseItem.getError() : pullResponseItem.getStatus();
}
@Override
protected void throwFirstError() {
super.throwFirstError();
if (isSwarm) {
checkDockerSwarmPullSuccessful();
} else {
checkDockerClientPullSuccessful();
}
}
/**
* Awaits the image to be pulled successful.
*
* @deprecated use {@link #awaitCompletion()} or {@link #awaitCompletion(long, TimeUnit)} instead
* @throws DockerClientException
* if the pull fails.
*/
public void awaitSuccess() {
try {
awaitCompletion();
} catch (InterruptedException e) {
throw new DockerClientException("", e);
}
}
}