forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExposedPort.java
More file actions
137 lines (121 loc) · 4.49 KB
/
Copy pathExposedPort.java
File metadata and controls
137 lines (121 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.github.dockerjava.api.model;
import static com.github.dockerjava.api.model.InternetProtocol.TCP;
import static com.github.dockerjava.api.model.InternetProtocol.UDP;
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.github.dockerjava.api.model.Ports.Binding;
import lombok.EqualsAndHashCode;
/**
* Represents a container port that Docker exposes to external clients. The port is defined by its {@link #getPort() port number} and an
* {@link InternetProtocol}. It can be published by Docker by {@link Ports#bind(ExposedPort, Binding) binding} it to a host port,
* represented by a {@link Binding}.
*/
@EqualsAndHashCode
public class ExposedPort implements Serializable {
private static final long serialVersionUID = 1L;
private final InternetProtocol protocol;
private final int port;
/**
* Creates an {@link ExposedPort} for the given parameters.
*
* @param port
* the {@link #getPort() port number}
* @param protocol
* the {@link InternetProtocol}
*/
public ExposedPort(int port, InternetProtocol protocol) {
this.port = port;
this.protocol = protocol;
}
/**
* Creates an {@link ExposedPort} for the given {@link #getPort() port number} and {@link InternetProtocol#DEFAULT}.
*
* @param port
* the {@link #getPort() port number}
*/
public ExposedPort(int port) {
this(port, InternetProtocol.DEFAULT);
}
/**
* Creates an {@link ExposedPort} for the given parameters.
*
* @param scheme
* the {@link #getScheme() scheme}, <code>tcp</code> or <code>udp</code>
* @param port
* the {@link #getPort() port number}
* @deprecated use {@link #ExposedPort(int, InternetProtocol)}
*/
@Deprecated
public ExposedPort(String scheme, int port) {
this(port, InternetProtocol.valueOf(scheme));
}
/**
* @return the {@link InternetProtocol} of the {@link #getPort() port} that the container exposes
*/
public InternetProtocol getProtocol() {
return protocol;
}
/**
* @return the scheme (internet protocol), <code>tcp</code> or <code>udp</code>
* @deprecated use {@link #getProtocol()}
*/
@Deprecated
public String getScheme() {
return protocol.toString();
}
/** @return the port number that the container exposes */
public int getPort() {
return port;
}
/**
* Creates an {@link ExposedPort} for {@link InternetProtocol#TCP}. This is a shortcut for
* <code>new ExposedPort(port, {@link InternetProtocol#TCP})</code>
*/
public static ExposedPort tcp(int port) {
return new ExposedPort(port, TCP);
}
/**
* Creates an {@link ExposedPort} for {@link InternetProtocol#UDP}. This is a shortcut for
* <code>new ExposedPort(port, {@link InternetProtocol#UDP})</code>
*/
public static ExposedPort udp(int port) {
return new ExposedPort(port, UDP);
}
/**
* Parses a textual port specification (as used by the Docker CLI) to an {@link ExposedPort}.
*
* @param serialized
* the specification, e.g. <code>80/tcp</code>
* @return an {@link ExposedPort} matching the specification
* @throws IllegalArgumentException
* if the specification cannot be parsed
*/
@JsonCreator
public static ExposedPort parse(String serialized) throws IllegalArgumentException {
try {
String[] parts = serialized.split("/");
switch (parts.length) {
case 1:
return new ExposedPort(Integer.parseInt(parts[0]));
case 2:
return new ExposedPort(Integer.parseInt(parts[0]), InternetProtocol.parse(parts[1]));
default:
throw new IllegalArgumentException();
}
} catch (Exception e) {
throw new IllegalArgumentException("Error parsing ExposedPort '" + serialized + "'");
}
}
/**
* Returns a string representation of this {@link ExposedPort} suitable for inclusion in a JSON message. The format is
* <code>port/protocol</code>, like the argument in {@link #parse(String)}.
*
* @return a string representation of this {@link ExposedPort}
*/
@Override
@JsonValue
public String toString() {
return port + "/" + protocol.toString();
}
}