项目中使用的ConfigUtil 类的封装,支持多元

本文深入探讨了配置工具类的设计与实现,详细介绍了如何通过资源包获取项目参数,包括会话信息名称、键值对获取、类路径获取、SCHEMA文件路径、推文根目录、DRM根目录等核心功能。此外,还展示了如何通过正则表达式模板进行字符串转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值