Skip to content

Commit 640a458

Browse files
author
Marcus Linke
committed
Merge branch 'master' into issue-246
2 parents 9fcfee3 + 901654b commit 640a458

17 files changed

+199
-35
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
Change Log
22
===
3+
Latest SNAPSHOT
4+
---
5+
* [#357] (https://github.com/docker-java/docker-java/pull/357) Wait container command needs possibility to abort operation
6+
37
v2.1.2
48
---
59
* [#350] (https://github.com/docker-java/docker-java/pull/350) Remove ServiceLoader logic

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ You can find the latest SNAPSHOT version including javadoc and source files on [
7474
<dependency>
7575
<groupId>com.github.docker-java</groupId>
7676
<artifactId>docker-java</artifactId>
77-
<version>2.1.3-SNAPSHOT</version>
77+
<version>3.0.0-SNAPSHOT</version>
7878
</dependency>
7979

8080
## Documentation

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<groupId>com.github.docker-java</groupId>
1111
<artifactId>docker-java</artifactId>
1212
<packaging>jar</packaging>
13-
<version>2.1.3-SNAPSHOT</version>
13+
<version>3.0.0-SNAPSHOT</version>
1414

1515
<name>docker-java</name>
1616
<url>https://github.com/docker-java/docker-java</url>

src/main/java/com/github/dockerjava/api/command/WaitContainerCmd.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
import javax.annotation.Nonnull;
55

66
import com.github.dockerjava.api.NotFoundException;
7+
import com.github.dockerjava.api.async.ResultCallback;
8+
import com.github.dockerjava.api.model.BuildResponseItem;
9+
import com.github.dockerjava.api.model.WaitResponse;
710

811
/**
912
* Wait a container
1013
*
1114
* Block until container stops, then returns its exit code
1215
*/
13-
public interface WaitContainerCmd extends SyncDockerCmd<Integer> {
16+
public interface WaitContainerCmd extends AsyncDockerCmd<WaitContainerCmd, WaitResponse> {
1417

1518
@CheckForNull
1619
public String getContainerId();
@@ -22,9 +25,9 @@ public interface WaitContainerCmd extends SyncDockerCmd<Integer> {
2225
* container not found
2326
*/
2427
@Override
25-
public Integer exec() throws NotFoundException;
28+
public <T extends ResultCallback<WaitResponse>> T exec(T resultCallback);
2629

27-
public static interface Exec extends DockerCmdSyncExec<WaitContainerCmd, Integer> {
30+
public static interface Exec extends DockerCmdAsyncExec<WaitContainerCmd, WaitResponse> {
2831
}
2932

30-
}
33+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.github.dockerjava.api.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
6+
7+
/**
8+
* Represents a wait container command response
9+
*/
10+
@JsonIgnoreProperties(ignoreUnknown = false)
11+
public class WaitResponse {
12+
13+
@JsonProperty("StatusCode")
14+
private Integer statusCode;
15+
16+
public Integer getStatusCode() {
17+
return statusCode;
18+
}
19+
}

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
import java.util.concurrent.CountDownLatch;
99
import java.util.concurrent.TimeUnit;
1010

11+
import javax.annotation.CheckForNull;
12+
1113
import org.slf4j.Logger;
1214
import org.slf4j.LoggerFactory;
1315

1416
import com.github.dockerjava.api.async.ResultCallback;
17+
import com.google.common.base.Throwables;
1518

1619
/**
1720
* Abstract template implementation of {@link ResultCallback}
@@ -30,6 +33,8 @@ public abstract class ResultCallbackTemplate<RC_T extends ResultCallback<A_RES_T
3033

3134
private boolean closed = false;
3235

36+
private Throwable firstError = null;
37+
3338
@Override
3439
public void onStart(Closeable stream) {
3540
this.stream = stream;
@@ -38,12 +43,15 @@ public void onStart(Closeable stream) {
3843

3944
@Override
4045
public void onError(Throwable throwable) {
46+
4147
if (closed)
4248
return;
4349

50+
if (this.firstError == null)
51+
this.firstError = throwable;
52+
4453
try {
4554
LOGGER.error("Error during callback", throwable);
46-
throw new RuntimeException(throwable);
4755
} finally {
4856
try {
4957
close();
@@ -76,6 +84,8 @@ public void close() throws IOException {
7684
@SuppressWarnings("unchecked")
7785
public RC_T awaitCompletion() throws InterruptedException {
7886
completed.await();
87+
// eventually (re)throws RuntimeException
88+
getFirstError();
7989
return (RC_T) this;
8090
}
8191

@@ -87,4 +97,14 @@ public RC_T awaitCompletion(long timeout, TimeUnit timeUnit) throws InterruptedE
8797
completed.await(timeout, timeUnit);
8898
return (RC_T) this;
8999
}
100+
101+
@CheckForNull
102+
protected RuntimeException getFirstError() {
103+
if (firstError != null) {
104+
// this call throws a RuntimeException
105+
return Throwables.propagate(firstError);
106+
} else {
107+
return null;
108+
}
109+
}
90110
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
import static com.google.common.base.Preconditions.checkNotNull;
44

55
import com.github.dockerjava.api.command.WaitContainerCmd;
6+
import com.github.dockerjava.api.model.WaitResponse;
67

78
/**
89
* Wait a container
910
*
1011
* Block until container stops, then returns its exit code
1112
*/
12-
public class WaitContainerCmdImpl extends AbstrDockerCmd<WaitContainerCmd, Integer> implements WaitContainerCmd {
13+
public class WaitContainerCmdImpl extends AbstrAsyncDockerCmd<WaitContainerCmd, WaitResponse> implements WaitContainerCmd {
1314

1415
private String containerId;
1516

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 javax.annotation.CheckForNull;
9+
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
import com.github.dockerjava.api.DockerClientException;
14+
import com.github.dockerjava.api.model.WaitResponse;
15+
import com.github.dockerjava.core.async.ResultCallbackTemplate;
16+
import com.google.common.base.Throwables;
17+
18+
/**
19+
*
20+
* @author marcus
21+
*
22+
*/
23+
public class WaitContainerResultCallback extends ResultCallbackTemplate<WaitContainerResultCallback, WaitResponse> {
24+
25+
private final static Logger LOGGER = LoggerFactory.getLogger(WaitContainerResultCallback.class);
26+
27+
@CheckForNull
28+
private WaitResponse waitResponse = null;
29+
30+
@Override
31+
public void onNext(WaitResponse waitResponse) {
32+
this.waitResponse = waitResponse;
33+
LOGGER.debug(waitResponse.toString());
34+
}
35+
36+
/**
37+
* Awaits the status code from the container.
38+
*
39+
* @throws DockerClientException
40+
* if the wait operation fails.
41+
*/
42+
public Integer awaitStatusCode() {
43+
try {
44+
awaitCompletion();
45+
} catch (InterruptedException e) {
46+
throw new DockerClientException("", e);
47+
}
48+
49+
return getStatusCode();
50+
}
51+
52+
/**
53+
* Awaits the status code from the container.
54+
*
55+
* @throws DockerClientException
56+
* if the wait operation fails.
57+
*/
58+
public Integer awaitStatusCode(long timeout, TimeUnit timeUnit) {
59+
try {
60+
awaitCompletion(timeout, timeUnit);
61+
} catch (InterruptedException e) {
62+
throw new DockerClientException("Awaiting status code interrupted: ", e);
63+
}
64+
65+
return getStatusCode();
66+
}
67+
68+
private Integer getStatusCode() {
69+
if (waitResponse == null) {
70+
throw new DockerClientException("Error while wait container");
71+
} else {
72+
return waitResponse.getStatusCode();
73+
}
74+
}
75+
}
Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
package com.github.dockerjava.jaxrs;
22

3-
import com.fasterxml.jackson.databind.node.ObjectNode;
4-
import com.github.dockerjava.api.command.WaitContainerCmd;
5-
import com.github.dockerjava.core.DockerClientConfig;
6-
import org.slf4j.Logger;
7-
import org.slf4j.LoggerFactory;
3+
import static javax.ws.rs.client.Entity.entity;
84

95
import javax.ws.rs.client.WebTarget;
106
import javax.ws.rs.core.MediaType;
117

12-
public class WaitContainerCmdExec extends AbstrSyncDockerCmdExec<WaitContainerCmd, Integer> implements
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
import com.github.dockerjava.api.async.ResultCallback;
12+
import com.github.dockerjava.api.command.WaitContainerCmd;
13+
import com.github.dockerjava.api.model.WaitResponse;
14+
import com.github.dockerjava.core.DockerClientConfig;
15+
import com.github.dockerjava.core.async.JsonStreamProcessor;
16+
import com.github.dockerjava.jaxrs.async.AbstractCallbackNotifier;
17+
import com.github.dockerjava.jaxrs.async.POSTCallbackNotifier;
18+
19+
public class WaitContainerCmdExec extends AbstrAsyncDockerCmdExec<WaitContainerCmd, WaitResponse> implements
1320
WaitContainerCmd.Exec {
1421

1522
private static final Logger LOGGER = LoggerFactory.getLogger(WaitContainerCmdExec.class);
@@ -19,14 +26,16 @@ public WaitContainerCmdExec(WebTarget baseResource, DockerClientConfig dockerCli
1926
}
2027

2128
@Override
22-
protected Integer execute(WaitContainerCmd command) {
29+
protected AbstractCallbackNotifier<WaitResponse> callbackNotifier(WaitContainerCmd command,
30+
ResultCallback<WaitResponse> resultCallback) {
31+
2332
WebTarget webResource = getBaseResource().path("/containers/{id}/wait").resolveTemplate("id",
2433
command.getContainerId());
2534

2635
LOGGER.trace("POST: {}", webResource);
27-
ObjectNode ObjectNode = webResource.request().accept(MediaType.APPLICATION_JSON).post(null, ObjectNode.class);
2836

29-
return ObjectNode.get("StatusCode").asInt();
37+
return new POSTCallbackNotifier<WaitResponse>(new JsonStreamProcessor<WaitResponse>(
38+
WaitResponse.class), resultCallback, webResource.request().accept(MediaType.APPLICATION_JSON), entity(null, MediaType.APPLICATION_JSON));
3039
}
3140

3241
}

src/test/java/com/github/dockerjava/client/DockerClientTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import com.github.dockerjava.api.DockerException;
1818
import com.github.dockerjava.api.command.CreateContainerResponse;
19+
import com.github.dockerjava.core.command.WaitContainerResultCallback;
1920

2021
/**
2122
* Unit test for DockerClient.
@@ -61,7 +62,7 @@ public void testRunShlex() throws DockerException {
6162
CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd(commands).exec();
6263
dockerClient.startContainerCmd(container.getId());
6364

64-
int exitcode = dockerClient.waitContainerCmd(container.getId()).exec();
65+
int exitcode = dockerClient.waitContainerCmd(container.getId()).exec(new WaitContainerResultCallback()).awaitStatusCode();
6566
assertThat(exitcode, equalTo(0));
6667
}
6768
}

0 commit comments

Comments
 (0)