項目中使用的ConfigUtil 類的封裝,支持多元

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();
	}

}

發佈了160 篇原創文章 · 獲贊 27 · 訪問量 31萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章