项目中使用的ConfigUtil 类的封装,支持多元
2015-04-22 11:24
375 查看
import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.ResourceBundle; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; /** * 项目参数工具类 * * @author sigangjun * */ public class ConfigUtil { /** * Logger for this class */ private static final Logger logger = Logger.getLogger(ConfigUtil.class); private static final ResourceBundle bundle = java.util.ResourceBundle.getBundle("config"); public static Map<String, String> map = new HashMap<>(); @Autowired private SyConfigServiceI syConfigServiceI; /** * 获得sessionInfo名字 * * @return */ public static final String getSessionInfoName() { return bundle.getString("sessionInfoName"); } /** * 通过键获取值 * * @param key * @return */ public static final String get(String key) { if (map.size() == 0) { new ConfigUtil().putMap(); } String value = map.get(key); if (value == null || value.equals("")) { value = bundle.getString(key); } value = matheresTemplate(value); return value; } private void putMap() { if (map.size() == 0) { SyConfigServiceI syConfigServiceI2 = (SyConfigServiceI) WebUtil.getBean("syConfigServiceI"); List<PdSysConfig> l2 = syConfigServiceI2.find(); for (PdSysConfig pdSysConfig : l2) { map.put(pdSysConfig.getSysName(), pdSysConfig.getSysValue()); } } } /** * 获取class路径 * * @return */ public static final String getLoaderPath() { return ConfigUtil.class.getClassLoader().getResource("").getPath(); } /** * 获取schema文件路径 * * @param ver * 版本号 * @param fileName * 文件名(不带后缀) * @return */ public static final String getSchemaPath(String ver, String fileName) { return getLoaderPath() + "/schema/" + ver + "/" + fileName + ".xsd"; } /** * 获取schema文件路径 * * @param fileName * 文件名(不带后缀) * @return */ public static final String getSchemaPath(String fileName) { return getLoaderPath() + "schema/096/" + fileName + ".xsd"; } public static final String getRootPath(String fileName, String type) { if (BroadcastTypeEnum.周期性播发单.getValue().equals(type)) { return getPeriodPushrootPath(fileName); } else { return getPushrootPath(fileName); } } /** * 获取pushroot地址 * * @param fileName * @return */ public static final String getPushrootPath(String fileName) { return get("pushVediofilePath") + "/pushroot_" + CommonUtil.getBroName(fileName); } /** * 获取pushroot地址 * * @param fileName * @return */ public static final String getPeriodPushrootPath(String fileName) { return get("periodPushVediofilePath") + "/pushroot_" + CommonUtil.getBroName(fileName); } public static final String getDrmRootPath(String fileName, String type) { if (BroadcastTypeEnum.周期性播发单.getValue().equals(type)) { return getDrmPeriodPath(fileName); } else { return getDrmPath(fileName); } } public static final String getDrmPath(String fileName) { return get("drmContentResultTarget") + "\\pushroot_" + CommonUtil.getBroName(fileName); } public static final String getDrmPeriodPath(String fileName) { return get("periodDrmContentResultTarget") + "\\pushroot_" + CommonUtil.getBroName(fileName); } /** * 版本 * * @return */ public static final String[] getVersion() { return ConfigUtil.get("verisonNum").split(","); } public static String convert(String s, Map<String, String> map) { Matcher m = Pattern.compile("<#=(.*?)#>").matcher(s); StringBuffer sb = new StringBuffer(); while (m.find()) { String value = map.get(m.group(1)); m.appendReplacement(sb, value != null ? value : "null"); } m.appendTail(sb); return sb.toString(); } public static void main(String[] args) { String s = get("c"); System.out.println("ssss:" + s); } private static String matheresTemplate(String str) { Pattern greedy = Pattern.compile("(\\{\\{.*?\\}\\})"); Matcher m = greedy.matcher(str); StringBuffer sb = new StringBuffer(); String value = ""; String key = ""; while (m.find()) { value = m.group(1); if (value.length() > 4) { if (value.contains("{{") && value.contains("}}")) { key = value.replace("{{", "").replace("}}", ""); value = get(key.trim()); } else { continue; } } m.appendReplacement(sb, value); } m.appendTail(sb); return sb.toString(); } }
相关文章推荐
- 重新封装的PHPLib DB类(推荐简单项目使用本类库)
- 让汉字永远自由使用_______请支持文泉驿中文字体字库开源项目
- 重新封装的PHPLib DB类(推荐简单项目使用本类库)
- 重新封装的PHPLib DB类(推荐简单项目使用本类库)
- 因为此版本的应用程序不支持其项目类型(.csproj),若要打开它,请使用支持此类型项
- 因为此版本的应用程序不支持其项目类型(.csproj),若要打开它,请使用支持此类型项目的版本
- 项目中使用到的封装---后台子线程执行任务,前台显示加载条
- log4cpp在项目中的封装使用(VC项目)
- 回调函数的使用封装(支持全局函数,类静态函数,类成员函数)
- 为已有的web project项目加入maven支持,并使用myeclipse的插件.
- 为已有的web project项目加入maven支持,并使用myeclipse的插件.
- Emacs使用Projectile管理项目,可快速访问项目里任何文件,支持全项目关键词搜索
- 为已有的web project项目加入maven支持,并使用myeclipse的插件.
- 最近项目使用的SqLServer分页存储过程及调用封装代码
- 最近项目使用的SqLServer分页存储过程及调用封装代码
- 项目过程中如何使用面向对象的特征:继承,封装,多态,接口
- Android Support Library 介绍~在项目总如何使用android支持库
- 使用cobertura进行代码覆盖率(支持WEB部署项目)
- 为已有的web project项目加入maven支持,并使用myeclipse的插件.
- Tolua使用,利用pkg文件,封装自己的lua支持的cocos2dx的接口