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

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

There is a newer version: 1.1.0
Show newest version
package com.jquicker.configure;

import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import com.jquicker.commons.util.PropertiesUtils;
import com.jquicker.commons.util.StringUtils;

/**
 * Redis配置
 * @author OL
 */
public class RedisConfig {

	private static Properties config = new Properties();
	
	/**
	 * 初始化配置
	 * @param resource
	 * @author OL
	 */
	public static void init(String resource) {
		Properties properties = loadProperties(resource, null);
		config.putAll(properties);
		config.put("recource", resource);
	}
	
	/**
	 * 初始化配置
	 * @param resource
	 * @param prefix configKey前缀(解决BD、Redis等同名配置)
	 * @author OL
	 */
	public static void init(String resource, String configPrefix) {
		Properties properties = loadProperties(resource, configPrefix);
		config.putAll(properties);
		config.put("config.recource", resource);
		if(StringUtils.isNotEmpty(configPrefix)){
			config.put("configPrefix", configPrefix + ".");
		}
	}
	
	/**
	 * 初始化配置
	 * @param properties
	 * @author OL
	 */
	public static void init(Properties properties) {
		config.putAll(properties);
	}
	
	/**
	 * 获取配置文件路径
	 * @return
	 * @author OL
	 */
	public static String getResource() {
		return config.getProperty("recource");
	}
	
	public static Properties get(){
		return config;
	}
	
	public static String get(String key){
		return get(key, "");
	}
	
	public static String get(String key, String defaultValue){
		return config.getProperty(key, defaultValue);
	}
	
	public static void set(String key, String value){
		config.put(key, value);
	}
	
	public static void setPrefix(String prefix){
		config.put("prefix", prefix);
	}
	
	public static String getPrefix(){
		return config.getProperty("prefix");
	}
	
	public static void setEnable(boolean enable){
		config.put("enable", enable + "");
	}
	
	/**
	 * 是否启用
	 * @param enable
	 * @return
	 * @author OL
	 */
	public static boolean isEnable(){
		return Boolean.valueOf(config.getProperty("enable"));
	}
	
	/**
	 * 加载配置文件
	 * @param resource
	 * @param prefix
	 * @return
	 * @author OL
	 */
	private static 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);
		}
		return properties;
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy