Skip to content

Commit f0666ec

Browse files
committed
Merge pull request #281 from docker-java/refact-streaming
Provide default callback implementations for various commands
2 parents 6c9dbdb + d54c521 commit f0666ec

24 files changed

+401
-148
lines changed

src/main/java/com/github/dockerjava/api/model/BuildResponseItem.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.dockerjava.api.model;
22

3+
import com.fasterxml.jackson.annotation.JsonIgnore;
34
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
45
import com.fasterxml.jackson.annotation.JsonProperty;
56

@@ -11,10 +12,29 @@ public class BuildResponseItem extends ResponseItem {
1112

1213
private static final long serialVersionUID = -1252904184236343612L;
1314

15+
private static final String BUILD_SUCCESS = "Successfully built";
16+
1417
@JsonProperty("stream")
1518
private String stream;
1619

1720
public String getStream() {
1821
return stream;
1922
}
23+
24+
/**
25+
* Returns whether the stream field indicates a successful build operation
26+
*/
27+
@JsonIgnore
28+
public boolean isBuildSuccessIndicated() {
29+
if(getStream() == null) return false;
30+
31+
return getStream().contains(BUILD_SUCCESS);
32+
}
33+
34+
@JsonIgnore
35+
public String getImageId() {
36+
if(!isBuildSuccessIndicated()) return null;
37+
38+
return getStream().replaceFirst(BUILD_SUCCESS, "").trim();
39+
}
2040
}

src/main/java/com/github/dockerjava/api/model/PullResponseItem.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.dockerjava.api.model;
22

3+
import com.fasterxml.jackson.annotation.JsonIgnore;
34
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
45

56
/**
@@ -10,4 +11,18 @@ public class PullResponseItem extends ResponseItem {
1011

1112
private static final long serialVersionUID = 6316219017613249047L;
1213

14+
/**
15+
* Returns whether the status indicates a successful pull operation
16+
*
17+
* @returns true: status indicates that pull was successful, false: status doesn't indicate a successful pull
18+
*/
19+
@JsonIgnore
20+
public boolean isPullSuccessIndicated() {
21+
if (getStatus() == null)
22+
return false;
23+
24+
return (getStatus().contains("Download complete") || getStatus().contains("Image is up to date") || getStatus()
25+
.contains("Downloaded newer image"));
26+
}
27+
1328
}

src/main/java/com/github/dockerjava/api/model/PushResponseItem.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.dockerjava.api.model;
22

3+
import com.fasterxml.jackson.annotation.JsonIgnore;
34
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
45

56
/**
@@ -9,4 +10,17 @@
910
public class PushResponseItem extends ResponseItem {
1011

1112
private static final long serialVersionUID = 8256977108011295857L;
13+
14+
/**
15+
* Returns whether the error field indicates an error
16+
*
17+
* @returns true: the error field indicates an error, false: the error field doesn't indicate an error
18+
*/
19+
@JsonIgnore
20+
public boolean isErrorIndicated() {
21+
if (getError() == null)
22+
return false;
23+
24+
return true;
25+
}
1226
}

src/main/java/com/github/dockerjava/api/model/ResponseItem.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ public String getId() {
5050
return id;
5151
}
5252

