|
| 1 | +package com.github.dockerjava.api.model; |
| 2 | + |
| 3 | +import org.apache.commons.lang.StringUtils; |
| 4 | +import org.apache.commons.lang.builder.EqualsBuilder; |
| 5 | +import org.apache.commons.lang.builder.HashCodeBuilder; |
| 6 | + |
| 7 | +import com.github.dockerjava.api.command.InspectContainerResponse.HostConfig; |
| 8 | +import com.github.dockerjava.api.command.InspectContainerResponse.NetworkSettings; |
| 9 | +import com.github.dockerjava.api.model.Ports.Binding; |
| 10 | + |
| 11 | +/** |
| 12 | + * In a {@link PortBinding}, a network socket on the Docker host, expressed |
| 13 | + * as a {@link Binding}, is bound to an {@link ExposedPort} of a container. |
| 14 | + * A {@link PortBinding} corresponds to the <code>--publish</code> |
| 15 | + * (<code>-p</code>) option of the <code>docker run</code> (and similar) |
| 16 | + * CLI command for adding port bindings to a container. |
| 17 | + * <p> |
| 18 | + * <i>Note: This is an abstraction used for creating new port bindings. |
| 19 | + * It is not to be confused with the abstraction used for querying existing |
| 20 | + * port bindings from a container configuration in |
| 21 | + * {@link NetworkSettings#getPorts()} and {@link HostConfig#getPortBindings()}. |
| 22 | + * In that context, a <code>Map<ExposedPort, Binding[]></code> is used.</i> |
| 23 | + */ |
| 24 | +public class PortBinding { |
| 25 | + private final Binding binding; |
| 26 | + private final ExposedPort exposedPort; |
| 27 | + |
| 28 | + public PortBinding(Binding binding, ExposedPort exposedPort) { |
| 29 | + this.binding = binding; |
| 30 | + this.exposedPort = exposedPort; |
| 31 | + } |
| 32 | + |
| 33 | + public Binding getBinding() { |
| 34 | + return binding; |
| 35 | + } |
| 36 | + |
| 37 | + public ExposedPort getExposedPort() { |
| 38 | + return exposedPort; |
| 39 | + } |
| 40 | + |
| 41 | + public static PortBinding parse(String serialized) throws IllegalArgumentException { |
| 42 | + try { |
| 43 | + String[] parts = StringUtils.splitByWholeSeparator(serialized, ":"); |
| 44 | + switch (parts.length) { |
| 45 | + case 3: |
| 46 | + // 127.0.0.1:80:8080/tcp |
| 47 | + return createFromSubstrings(parts[0] + ":" + parts[1], parts[2]); |
| 48 | + case 2: |
| 49 | + // 80:8080 // 127.0.0.1::8080 |
| 50 | + return createFromSubstrings(parts[0], parts[1]); |
| 51 | + case 1: |
| 52 | + // 8080 |
| 53 | + return createFromSubstrings("", parts[0]); |
| 54 | + default: |
| 55 | + throw new IllegalArgumentException(); |
| 56 | + } |
| 57 | + } catch (Exception e) { |
| 58 | + throw new IllegalArgumentException("Error parsing PortBinding '" |
| 59 | + + serialized + "'", e); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private static PortBinding createFromSubstrings(String binding, String exposedPort) |
| 64 | + throws IllegalArgumentException { |
| 65 | + return new PortBinding(Binding.parse(binding), ExposedPort.parse(exposedPort)); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public boolean equals(Object obj) { |
| 70 | + if (obj instanceof PortBinding) { |
| 71 | + PortBinding other = (PortBinding) obj; |
| 72 | + return new EqualsBuilder().append(binding, other.getBinding()) |
| 73 | + .append(exposedPort, other.getExposedPort()).isEquals(); |
| 74 | + } else |
| 75 | + return super.equals(obj); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public int hashCode() { |
| 80 | + return new HashCodeBuilder().append(binding).append(exposedPort).toHashCode(); |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments