欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

常量資源文件工具類

張軍 3147 0

常量資源文件工具類,讀取配置文件常量的工具類,可放入內存,速度及快

張軍博客

package zj.message.util;

import java.io.Serializable;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import zj.check.util.CheckUtil;
import zj.java.util.JavaUtil;

/**
 * 常量資源文件工具類<br>
 * 
 * @version 1.00 (2011.12.02)
 * @author SHNKCS 張軍 {@link  <a target=_blank href="http://www.dlhighland.cn">張軍個人網站</a>&nbsp;&nbsp;&nbsp;&nbsp;<a target=_blank href="http://user.qzone.qq.com/360901061/">張軍QQ空間</a>}
 */
public class MessageConstantsUtil implements Serializable {
	private static final long serialVersionUID = 1L;
	private transient static final Logger log = Logger.getLogger(MessageConstantsUtil.class);
	public static String AJAX_SUCCESS;
	public static String AJAX_FAIL;
	public static String AJAX_MSG;
	/** 資源鍵值對 **/
	public static final Map<String, String> CONSTANT_KEY_VALUE = Collections.synchronizedMap(new HashMap<String, String>());
	/** 資源文件地址集合,無序 **/
	public static Set<String> CONFIGS = new HashSet<String>();
	/** 國際化資源文件/資源文件內容初使化 **/
	static {
		AJAX_SUCCESS = "success";
		AJAX_FAIL = "fail";
		AJAX_MSG = "msg";
		loadConfig(null);
		setConstantKeyValueToMemory();
	}

	// 資源文件
	/**
	 * 設置資源文件路徑
	 * 
	 * @param configFile
	 *            <p>
	 *            ./constant.properties
	 *            </p>
	 *            <p>
	 *            ./systemConfig.properties
	 *            </p>
	 *            <p>
	 *            ./app.properties
	 *            </p>
	 * @return 資源文件值
	 */
	public static void loadConfig(String configFile) {
		// 默認注冊資源文件地址
		CONFIGS.add("./constant.properties");
		CONFIGS.add("./systemConfig.properties");
		CONFIGS.add("./app.properties");
		// 添加新的資源文件地址
		String[] configs = JavaUtil.split(configFile, ",");
		for (String s : configs) {
			if (CheckUtil.isNull(s))
				continue;
			CONFIGS.add(s);
		}
	}

	/**
	 * 獲取資源文件值
	 * 
	 * @param key
	 *            資源文件key
	 * @param notExistIsNull
	 *            <p>
	 *            true:如果資源文件鍵不存在,則返回null
	 *            </p>
	 *            <p>
	 *            false:如果資源文件鍵不存在,則返回資源文件key
	 *            </p>
	 * @return 資源文件值
	 */
	public static String getConstantValue(String key, boolean notExistIsNull) {
		String value = null;
		boolean exists = false;
		for (String path : CONFIGS) {
			value = ConfigUtil.getConfig(path, key);
			if (value != null && !value.equals(key)) {
				exists = true;
				break;
			}
		}
		if (!exists) {
			if (notExistIsNull) {
				value = null;
			} else {
				value = key;
			}
		}
		CONSTANT_KEY_VALUE.put(key, value);
		return value;
	}

	/**
	 * 獲取資源文件值
	 * 
	 * @param key
	 *            資源文件key
	 * @return 資源文件值
	 */
	public static String getConstantValue(String key) {
		return getConstantValue(key, true);
	}

	/**
	 * 獲取資源文件值
	 * 
	 * @param key
	 *            資源文件key
	 * @param notExistIsNull
	 *            <p>
	 *            true:如果資源文件鍵不存在,則返回null
	 *            </p>
	 *            <p>
	 *            false:如果資源文件鍵不存在,則返回資源文件key
	 *            </p>
	 * @param arguments
	 *            資源文件參數
	 * @return 資源文件值
	 */
	public static String getConstantValueByParams(String key, boolean notExistIsNull, Object... arguments) {
		return getValueByParams(getConstantValue(key, notExistIsNull), arguments);
	}

	/**
	 * 獲取資源文件值
	 * 
	 * @param key
	 *            資源文件key
	 * @param arguments
	 *            資源文件參數
	 * @return 資源文件值
	 */
	public static String getConstantValueByParams(String key, Object... arguments) {
		return getConstantValueByParams(key, false, arguments);
	}

	/**
	 * 獲取資源文件值
	 * 
	 * @param key
	 *            資源文件key
	 * @param paths
	 *            資源文件路徑集合
	 * @param notExistIsNull
	 *            <p>
	 *            true:如果資源文件鍵不存在,則返回null
	 *            </p>
	 *            <p>
	 *            false:如果資源文件鍵不存在,則返回資源文件key
	 *            </p>
	 * @return 資源文件值
	 */
	public static String getConstantValueByMemory(String key, Set<String> paths, boolean notExistIsNull) {
		String value = null;
		if (CheckUtil.isNull(key)) {
			// 先清除緩存
			CONSTANT_KEY_VALUE.clear();
			// 最好只調用一次
			CONSTANT_KEY_VALUE.putAll(ConfigUtil.getConstantKeyValues(paths));
			log.debug("加載所有常量數據至緩存成功");
		} else {
			if ((value = CONSTANT_KEY_VALUE.get(key)) == null) {
				value = getConstantValue(key, notExistIsNull);
				CONSTANT_KEY_VALUE.put(key, value);
			}
		}
		return value;
	}

