All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jquicker.configure.DBConfig Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
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 entry = it.next();
				String key = entry.getKey().toString();
				if (key.startsWith(prefix)) {
					properties.put(key.substring(prefix.length()), entry.getValue());
				}
			}
		} else {
			properties.putAll(temp);
		}
		PropertiesUtils.extend(properties, defaultProperties);
		return properties;
	}
	
	/**
	 * 初始化数据源
	 * @param config
	 * @param dataSourceName
	 * @author OL
	 */
	private void initDataSource(Properties config, String dataSourceName) {
		try {
			DruidDataSource druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(config); // 创建数据源
			
			// 添加日志过滤器(打印数据库操作相关的日志)
			Log4j2Filter logFilter = new Log4j2Filter();
			logFilter.setConnectionLogEnabled(false); // 打印连接信息
			logFilter.setStatementLogEnabled(true); // 打印将要执行的SQL以及参数
			logFilter.setStatementExecutableSqlLogEnable(false); // 打印实际执行的SQL
			logFilter.setResultSetLogEnabled(false); // 打印结果集
			
			List filters = druidDataSource.getProxyFilters();
			filters.add(logFilter);
			
			dataSourceName = dataSourceName == null ? defaultName : dataSourceName;
			DataSourceManager.addDataSource(dataSourceName, druidDataSource);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy