package zj.message.util; import java.io.Serializable; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import zj.check.util.CheckUtil; import zj.java.util.JavaUtil; /** * 常量資源文件工具類(lèi)<br> * * @version 1.00 (2011.12.02) * @author SHNKCS 張軍 {@link <a target=_blank href="http://www.shanghaijiadun.com">上海加盾信息科技有限公司</a> <a target=_blank href="http://m.sfpk123.com">張軍個(gè)人網(wǎng)站</a> <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; /** 資源鍵值對(duì) **/ public static final Map<String, String> CONSTANT_KEY_VALUE = Collections.synchronizedMap(new HashMap<String, String>()); /** 資源文件地址集合,有序 **/ public static List<String> CONFIGS = new ArrayList<String>(); /** 國(guó)際化資源文件/資源文件內(nèi)容初使化 **/ static { AJAX_SUCCESS = "success"; AJAX_FAIL = "fail"; AJAX_MSG = "msg"; loadConfig(null); } // 資源文件 /** * 設(shè)置資源文件路徑 * * @param configFile * <p> * ./constant.properties * </p> * <p> * ./systemConfig.properties * </p> * <p> * ./app.properties * </p> * @return 資源文件值 */ public static void loadConfig(String configFile) { // 默認(rèn)注冊(cè)資源文件地址 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); } // 如果設(shè)置絕對(duì)路徑,重新調(diào)用此方法 setConstantKeyValueToMemory(); } /** * 獲取資源文件值 * * @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 * 資源文件參數(shù) * @return 資源文件值 */ public static String getConstantValueByParams(String key, boolean notExistIsNull, Object... arguments) { return getValueByParams(getConstantValue(key, notExistIsNull), arguments); } /** * 獲取資源文件值 * * @param key * 資源文件key * @param arguments * 資源文件參數(shù) * @return 資源文件值 */ public static String getConstantValueByParams(String key, Object... arguments) { return getConstantValueByParams(key, true, arguments); } /** * 獲取資源文件值 * * @param key * 資源文件key * @param paths * 資源文件路徑集合 * @param notExistIsNull * <p> * true:如果資源文件鍵不存在,則返回null * </p> * <p> * false:如果資源文件鍵不存在,則返回資源文件key * </p> * @return 資源文件值 */ public static String getConstantValueByMemory(String key, List<String> paths, boolean notExistIsNull) { String value = null; if (CheckUtil.isNull(key)) { // 不需要清除緩存,會(huì)覆蓋最新的值 // 先清除緩存 // CONSTANT_KEY_VALUE.clear(); // 最好只調(diào)用一次 CONSTANT_KEY_VALUE.putAll(ConfigUtil.getConstantKeyValues(paths)); log.debug("[信息] 加載所有常量數(shù)據(jù)至緩存成功:"+paths); } else { if ((value = CONSTANT_KEY_VALUE.get(key)) == null) { value = getConstantValue(key, notExistIsNull); CONSTANT_KEY_VALUE.put(key, value); } } return value; } /** * 設(shè)置所有資源鍵值 */ public static void setConstantKeyValueToMemory() { setConstantKeyValueToMemory(true); } /** * 設(shè)置所有資源鍵值 * * @param notExistIsNull * <p> * true:如果資源文件鍵不存在,則返回null * </p> * <p> * false:如果資源文件鍵不存在,則返回資源文件key * </p> */ public static void setConstantKeyValueToMemory(boolean notExistIsNull) { getConstantValueByMemory(null, CONFIGS, notExistIsNull); } /** * 獲取資源文件值 * * @param key * 資源文件key(不能為null) * @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 * 資源文件參數(shù) * @return 資源文件值 */ public static String getConstantValueByMemoryParams(String key, boolean notExistIsNull, Object... arguments) { return getValueByParams(getConstantValueByMemory(key, notExistIsNull), arguments); } /** * 獲取資源文件值 * * @param key * 資源文件key * @param arguments * 資源文件參數(shù) * @return 資源文件值 */ public static String getConstantValueByMemoryParams(String key, Object... arguments) { return getConstantValueByMemoryParams(key, true, arguments); } /** * 配置文件內(nèi)容取得帶參數(shù) * * @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()); } }
package zj.message.util; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; import zj.check.util.CheckUtil; import zj.io.util.FileUtil; import zj.java.util.JavaUtil; /** * 資源文件工具類(lèi)<br> * * @version 1.00 (2011.12.02) * @author SHNKCS 張軍 {@link <a target=_blank href="http://www.shanghaijiadun.com">上海加盾信息科技有限公司</a> <a target=_blank href="http://m.sfpk123.com">張軍個(gè)人網(wǎng)站</a> <a target=_blank href="http://user.qzone.qq.com/360901061/">張軍QQ空間</a>} */ public final class ConfigUtil implements Serializable { // private transient static final Logger logger = Logger.getLogger(ConfigUtil.class); private static final long serialVersionUID = 1L; /** * 根據(jù)配置文件的名字獲取配置文件的值 * * @param path * 配置文件路徑 * @param key * 配置文件信息的Key * @return */ public static String getConfig(String path, String key) { // 根據(jù)配置文件的key獲取配置文件的值 String value = null; InputStream in = null; try { key = JavaUtil.trim(key); path = JavaUtil.trim(path); String rpath = FileUtil.getPathByResourceNoException(path); if (CheckUtil.isNull(rpath)) { in = ConfigUtil.class.getResourceAsStream(path); } else { File configFile = new File(rpath); //logger.debug("配置文件路徑【" + FileUtil.linuxSeparator(configFile.getAbsolutePath()) + "】"); if (!configFile.exists()) return key; in = new FileInputStream(configFile); } //logger.debug("流【" + in + "】"); // 配置文件內(nèi)容解析 Properties prop = new Properties(); prop.load(in); // 根據(jù)配置文件的key獲取配置文件的值 value = prop.getProperty(key); } catch (Exception e) { // 不做處理 value = key; } finally { try { if (in != null) { in.close(); } } catch (Exception e) { } } return value; } /** * 取得配置文件所有key和value值 * * @param paths * 所有路徑 * @return */ public static Map<String, String> getConstantKeyValues(String... paths) { return getConstantKeyValues(Arrays.asList(paths)); } /** * 取得配置文件所有key和value值 * * @param paths * 所有路徑 * @return */ public static Map<String, String> getConstantKeyValues(List<String> ppaths) { // 根據(jù)配置文件的key和value的值 Map<String, String> keyValues = new HashMap<String, String>(); // 去重復(fù) List<String> paths = new ArrayList<String>(); for (String path : ppaths) { if (!paths.contains(path)) { paths.add(path); } } for (String path : paths) { InputStream in = null; try { // 當(dāng)前路徑中查找 // File configFile = new File(FileUtil.getPathByResourceNoException(path)); // if (!configFile.exists()) // continue; // in = new FileInputStream(configFile); String rpath = FileUtil.getPathByResourceNoException(path); if (CheckUtil.isNull(rpath)) { in = ConfigUtil.class.getResourceAsStream(path); } else { File configFile = new File(rpath); //logger.debug("配置文件路徑【" + FileUtil.linuxSeparator(configFile.getAbsolutePath()) + "】"); if (!configFile.exists()) continue; in = new FileInputStream(configFile); } //logger.debug("流【" + in + "】"); // 配置文件內(nèi)容解析 Properties prop = new Properties(); prop.load(in); // 根據(jù)配置文件的key獲取配置文件的值 Set<Map.Entry<Object, Object>> set = prop.entrySet(); for (Map.Entry<Object, Object> map : set) { String key = JavaUtil.trim(JavaUtil.objToStr(map.getKey())); if (key == null || key.equals("")) { continue; } String value = JavaUtil.objToStr(map.getValue()); keyValues.put(key, value); } } catch (Exception e) { // 不做處理 // logger.error("獲取配置文件[" + path + "]錯(cuò)誤:" + e.getMessage()); } finally { try { if (in != null) { in.close(); } } catch (Exception e) { } } } return keyValues; } }
本文為張軍原創(chuàng)文章,轉(zhuǎn)載無(wú)需和我聯(lián)系,但請(qǐng)注明來(lái)自張軍的軍軍小站,個(gè)人博客http://m.sfpk123.com
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
