// Path: spring-cloud-services-spring-connector/src/main/java/io/pivotal/spring/cloud/config/java/ServiceInfoPropertySourceAdapter.java
// public abstract class ServiceInfoPropertySourceAdapter<T extends ServiceInfo>
// 		implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
// 
// 	public static final String SPRING_AUTOCONFIGURE_EXCLUDE = "spring.autoconfigure.exclude";
// 
// 	private static final String RABBIT_AUTOCONFIG_CLASS = "org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration";
// 
// 	private ConfigurableEnvironment environment;
// 
// 	private Cloud cloud;
// 
// 	protected abstract PropertySource<?> toPropertySource(T serviceInfo);
// 
// 	@SuppressWarnings("unchecked")
// 	@Override
// 	public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
// 		environment = event.getEnvironment();
// 
// 		if (cloud != null) {
// 			return;
// 		}
// 
// 		try {
// 			cloud = new CloudFactory().getCloud();
// 		} catch (CloudException e) {
// 			// not running on a known cloud environment, so nothing to do
// 			return;
// 		}
// 
// 		for (ServiceInfo serviceInfo : cloud.getServiceInfos()) {
// 			try {
// 				PropertySource<?> propertySource = toPropertySource((T) serviceInfo);
// 				event.getEnvironment().getPropertySources().addFirst(propertySource);
// 			} catch (ClassCastException e) {
// 				// non-matching ServiceInfo
// 			}
// 		}
// 
// 		conditionallyExcludeRabbitAutoConfiguration();
// 	}
// 
// 	private void conditionallyExcludeRabbitAutoConfiguration() {
// 		if (appIsBoundToRabbitMQ()) {
// 			return;
// 		}
// 
// 		Map<String, Object> properties = new LinkedHashMap<>();
// 		String existingExcludes = environment.getProperty(SPRING_AUTOCONFIGURE_EXCLUDE);
// 		if (existingExcludes == null) {
// 			properties.put(SPRING_AUTOCONFIGURE_EXCLUDE, RABBIT_AUTOCONFIG_CLASS);
// 		} else if (!existingExcludes.contains(RABBIT_AUTOCONFIG_CLASS)) {
// 			properties.put(SPRING_AUTOCONFIGURE_EXCLUDE, RABBIT_AUTOCONFIG_CLASS + "," + existingExcludes);
// 		}
// 
// 		PropertySource<?> propertySource = new MapPropertySource("springCloudServicesRabbitAutoconfigExcluder",
// 				properties);
// 		environment.getPropertySources().addFirst(propertySource);
// 	}
// 
// 	private boolean appIsBoundToRabbitMQ() {
// 		boolean hasEnvironmentBinding = environment.containsProperty("spring.rabbitmq.host") &&
// 				!environment.getProperty("spring.rabbitmq.host").equals("localhost");
// 		
// 		boolean hasCloudBinding = false;
// 		try {
// 			Class<?> rabbitCFClass = Class.forName("org.springframework.amqp.rabbit.connection.ConnectionFactory");
// 			hasCloudBinding = !cloud.getServiceInfos(rabbitCFClass).isEmpty();
// 		} catch (Exception e) {/* Nothing to do if ConnectionFactory can't be loaded.*/}
// 		
// 		return hasEnvironmentBinding || hasCloudBinding;
// 	}
// 
// 	@Override
// 	public int getOrder() {
// 		return Ordered.HIGHEST_PRECEDENCE + 4;
// 	}
// }
// 
// Path: spring-cloud-services-connector-core/src/main/java/io/pivotal/spring/cloud/service/common/EurekaServiceInfo.java
// @ServiceInfo.ServiceLabel("eureka")
// public class EurekaServiceInfo extends UriBasedServiceInfo {
// 	private String clientId;
// 	private String clientSecret;
// 	private String accessTokenUri;
// 
// 	public EurekaServiceInfo(String id, String uriString, String clientId, String clientSecret, String accessTokenUri) {
// 		super(id, uriString);
// 		this.clientId = clientId;
// 		this.clientSecret = clientSecret;
// 		this.accessTokenUri = accessTokenUri;
// 	}
// 
// 	public String getClientId() {
// 		return clientId;
// 	}
// 
// 	public String getClientSecret() {
// 		return clientSecret;
// 	}
// 
// 	public String getAccessTokenUri() {
// 		return accessTokenUri;
// 	}
// 
// }


import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import io.pivotal.spring.cloud.config.java.ServiceInfoPropertySourceAdapter;
import io.pivotal.spring.cloud.service.common.EurekaServiceInfo;
/*
 * Copyright 2015 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 io.pivotal.spring.cloud.service.eureka;




/**
 * Provides eureka.client.* properties and enables {@link EurekaClientOAuth2AutoConfiguration}
 *
 * @author Will Tran
 */
public class EurekaServiceConnector extends ServiceInfoPropertySourceAdapter<EurekaServiceInfo> {
	private static final String EUREKA_CLIENT = "eureka.client.";
	private static final String EUREKA_CLIENT_OAUTH2 = EUREKA_CLIENT + "oauth2.";
	private static final String EUREKA_API_PREFIX = "/eureka/";
	private static final String DEFAULT_REGION = "default";
	private static final String PROPERTY_SOURCE_NAME = "springCloudServicesServiceRegistry";

