Skip to content

Commit e796c76

Browse files
committed
Merge pull request docker-java#34 from karchie/mount-host-volumes
added BoundHostVolumes for host directories bound as in-container volume...
2 parents caaf9c5 + ec08b6b commit e796c76

3 files changed

Lines changed: 100 additions & 3 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package com.kpelykh.docker.client.model;
20+
21+
import java.io.IOException;
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
25+
import org.codehaus.jackson.JsonGenerator;
26+
import org.codehaus.jackson.map.JsonSerializer;
27+
import org.codehaus.jackson.map.SerializerProvider;
28+
import org.codehaus.jackson.map.annotate.JsonSerialize;
29+
30+
31+
/**
32+
* @author Kevin A. Archie <karchie@wustl.edu>
33+
*
34+
*/
35+
@JsonSerialize(using=BoundHostVolumes.Serializer.class)
36+
public class BoundHostVolumes {
37+
private static final String[] STRING_ARRAY = new String[0];
38+
private final String[] dests, binds;
39+
40+
/**
41+
*
42+
* @param specs Iterable of String binding specs, each of form "{host-path}:{container-patch}:[rw|ro]"
43+
* @throws MalformedVolumeSpecException if any specs are null or empty
44+
*/
45+
public BoundHostVolumes(final Iterable<String> specs) {
46+
final List<String> dests = new ArrayList<String>(), binds = new ArrayList<String>();
47+
for (final String spec : specs) {
48+
if (null == spec || "".equals(spec)) {
49+
// skip empty spec lines
50+
} else {
51+
final String[] sspec = spec.split(":");
52+
dests.add(sspec.length > 1 ? sspec[1] : sspec[0]);
53+
binds.add(spec);
54+
}
55+
}
56+
this.dests = dests.toArray(STRING_ARRAY);
57+
this.binds = binds.toArray(STRING_ARRAY);
58+
}
59+
60+
public String[] asBinds() {
61+
return binds;
62+
}
63+
64+
private BoundHostVolumes writeVolumes(final JsonGenerator jg) throws IOException {
65+
jg.writeStartObject();
66+
for (final String dest : dests) {
67+
jg.writeObjectFieldStart(dest);
68+
jg.writeEndObject();
69+
}
70+
jg.writeEndObject();
71+
return this;
72+
}
73+
74+
/**
75+
* This is an ugly hack. We assume that the serializer only gets called when
76+
* a containing ContainerConfig gets serialized, when POSTing to
77+
* /containers/create . In that context, we pass only the container-path
78+
* part (the key in the volumes map).
79+
*
80+
* @author Kevin A. Archie <karchie@wustl.edu>
81+
*
82+
*/
83+
public static class Serializer extends JsonSerializer<BoundHostVolumes> {
84+
/* (non-Javadoc)
85+
* @see org.codehaus.jackson.map.JsonSerializer#serialize(java.lang.Object, org.codehaus.jackson.JsonGenerator, org.codehaus.jackson.map.SerializerProvider)
86+
*/
87+
@Override
88+
public void serialize(final BoundHostVolumes volumes, final JsonGenerator jg, final SerializerProvider sp)
89+
throws IOException {
90+
volumes.writeVolumes(jg);
91+
}
92+
}
93+
}

‎src/main/java/com/kpelykh/docker/client/model/ContainerConfig.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class ContainerConfig {
2828
@JsonProperty("Cmd") private String[] cmd;
2929
@JsonProperty("Dns") private String[] dns;
3030
@JsonProperty("Image") private String image;
31-
@JsonProperty("Volumes") private Object volumes;
31+
@JsonProperty("Volumes") private BoundHostVolumes volumes;
3232
@JsonProperty("VolumesFrom") private String volumesFrom = "";
3333
@JsonProperty("Entrypoint") private String[] entrypoint = new String[]{};
3434
@JsonProperty("NetworkDisabled") private boolean networkDisabled = false;
@@ -216,11 +216,11 @@ public ContainerConfig setImage(String image) {
216216
return this;
217217
}
218218

219-
public Object getVolumes() {
219+
public BoundHostVolumes getVolumes() {
220220
return volumes;
221221
}
222222

223-
public ContainerConfig setVolumes(Object volumes) {
223+
public ContainerConfig setVolumes(BoundHostVolumes volumes) {
224224
this.volumes = volumes;
225225
return this;
226226
}

‎src/main/java/com/kpelykh/docker/client/model/HostConfig.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ public String[] getBinds() {
4545
public void setBinds(String[] binds) {
4646
this.binds = binds;
4747
}
48+
49+
public void setBinds(final BoundHostVolumes volumes) {
50+
setBinds(volumes.asBinds());
51+
}
4852

4953
public String getContainerIDFile() {
5054
return containerIDFile;

0 commit comments

Comments
 (0)