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