|
| 1 | +package com.github.dockerjava.api.model; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.Map.Entry; |
| 5 | + |
| 6 | +import org.apache.commons.lang.builder.EqualsBuilder; |
| 7 | +import org.apache.commons.lang.builder.HashCodeBuilder; |
| 8 | + |
| 9 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 10 | +import com.fasterxml.jackson.core.JsonParser; |
| 11 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 12 | +import com.fasterxml.jackson.core.ObjectCodec; |
| 13 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 14 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
| 15 | +import com.fasterxml.jackson.databind.JsonNode; |
| 16 | +import com.fasterxml.jackson.databind.JsonSerializer; |
| 17 | +import com.fasterxml.jackson.databind.SerializerProvider; |
| 18 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 19 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
| 20 | +import com.fasterxml.jackson.databind.node.NullNode; |
| 21 | + |
| 22 | +/** |
| 23 | + * Represents a bind mounted volume in a Docker container. |
| 24 | + * |
| 25 | + * @see Bind |
| 26 | + */ |
| 27 | +@JsonDeserialize(using = VolumeRW.Deserializer.class) |
| 28 | +@JsonSerialize(using = VolumeRW.Serializer.class) |
| 29 | +public class VolumeRW { |
| 30 | + |
| 31 | + private Volume volume; |
| 32 | + |
| 33 | + private AccessMode accessMode = AccessMode.rw; |
| 34 | + |
| 35 | + public VolumeRW(Volume volume) { |
| 36 | + this.volume = volume; |
| 37 | + } |
| 38 | + |
| 39 | + public VolumeRW(Volume volume, AccessMode accessMode) { |
| 40 | + this.volume = volume; |
| 41 | + this.accessMode = accessMode; |
| 42 | + } |
| 43 | + |
| 44 | + public Volume getVolume() { |
| 45 | + return volume; |
| 46 | + } |
| 47 | + |
| 48 | + public AccessMode getAccessMode() { |
| 49 | + return accessMode; |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + /** |
| 54 | + * Returns a string representation of this {@link VolumeRW} suitable |
| 55 | + * for inclusion in a JSON message. |
| 56 | + * The returned String is simply the container path, {@link #getPath()}. |
| 57 | + * |
| 58 | + * @return a string representation of this {@link VolumeRW} |
| 59 | + */ |
| 60 | + @Override |
| 61 | + public String toString() { |
| 62 | + return getVolume() + ":" + getAccessMode(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public boolean equals(Object obj) { |
| 67 | + if (obj instanceof VolumeRW) { |
| 68 | + VolumeRW other = (VolumeRW) obj; |
| 69 | + return new EqualsBuilder().append(getVolume(), other.getVolume()).append(accessMode, other.getAccessMode()) |
| 70 | + .isEquals(); |
| 71 | + } else |
| 72 | + return super.equals(obj); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public int hashCode() { |
| 77 | + return new HashCodeBuilder().append(getVolume()).append(getAccessMode()).toHashCode(); |
| 78 | + } |
| 79 | + |
| 80 | + public static class Serializer extends JsonSerializer<VolumeRW> { |
| 81 | + |
| 82 | + @Override |
| 83 | + public void serialize(VolumeRW volumeRW, JsonGenerator jsonGen, |
| 84 | + SerializerProvider serProvider) throws IOException, |
| 85 | + JsonProcessingException { |
| 86 | + |
| 87 | + jsonGen.writeStartObject(); |
| 88 | + jsonGen.writeFieldName(volumeRW.getVolume().getPath()); |
| 89 | + jsonGen.writeString(Boolean.toString(volumeRW.getAccessMode().toBoolean())); |
| 90 | + jsonGen.writeEndObject(); |
| 91 | + } |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + public static class Deserializer extends JsonDeserializer<VolumeRW> { |
| 96 | + @Override |
| 97 | + public VolumeRW deserialize(JsonParser jsonParser, |
| 98 | + DeserializationContext deserializationContext) |
| 99 | + throws IOException, JsonProcessingException { |
| 100 | + ObjectCodec oc = jsonParser.getCodec(); |
| 101 | + JsonNode node = oc.readTree(jsonParser); |
| 102 | + if (!node.equals(NullNode.getInstance())) { |
| 103 | + Entry<String, JsonNode> field = node.fields().next(); |
| 104 | + String volume = field.getKey(); |
| 105 | + AccessMode accessMode = AccessMode.fromBoolean(field.getValue().asBoolean()); |
| 106 | + return new VolumeRW(new Volume(volume), accessMode); |
| 107 | + } else { |
| 108 | + return null; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +} |
0 commit comments