X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 41 additions & 20 deletions src/main/java/com/github/dockerjava/api/model/ExposedPort.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
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.IOException;
import java.util.Map.Entry;

Expand All @@ -22,56 +25,75 @@

/**
* Represents a container port that Docker exposes to external clients.
* The port is defined by its {@link #getPort() port number} and a
* {@link #getScheme() scheme}, e.g. <code>tcp</code>.
* 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}.
*/
@JsonDeserialize(using = ExposedPort.Deserializer.class)
@JsonSerialize(using = ExposedPort.Serializer.class)
public class ExposedPort {

private final String scheme;

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 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.scheme = scheme;
this.port = port;
this(port, InternetProtocol.valueOf(scheme));
}

/** @return the {@link InternetProtocol} */
public InternetProtocol getProtocol() {
return protocol;
}

/**
* @return the scheme (IP protocol), <code>tcp</code> or <code>udp</code>
* @return the scheme (internet protocol), <code>tcp</code> or <code>udp</code>
* @deprecated use {@link #getProtocol()}
*/
@Deprecated
public String getScheme() {
return scheme;
return protocol.toString();
}

/** @return the port number */
public int getPort() {
return port;
}

/**
* Creates an {@link ExposedPort} for the TCP scheme.
* This is a shortcut for <code>new ExposedPort("tcp", port)</code>
* 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("tcp", port);
return new ExposedPort(port, TCP);
}

/**
* Creates an {@link ExposedPort} for the UDP scheme.
* This is a shortcut for <code>new ExposedPort("udp", port)</code>
* 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("udp", port);
return new ExposedPort(port, UDP);
}

/**
Expand All @@ -85,8 +107,7 @@ public static ExposedPort udp(int port) {
public static ExposedPort parse(String serialized) throws IllegalArgumentException {
try {
String[] parts = serialized.split("/");
ExposedPort out = new ExposedPort(parts[1], Integer.valueOf(parts[0]));
return out;
return new ExposedPort(Integer.valueOf(parts[0]), InternetProtocol.parse(parts[1]));
} catch (Exception e) {
throw new IllegalArgumentException("Error parsing ExposedPort '" + serialized + "'");
}
Expand All @@ -95,28 +116,28 @@ public static ExposedPort parse(String serialized) throws IllegalArgumentExcepti
/**
* Returns a string representation of this {@link ExposedPort} suitable
* for inclusion in a JSON message.
* The format is <code>port/scheme</code>, like the argument in {@link #parse(String)}.
* The format is <code>port/protocol</code>, like the argument in {@link #parse(String)}.
*
* @return a string representation of this {@link ExposedPort}
*/
@Override
public String toString() {
return port + "/" + scheme;
return port + "/" + protocol.toString();
}

@Override
public boolean equals(Object obj) {
if (obj instanceof ExposedPort) {
ExposedPort other = (ExposedPort) obj;
return new EqualsBuilder().append(scheme, other.getScheme())
return new EqualsBuilder().append(protocol, other.getProtocol())
.append(port, other.getPort()).isEquals();
} else
return super.equals(obj);
}

@Override
public int hashCode() {
return new HashCodeBuilder().append(scheme).append(port).toHashCode();
return new HashCodeBuilder().append(protocol).append(port).toHashCode();
}

public static class Deserializer extends JsonDeserializer<ExposedPort> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.github.dockerjava.api.model;


/**
* The IP protocols supported by Docker.
*
* @see #TCP
* @see #UDP
*/
public enum InternetProtocol {
/** The <i>Transmission Control Protocol</i> */
TCP,

/** The <i>User Datagram Protocol</i> */
UDP;

/**
* The default {@link InternetProtocol}: {@link #TCP}
*/
public static final InternetProtocol DEFAULT = TCP;

/**
* Returns a string representation of this {@link InternetProtocol} suitable
* for inclusion in a JSON message.
* The output is the lowercased name of the Protocol, e.g. <code>tcp</code>.
*
* @return a string representation of this {@link InternetProtocol}
*/
@Override
public String toString() {
return super.toString().toLowerCase();
}

/**
* Parses a string to an {@link InternetProtocol}.
*
* @param serialized the protocol, e.g. <code>tcp</code> or <code>TCP</code>
* @return an {@link InternetProtocol} described by the string
* @throws IllegalArgumentException if the argument cannot be parsed
*/
public static InternetProtocol parse(String serialized) throws IllegalArgumentException {
try {
return valueOf(serialized.toUpperCase());
} catch (Exception e) {
throw new IllegalArgumentException("Error parsing Protocol '" + serialized + "'");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.dockerjava.api.model;

import static com.github.dockerjava.api.model.InternetProtocol.*;
import static org.testng.Assert.assertEquals;

import org.testng.annotations.Test;

public class InternetProtocolTest {

@Test
public void defaultProtocol() {
assertEquals(InternetProtocol.DEFAULT, TCP);
}

@Test
public void stringify() {
assertEquals(TCP.toString(), "tcp");
}

@Test
public void parseUpperCase() {
assertEquals(InternetProtocol.parse("TCP"), TCP);
}

@Test
public void parseLowerCase() {
assertEquals(InternetProtocol.parse("tcp"), TCP);
}

@Test(expectedExceptions = IllegalArgumentException.class,
expectedExceptionsMessageRegExp = "Error parsing Protocol.*")
public void parseInvalidInput() {
InternetProtocol.parse("xx");
}

@Test(expectedExceptions = IllegalArgumentException.class,
expectedExceptionsMessageRegExp = "Error parsing Protocol 'null'")
public void parseNull() {
InternetProtocol.parse(null);
}

}
X Tutup