X Tutup
Skip to content

Commit ee778e2

Browse files
author
Marcus Linke
committed
Added pause/unpause commands
1 parent be4f77a commit ee778e2

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.github.dockerjava.client.command;
2+
3+
import javax.ws.rs.core.MediaType;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import com.github.dockerjava.client.DockerException;
9+
import com.google.common.base.Preconditions;
10+
import com.sun.jersey.api.client.ClientResponse;
11+
import com.sun.jersey.api.client.UniformInterfaceException;
12+
import com.sun.jersey.api.client.WebResource;
13+
14+
/**
15+
* Pause a container.
16+
*
17+
* @param containerId - Id of the container
18+
*
19+
*/
20+
public class PauseContainerCmd extends AbstrDockerCmd<PauseContainerCmd, Integer> {
21+
22+
private static final Logger LOGGER = LoggerFactory.getLogger(PauseContainerCmd.class);
23+
24+
private String containerId;
25+
26+
public PauseContainerCmd(String containerId) {
27+
withContainerId(containerId);
28+
}
29+
30+
public PauseContainerCmd withContainerId(String containerId) {
31+
Preconditions.checkNotNull(containerId, "containerId was not specified");
32+
this.containerId = containerId;
33+
return this;
34+
}
35+
36+
@Override
37+
public String toString() {
38+
return new StringBuilder("pause ")
39+
.append(containerId)
40+
.toString();
41+
}
42+
43+
protected Integer impl() throws DockerException {
44+
WebResource webResource = baseResource.path(String.format("/containers/%s/pause", containerId));
45+
46+
ClientResponse response = null;
47+
48+
try {
49+
LOGGER.trace("POST: {}", webResource);
50+
response = webResource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).post(ClientResponse.class);
51+
} catch (UniformInterfaceException exception) {
52+
if (exception.getResponse().getStatus() == 404) {
53+
LOGGER.warn("No such container {}", containerId);
54+
} else if (exception.getResponse().getStatus() == 204) {
55+
//no error
56+
LOGGER.trace("Successfully paused container {}", containerId);
57+
} else if (exception.getResponse().getStatus() == 500) {
58+
throw new DockerException("Server error", exception);
59+
} else {
60+
throw new DockerException(exception);
61+
}
62+
}
63+
64+
return response.getStatus();
65+
}
66+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.github.dockerjava.client.command;
2+
3+
import javax.ws.rs.core.MediaType;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import com.github.dockerjava.client.DockerException;
9+
import com.google.common.base.Preconditions;
10+
import com.sun.jersey.api.client.ClientResponse;
11+
import com.sun.jersey.api.client.UniformInterfaceException;
12+
import com.sun.jersey.api.client.WebResource;
13+
14+
/**
15+
* Unpause a container.
16+
*
17+
* @param containerId - Id of the container
18+
*
19+
*/
20+
public class UnpauseContainerCmd extends AbstrDockerCmd<UnpauseContainerCmd, Integer> {
21+
22+
private static final Logger LOGGER = LoggerFactory.getLogger(UnpauseContainerCmd.class);
23+
24+
private String containerId;
25+
26+
public UnpauseContainerCmd(String containerId) {
27+
withContainerId(containerId);
28+
}
29+
30+
public UnpauseContainerCmd withContainerId(String containerId) {
31+
Preconditions.checkNotNull(containerId, "containerId was not specified");
32+
this.containerId = containerId;
33+
return this;
34+
}
35+
36+
@Override
37+
public String toString() {
38+
return new StringBuilder("pause ")
39+
.append(containerId)
40+
.toString();
41+
}
42+
43+
protected Integer impl() throws DockerException {
44+
WebResource webResource = baseResource.path(String.format("/containers/%s/unpause", containerId));
45+
46+
ClientResponse response = null;
47+
48+
try {
49+
LOGGER.trace("POST: {}", webResource);
50+
response = webResource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).post(ClientResponse.class);
51+
} catch (UniformInterfaceException exception) {
52+
if (exception.getResponse().getStatus() == 404) {
53+
LOGGER.warn("No such container {}", containerId);
54+
} else if (exception.getResponse().getStatus() == 204) {
55+
//no error
56+
LOGGER.trace("Successfully paused container {}", containerId);
57+
} else if (exception.getResponse().getStatus() == 500) {
58+
throw new DockerException("Server error", exception);
59+
} else {
60+
throw new DockerException(exception);
61+
}
62+
}
63+
64+
return response.getStatus();
65+
}
66+
}

0 commit comments

Comments
 (0)
X Tutup