	/**
	 * 獲取資源文件值
	 * 
	 * @param key
	 *            資源文件key
	 * @param paths
	 *            資源文件路徑集合
	 * @return 資源文件值
	 */
	public static String getConstantValueByMemory(String key, Set<String> paths) {
		return getConstantValueByMemory(key, paths, false);
	}

	/**
	 * 設置所有資源鍵值
	 */
	public static void setConstantKeyValueToMemory() {
		setConstantKeyValueToMemory(CONFIGS);
	}

	/**
	 * 設置所有資源鍵值
	 */
	public static void setConstantKeyValueToMemory(boolean notExistIsNull) {
		setConstantKeyValueToMemory(CONFIGS, notExistIsNull);
	}

	/**
	 * 設置所有資源文件鍵值到內存中
	 * 
	 * @param config
	 */
	public static void setConstantKeyValueToMemory(Set<String> config) {
		getConstantValueByMemory(null, config);
	}

	/**
	 * 設置所有資源文件鍵值到內存中
	 * 
	 * @param config
	 */
	public static void setConstantKeyValueToMemory(Set<String> config, boolean notExistIsNull) {
		getConstantValueByMemory(null, config, notExistIsNull);
	}

	/**
	 * 獲取資源文件值
	 * 
	 * @param key
	 *            資源文件key
	 * @param notExistIsNull
	 *            <p>
	 *            true:如果資源文件鍵不存在,則返回null
	 *            </p>
	 *            <p>
	 *            false:如果資源文件鍵不存在,則返回資源文件key
	 *            </p>
	 * @return 資源文件值
	 */
	public static String getConstantValueByMemory(String key, boolean notExistIsNull) {
		return getConstantValueByMemory(key, CONFIGS, notExistIsNull);
	}

	/**
	 * 獲取資源文件值
	 * 
	 * @param key
	 *            資源文件key
	 * @return 資源文件值
	 */
	public static String getConstantValueByMemory(String key) {
		return getConstantValueByMemory(key, true);
	}

	/**
	 * 獲取資源文件值
	 * 
	 * @param key
	 *            資源文件key
	 * @param notExistIsNull
	 *            <p>
	 *            true:如果資源文件鍵不存在,則返回null
	 *            </p>
	 *            <p>
	 *            false:如果資源文件鍵不存在,則返回資源文件key
	 *            </p>
	 * @param arguments
	 *            資源文件參數
	 * @return 資源文件值
	 */
	public static String getConstantValueByMemoryParams(String key, boolean notExistIsNull, Object... arguments) {
		return getValueByParams(getConstantValueByMemory(key, notExistIsNull), arguments);
	}

	/**
	 * 獲取資源文件值
	 * 
	 * @param key
	 *            資源文件key
	 * @param arguments
	 *            資源文件參數
	 * @return 資源文件值
	 */
	public static String getConstantValueByMemoryParams(String key, Object... arguments) {
		return getConstantValueByMemoryParams(key, false, arguments);
	}

	/**
	 * 配置文件內容取得帶參數
	 * 
	 * @param value
	 * @param arguments
	 * @return
	 */
	public static String getValueByParams(String value, Object... arguments) {
		try {
			return MessageFormat.format(value, arguments);
		} catch (Exception e) {
			e.printStackTrace();
			return value;
		}
	}

	/**
	 * 打印debug信息
	 * 
	 * @return
	 */
	public static void debugString() {
		log.debug("資源文件列表如下:");
		for (String s : CONFIGS) {
			log.debug("CONFIGS:" + s);
		}
		log.debug("CONSTANT_KEY_VALUE:" + CONSTANT_KEY_VALUE.entrySet());
	}

}



更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 大香久久| 亚洲午夜av久久乱码 | 国产精品精品视频一区二区三区 | 亚洲精品一区中文字幕乱码 | 丁香久久 | 蜜臀在线免费观看 | 仇爱电视剧泰剧在线观看免费播放 | 99er在线观看 | 精品国产一二三区 | 国内视频在线 | 日本久久中文字幕 | 久久日本精品在线热 | 久久久久无码国产精品一区 | 亚洲国产日韩在线一区 | 亚洲一区二区在线 | 九九视频九九热 | 在线三级电影 | 国产精品a在线观看香蕉 | 久久久久久久国产视频 | 99热欧美 | 91网站免费观看直播 | 精品视频一区二区三区免费 | 丝袜 亚洲 另类 欧美 变态 | xxxxxx免费| 精品无人区乱码一区二区三区手机 | 日本人69视频jizz免费看 | 欧美激情一区二区三级高清视频 | 亚洲国产精品无码AV久久久 | 欧美性猛交xxxx乱大交蜜桃 | 国产精品成人无码A片免费网址 | 国产偷国产偷在线高清 | 日韩在线一区二区三区 | 欧美一区二区三区久久 | aaa在线 | 亚洲欧美日韩在线一区二区三区 | 97精品国产高清在线看入口 | 午夜99| 欧美电影精品久久久久 | 亚洲国产日韩在线观看 | 免费观看毛片 | 一区二区不卡在线观看 |