53+
public String getError() {
54+
return error;
55+
}
56+
57+
public ErrorDetail getErrorDetail() {
58+
return errorDetail;
59+
}
60+
5361
@JsonIgnoreProperties(ignoreUnknown = false)
5462
public static class ProgressDetail implements Serializable {
5563
private static final long serialVersionUID = -1954994695645715264L;

src/main/java/com/github/dockerjava/core/async/ResultCallbackTemplate.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,22 @@ public abstract class ResultCallbackTemplate<RC_T extends ResultCallback<A_RES_T
2424

2525
private final static Logger LOGGER = LoggerFactory.getLogger(ResultCallbackTemplate.class);
2626

27-
private final CountDownLatch finished = new CountDownLatch(1);
27+
private final CountDownLatch completed = new CountDownLatch(1);
2828

2929
private Closeable stream;
3030

31+
private boolean closed = false;
32+
3133
@Override
3234
public void onStart(Closeable stream) {
3335
this.stream = stream;
34-
}
35-
36-
@Override
37-
public void onNext(A_RES_T object) {
36+
this.closed = false;
3837
}
3938

4039
@Override
4140
public void onError(Throwable throwable) {
41+
if(closed) return;
42+
4243
try {
4344
LOGGER.error("Error during callback", throwable);
4445
throw new RuntimeException(throwable);
@@ -64,15 +65,16 @@ public void onComplete() {
6465
public void close() throws IOException {
6566
if (stream != null)
6667
stream.close();
67-
finished.countDown();
68+
completed.countDown();
69+
closed = true;
6870
}
6971

7072
/**
7173
* Blocks until {@link ResultCallback#onComplete()} was called
7274
*/
7375
@SuppressWarnings("unchecked")
7476
public RC_T awaitCompletion() throws InterruptedException {
75-
finished.await();
77+
completed.await();
7678
return (RC_T) this;
7779
}
7880

@@ -81,7 +83,7 @@ public RC_T awaitCompletion() throws InterruptedException {
8183
*/
8284
@SuppressWarnings("unchecked")
8385
public RC_T awaitCompletion(long timeout, TimeUnit timeUnit) throws InterruptedException {
84-
finished.await(timeout, timeUnit);
86+
completed.await(timeout, timeUnit);
8587
return (RC_T) this;
8688
}
8789
}

src/main/java/com/github/dockerjava/core/command/AbstrAsyncDockerCmd.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@
44

55
import java.io.IOException;
66

7-
import org.slf4j.Logger;
8-
import org.slf4j.LoggerFactory;
9-
107
import com.github.dockerjava.api.async.ResultCallback;
118
import com.github.dockerjava.api.command.AsyncDockerCmd;
129
import com.github.dockerjava.api.command.DockerCmdAsyncExec;
1310

1411
public abstract class AbstrAsyncDockerCmd<CMD_T extends AsyncDockerCmd<CMD_T, A_RES_T>, A_RES_T> implements
1512
AsyncDockerCmd<CMD_T, A_RES_T> {
1613

17-
private final static Logger LOGGER = LoggerFactory.getLogger(AbstrAsyncDockerCmd.class);
18-
1914
protected DockerCmdAsyncExec<CMD_T, A_RES_T> execution;
2015

2116
public AbstrAsyncDockerCmd(DockerCmdAsyncExec<CMD_T, A_RES_T> execution) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Created on 21.07.2015
3+
*/
4+
package com.github.dockerjava.core.command;
5+
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
import com.github.dockerjava.api.model.Frame;
10+
import com.github.dockerjava.core.async.ResultCallbackTemplate;
11+
12+
/**
13+
*
14+
* @author marcus
15+
*
16+
*/
17+
public class AttachContainerResultCallback extends ResultCallbackTemplate<AttachContainerResultCallback, Frame> {
18+
19+
private final static Logger LOGGER = LoggerFactory.getLogger(AttachContainerResultCallback.class);
20+
21+
@Override
22+
public void onNext(Frame item) {
23+
LOGGER.debug(item.toString());
24+
}
25+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Created on 21.07.2015
3+
*/
4+
package com.github.dockerjava.core.command;
5+
6+
import java.util.concurrent.TimeUnit;
7+
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
import com.github.dockerjava.api.DockerClientException;
12+
import com.github.dockerjava.api.model.BuildResponseItem;
13+
import com.github.dockerjava.core.async.ResultCallbackTemplate;
14+
15+
/**
16+
*
17+
* @author marcus
18+
*
19+
*/
20+
public class BuildImageResultCallback extends ResultCallbackTemplate<BuildImageResultCallback, BuildResponseItem> {
21+
22+
private final static Logger LOGGER = LoggerFactory.getLogger(BuildImageResultCallback.class);
23+
24+
private BuildResponseItem latestItem = null;
25+
26+
@Override
27+
public void onNext(BuildResponseItem item) {
28+
this.latestItem = item;
29+
LOGGER.debug(item.toString());
30+
}
31+
32+
/**
33+
* Awaits the image id from the response stream.
34+
*
35+
* @throws DockerClientException
36+
* if the build fails.
37+
*/
38+
public String awaitImageId() {
39+
try {
40+
awaitCompletion();
41+
} catch (InterruptedException e) {
42+
throw new DockerClientException("", e);
43+
}
44+
45+
return getImageId();
46+
}
47+
48+
/**
49+
* Awaits the image id from the response stream.
50+
*
51+
* @throws DockerClientException
52+
* if the build fails or the timeout occurs.
53+
*/
54+
public String awaitImageId(long timeout, TimeUnit timeUnit) {
55+
try {
56+
awaitCompletion(timeout, timeUnit);
57+
} catch (InterruptedException e) {
58+
throw new DockerClientException("", e);
59+
}
60+
61+
return getImageId();
62+
}
63+
64+
private String getImageId() {
65+
if (latestItem == null || !latestItem.isBuildSuccessIndicated()) {
66+
throw new DockerClientException("Could not build image: " + latestItem.getError());
67+
} else {
68+
return latestItem.getImageId();
69+
}
70+
}
71+
72+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Created on 21.07.2015
3+
*/
4+
package com.github.dockerjava.core.command;
5+
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
import com.github.dockerjava.api.model.Event;
10+
import com.github.dockerjava.core.async.ResultCallbackTemplate;
11+
12+
/**
13+
*
14+
* @author marcus
15+
*
16+
*/
17+
public class EventsResultCallback extends ResultCallbackTemplate<EventsResultCallback, Event> {
18+
19+
private final static Logger LOGGER = LoggerFactory.getLogger(EventsResultCallback.class);
20+
21+
@Override
22+
public void onNext(Event item) {
23+
LOGGER.debug(item.toString());
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Created on 21.07.2015
3+
*/
4+
package com.github.dockerjava.core.command;
5+
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
import com.github.dockerjava.api.model.Frame;
10+
import com.github.dockerjava.core.async.ResultCallbackTemplate;
11+
12+
/**
13+
*
14+
* @author marcus
15+
*
16+
*/
17+
public class LogContainerResultCallback extends ResultCallbackTemplate<LogContainerResultCallback, Frame> {
18+
19+
private final static Logger LOGGER = LoggerFactory.getLogger(LogContainerResultCallback.class);
20+
21+
@Override
22+
public void onNext(Frame item) {
23+
LOGGER.debug(item.toString());
24+
}
25+
}

0 commit comments

Comments
 (0)