package com.github.ledoyen.automocker.extension.sql;

import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Joiner;

public class DatasourceLocator implements InitializingBean {

	@Autowired
	private Map<String, DataSource> datasourcesByName;

	public DataSource getDataSource() {
		if (datasourcesByName.size() == 1) {
			return datasourcesByName.values()
					.iterator()
					.next();
		} else {
			throw new IllegalArgumentException("Multiple datasources available: " + Joiner.on(", ")
					.join(datasourcesByName.keySet()));
		}
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		if (datasourcesByName.size() == 1) {

		}
	}

}

@@ -711,16 +711,631 @@
 %0D%0A%09%7D%0D%0A%0D%0A
+%09public DataSource getDataSource(String dsName) %7B%0D%0A%09%09if (!datasourcesByName.containsKey(dsName)) %7B%0D%0A%09%09%09throw new IllegalArgumentException(%0D%0A%09%09%09%09%09%22No datasource names %5B%22 + dsName + %22%5D (availables: %22 + Joiner.on(%22, %22)%0D%0A%09%09%09%09%09%09%09.join(datasourcesByName.keySet()) + %22)%22);%0D%0A%09%09%7D%0D%0A%09%09return datasourcesByName.get(dsName);%0D%0A%09%7D%0D%0A%0D%0A%09public String getName() %7B%0D%0A%09%09if (datasourcesByName.size() == 1) %7B%0D%0A%09%09%09return datasourcesByName.keySet()%0D%0A%09%09%09%09%09.iterator()%0D%0A%09%09%09%09%09.next();%0D%0A%09%09%7D else %7B%0D%0A%09%09%09throw new IllegalArgumentException(%22Multiple datasources available: %22 + Joiner.on(%22, %22)%0D%0A%09%09%09%09%09.join(datasourcesByName.keySet()));%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%0D%0A
 %09@Overri
@@ -1338,16 +1338,16 @@
 erride%0D%0A
-
 %09public 
@@ -1439,17 +1439,15 @@
 %0A%0D%0A%09%09%7D%0D%0A
+
 %09%7D%0D%0A
-%0D%0A
 %7D%0D%0A

add Datasource locator utilities