Home / Class/ DefaultConnectionPorts Class — spring-boot Architecture

DefaultConnectionPorts Class — spring-boot Architecture

Architecture documentation for the DefaultConnectionPorts class in DefaultConnectionPorts.java from the spring-boot codebase.

Entity Profile

Source Code

core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DefaultConnectionPorts.java lines 42–153

class DefaultConnectionPorts implements ConnectionPorts {

	private final Map<ContainerPort, Integer> mappings;

	private final Map<Integer, Integer> portMappings;

	DefaultConnectionPorts(DockerCliInspectResponse inspectResponse) {
		this.mappings = !isHostNetworkMode(inspectResponse)
				? buildMappingsForNetworkSettings(inspectResponse.networkSettings())
				: buildMappingsForHostNetworking(inspectResponse.config());
		Map<Integer, Integer> portMappings = new HashMap<>();
		this.mappings.forEach((containerPort, hostPort) -> portMappings.put(containerPort.number(), hostPort));
		this.portMappings = Collections.unmodifiableMap(portMappings);
	}

	private static boolean isHostNetworkMode(DockerCliInspectResponse inspectResponse) {
		HostConfig config = inspectResponse.hostConfig();
		return (config != null) && "host".equals(config.networkMode());
	}

	private Map<ContainerPort, Integer> buildMappingsForNetworkSettings(@Nullable NetworkSettings networkSettings) {
		if (networkSettings == null || CollectionUtils.isEmpty(networkSettings.ports())) {
			return Collections.emptyMap();
		}
		Map<ContainerPort, Integer> mappings = new HashMap<>();
		networkSettings.ports().forEach((containerPortString, hostPorts) -> {
			if (!CollectionUtils.isEmpty(hostPorts)) {
				ContainerPort containerPort = ContainerPort.parse(containerPortString);
				hostPorts.stream()
					.filter(this::isIpV4)
					.forEach((hostPort) -> mappings.put(containerPort, getPortNumber(hostPort)));
			}
		});
		return Collections.unmodifiableMap(mappings);
	}

	private boolean isIpV4(@Nullable HostPort hostPort) {
		String ip = (hostPort != null) ? hostPort.hostIp() : null;
		return !StringUtils.hasLength(ip) || ip.contains(".");
	}

	private static int getPortNumber(HostPort hostPort) {
		return Integer.parseInt(hostPort.hostPort());
	}

	private Map<ContainerPort, Integer> buildMappingsForHostNetworking(Config config) {
		if (CollectionUtils.isEmpty(config.exposedPorts())) {
			return Collections.emptyMap();
		}
		Map<ContainerPort, Integer> mappings = new HashMap<>();
		for (String entry : config.exposedPorts().keySet()) {
			ContainerPort containerPort = ContainerPort.parse(entry);
			mappings.put(containerPort, containerPort.number());
		}
		return Collections.unmodifiableMap(mappings);
	}

	@Override
	public int get(int containerPort) {
		Integer hostPort = this.portMappings.get(containerPort);
		Assert.state(hostPort != null,
				() -> "No host port mapping found for container port %s".formatted(containerPort));
		return hostPort;
	}

	@Override
	public List<Integer> getAll() {
		return getAll(null);
	}

	@Override
	public List<Integer> getAll(@Nullable String protocol) {
		List<Integer> hostPorts = new ArrayList<>();
		this.mappings.forEach((containerPort, hostPort) -> {
			if (protocol == null || protocol.equalsIgnoreCase(containerPort.protocol())) {
				hostPorts.add(hostPort);
			}
		});
		return Collections.unmodifiableList(hostPorts);
	}

	Map<ContainerPort, Integer> getMappings() {
		return this.mappings;
	}

	/**
	 * A container port consisting of a number and protocol.
	 *
	 * @param number the port number
	 * @param protocol the protocol (e.g. tcp)
	 */
	record ContainerPort(int number, String protocol) {

		@Override
		public String toString() {
			return "%d/%s".formatted(this.number, this.protocol);
		}

		static ContainerPort parse(String value) {
			try {
				String[] parts = value.split("/");
				Assert.state(parts.length == 2, "Unable to split string");
				return new ContainerPort(Integer.parseInt(parts[0]), parts[1]);
			}
			catch (RuntimeException ex) {
				throw new IllegalStateException("Unable to parse container port '%s'".formatted(value), ex);
			}
		}

	}

}

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free