1
|
package eu.etaxonomy.cdm.database;
|
2
|
|
3
|
import java.util.HashMap;
|
4
|
import java.util.Map;
|
5
|
|
6
|
import org.apache.log4j.Logger;
|
7
|
import org.springframework.beans.factory.xml.XmlBeanFactory;
|
8
|
import org.springframework.core.io.FileSystemResource;
|
9
|
import org.springframework.stereotype.Component;
|
10
|
|
11
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
12
|
|
13
|
@Component
|
14
|
public class DataSourceBeanLoader {
|
15
|
|
16
|
private static final Logger logger = Logger.getLogger(DataSourceBeanLoader.class);
|
17
|
|
18
|
private static final String DATASOURCE_BEANDEF_FILE = "datasources.xml";
|
19
|
private static final String DATASOURCE_BEANDEF_PATH = CdmUtils.getCdmHomeDir().getPath();
|
20
|
|
21
|
private static String userdefinedBeanDefinitionFile = null;
|
22
|
|
23
|
public void setBeanDefinitionFile(String filename){
|
24
|
userdefinedBeanDefinitionFile = filename;
|
25
|
}
|
26
|
|
27
|
|
28
|
/**
|
29
|
* @return
|
30
|
*/
|
31
|
public static <T> Map<String, T> loadDataSources(final Class<T> requiredType) {
|
32
|
|
33
|
Map<String, T> dataSources = new HashMap<String, T>();
|
34
|
String path = DATASOURCE_BEANDEF_PATH + (userdefinedBeanDefinitionFile == null ? DATASOURCE_BEANDEF_FILE : userdefinedBeanDefinitionFile);
|
35
|
|
36
|
logger.info("loading DataSourceBeans from: " + path);
|
37
|
FileSystemResource file = new FileSystemResource(path);
|
38
|
XmlBeanFactory beanFactory = new XmlBeanFactory(file);
|
39
|
|
40
|
for(String beanName : beanFactory.getBeanDefinitionNames()){
|
41
|
T datasource = beanFactory.getBean(beanName, requiredType);
|
42
|
dataSources.put(beanName, datasource);
|
43
|
}
|
44
|
return (Map<String, T>) dataSources;
|
45
|
}
|
46
|
|
47
|
}
|