Skip to content

Commit cc5331d

Browse files
author
Marcus Linke
committed
Simplify volumesFrom handling
1 parent afdafe9 commit cc5331d

File tree

7 files changed

+106
-143
lines changed

7 files changed

+106
-143
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.github.dockerjava.api.model.ExposedPort;
77
import com.github.dockerjava.api.model.HostConfig;
88
import com.github.dockerjava.api.model.Volume;
9-
import com.github.dockerjava.api.model.VolumeFrom;
9+
import com.github.dockerjava.api.model.VolumesFrom;
1010

1111
public interface CreateContainerCmd extends DockerCmd<CreateContainerResponse>{
1212

@@ -94,9 +94,9 @@ public interface CreateContainerCmd extends DockerCmd<CreateContainerResponse>{
9494

9595
public CreateContainerCmd withVolumes(Volume... volumes);
9696

97-
public VolumeFrom[] getVolumesFrom();
97+
public VolumesFrom[] getVolumesFrom();
9898

99-
public CreateContainerCmd withVolumesFrom(VolumeFrom... volumesFrom);
99+
public CreateContainerCmd withVolumesFrom(VolumesFrom... volumesFrom);
100100

101101
public HostConfig getHostConfig();
102102

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class HostConfig {
3333
private String[] dnsSearch;
3434

3535
@JsonProperty("VolumesFrom")
36-
private VolumesFrom volumesFrom;
36+
private VolumesFrom[] volumesFrom;
3737

3838
@JsonProperty("ContainerIDFile")
3939
private String containerIDFile;
@@ -57,7 +57,7 @@ public HostConfig() {
5757
}
5858

5959
public HostConfig(String[] binds, Links links, LxcConf[] lxcConf, Ports portBindings, boolean publishAllPorts,
60-
boolean privileged, String[] dns, String[] dnsSearch, VolumesFrom volumesFrom, String containerIDFile,
60+
boolean privileged, String[] dns, String[] dnsSearch, VolumesFrom[] volumesFrom, String containerIDFile,
6161
Capability[] capAdd, Capability[] capDrop, RestartPolicy restartPolicy, String networkMode, Device[] devices) {
6262
this.binds = binds;
6363
this.links = links;
@@ -100,7 +100,7 @@ public String[] getDns() {
100100
return dns;
101101
}
102102

103-
public VolumesFrom getVolumesFrom() {
103+
public VolumesFrom[] getVolumesFrom() {
104104
return volumesFrom;
105105
}
106106

@@ -168,7 +168,7 @@ public void setDnsSearch(String[] dnsSearch) {
168168
this.dnsSearch = dnsSearch;
169169
}
170170

171-
public void setVolumesFrom(VolumesFrom volumesFrom) {
171+
public void setVolumesFrom(VolumesFrom[] volumesFrom) {
172172
this.volumesFrom = volumesFrom;
173173
}
174174

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

Lines changed: 0 additions & 87 deletions
This file was deleted.

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

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

33
import java.io.IOException;
4-
import java.util.ArrayList;
5-
import java.util.Iterator;
6-
import java.util.List;
4+
5+
import org.apache.commons.lang.builder.EqualsBuilder;
6+
import org.apache.commons.lang.builder.HashCodeBuilder;
77

88
import com.fasterxml.jackson.core.JsonGenerator;
99
import com.fasterxml.jackson.core.JsonParser;
@@ -21,30 +21,94 @@
2121
@JsonDeserialize(using = VolumesFrom.Deserializer.class)
2222
public class VolumesFrom {
2323

24-
private VolumeFrom[] volumesFrom;
24+
private String container;
25+
26+
private AccessMode accessMode;
2527

26-
public VolumesFrom(VolumeFrom... volumesFrom) {
27-
this.volumesFrom = volumesFrom;
28+
public VolumesFrom(String container) {
29+
this(container, AccessMode.DEFAULT);
2830
}
2931

30-
public VolumeFrom[] getVolumesFrom() {
31-
return volumesFrom;
32+
public VolumesFrom(String container, AccessMode accessMode) {
33+
this.container = container;
34+
this.accessMode = accessMode;
35+
}
36+
37+
public String getContainer() {
38+
return container;
3239
}
40+
41+
public AccessMode getAccessMode() {
42+
return accessMode;
43+
}
44+
3345

46+
/**
47+
* Parses a volume from specification to a {@link VolumesFrom}.
48+
*
49+
* @param serialized the specification, e.g. <code>container:ro</code>
50+
* @return a {@link VolumesFrom} matching the specification
51+
* @throws IllegalArgumentException if the specification cannot be parsed
52+
*/
53+
public static VolumesFrom parse(String serialized) {
54+
try {
55+
String[] parts = serialized.split(":");
56+
switch (parts.length) {
57+
case 1: {
58+
return new VolumesFrom(parts[0]);
59+
}
60+
case 2: {
61+
return new VolumesFrom(parts[0], AccessMode.valueOf(parts[1]));
62+
}
63+
64+
default: {
65+
throw new IllegalArgumentException();
66+
}
67+
}
68+
} catch (Exception e) {
69+
throw new IllegalArgumentException("Error parsing Bind '" + serialized
70+
+ "'");
71+
}
72+
}
73+
74+
@Override
75+
public boolean equals(Object obj) {
76+
if (obj instanceof VolumesFrom) {
77+
VolumesFrom other = (VolumesFrom) obj;
78+
return new EqualsBuilder().append(container, other.getContainer())
79+
.append(accessMode, other.getAccessMode()).isEquals();
80+
} else
81+
return super.equals(obj);
82+
}
83+
84+
@Override
85+
public int hashCode() {
86+
return new HashCodeBuilder().append(container)
87+
.append(accessMode).toHashCode();
88+
}
89+
90+
/**
91+
* Returns a string representation of this {@link VolumesFrom} suitable
92+
* for inclusion in a JSON message.
93+
* The format is <code>&lt;container&gt;:&lt;access mode&gt;</code>,
94+
* like the argument in {@link #parse(String)}.
95+
*
96+
* @return a string representation of this {@link VolumesFrom}
97+
*/
98+
@Override
99+
public String toString() {
100+
return container + ":" + accessMode.toString();
101+
}
102+
34103
public static class Serializer extends JsonSerializer<VolumesFrom> {
35104

36105
@Override
37-
public void serialize(VolumesFrom volumesFrom, JsonGenerator jsonGen,
106+
public void serialize(VolumesFrom volumeFrom, JsonGenerator jsonGen,
38107
SerializerProvider serProvider) throws IOException,
39108
JsonProcessingException {
40109

41-
//
42-
jsonGen.writeStartArray();
43-
for (VolumeFrom bind : volumesFrom.getVolumesFrom()) {
44-
jsonGen.writeString(bind.toString());
45-
}
46-
jsonGen.writeEndArray();
47-
//
110+
jsonGen.writeString(volumeFrom.toString());
111+
48112
}
49113

50114
}
@@ -55,14 +119,10 @@ public VolumesFrom deserialize(JsonParser jsonParser,
55119
DeserializationContext deserializationContext)
56120
throws IOException, JsonProcessingException {
57121

58-
List<VolumeFrom> volumesFrom = new ArrayList<VolumeFrom>();
59122
ObjectCodec oc = jsonParser.getCodec();
60123
JsonNode node = oc.readTree(jsonParser);
61-
for (Iterator<JsonNode> it = node.iterator(); it.hasNext();) {
62-
JsonNode field = it.next();
63-
volumesFrom.add(VolumeFrom.parse(field.asText()));
64-
}
65-
return new VolumesFrom(volumesFrom.toArray(new VolumeFrom[0]));
124+
return VolumesFrom.parse(node.asText());
125+
66126
}
67127
}
68128

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
import com.github.dockerjava.api.model.ExposedPorts;
1616
import com.github.dockerjava.api.model.HostConfig;
1717
import com.github.dockerjava.api.model.Volume;
18-
import com.github.dockerjava.api.model.VolumeFrom;
19-
import com.github.dockerjava.api.model.Volumes;
2018
import com.github.dockerjava.api.model.VolumesFrom;
21-
19+
import com.github.dockerjava.api.model.Volumes;
2220

2321
/**
2422
*
@@ -305,13 +303,13 @@ public CreateContainerCmdImpl withVolumes(Volume... volumes) {
305303
}
306304

307305
@Override
308-
public VolumeFrom[] getVolumesFrom() {
309-
return hostConfig.getVolumesFrom() == null ? new VolumeFrom[0] : hostConfig.getVolumesFrom().getVolumesFrom();
306+
public VolumesFrom[] getVolumesFrom() {
307+
return hostConfig.getVolumesFrom();
310308
}
311309

312310
@Override
313-
public CreateContainerCmdImpl withVolumesFrom(VolumeFrom... volumesFrom) {
314-
this.hostConfig.setVolumesFrom(new VolumesFrom(volumesFrom));
311+
public CreateContainerCmdImpl withVolumesFrom(VolumesFrom... volumesFrom) {
312+
this.hostConfig.setVolumesFrom(volumesFrom);
315313
return this;
316314
}
317315

src/test/java/com/github/dockerjava/api/model/VolumeFrom_SerializingTest.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,24 @@
22

33
import static org.testng.Assert.assertEquals;
44

5-
import java.util.Map;
6-
75
import org.testng.annotations.Test;
86

97
import com.fasterxml.jackson.databind.ObjectMapper;
10-
import com.github.dockerjava.api.model.Ports.Binding;
118

129
public class VolumeFrom_SerializingTest {
1310
private final ObjectMapper objectMapper = new ObjectMapper();
14-
private final String json =
15-
"[\"container1:ro\",\"container2:rw\"]";
11+
private final String json = "\"container1:ro\"";
12+
1613
@Test
1714
public void deserializing() throws Exception {
18-
VolumesFrom volumesFrom = objectMapper.readValue(json, VolumesFrom.class);
19-
20-
VolumesFrom expected = new VolumesFrom(new VolumeFrom("container1", AccessMode.ro), new VolumeFrom("container2", AccessMode.rw));
21-
22-
assertEquals(volumesFrom.getVolumesFrom(), expected.getVolumesFrom());
15+
VolumesFrom volumeFrom = objectMapper.readValue(json, VolumesFrom.class);
16+
assertEquals(volumeFrom, new VolumesFrom("container1", AccessMode.ro));
2317
}
24-
18+
2519
@Test
2620
public void serializing() throws Exception {
27-
VolumesFrom volumesFrom = new VolumesFrom(new VolumeFrom("container1", AccessMode.ro), new VolumeFrom("container2", AccessMode.rw));
28-
29-
assertEquals(objectMapper.writeValueAsString(volumesFrom), json);
21+
VolumesFrom volumeFrom = new VolumesFrom("container1", AccessMode.ro);
22+
assertEquals(objectMapper.writeValueAsString(volumeFrom), json);
3023
}
31-
32-
24+
3325
}

src/test/java/com/github/dockerjava/core/command/CreateContainerCmdImplTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import com.github.dockerjava.api.model.Link;
3333
import com.github.dockerjava.api.model.Links;
3434
import com.github.dockerjava.api.model.Volume;
35-
import com.github.dockerjava.api.model.VolumeFrom;
35+
import com.github.dockerjava.api.model.VolumesFrom;
3636
import com.github.dockerjava.client.AbstractDockerClientTest;
3737

3838
@Test(groups = "integration")
@@ -126,14 +126,14 @@ public void createContainerWithVolumesFrom() throws DockerException {
126126
// create a second container with volumes from first container
127127
CreateContainerResponse container2 = dockerClient
128128
.createContainerCmd("busybox").withCmd("sleep", "9999")
129-
.withVolumesFrom(new VolumeFrom(container1Name)).exec();
129+
.withVolumesFrom(new VolumesFrom(container1Name)).exec();
130130
LOG.info("Created container2 {}", container2.toString());
131131

132132
InspectContainerResponse inspectContainerResponse2 = dockerClient
133133
.inspectContainerCmd(container2.getId()).exec();
134134

135135
// No volumes are created, the information is just stored in .HostConfig.VolumesFrom
136-
assertThat(inspectContainerResponse2.getHostConfig().getVolumesFrom().getVolumesFrom(), hasItemInArray(new VolumeFrom(container1Name)));
136+
assertThat(inspectContainerResponse2.getHostConfig().getVolumesFrom(), hasItemInArray(new VolumesFrom(container1Name)));
137137

138138
// To ensure that the information stored in VolumesFrom really is considered
139139
// when starting the container, we start it and verify that it has the same
@@ -145,7 +145,7 @@ public void createContainerWithVolumesFrom() throws DockerException {
145145

146146
inspectContainerResponse2 = dockerClient.inspectContainerCmd(container2.getId()).exec();
147147

148-
assertThat(inspectContainerResponse2.getHostConfig().getVolumesFrom().getVolumesFrom(), hasItemInArray(new VolumeFrom(container1Name)));
148+
assertThat(inspectContainerResponse2.getHostConfig().getVolumesFrom(), hasItemInArray(new VolumesFrom(container1Name)));
149149
assertContainerHasVolumes(inspectContainerResponse2, volume1, volume2);
150150
}
151151

0 commit comments

Comments
 (0)