	@Override
// Path: spring-cloud-services-spring-connector/src/main/java/io/pivotal/spring/cloud/config/java/ServiceInfoPropertySourceAdapter.java
// public abstract class ServiceInfoPropertySourceAdapter<T extends ServiceInfo>
// 		implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
// 
// 	public static final String SPRING_AUTOCONFIGURE_EXCLUDE = "spring.autoconfigure.exclude";
// 
// 	private static final String RABBIT_AUTOCONFIG_CLASS = "org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration";
// 
// 	private ConfigurableEnvironment environment;
// 
// 	private Cloud cloud;
// 
// 	protected abstract PropertySource<?> toPropertySource(T serviceInfo);
// 
// 	@SuppressWarnings("unchecked")
// 	@Override
// 	public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
// 		environment = event.getEnvironment();
// 
// 		if (cloud != null) {
// 			return;
// 		}
// 
// 		try {
// 			cloud = new CloudFactory().getCloud();
// 		} catch (CloudException e) {
// 			// not running on a known cloud environment, so nothing to do
// 			return;
// 		}
// 
// 		for (ServiceInfo serviceInfo : cloud.getServiceInfos()) {
// 			try {
// 				PropertySource<?> propertySource = toPropertySource((T) serviceInfo);
// 				event.getEnvironment().getPropertySources().addFirst(propertySource);
// 			} catch (ClassCastException e) {
// 				// non-matching ServiceInfo
// 			}
// 		}
// 
// 		conditionallyExcludeRabbitAutoConfiguration();
// 	}
// 
// 	private void conditionallyExcludeRabbitAutoConfiguration() {
// 		if (appIsBoundToRabbitMQ()) {
// 			return;
// 		}
// 
// 		Map<String, Object> properties = new LinkedHashMap<>();
// 		String existingExcludes = environment.getProperty(SPRING_AUTOCONFIGURE_EXCLUDE);
// 		if (existingExcludes == null) {
// 			properties.put(SPRING_AUTOCONFIGURE_EXCLUDE, RABBIT_AUTOCONFIG_CLASS);
// 		} else if (!existingExcludes.contains(RABBIT_AUTOCONFIG_CLASS)) {
// 			properties.put(SPRING_AUTOCONFIGURE_EXCLUDE, RABBIT_AUTOCONFIG_CLASS + "," + existingExcludes);
// 		}
// 
// 		PropertySource<?> propertySource = new MapPropertySource("springCloudServicesRabbitAutoconfigExcluder",
// 				properties);
// 		environment.getPropertySources().addFirst(propertySource);
// 	}
// 
// 	private boolean appIsBoundToRabbitMQ() {
// 		boolean hasEnvironmentBinding = environment.containsProperty("spring.rabbitmq.host") &&
// 				!environment.getProperty("spring.rabbitmq.host").equals("localhost");
// 		
// 		boolean hasCloudBinding = false;
// 		try {
// 			Class<?> rabbitCFClass = Class.forName("org.springframework.amqp.rabbit.connection.ConnectionFactory");
// 			hasCloudBinding = !cloud.getServiceInfos(rabbitCFClass).isEmpty();
// 		} catch (Exception e) {/* Nothing to do if ConnectionFactory can't be loaded.*/}
// 		
// 		return hasEnvironmentBinding || hasCloudBinding;
// 	}
// 
// 	@Override
// 	public int getOrder() {
// 		return Ordered.HIGHEST_PRECEDENCE + 4;
// 	}
// }
// 
// Path: spring-cloud-services-connector-core/src/main/java/io/pivotal/spring/cloud/service/common/EurekaServiceInfo.java
// @ServiceInfo.ServiceLabel("eureka")
// public class EurekaServiceInfo extends UriBasedServiceInfo {
// 	private String clientId;
// 	private String clientSecret;
// 	private String accessTokenUri;
// 
// 	public EurekaServiceInfo(String id, String uriString, String clientId, String clientSecret, String accessTokenUri) {
// 		super(id, uriString);
// 		this.clientId = clientId;
// 		this.clientSecret = clientSecret;
// 		this.accessTokenUri = accessTokenUri;
// 	}
// 
// 	public String getClientId() {
// 		return clientId;
// 	}
// 
// 	public String getClientSecret() {
// 		return clientSecret;
// 	}
// 
// 	public String getAccessTokenUri() {
// 		return accessTokenUri;
// 	}
// 
// }


// Path: spring-cloud-services-spring-connector/src/main/java/io/pivotal/spring/cloud/service/eureka/EurekaServiceConnector.java
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import io.pivotal.spring.cloud.config.java.ServiceInfoPropertySourceAdapter;
import io.pivotal.spring.cloud.service.common.EurekaServiceInfo;

/*
 * Copyright 2015 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 io.pivotal.spring.cloud.service.eureka;




/**
 * Provides eureka.client.* properties and enables {@link EurekaClientOAuth2AutoConfiguration}
 *
 * @author Will Tran
 */
public class EurekaServiceConnector extends ServiceInfoPropertySourceAdapter<EurekaServiceInfo> {
	private static final String EUREKA_CLIENT = "eureka.client.";
	private static final String EUREKA_CLIENT_OAUTH2 = EUREKA_CLIENT + "oauth2.";
	private static final String EUREKA_API_PREFIX = "/eureka/";
	private static final String DEFAULT_REGION = "default";
	private static final String PROPERTY_SOURCE_NAME = "springCloudServicesServiceRegistry";

	@Override
	protected PropertySource<?> toPropertySource(EurekaServiceInfo eurekaServiceInfo) {