<commit_before>package org.springframework.cloud.config.java;

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.SingletonBeanRegistry;
import org.springframework.cloud.Cloud;
import org.springframework.cloud.CloudFactory;
import org.springframework.cloud.service.PooledServiceConnectorConfig;
import org.springframework.cloud.service.document.MongoDbFactoryConfig;
import org.springframework.cloud.service.messaging.RabbitConnectionFactoryConfig;
import org.springframework.cloud.service.relational.DataSourceConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.redis.connection.RedisConnectionFactory;

/**
 * JavaConfig base class for simplified access to the bound services.
 * 
 * 
 * Note that this class doesn't directly expose low-level methods such as getServiceInfo()
 * to keep focus on typical needs of a Spring application. But, since it exposes the cloud object itself, 
 * you can always call methods on it to get that kind of information.
 * 
 * @author <NAME>
 *
 */
@Configuration
public class AbstractCloudConfig implements BeanFactoryAware {

	private static final String CLOUD_FACTORY_BEAN_NAME = "__cloud_factory__";
	
	private BeanFactory beanFactory;
	private CloudFactory cloudFactory;

	/**
	 * Get the cloud factory.
	 * 
	 * Most applications will never need this method, but provided here to cover corner cases.
	 * 
	 * Implementation note: This roundabout way of implementation is required to ensure that
	 * a CloudFoundry bean if created in some other configuration is available, we should use
	 * that.
	 *  
	 * @return
	 */
	protected CloudFactory cloudFactory() {
		if (cloudFactory == null) {
			cloudFactory = beanFactory.getBean(CloudFactory.class);
			if (cloudFactory == null) {
				cloudFactory = new CloudFactory();
				((SingletonBeanRegistry)beanFactory).registerSingleton(CLOUD_FACTORY_BEAN_NAME, cloudFactory);
			}
		}
		return cloudFactory;
	}

	@Bean
	public Cloud cloud() {
		return cloudFactory().getCloud();
	}
	
	// Relational database
	public DataSource dataSource() {
		return dataSource((DataSourceConfig)null);
	}
	
	public DataSource dataSource(DataSourceConfig dataSourceConfig) {
		return cloud().getSingletonServiceConnector(DataSource.class, dataSourceConfig);<commit_msg>Fixed bugs for situation when no other config introduced a CloudFactory<commit_after>package org.springframework.cloud.config.java;

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.SingletonBeanRegistry;
import org.springframework.cloud.Cloud;
import org.springframework.cloud.CloudFactory;
import org.springframework.cloud.service.PooledServiceConnectorConfig;
import org.springframework.cloud.service.document.MongoDbFactoryConfig;
import org.springframework.cloud.service.messaging.RabbitConnectionFactoryConfig;
import org.springframework.cloud.service.relational.DataSourceConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.redis.connection.RedisConnectionFactory;

/**
 * JavaConfig base class for simplified access to the bound services.
 * 
 * 
 * Note that this class doesn't directly expose low-level methods such as getServiceInfo()
 * to keep focus on typical needs of a Spring application. But, since it exposes the cloud object itself, 
 * you can always call methods on it to get that kind of information.
 * 
 * @author <NAME>
 *
 */
@Configuration
public abstract class AbstractCloudConfig implements BeanFactoryAware {

	private static final String CLOUD_FACTORY_BEAN_NAME = "__cloud_factory__";
	
	private BeanFactory beanFactory;
	private CloudFactory cloudFactory;

	/**
	 * Get the cloud factory.
	 * 
	 * Most applications will never need this method, but provided here to cover corner cases.
	 * 
	 * Implementation note: This roundabout way of implementation is required to ensure that
	 * a CloudFoundry bean if created in some other configuration is available, we should use
	 * that.
	 *  
	 * @return
	 */
	protected CloudFactory cloudFactory() {
		if (cloudFactory == null) {
			try {
				cloudFactory = beanFactory.getBean(CloudFactory.class);
			} catch (NoSuchBeanDefinitionException ex) {
				cloudFactory = new CloudFactory();
				((SingletonBeanRegistry)beanFactory).registerSingleton(CLOUD_FACTORY_BEAN_NAME, cloudFactory);
			}
		}
		return cloudFactory;
	}

	@Bean
	public Cloud cloud() {
		return cloudFactory().getCloud();
	}
	
	// Relational database
	public DataSource dataSource() {
		return dataSource((DataSourceConfig)null);
	}
	
	public DataSource dataSource(DataSourceConfig dataSourceConfig) {
		return cloud().getSingletonServiceConnector(DataSource.class, dataSourceConfig);
{'fraction_non_alphanumeric': 0.07905209840810419, 'fraction_numerical': 0.0, 'mean_word_length': 5.47423887587822, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1122041', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1389, 'n_words': 399}