// Path: spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/support/ZoneUtils.java
// public final class ZoneUtils {
// 
// 	private ZoneUtils() {
// 		throw new AssertionError("Must not instantiate utility class.");
// 	}
// 
// 	/**
// 	 * Approximates Eureka zones from a host name. This method approximates the zone to be
// 	 * everything after the first "." in the host name.
// 	 * @param host The host name to extract the host name from
// 	 * @return The approximate zone
// 	 */
// 	public static String extractApproximateZone(String host) {
// 		String[] split = StringUtils.split(host, ".");
// 		return split == null ? host : split[1];
// 	}
// 
// }
// 
// Path: spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/loadbalancer/LoadBalancerEurekaAutoConfiguration.java
// public static final String LOADBALANCER_ZONE = "spring.cloud.loadbalancer.zone";


import com.netflix.appinfo.EurekaInstanceConfig;
import com.netflix.discovery.EurekaClientConfig;
import jakarta.annotation.PostConstruct;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.cloud.loadbalancer.config.LoadBalancerZoneConfig;
import org.springframework.cloud.netflix.eureka.support.ZoneUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import static org.springframework.cloud.netflix.eureka.loadbalancer.LoadBalancerEurekaAutoConfiguration.LOADBALANCER_ZONE;
/*
 * Copyright 2013-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.cloud.netflix.eureka.loadbalancer;




/**
 * A configuration for Spring Cloud LoadBalancer that retrieves client instance zone from
 * Eureka and sets it as a property. Based on
 * {@link EurekaLoadBalancerClientConfiguration}.
 *
 * @author Olga Maciaszek-Sharma
 * @since 2.2.1
 * @see EurekaLoadBalancerClientConfiguration
 */
@Configuration
@ConditionalOnBean({ LoadBalancerZoneConfig.class, EurekaLoadBalancerProperties.class })
public class EurekaLoadBalancerClientConfiguration {

	private static final Log LOG = LogFactory.getLog(EurekaLoadBalancerClientConfiguration.class);

	private final EurekaClientConfig clientConfig;

	private final EurekaInstanceConfig eurekaConfig;

	private final LoadBalancerZoneConfig zoneConfig;

	private final EurekaLoadBalancerProperties eurekaLoadBalancerProperties;

	public EurekaLoadBalancerClientConfiguration(@Autowired(required = false) EurekaClientConfig clientConfig,
			@Autowired(required = false) EurekaInstanceConfig eurekaInstanceConfig, LoadBalancerZoneConfig zoneConfig,
			EurekaLoadBalancerProperties eurekaLoadBalancerProperties) {
		this.clientConfig = clientConfig;
		this.eurekaConfig = eurekaInstanceConfig;
		this.zoneConfig = zoneConfig;
		this.eurekaLoadBalancerProperties = eurekaLoadBalancerProperties;
	}

	@PostConstruct
	public void postprocess() {
		if (!StringUtils.isEmpty(zoneConfig.getZone())) {
			return;
		}
		String zone = getZoneFromEureka();
		if (!StringUtils.isEmpty(zone)) {
			if (LOG.isDebugEnabled()) {
// Path: spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/support/ZoneUtils.java
// public final class ZoneUtils {
// 
// 	private ZoneUtils() {
// 		throw new AssertionError("Must not instantiate utility class.");
// 	}
// 
// 	/**
// 	 * Approximates Eureka zones from a host name. This method approximates the zone to be
// 	 * everything after the first "." in the host name.
// 	 * @param host The host name to extract the host name from
// 	 * @return The approximate zone
// 	 */
// 	public static String extractApproximateZone(String host) {
// 		String[] split = StringUtils.split(host, ".");
// 		return split == null ? host : split[1];
// 	}
// 
// }
// 
// Path: spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/loadbalancer/LoadBalancerEurekaAutoConfiguration.java
// public static final String LOADBALANCER_ZONE = "spring.cloud.loadbalancer.zone";


// Path: spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/loadbalancer/EurekaLoadBalancerClientConfiguration.java
import com.netflix.appinfo.EurekaInstanceConfig;
import com.netflix.discovery.EurekaClientConfig;
import jakarta.annotation.PostConstruct;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.cloud.loadbalancer.config.LoadBalancerZoneConfig;
import org.springframework.cloud.netflix.eureka.support.ZoneUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import static org.springframework.cloud.netflix.eureka.loadbalancer.LoadBalancerEurekaAutoConfiguration.LOADBALANCER_ZONE;

/*
 * Copyright 2013-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.cloud.netflix.eureka.loadbalancer;




/**
 * A configuration for Spring Cloud LoadBalancer that retrieves client instance zone from
 * Eureka and sets it as a property. Based on
 * {@link EurekaLoadBalancerClientConfiguration}.
 *
 * @author Olga Maciaszek-Sharma
 * @since 2.2.1
 * @see EurekaLoadBalancerClientConfiguration
 */
@Configuration
@ConditionalOnBean({ LoadBalancerZoneConfig.class, EurekaLoadBalancerProperties.class })
public class EurekaLoadBalancerClientConfiguration {

	private static final Log LOG = LogFactory.getLog(EurekaLoadBalancerClientConfiguration.class);

	private final EurekaClientConfig clientConfig;

	private final EurekaInstanceConfig eurekaConfig;

	private final LoadBalancerZoneConfig zoneConfig;

	private final EurekaLoadBalancerProperties eurekaLoadBalancerProperties;

	public EurekaLoadBalancerClientConfiguration(@Autowired(required = false) EurekaClientConfig clientConfig,
			@Autowired(required = false) EurekaInstanceConfig eurekaInstanceConfig, LoadBalancerZoneConfig zoneConfig,
			EurekaLoadBalancerProperties eurekaLoadBalancerProperties) {
		this.clientConfig = clientConfig;
		this.eurekaConfig = eurekaInstanceConfig;
		this.zoneConfig = zoneConfig;
		this.eurekaLoadBalancerProperties = eurekaLoadBalancerProperties;
	}

	@PostConstruct
	public void postprocess() {
		if (!StringUtils.isEmpty(zoneConfig.getZone())) {
			return;
		}
		String zone = getZoneFromEureka();
		if (!StringUtils.isEmpty(zone)) {
			if (LOG.isDebugEnabled()) {
				LOG.debug("Setting the value of '" + LOADBALANCER_ZONE + "' to " + zone);