/*
 * Copyright 2013-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
 *
 *      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.cloudfoundry.discovery;

import java.net.MalformedURLException;
import java.net.URI;

import org.cloudfoundry.client.lib.CloudCredentials;
import org.cloudfoundry.client.lib.CloudFoundryClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * @author Josh Long
 */
@Configuration
@EnableConfigurationProperties
@ConditionalOnClass(CloudFoundryClient.class)
@ConditionalOnProperty(value = "spring.cloud.cloudfoundry.discovery.enabled", matchIfMissing = true)
public class CloudFoundryDiscoveryClientConfiguration {

	@Autowired
	private CloudFoundryDiscoveryProperties discovery;

	@Bean
	@ConditionalOnMissingBean(CloudCredentials.class)
	public CloudCredentials cloudCredentials() {
		return new CloudCredentials(this.discovery.getUsername(),
				this.discovery.getPassword());
	}

	@Bean
	@ConditionalOnMissingBean(CloudFoundryClient.class)
	public CloudFoundryClient cloudFoundryClient(CloudCredentials cc)
			throws MalformedURLException {
		CloudFoundryClient cloudFoundryClient;
		if (this.discovery.getOrg() != null && this.discovery.getSpace() != null) {
			cloudFoundryClient = new CloudFoundryClient(cc,
					URI.create(this.discovery.getUrl()).toURL(), this.discovery.getOrg(),
					this.discovery.getSpace());
		}
		else {
			cloudFoundryClient = new CloudFoundryClient(cc,
					URI.create(this.discovery.getUrl()).toURL());
		}
		cloudFoundryClient.login();
		return cloudFoundryClient;
	}

	@Bean
	@ConditionalOnMissingBean(CloudFoundryDiscoveryClient.class)
	public CloudFoundryDiscoveryClient cloudFoundryDiscoveryClient(
			CloudFoundryClient cloudFoundryClient, Environment environment) {
		return new CloudFoundryDiscoveryClient(cloudFoundryClient, environment);
	}

	@Bean
	public CloudFoundryDiscoveryProperties cloudFoundryDiscoveryProperties() {
		return new CloudFoundryDiscoveryProperties();
	}
}
@@ -1436,39 +1436,8 @@
 ion%0A
-@EnableConfigurationProperties%0A
 @Con
@@ -1579,16 +1579,86 @@
 = true)%0A
+@EnableConfigurationProperties(CloudFoundryDiscoveryProperties.class)%0A
 public c
@@ -2881,139 +2881,5 @@
 %09%7D%0A%0A
-%09@Bean%0A%09public CloudFoundryDiscoveryProperties cloudFoundryDiscoveryProperties() %7B%0A%09%09return new CloudFoundryDiscoveryProperties();%0A%09%7D%0A
 %7D

Switch to @EnableConfigurationProperties to make override predictable