Skip to content

Commit 7aaefb4

Browse files
committed
Merge branch 'master' into serverAddress
Conflicts: README.md src/test/java/com/github/dockerjava/api/model/Ports_SerializingTest.java
2 parents 2c6ab38 + d74799e commit 7aaefb4

12 files changed

Lines changed: 476 additions & 32 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ Run build without integration tests:
5858
<dependency>
5959
<groupId>com.github.docker-java</groupId>
6060
<artifactId>docker-java</artifactId>
61-
<version>RELEASE</version>
61+
<version>0.10.3</version>
6262
</dependency>
6363

6464
### Latest SNAPSHOT version
6565

6666
<dependency>
6767
<groupId>com.github.docker-java</groupId>
6868
<artifactId>docker-java</artifactId>
69-
<version>LATEST</version>
69+
<version>0.10.4-SNAPSHOT</version>
7070
</dependency>
7171

7272
Latest SNAPSHOT is published to maven repo: https://oss.sonatype.org/content/groups/public via ![Build on CloudBees](http://cloudbees.prod.acquia-sites.com/sites/default/files/styles/large/public/Button-Powered-by-CB.png?itok=uMDWINfY)

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<groupId>com.github.docker-java</groupId>
1111
<artifactId>docker-java</artifactId>
1212
<packaging>jar</packaging>
13-
<version>0.10.3-SNAPSHOT</version>
13+
<version>0.10.4-SNAPSHOT</version>
1414

1515
<name>docker-java</name>
1616
<url>https://github.com/docker-java/docker-java</url>

src/main/java/com/github/dockerjava/api/command/StartContainerCmd.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.github.dockerjava.api.model.Device;
77
import com.github.dockerjava.api.model.Link;
88
import com.github.dockerjava.api.model.LxcConf;
9+
import com.github.dockerjava.api.model.PortBinding;
910
import com.github.dockerjava.api.model.Ports;
1011
import com.github.dockerjava.api.model.RestartPolicy;
1112

@@ -53,8 +54,21 @@ public interface StartContainerCmd extends DockerCmd<Void> {
5354

5455
public StartContainerCmd withLxcConf(LxcConf... lxcConf);
5556

57+
/**
58+
* Add the port bindings that are contained in the given {@link Ports}
59+
* object.
60+
*
61+
* @see #withPortBindings(PortBinding...)
62+
*/
5663
public StartContainerCmd withPortBindings(Ports portBindings);
5764

65+
/**
66+
* Add one or more {@link PortBinding}s.
67+
* This corresponds to the <code>--publish</code> (<code>-p</code>)
68+
* option of the <code>docker run</code> CLI command.
69+
*/
70+
public StartContainerCmd withPortBindings(PortBinding... portBindings);
71+
5872
public StartContainerCmd withPrivileged(boolean privileged);
5973

6074
public StartContainerCmd withPublishAllPorts(boolean publishAllPorts);

src/main/java/com/github/dockerjava/api/model/ExposedPort.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ public ExposedPort(int port, InternetProtocol protocol) {
4848
this.protocol = protocol;
4949
}
5050

51+
/**
52+
* Creates an {@link ExposedPort} for the given
53+
* {@link #getPort() port number} and {@link InternetProtocol#DEFAULT}.
54+
*
55+
* @param port the {@link #getPort() port number}
56+
*/
57+
public ExposedPort(int port) {
58+
this(port, InternetProtocol.DEFAULT);
59+
}
60+
5161
/**
5262
* Creates an {@link ExposedPort} for the given parameters.
5363
*
@@ -61,7 +71,8 @@ public ExposedPort(String scheme, int port) {
6171
this(port, InternetProtocol.valueOf(scheme));
6272
}
6373

64-
/** @return the {@link InternetProtocol} */
74+
/** @return the {@link InternetProtocol} of the {@link #getPort() port}
75+
* that the container exposes */
6576
public InternetProtocol getProtocol() {
6677
return protocol;
6778
}
@@ -75,7 +86,7 @@ public String getScheme() {
7586
return protocol.toString();
7687
}
7788

78-
/** @return the port number */
89+
/** @return the port number that the container exposes */
7990
public int getPort() {
8091
return port;
8192
}
@@ -107,7 +118,14 @@ public static ExposedPort udp(int port) {
107118
public static ExposedPort parse(String serialized) throws IllegalArgumentException {
108119
try {
109120
String[] parts = serialized.split("/");
110-
return new ExposedPort(Integer.valueOf(parts[0]), InternetProtocol.parse(parts[1]));
121+
switch (parts.length) {
122+
case 1:
123+
return new ExposedPort(Integer.valueOf(parts[0]));
124+
case 2:
125+
return new ExposedPort(Integer.valueOf(parts[0]), InternetProtocol.parse(parts[1]));
126+
default:
127+
throw new IllegalArgumentException();
128+
}
111129
} catch (Exception e) {
112130
throw new IllegalArgumentException("Error parsing ExposedPort '" + serialized + "'");
113131
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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&lt;ExposedPort, Binding[]&gt;</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

Comments
 (0)