您的位置:首页 > 其它

项目中使用的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();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