66import java .util .Map ;
77import java .util .Map .Entry ;
88
9+ import org .apache .commons .lang .ArrayUtils ;
910import org .apache .commons .lang .builder .EqualsBuilder ;
11+ import org .apache .commons .lang .builder .ToStringBuilder ;
1012
1113import com .fasterxml .jackson .core .JsonGenerator ;
1214import com .fasterxml .jackson .core .JsonParser ;
2325import com .github .dockerjava .api .command .InspectContainerResponse .HostConfig ;
2426import com .github .dockerjava .api .command .InspectContainerResponse .NetworkSettings ;
2527
26- import org .apache .commons .lang .builder .ToStringBuilder ;
27-
2828/**
2929 * A container for port bindings, made available as a {@link Map} via its
3030 * {@link #getBindings()} method.
3636@ JsonSerialize (using = Ports .Serializer .class )
3737public class Ports {
3838
39- private final Map <ExposedPort , Binding > ports = new HashMap <ExposedPort , Binding >();
39+ private final Map <ExposedPort , Binding [] > ports = new HashMap <ExposedPort , Binding [] >();
4040
4141 public Ports () { }
4242
@@ -45,7 +45,12 @@ public Ports(ExposedPort exposedPort, Binding host) {
4545 }
4646
4747 public void bind (ExposedPort exposedPort , Binding host ) {
48- ports .put (exposedPort , host );
48+ if (ports .containsKey (exposedPort )) {
49+ Binding [] bindings = ports .get (exposedPort );
50+ ports .put (exposedPort , (Binding []) ArrayUtils .add (bindings , host ));
51+ } else {
52+ ports .put (exposedPort , new Binding []{host });
53+ }
4954 }
5055
5156 @ Override
@@ -54,10 +59,10 @@ public String toString(){
5459 }
5560
5661 /**
57- * @return the port bindings as a {@link Map} that contains one
58- * {@link Binding} per {@link ExposedPort}.
62+ * @return the port bindings as a {@link Map} that contains one or more
63+ * {@link Binding}s per {@link ExposedPort}.
5964 */
60- public Map <ExposedPort , Binding > getBindings (){
65+ public Map <ExposedPort , Binding [] > getBindings (){
6166 return ports ;
6267 }
6368
@@ -132,11 +137,15 @@ public Ports deserialize(JsonParser jsonParser, DeserializationContext deseriali
132137 JsonNode node = oc .readTree (jsonParser );
133138 for (Iterator <Map .Entry <String , JsonNode >> it = node .fields (); it .hasNext ();) {
134139
135- Map .Entry <String , JsonNode > field = it .next ();
136- if (!field .getValue ().equals (NullNode .getInstance ())) {
137- String hostIp = field .getValue ().get (0 ).get ("HostIp" ).textValue ();
138- int hostPort = field .getValue ().get (0 ).get ("HostPort" ).asInt ();
139- out .bind (ExposedPort .parse (field .getKey ()), new Binding (hostIp , hostPort ));
140+ Map .Entry <String , JsonNode > portNode = it .next ();
141+ JsonNode bindingsArray = portNode .getValue ();
142+ for (int i = 0 ; i < bindingsArray .size (); i ++) {
143+ JsonNode bindingNode = bindingsArray .get (i );
144+ if (!bindingNode .equals (NullNode .getInstance ())) {
145+ String hostIp = bindingNode .get ("HostIp" ).textValue ();
146+ int hostPort = bindingNode .get ("HostPort" ).asInt ();
147+ out .bind (ExposedPort .parse (portNode .getKey ()), new Binding (hostIp , hostPort ));
148+ }
140149 }
141150 }
142151 return out ;
@@ -150,13 +159,15 @@ public void serialize(Ports portBindings, JsonGenerator jsonGen,
150159 SerializerProvider serProvider ) throws IOException , JsonProcessingException {
151160
152161 jsonGen .writeStartObject ();
153- for (Entry <ExposedPort , Binding > entry : portBindings .getBindings ().entrySet ()){
162+ for (Entry <ExposedPort , Binding [] > entry : portBindings .getBindings ().entrySet ()){
154163 jsonGen .writeFieldName (entry .getKey ().toString ());
155164 jsonGen .writeStartArray ();
156- jsonGen .writeStartObject ();
157- jsonGen .writeStringField ("HostIp" , entry .getValue ().getHostIp ());
158- jsonGen .writeStringField ("HostPort" , "" + entry .getValue ().getHostPort ());
159- jsonGen .writeEndObject ();
165+ for (Binding binding : entry .getValue ()) {
166+ jsonGen .writeStartObject ();
167+ jsonGen .writeStringField ("HostIp" , binding .getHostIp ());
168+ jsonGen .writeStringField ("HostPort" , "" + binding .getHostPort ());
169+ jsonGen .writeEndObject ();
170+ }
160171 jsonGen .writeEndArray ();
161172 }
162173 jsonGen .writeEndObject ();
0 commit comments