Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.api.model.Volume;
import com.github.dockerjava.api.model.VolumeFrom;

public interface CreateContainerCmd extends DockerCmd<CreateContainerResponse>{

Expand Down Expand Up @@ -93,9 +94,9 @@ public interface CreateContainerCmd extends DockerCmd<CreateContainerResponse>{

public CreateContainerCmd withVolumes(Volume... volumes);

public String[] getVolumesFrom();
public VolumeFrom[] getVolumesFrom();

public CreateContainerCmd withVolumesFrom(String... volumesFrom);
public CreateContainerCmd withVolumesFrom(VolumeFrom... volumesFrom);

public HostConfig getHostConfig();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public enum AccessMode {
*/
public static final AccessMode DEFAULT = rw;


}
18 changes: 1 addition & 17 deletions src/main/java/com/github/dockerjava/api/model/Bind.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.dockerjava.api.model;

import static com.github.dockerjava.api.model.AccessMode.ro;
import static com.github.dockerjava.api.model.AccessMode.rw;


import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
Expand Down Expand Up @@ -29,14 +28,6 @@ public Bind(String path, Volume volume, AccessMode accessMode) {
this.accessMode = accessMode;
}

/**
* @deprecated use {@link #Bind(String, Volume, AccessMode)}
*/
@Deprecated
public Bind(String path, Volume volume, boolean readOnly) {
this(path, volume, readOnly ? ro : rw);
}

public String getPath() {
return path;
}
Expand All @@ -49,13 +40,6 @@ public AccessMode getAccessMode() {
return accessMode;
}

/**
* @deprecated use {@link #getAccessMode()}
*/
@Deprecated
public boolean isReadOnly() {
return ro.equals(accessMode);
}

/**
* Parses a bind mount specification to a {@link Bind}.
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/github/dockerjava/api/model/HostConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class HostConfig {
private String[] dnsSearch;

@JsonProperty("VolumesFrom")
private String[] volumesFrom;
private VolumesFrom volumesFrom;

@JsonProperty("ContainerIDFile")
private String containerIDFile;
Expand All @@ -57,7 +57,7 @@ public HostConfig() {
}

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

public String[] getVolumesFrom() {
public VolumesFrom getVolumesFrom() {
return volumesFrom;
}

Expand Down Expand Up @@ -168,7 +168,7 @@ public void setDnsSearch(String[] dnsSearch) {
this.dnsSearch = dnsSearch;
}

public void setVolumesFrom(String[] volumesFrom) {
public void setVolumesFrom(VolumesFrom volumesFrom) {
this.volumesFrom = volumesFrom;
}

Expand Down
17 changes: 11 additions & 6 deletions src/main/java/com/github/dockerjava/api/model/Volume.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,23 @@ public class Volume {

private String path;

private boolean readWrite = true;
private AccessMode accessMode = AccessMode.rw;

public Volume(String path) {
this.path = path;
}

public Volume(String path, AccessMode accessMode) {
this.path = path;
this.accessMode = accessMode;
}

public String getPath() {
return path;
}

public boolean isReadWrite() {
return readWrite;
public AccessMode getAccessMode() {
return accessMode;
}

public static Volume parse(String serialized) {
Expand All @@ -64,15 +69,15 @@ public String toString() {
public boolean equals(Object obj) {
if (obj instanceof Volume) {
Volume other = (Volume) obj;
return new EqualsBuilder().append(path, other.getPath()).append(readWrite, other.isReadWrite())
return new EqualsBuilder().append(path, other.getPath()).append(accessMode, other.getAccessMode())
.isEquals();
} else
return super.equals(obj);
}

@Override
public int hashCode() {
return new HashCodeBuilder().append(path).append(readWrite).toHashCode();
return new HashCodeBuilder().append(path).append(accessMode).toHashCode();
}

public static class Serializer extends JsonSerializer<Volume> {
Expand All @@ -84,7 +89,7 @@ public void serialize(Volume volume, JsonGenerator jsonGen,

jsonGen.writeStartObject();
jsonGen.writeFieldName(volume.getPath());
jsonGen.writeString(Boolean.toString(volume.isReadWrite()));
jsonGen.writeString(Boolean.toString(volume.getAccessMode().equals(AccessMode.rw) ? true: false));
jsonGen.writeEndObject();
}

Expand Down
87 changes: 87 additions & 0 deletions src/main/java/com/github/dockerjava/api/model/VolumeFrom.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.github.dockerjava.api.model;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

public class VolumeFrom {

private String container;

private AccessMode accessMode;

public VolumeFrom(String container) {
this(container, AccessMode.DEFAULT);
}

public VolumeFrom(String container, AccessMode accessMode) {
this.container = container;
this.accessMode = accessMode;
}

public String getContainer() {
return container;
}

public AccessMode getAccessMode() {
return accessMode;
}


/**
* Parses a volume from specification to a {@link VolumeFrom}.
*
* @param serialized the specification, e.g. <code>container:ro</code>
* @return a {@link VolumeFrom} matching the specification
* @throws IllegalArgumentException if the specification cannot be parsed
*/
public static VolumeFrom parse(String serialized) {
try {
String[] parts = serialized.split(":");
switch (parts.length) {
case 1: {
return new VolumeFrom(parts[0]);
}
case 2: {
return new VolumeFrom(parts[0], AccessMode.valueOf(parts[1]));
}

default: {
throw new IllegalArgumentException();
}
}
} catch (Exception e) {
throw new IllegalArgumentException("Error parsing Bind '" + serialized
+ "'");
}
}

@Override
public boolean equals(Object obj) {
if (obj instanceof VolumeFrom) {
VolumeFrom other = (VolumeFrom) obj;
return new EqualsBuilder().append(container, other.getContainer())
.append(accessMode, other.getAccessMode()).isEquals();
} else
return super.equals(obj);
}

@Override
public int hashCode() {
return new HashCodeBuilder().append(container)
.append(accessMode).toHashCode();
}

/**
* Returns a string representation of this {@link VolumeFrom} suitable
* for inclusion in a JSON message.
* The format is <code>&lt;container&gt;:&lt;access mode&gt;</code>,
* like the argument in {@link #parse(String)}.
*
* @return a string representation of this {@link VolumeFrom}
*/
@Override
public String toString() {
return container + ":" + accessMode.toString();
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/github/dockerjava/api/model/Volumes.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void serialize(Volumes volumes, JsonGenerator jsonGen,
jsonGen.writeStartObject();
for (Volume volume : volumes.getVolumes()) {
jsonGen.writeFieldName(volume.getPath());
jsonGen.writeString(Boolean.toString(volume.isReadWrite()));
jsonGen.writeString(Boolean.toString(volume.getAccessMode().equals(AccessMode.rw) ? true: false));
}
jsonGen.writeEndObject();
}
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/com/github/dockerjava/api/model/VolumesFrom.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.github.dockerjava.api.model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

@JsonSerialize(using = VolumesFrom.Serializer.class)
@JsonDeserialize(using = VolumesFrom.Deserializer.class)
public class VolumesFrom {

private VolumeFrom[] volumesFrom;

public VolumesFrom(VolumeFrom... volumesFrom) {
this.volumesFrom = volumesFrom;
}

public VolumeFrom[] getVolumesFrom() {
return volumesFrom;
}

public static class Serializer extends JsonSerializer<VolumesFrom> {

@Override
public void serialize(VolumesFrom volumesFrom, JsonGenerator jsonGen,
SerializerProvider serProvider) throws IOException,
JsonProcessingException {

//
jsonGen.writeStartArray();
for (VolumeFrom bind : volumesFrom.getVolumesFrom()) {
jsonGen.writeString(bind.toString());
}
jsonGen.writeEndArray();
//
}

}

public static class Deserializer extends JsonDeserializer<VolumesFrom> {
@Override
public VolumesFrom deserialize(JsonParser jsonParser,
DeserializationContext deserializationContext)
throws IOException, JsonProcessingException {

List<VolumeFrom> volumesFrom = new ArrayList<VolumeFrom>();
ObjectCodec oc = jsonParser.getCodec();
JsonNode node = oc.readTree(jsonParser);
for (Iterator<JsonNode> it = node.iterator(); it.hasNext();) {
JsonNode field = it.next();
volumesFrom.add(VolumeFrom.parse(field.asText()));
}
return new VolumesFrom(volumesFrom.toArray(new VolumeFrom[0]));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import com.github.dockerjava.api.model.ExposedPorts;
import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.api.model.Volume;
import com.github.dockerjava.api.model.VolumeFrom;
import com.github.dockerjava.api.model.Volumes;
import com.github.dockerjava.api.model.VolumesFrom;


/**
Expand Down Expand Up @@ -303,13 +305,13 @@ public CreateContainerCmdImpl withVolumes(Volume... volumes) {
}

@Override
public String[] getVolumesFrom() {
return hostConfig.getVolumesFrom();
public VolumeFrom[] getVolumesFrom() {
return hostConfig.getVolumesFrom() == null ? new VolumeFrom[0] : hostConfig.getVolumesFrom().getVolumesFrom();
}

@Override
public CreateContainerCmdImpl withVolumesFrom(String... volumesFrom) {
this.hostConfig.setVolumesFrom(volumesFrom);
public CreateContainerCmdImpl withVolumesFrom(VolumeFrom... volumesFrom) {
this.hostConfig.setVolumesFrom(new VolumesFrom(volumesFrom));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.dockerjava.api.model;

import static org.testng.Assert.assertEquals;

import java.util.Map;

import org.testng.annotations.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dockerjava.api.model.Ports.Binding;

public class VolumeFrom_SerializingTest {
private final ObjectMapper objectMapper = new ObjectMapper();
private final String json =
"[\"container1:ro\",\"container2:rw\"]";
@Test
public void deserializing() throws Exception {
VolumesFrom volumesFrom = objectMapper.readValue(json, VolumesFrom.class);

VolumesFrom expected = new VolumesFrom(new VolumeFrom("container1", AccessMode.ro), new VolumeFrom("container2", AccessMode.rw));

assertEquals(volumesFrom.getVolumesFrom(), expected.getVolumesFrom());
}

@Test
public void serializing() throws Exception {
VolumesFrom volumesFrom = new VolumesFrom(new VolumeFrom("container1", AccessMode.ro), new VolumeFrom("container2", AccessMode.rw));

assertEquals(objectMapper.writeValueAsString(volumesFrom), json);
}


}
Loading