com.jquicker.configure.DBConfig Maven / Gradle / Ivy
package com.jquicker.configure;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.logging.Log4j2Filter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.jquicker.commons.util.PropertiesUtils;
import com.jquicker.persistent.rdb.ds.DataSourceManager;
/**
* 数据库配置
* @author OL
*/
public class DBConfig {
// 默认数据源名称
public static final String defaultName = "dataSource-default";
// 默认配置
private static Properties defaultProperties = PropertiesUtils.load("/ds-default.properties");
private static Map configs = new HashMap();
private Properties config = new Properties();
private String name;
public DBConfig() {
if(configs.containsKey(defaultName)){
throw new RuntimeException("数据源名称重复:" + defaultName);
}
this.name = defaultName;
config.put("name", name);
configs.put(name, this);
}
public DBConfig(String dataSourceName) {
if(configs.containsKey(dataSourceName)){
throw new RuntimeException("数据源名称重复:" + dataSourceName);
}
this.name = dataSourceName;
config.put("name", name);
configs.put(name, this);
}
/**
* 设置DB配置文件路径
* @param resource
* @author OL
*/
public void init(String resource) {
init(resource, null);
}
/**
* 设置DB配置文件路径
* @param resource
* @param prefix 前缀(解决BD、Redis等同名配置)
* @author OL
*/
public void init(String resource, String prefix) {
Properties properties = this.loadProperties(resource, prefix);
config.putAll(properties); // 如果配置文件中配置了name,原config的name会被覆盖
config.put("config.recource", resource);
if(name.equals(defaultName)) {
config.put("name", defaultName);
}
this.initDataSource(config, this.getName());
}
/**
* 初始化配置
* @param properties
* @author OL
*/
public void init(Properties properties) {
config.putAll(properties);
}
public Properties get(){
return this.config;
}
public String get(String key){
return this.get(key, "");
}
public String get(String key, String defaultValue){
return config.getProperty(key, defaultValue);
}
public void set(String key, String value){
config.put(key, value);
}
/**
* 设置数据源
* @param className
* @author OL
*/
public void setDataSource(String className) {
config.put("dataSource", className);
}
public String getDataSource() {
return this.get("dataSource");
}
/**
* 获取数据源名称
* @return
* @author OL
*/
public String getName(){
return config.getProperty("name");
}
/**
* 获取配置文件路径
* @return
* @author OL
*/
public String getResource() {
return config.getProperty("recource");
}
public static DBConfig getConfig(){
return configs.get(defaultName);
}
public static DBConfig getConfig(String dataSourceName){
return configs.get(dataSourceName);
}
private Properties loadProperties(String resource, String prefix) {
Properties properties = new Properties();
Properties temp = PropertiesUtils.load(resource);
if(prefix != null) {
Set> set = temp.entrySet();
Iterator> it = set.iterator();
while (it.hasNext()) {
Entry
© 2015 - 2024 Weber Informatics LLC | Privacy Policy