/*
 * Copyright 2013-2016 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
 *
 *      http://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.client.loadbalancer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.AsyncClientHttpRequestInterceptor;
import org.springframework.web.client.AsyncRestTemplate;

/**
 * Auto configuration for Ribbon (client side load balancing).
 *
 * @author Rob Worsnop
 */
@Configuration
@ConditionalOnClass(AsyncRestTemplate.class)
@ConditionalOnBean(LoadBalancerClient.class)
public class AsyncLoadBalancerAutoConfiguration {

	@LoadBalanced
	@Autowired(required = false)
	private List<AsyncRestTemplate> restTemplates = Collections.emptyList();

	@Bean
	public SmartInitializingSingleton loadBalancedRestTemplateInitializer(
			final List<AsyncRestTemplateCustomizer> customizers) {
		return new SmartInitializingSingleton() {
			@Override
			public void afterSingletonsInstantiated() {
				for (AsyncRestTemplate restTemplate : AsyncLoadBalancerAutoConfiguration.this.restTemplates) {
					for (AsyncRestTemplateCustomizer customizer : customizers) {
						customizer.customize(restTemplate);
					}
				}
			}
		};
	}

	@Configuration
	static class LoadBalancerInterceptorConfig {
		@Bean
		public AsyncLoadBalancerInterceptor ribbonInterceptor(LoadBalancerClient loadBalancerClient) {
			return new AsyncLoadBalancerInterceptor(loadBalancerClient);
		}

		@Bean
		@ConditionalOnMissingBean
		public AsyncRestTemplateCustomizer restTemplateCustomizer(
				final AsyncLoadBalancerInterceptor loadBalancerInterceptor) {
			return new AsyncRestTemplateCustomizer() {
				@Override
				public void customize(AsyncRestTemplate restTemplate) {
					List<AsyncClientHttpRequestInterceptor> list = new ArrayList<>(
							restTemplate.getInterceptors());
					list.add(loadBalancerInterceptor);
					restTemplate.setInterceptors(list);
				}
			};
		}
	}
}

@@ -1040,90 +1040,8 @@
 ss;%0A
-import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;%0A
 impo
@@ -1442,53 +1442,8 @@
 ss)%0A
-@ConditionalOnBean(LoadBalancerClient.class)%0A
 publ
@@ -1489,16 +1489,139 @@
 tion %7B%0A%0A
+%0A%09@ConditionalOnBean(AsyncRestTemplateCustomizer.class)%0A%09@Configuration%0A%09static class AsyncRestTemplateCustomizerConfig %7B%0A%09
 %09@LoadBa
@@ -1627,16 +1627,17 @@
 alanced%0A
+%09
 %09@Autowi
@@ -1658,16 +1658,17 @@
  false)%0A
+%09
 %09private
@@ -1734,23 +1734,25 @@
 ist();%0A%0A
+%09
 %09@Bean%0A
+%09
 %09public 
@@ -1790,16 +1790,21 @@
 Balanced
+Async
 RestTemp
@@ -1820,16 +1820,17 @@
 alizer(%0A
+%09
 %09%09%09final
@@ -1881,16 +1881,17 @@
 rs) %7B%0A%09%09
+%09
 return n
@@ -1924,16 +1924,17 @@
 ton() %7B%0A
+%09
 %09%09%09@Over
@@ -1941,16 +1941,17 @@
 ride%0A%09%09%09
+%09
 public v
@@ -1986,24 +1986,25 @@
 ted() %7B%0A%09%09%09%09
+%09
 for (AsyncRe
@@ -2038,37 +2038,36 @@
 sync
-LoadBalancerAutoConfiguration
+RestTemplateCustomizerConfig
 .thi
@@ -2081,24 +2081,25 @@
 emplates) %7B%0A
+%09
 %09%09%09%09%09for (As
@@ -2158,16 +2158,17 @@
 %7B%0A%09%09%09%09%09%09
+%09
 customiz
@@ -2195,16 +2195,17 @@
 plate);%0A
+%09
 %09%09%09%09%09%7D%0A%09
@@ -2211,24 +2211,77 @@
 %09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09%7D%0A%09%09
+%09
 %7D;%0A
+%09%09%7D%0A
 %09%7D%0A%0A
+%09@ConditionalOnBean(LoadBalancerClient.class)%0A
 %09@Co
@@ -2388,14 +2388,25 @@
 tor 
-ribbon
+asyncLoadBalancer
 Inte
@@ -2535,36 +2535,8 @@
 ean%0A
-%09%09@ConditionalOnMissingBean%0A
 %09%09pu
@@ -2568,17 +2568,22 @@
 tomizer 
-r
+asyncR
 estTempl

tweak AsyncRestTemplate bean names and loading