Home / Class/ DockerHost Class — spring-boot Architecture

DockerHost Class — spring-boot Architecture

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

Entity Profile

Source Code

core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerHost.java lines 35–128

final class DockerHost {

	private static final String LOCALHOST = "127.0.0.1";

	private final String host;

	private DockerHost(String host) {
		this.host = host;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null || getClass() != obj.getClass()) {
			return false;
		}
		DockerHost other = (DockerHost) obj;
		return this.host.equals(other.host);
	}

	@Override
	public int hashCode() {
		return this.host.hashCode();
	}

	@Override
	public String toString() {
		return this.host;
	}

	/**
	 * Get or deduce a new {@link DockerHost} instance.
	 * @param host the host to use or {@code null} to deduce
	 * @param contextsSupplier a supplier to provide a list of
	 * {@link DockerCliContextResponse}
	 * @return a new docker host instance
	 */
	static DockerHost get(@Nullable String host, Supplier<List<DockerCliContextResponse>> contextsSupplier) {
		return get(host, System::getenv, contextsSupplier);
	}

	/**
	 * Get or deduce a new {@link DockerHost} instance.
	 * @param host the host to use or {@code null} to deduce
	 * @param systemEnv access to the system environment
	 * @param contextsSupplier a supplier to provide a list of
	 * {@link DockerCliContextResponse}
	 * @return a new docker host instance
	 */
	static DockerHost get(@Nullable String host, Function<String, @Nullable String> systemEnv,
			Supplier<List<DockerCliContextResponse>> contextsSupplier) {
		host = (StringUtils.hasText(host)) ? host : fromServicesHostEnv(systemEnv);
		host = (StringUtils.hasText(host)) ? host : fromDockerHostEnv(systemEnv);
		host = (StringUtils.hasText(host)) ? host : fromCurrentContext(contextsSupplier);
		host = (StringUtils.hasText(host)) ? host : LOCALHOST;
		return new DockerHost(host);
	}

	private static @Nullable String fromServicesHostEnv(Function<String, @Nullable String> systemEnv) {
		return systemEnv.apply("SERVICES_HOST");
	}

	private static @Nullable String fromDockerHostEnv(Function<String, @Nullable String> systemEnv) {
		return fromEndpoint(systemEnv.apply("DOCKER_HOST"));
	}

	private static @Nullable String fromCurrentContext(Supplier<List<DockerCliContextResponse>> contextsSupplier) {
		DockerCliContextResponse current = getCurrentContext(contextsSupplier.get());
		return (current != null) ? fromEndpoint(current.dockerEndpoint()) : null;
	}

	private static @Nullable DockerCliContextResponse getCurrentContext(List<DockerCliContextResponse> candidates) {
		return candidates.stream().filter(DockerCliContextResponse::current).findFirst().orElse(null);
	}

	private static @Nullable String fromEndpoint(@Nullable String endpoint) {
		return (StringUtils.hasLength(endpoint)) ? fromUri(URI.create(endpoint)) : null;
	}

	private static @Nullable String fromUri(URI uri) {
		try {
			return switch (uri.getScheme()) {
				case "http", "https", "tcp" -> uri.getHost();
				default -> null;
			};
		}
		catch (Exception ex) {
			return null;
		}
	}

}

Analyze Your Own Codebase

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

Try Supermodel Free