cleanup
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / database / DataSourceBeanLoader.java
1 /**
2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.cdm.database;
10
11 import java.util.HashMap;
12 import java.util.Map;
13
14 import org.apache.logging.log4j.LogManager;
15 import org.apache.logging.log4j.Logger;
16 import org.springframework.beans.factory.xml.XmlBeanFactory;
17 import org.springframework.core.io.FileSystemResource;
18
19 // @Component
20 public class DataSourceBeanLoader {
21
22 private static final Logger logger = LogManager.getLogger();
23
24 private static final String DATASOURCE_BEANDEF_FILE = "datasources.xml";
25 // see #8506 private static final String DATASOURCE_BEANDEF_PATH = ConfigFileUtil.getCdmHomeDir().getPath();
26
27 private static String userdefinedBeanDefinitionFile = null;
28
29 public void setBeanDefinitionFile(String filename){
30 userdefinedBeanDefinitionFile = filename;
31 }
32
33 public static <T> Map<String, T> loadDataSources(final Class<T> requiredType) {
34
35 Map<String, T> dataSources = new HashMap<>();
36 String path = ""; // commented to avoid compile problems see #8506// DATASOURCE_BEANDEF_PATH + (userdefinedBeanDefinitionFile == null ? DATASOURCE_BEANDEF_FILE : userdefinedBeanDefinitionFile);
37
38 logger.info("loading DataSourceBeans from: " + path);
39 FileSystemResource file = new FileSystemResource(path);
40 XmlBeanFactory beanFactory = new XmlBeanFactory(file);
41
42 for(String beanName : beanFactory.getBeanDefinitionNames()){
43 T datasource = beanFactory.getBean(beanName, requiredType);
44 dataSources.put(beanName, datasource);
45 }
46 return dataSources;
47 }
48 }